3 # Part of a unified Makefile for building kernels. This part includes all
4 # the definitions that need to be after all the % directives except %RULES
5 # and ones that act like they are part of %RULES.
7 # Most make variables should not be defined in this file. Instead, they
8 # should be defined in the kern.pre.mk so that port makefiles can
9 # override or augment them.
11 # In case the config had a makeoptions DESTDIR...
13 MKMODULESENV+= DESTDIR="${DESTDIR}"
15 SYSDIR?= ${S:C;^[^/];${.CURDIR}/&;:tA}
16 MKMODULESENV+= KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}"
18 .if defined(CONF_CFLAGS)
19 MKMODULESENV+= CONF_CFLAGS="${CONF_CFLAGS}"
23 MKMODULESENV+= WITH_CTF="${WITH_CTF}"
26 .if defined(WITH_EXTRA_TCP_STACKS)
27 MKMODULESENV+= WITH_EXTRA_TCP_STACKS="${WITH_EXTRA_TCP_STACKS}"
30 # Allow overriding the kernel debug directory, so kernel and user debug may be
31 # installed in different directories. Setting it to "" restores the historical
32 # behavior of installing debug files in the kernel directory.
33 KERN_DEBUGDIR?= ${DEBUGDIR}
37 .for target in all clean cleandepend cleandir clobber depend install \
38 ${_obj} reinstall tags
39 ${target}: kernel-${target}
40 .if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
41 ${target}: modules-${target}
43 cd $S/modules; ${MKMODULESENV} ${MAKE} \
44 ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
48 # Handle ports (as defined by the user) that build kernel modules
49 .if !defined(NO_MODULES) && defined(PORTS_MODULES)
51 # The ports tree needs some environment variables defined to match the new kernel
53 # Ports search for some dependencies in PATH, so add the location of the installed files
54 LOCALBASE?= /usr/local
55 # SRC_BASE is how the ports tree refers to the location of the base source files
56 .if !defined(SRC_BASE)
57 SRC_BASE= ${SYSDIR:H:tA}
59 # OSVERSION is used by some ports to determine build options
60 .if !defined(OSRELDATE)
61 # Definition copied from src/Makefile.inc1
62 OSRELDATE!= awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
63 ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
65 # Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
66 WRKDIRPREFIX?= ${.OBJDIR}
72 PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
73 SRC_BASE=${SRC_BASE} \
74 OSVERSION=${OSRELDATE} \
75 WRKDIRPREFIX=${WRKDIRPREFIX}
77 # The WRKDIR needs to be cleaned before building, and trying to change the target
78 # with a :C pattern below results in install -> instclean
80 .for __i in ${PORTS_MODULES}
81 @${ECHO} "===> Ports module ${__i} (all)"
82 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build
85 .for __target in install reinstall clean
86 ${__target}: ports-${__target}
88 .for __i in ${PORTS_MODULES}
89 @${ECHO} "===> Ports module ${__i} (${__target})"
90 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/install/deinstall reinstall/:C/reinstall/deinstall reinstall/}
95 .ORDER: kernel-install modules-install
98 kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA}
100 kernel-cleandir: kernel-clean kernel-cleandepend
103 find . -maxdepth 1 ! -type d ! -name version -delete
107 .if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
110 .if !defined(NO_MODULES_OBJ)
111 modules-all modules-depend: modules-obj
116 FULLKERNEL= ${KERNEL_KO}
118 FULLKERNEL= ${KERNEL_KO}.full
119 ${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
120 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
121 ${FULLKERNEL} ${.TARGET}
122 ${KERNEL_KO}.debug: ${FULLKERNEL}
123 ${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
124 install.debug reinstall.debug: gdbinit
125 cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
127 # Install gdbinit files for kernel debugging.
129 grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
130 sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
131 cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
132 .if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
133 cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
134 ${.CURDIR}/gdbinit.machine
138 ${FULLKERNEL}: ${SYSTEM_DEP} vers.o
140 @echo linking ${.TARGET}
142 .if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
143 @sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
145 .if ${MK_CTF} != "no"
146 @echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
147 @${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
150 ${OBJCOPY} --strip-debug ${.TARGET}
154 OBJS_DEPEND_GUESS+= assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} \
155 ${MFILES:T:S/.m$/.h/}
156 LNFILES= ${CFILES:T:S/.c$/.ln/}
158 .for mfile in ${MFILES}
159 # XXX the low quality .m.o rules gnerated by config are normally used
160 # instead of the .m.c rules here.
161 ${mfile:T:S/.m$/.c/}: ${mfile}
162 ${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
163 ${mfile:T:S/.m$/.h/}: ${mfile}
164 ${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
168 rm -f *.o *.so *.pico *.ko *.s eddep errs \
169 ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
170 linterrs tags vers.c \
171 vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
172 ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
176 ${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC} 2>&1 | \
179 # This is a hack. BFD "optimizes" away dynamic mode if there are no
180 # dynamic references. We could probably do a '-Bforcedynamic' mode like
181 # in the a.out ld. For now, this works.
182 HACK_EXTRA_FLAGS?= -shared
185 ${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.pico
188 assym.s: $S/kern/genassym.sh genassym.o
189 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
191 genassym.o: $S/$M/$M/genassym.c
192 ${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/$M/$M/genassym.c
194 ${SYSTEM_OBJS} genassym.o vers.o: opt_global.h
196 .if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
199 # Skip reading .depend when not needed to speed up tree-walks and simple
200 # lookups. For install, only do this if no other targets are specified.
201 # Also skip generating or including .depend.* files if in meta+filemon mode
202 # since it will track dependencies itself. OBJS_DEPEND_GUESS is still used
203 # for _meta_filemon but not for _SKIP_DEPEND.
204 .if !empty(.MAKEFLAGS:M-V${_V_READ_DEPEND}) || make(*obj) || \
205 ${.TARGETS:M*clean*} == ${.TARGETS} || \
206 ${.TARGETS:M*install*} == ${.TARGETS} || \
207 defined(_meta_filemon)
210 .if defined(_SKIP_DEPEND) || defined(_meta_filemon)
211 .MAKE.DEPENDFILE= /dev/null
214 kernel-depend: .depend
215 SRCS= assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
216 ${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
217 ${MFILES:T:S/.m$/.h/}
218 DEPENDFILES= .depend .depend.*
219 DEPENDOBJS+= ${SYSTEM_OBJS} genassym.o
220 DEPENDFILES_OBJS= ${DEPENDOBJS:O:u:C/^/.depend./}
221 .if ${MAKE_VERSION} < 20160220
224 .if defined(_SKIP_DEPEND)
225 # Don't bother reading any .meta files
226 ${DEPENDOBJS}: .NOMETA
228 # Unset these to avoid looping/statting on them later.
231 .undef DEPENDFILES_OBJS
232 .endif # defined(_SKIP_DEPEND)
233 DEPEND_CFLAGS+= -MD ${DEPEND_MP} -MF.depend.${.TARGET}
234 DEPEND_CFLAGS+= -MT${.TARGET}
235 .if !defined(_meta_filemon)
236 .if defined(.PARSEDIR)
237 # Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
238 # as those are the only ones we will include.
239 DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
240 CFLAGS+= ${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
242 CFLAGS+= ${DEPEND_CFLAGS}
244 .for __depend_obj in ${DEPENDFILES_OBJS}
245 .if ${MAKE_VERSION} < 20160220
246 .sinclude "${.OBJDIR}/${__depend_obj}"
248 .dinclude "${.OBJDIR}/${__depend_obj}"
251 .endif # !defined(_meta_filemon)
253 # Always run 'make depend' to generate dependencies early and to avoid the
254 # need for manually running it. For the kernel this is mostly a NOP since
255 # all dependencies are correctly added or accounted for. This is mostly to
256 # ensure downstream uses of kernel-depend are handled.
257 beforebuild: kernel-depend
259 # Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
260 # For meta+filemon the .meta file is checked for since it is the dependency
262 .for __obj in ${DEPENDOBJS:O:u}
263 .if (defined(_meta_filemon) && !exists(${.OBJDIR}/${__obj}.meta)) || \
264 (!defined(_meta_filemon) && !exists(${.OBJDIR}/.depend.${__obj}))
265 .if ${SYSTEM_OBJS:M${__obj}}
266 ${__obj}: ${OBJS_DEPEND_GUESS}
268 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
269 .elif defined(_meta_filemon)
270 # For meta mode we still need to know which file to depend on to avoid
271 # ambiguous suffix transformation rules from .PATH. Meta mode does not
272 # use .depend files. We really only need source files, not headers since
273 # they are typically in SRCS/beforebuild already. For target-specific
274 # guesses do include headers though since they may not be in SRCS.
275 .if ${SYSTEM_OBJS:M${__obj}}
276 ${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
278 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
282 .NOPATH: .depend ${DEPENDFILES_OBJS}
284 .depend: .PRECIOUS ${SRCS}
287 .if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
288 _ILINKS+= ${MACHINE_CPUARCH}
290 .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
294 # Ensure that the link exists without depending on it when it exists.
295 .for _link in ${_ILINKS}
296 .if !exists(${.OBJDIR}/${_link})
297 ${SRCS} ${CLEAN:M*.o}: ${_link}
302 @case ${.TARGET} in \
304 path=${S}/${MACHINE}/include ;; \
306 path=${S}/${.TARGET}/include ;; \
308 ${ECHO} ${.TARGET} "->" $$path ; \
309 ln -fhs $$path ${.TARGET}
311 # .depend needs include links so we remove them only together.
312 kernel-cleandepend: .PHONY
313 rm -f ${DEPENDFILES} ${_ILINKS}
316 @[ -f .depend ] || { echo "you must make depend first"; exit 1; }
317 sh $S/conf/systags.sh
319 kernel-install: .PHONY
320 @if [ ! -f ${KERNEL_KO} ] ; then \
321 echo "You must build a kernel first." ; \
324 .if exists(${DESTDIR}${KODIR})
325 -thiskernel=`sysctl -n kern.bootfile` ; \
326 if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
327 chflags -R noschg ${DESTDIR}${KODIR} ; \
328 rm -rf ${DESTDIR}${KODIR} ; \
329 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
331 if [ -d ${DESTDIR}${KODIR}.old ] ; then \
332 chflags -R noschg ${DESTDIR}${KODIR}.old ; \
333 rm -rf ${DESTDIR}${KODIR}.old ; \
335 mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
336 if [ -n "${KERN_DEBUGDIR}" -a \
337 -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
338 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
339 mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
341 sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
344 mkdir -p ${DESTDIR}${KODIR}
345 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
346 .if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
347 mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
348 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
350 .if defined(KERNEL_EXTRA_INSTALL)
351 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
357 @-chflags -R noschg ${DESTDIR}${KODIR}
358 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
359 .if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
360 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
363 config.o env.o hints.o vers.o vnode_if.o:
367 config.ln env.ln hints.ln vers.ln vnode_if.ln:
370 .if ${MK_REPRODUCIBLE_BUILD} != "no"
373 vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
374 MAKE=${MAKE} sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT}
376 vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
377 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
379 vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
381 vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
382 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
384 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
386 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
388 .if ${MFS_IMAGE:Uno} != "no"
389 .if empty(MD_ROOT_SIZE_CONFIGURED)
390 # Generate an object file from the file system image to embed in the kernel
391 # via linking. Make sure the contents are in the mfs section and rename the
392 # start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
394 embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
395 ${OBJCOPY} --input-target binary \
396 --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
397 --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
398 ${MFS_IMAGE} ${.TARGET}
400 --rename-section .data=mfs,contents,alloc,load,readonly,data \
402 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
404 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
406 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
411 # XXX strictly, everything depends on Makefile because changes to ${PROF}
412 # only appear there, but we don't handle that.