xref: /freebsd/stand/defs.mk (revision 77013d11e6483b970af25e13c9b892075742f7e5)
1# $FreeBSD$
2
3.if !defined(__BOOT_DEFS_MK__)
4__BOOT_DEFS_MK__=${MFILE}
5
6# We need to define all the MK_ options before including src.opts.mk
7# because it includes bsd.own.mk which needs the right MK_ values,
8# espeically MK_CTF.
9
10MK_CTF=		no
11MK_SSP=		no
12MK_PROFILE=	no
13MAN=
14.if !defined(PIC)
15NO_PIC=
16INTERNALLIB=
17.endif
18# Should be NO_CPU_FLAGS, but bsd.cpu.mk is included too early in bsd.init.mk
19# via the early include of bsd.opts.mk. Moving Makefile.inc include earlier in
20# that file causes weirdness, so this is the next best thing. We need to do this
21# because the loader needs very specific flags to work right, and things like
22# CPUTYPE?=native prevent that, and introduce an endless game of whack-a-mole
23# to disable more and more features. Boot loader performance is never improved
24# enough to make that hassle worth chasing.
25_CPUCFLAGS=
26
27.if ${LDFLAGS:M-nostdlib}
28# Sanitizers won't work unless we link against libc (e.g. in userboot/test).
29MK_ASAN:=	no
30MK_UBSAN:=	no
31.endif
32
33.include <src.opts.mk>
34.include <bsd.linker.mk>
35
36WARNS?=		1
37
38BOOTSRC=	${SRCTOP}/stand
39EFISRC=		${BOOTSRC}/efi
40EFIINC=		${EFISRC}/include
41EFIINCMD=	${EFIINC}/${MACHINE}
42FDTSRC=		${BOOTSRC}/fdt
43FICLSRC=	${BOOTSRC}/ficl
44LDRSRC=		${BOOTSRC}/common
45LIBLUASRC=	${BOOTSRC}/liblua
46LIBOFWSRC=	${BOOTSRC}/libofw
47LUASRC=		${SRCTOP}/contrib/lua/src
48SASRC=		${BOOTSRC}/libsa
49SYSDIR=		${SRCTOP}/sys
50UBOOTSRC=	${BOOTSRC}/uboot
51ZFSSRC=		${SASRC}/zfs
52OZFS=		${SRCTOP}/sys/contrib/openzfs
53ZFSOSSRC=	${OZFS}/module/os/freebsd/
54ZFSOSINC=	${OZFS}/include/os/freebsd
55LIBCSRC=	${SRCTOP}/lib/libc
56
57BOOTOBJ=	${OBJTOP}/stand
58
59# BINDIR is where we install
60BINDIR?=	/boot
61
62# LUAPATH is where we search for and install lua scripts.
63LUAPATH?=	/boot/lua
64FLUASRC?=	${SRCTOP}/libexec/flua
65
66LIBSA=		${BOOTOBJ}/libsa/libsa.a
67.if ${MACHINE} == "i386"
68LIBSA32=	${LIBSA}
69.else
70LIBSA32=	${BOOTOBJ}/libsa32/libsa32.a
71.endif
72
73# Standard options:
74CFLAGS+=	-nostdinc
75# Allow CFLAGS_EARLY.file/target so that code that needs specific stack
76# of include paths can set them up before our include paths. Normally
77# the only thing that should be there are -I directives, and as few of
78# those as possible.
79CFLAGS+=	${CFLAGS_EARLY} ${CFLAGS_EARLY.${.IMPSRC:T}} ${CFLAGS_EARLY.${.TARGET:T}}
80.if ${MACHINE_ARCH} == "amd64" && ${DO32:U0} == 1
81CFLAGS+=	-I${BOOTOBJ}/libsa32
82.else
83CFLAGS+=	-I${BOOTOBJ}/libsa
84.endif
85CFLAGS+=	-I${SASRC} -D_STANDALONE
86CFLAGS+=	-I${SYSDIR}
87# Spike the floating point interfaces
88CFLAGS+=	-Ddouble=jagged-little-pill -Dfloat=floaty-mcfloatface
89.if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "amd64"
90# Slim down the image. This saves about 15% in size with clang 6 on x86
91# Our most constrained /boot/loader env is BIOS booting on x86, where
92# our text + data + BTX have to fit into 640k below the ISA hole.
93# Experience has shown that problems arise between ~520k to ~530k.
94CFLAGS.clang+=	-Oz
95CFLAGS.gcc+=	-Os
96CFLAGS+=	-ffunction-sections -fdata-sections
97.endif
98
99# GELI Support, with backward compat hooks (mostly)
100.if defined(LOADER_NO_GELI_SUPPORT)
101MK_LOADER_GELI=no
102.warning "Please move from LOADER_NO_GELI_SUPPORT to WITHOUT_LOADER_GELI"
103.endif
104.if defined(LOADER_GELI_SUPPORT)
105MK_LOADER_GELI=yes
106.warning "Please move from LOADER_GELI_SUPPORT to WITH_LOADER_GELI"
107.endif
108.if ${MK_LOADER_GELI} == "yes"
109CFLAGS+=	-DLOADER_GELI_SUPPORT
110CFLAGS+=	-I${SASRC}/geli
111.endif # MK_LOADER_GELI
112
113# These should be confined to loader.mk, but can't because uboot/lib
114# also uses it. It's part of loader, but isn't a loader so we can't
115# just include loader.mk
116.if ${LOADER_DISK_SUPPORT:Uyes} == "yes"
117CFLAGS+= -DLOADER_DISK_SUPPORT
118.endif
119
120# Machine specific flags for all builds here
121
122# Ensure PowerPC64 and PowerPC64LE boot loaders are compiled as 32 bit
123# and in big endian.
124.if ${MACHINE_ARCH:Mpowerpc64*} != ""
125CFLAGS+=	-m32 -mcpu=powerpc -mbig-endian
126.endif
127
128# For amd64, there's a bit of mixed bag. Some of the tree (i386, lib*32) is
129# build 32-bit and some 64-bit (lib*, efi). Centralize all the 32-bit magic here
130# and activate it when DO32 is explicitly defined to be 1.
131.if ${MACHINE_ARCH} == "amd64" && ${DO32:U0} == 1
132CFLAGS+=	-m32
133# LD_FLAGS is passed directly to ${LD}, not via ${CC}:
134LD_FLAGS+=	-m elf_i386_fbsd
135AFLAGS+=	--32
136.endif
137
138# Add in the no float / no SIMD stuff and announce we're freestanding
139# aarch64 and riscv don't have -msoft-float, but all others do.
140CFLAGS+=	-ffreestanding ${CFLAGS_NO_SIMD}
141.if ${MACHINE_CPUARCH} == "aarch64"
142CFLAGS+=	-mgeneral-regs-only -ffixed-x18 -fPIC
143.elif ${MACHINE_CPUARCH} == "riscv"
144CFLAGS+=	-march=rv64imac -mabi=lp64 -fPIC
145CFLAGS.clang+=	-mcmodel=medium
146CFLAGS.gcc+=	-mcmodel=medany
147.else
148CFLAGS+=	-msoft-float
149.endif
150
151# -msoft-float seems to be insufficient for powerpcspe
152.if ${MACHINE_ARCH} == "powerpcspe"
153CFLAGS+=	-mno-spe
154.endif
155
156.if ${MACHINE_CPUARCH} == "i386" || (${MACHINE_CPUARCH} == "amd64" && ${DO32:U0} == 1)
157CFLAGS+=	-march=i386
158CFLAGS.gcc+=	-mpreferred-stack-boundary=2
159.endif
160.if ${MACHINE_CPUARCH} == "amd64" && ${DO32:U0} == 0
161CFLAGS+=	-fPIC -mno-red-zone
162.endif
163
164.if ${MACHINE_CPUARCH} == "arm"
165# Do not generate movt/movw, because the relocation fixup for them does not
166# translate to the -Bsymbolic -pie format required by self_reloc() in loader(8).
167# Also, the fpu is not available in a standalone environment.
168CFLAGS.clang+=	-mno-movt
169CFLAGS.clang+=  -mfpu=none
170CFLAGS+=	-fPIC
171.endif
172
173# Some RISC-V linkers have support for relaxations, while some (lld) do not
174# yet. If this is the case we inhibit the compiler from emitting relaxations.
175.if ${LINKER_FEATURES:Mriscv-relaxations} == ""
176CFLAGS+=	-mno-relax
177.endif
178
179# The boot loader build uses dd status=none, where possible, for reproducible
180# build output (since performance varies from run to run). Trouble is that
181# option was recently (10.3) added to FreeBSD and is non-standard. Only use it
182# when this test succeeds rather than require dd to be a bootstrap tool.
183DD_NOSTATUS!=(dd status=none count=0 2> /dev/null && echo status=none) || true
184DD=dd ${DD_NOSTATUS}
185
186.if ${MACHINE_CPUARCH} == "mips"
187CFLAGS+=	-G0 -fno-pic -mno-abicalls
188.endif
189
190#
191# Have a sensible default
192#
193.if ${MK_LOADER_LUA} == "yes"
194LOADER_DEFAULT_INTERP?=lua
195.elif ${MK_FORTH} == "yes"
196LOADER_DEFAULT_INTERP?=4th
197.else
198LOADER_DEFAULT_INTERP?=simp
199.endif
200LOADER_INTERP?=${LOADER_DEFAULT_INTERP}
201
202# Make sure we use the machine link we're about to create
203CFLAGS+=-I.
204
205all: ${PROG}
206
207CLEANFILES+= teken_state.h
208teken.c: teken_state.h
209
210teken_state.h: ${SYSDIR}/teken/sequences
211	awk -f ${SYSDIR}/teken/gensequences \
212		${SYSDIR}/teken/sequences > teken_state.h
213
214.if !defined(NO_OBJ)
215_ILINKS=include/machine
216.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
217_ILINKS+=include/${MACHINE_CPUARCH}
218.endif
219.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
220_ILINKS+=include/x86
221.endif
222CFLAGS+= -Iinclude
223CLEANDIRS+= include
224
225beforedepend: ${_ILINKS}
226beforebuild: ${_ILINKS}
227
228# Ensure that the links exist without depending on it when it exists which
229# causes all the modules to be rebuilt when the directory pointed to changes.
230.for _link in ${_ILINKS}
231.if !exists(${.OBJDIR}/${_link})
232${OBJS}:       ${_link}
233.endif # _link exists
234.endfor
235
236.NOPATH: ${_ILINKS}
237
238${_ILINKS}: .NOMETA
239	@case ${.TARGET:T} in \
240	machine) \
241		if [ ${DO32:U0} -eq 0 ]; then \
242			path=${SYSDIR}/${MACHINE}/include ; \
243		else \
244			path=${SYSDIR}/${MACHINE:C/amd64/i386/}/include ; \
245		fi ;; \
246	*) \
247		path=${SYSDIR}/${.TARGET:T}/include ;; \
248	esac ; \
249	case ${.TARGET} in \
250	*/*) mkdir -p ${.TARGET:H};; \
251	esac ; \
252	path=`(cd $$path && /bin/pwd)` ; \
253	${ECHO} ${.TARGET} "->" $$path ; \
254	ln -fns $$path ${.TARGET}
255.endif # !NO_OBJ
256.endif # __BOOT_DEFS_MK__
257