xref: /freebsd/sys/conf/kern.post.mk (revision 0957b409)
1# $FreeBSD$
2
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.
6#
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.
10
11# In case the config had a makeoptions DESTDIR...
12.if defined(DESTDIR)
13MKMODULESENV+=	DESTDIR="${DESTDIR}"
14.endif
15SYSDIR?= ${S:C;^[^/];${.CURDIR}/&;:tA}
16MKMODULESENV+=	KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}"
17MKMODULESENV+=  MODULE_TIED=yes
18
19.if defined(CONF_CFLAGS)
20MKMODULESENV+=	CONF_CFLAGS="${CONF_CFLAGS}"
21.endif
22
23.if defined(WITH_CTF)
24MKMODULESENV+=	WITH_CTF="${WITH_CTF}"
25.endif
26
27.if defined(WITH_EXTRA_TCP_STACKS)
28MKMODULESENV+=	WITH_EXTRA_TCP_STACKS="${WITH_EXTRA_TCP_STACKS}"
29.endif
30
31.if defined(SAN_CFLAGS)
32MKMODULESENV+=	SAN_CFLAGS="${SAN_CFLAGS}"
33.endif
34
35.if defined(GCOV_CFLAGS)
36MKMODULESENV+=	GCOV_CFLAGS="${GCOV_CFLAGS}"
37.endif
38
39# Allow overriding the kernel debug directory, so kernel and user debug may be
40# installed in different directories. Setting it to "" restores the historical
41# behavior of installing debug files in the kernel directory.
42KERN_DEBUGDIR?=	${DEBUGDIR}
43
44.MAIN: all
45
46.if !defined(NO_MODULES)
47# Default prefix used for modules installed from ports
48LOCALBASE?=	/usr/local
49
50LOCAL_MODULES_DIR?= ${LOCALBASE}/sys/modules
51
52# Default to installing all modules installed by ports unless overridden
53# by the user.
54.if !defined(LOCAL_MODULES) && exists($LOCAL_MODULES_DIR)
55LOCAL_MODULES!= ls ${LOCAL_MODULES_DIR}
56.endif
57.endif
58
59.for target in all clean cleandepend cleandir clobber depend install \
60    ${_obj} reinstall tags
61${target}: kernel-${target}
62.if !defined(NO_MODULES)
63${target}: modules-${target}
64modules-${target}:
65.if !defined(MODULES_WITH_WORLD) && exists($S/modules)
66	cd $S/modules; ${MKMODULESENV} ${MAKE} \
67	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
68.endif
69.for module in ${LOCAL_MODULES}
70	cd ${LOCAL_MODULES_DIR}/${module}; ${MKMODULESENV} ${MAKE} \
71	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
72.endfor
73.endif
74.endfor
75
76# Handle ports (as defined by the user) that build kernel modules
77.if !defined(NO_MODULES) && defined(PORTS_MODULES)
78#
79# The ports tree needs some environment variables defined to match the new kernel
80#
81# SRC_BASE is how the ports tree refers to the location of the base source files
82.if !defined(SRC_BASE)
83SRC_BASE=	${SYSDIR:H:tA}
84.endif
85# OSVERSION is used by some ports to determine build options
86.if !defined(OSRELDATE)
87# Definition copied from src/Makefile.inc1
88OSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
89		    ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
90.endif
91# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
92#
93# Ports search for some dependencies in PATH, so add the location of the
94# installed files
95WRKDIRPREFIX?=	${.OBJDIR}
96PORTSMODULESENV=\
97	env \
98	-u CC \
99	-u CXX \
100	-u CPP \
101	-u MAKESYSPATH \
102	-u MK_AUTO_OBJ \
103	-u MAKEOBJDIR \
104	MAKEFLAGS="${MAKEFLAGS:M*:tW:S/^-m /-m_/g:S/ -m / -m_/g:tw:N-m_*:NMK_AUTO_OBJ=*}" \
105	SYSDIR=${SYSDIR} \
106	PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
107	SRC_BASE=${SRC_BASE} \
108	OSVERSION=${OSRELDATE} \
109	WRKDIRPREFIX=${WRKDIRPREFIX}
110
111# The WRKDIR needs to be cleaned before building, and trying to change the target
112# with a :C pattern below results in install -> instclean
113all:
114.for __i in ${PORTS_MODULES}
115	@${ECHO} "===> Ports module ${__i} (all)"
116	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build
117.endfor
118
119.for __target in install reinstall clean
120${__target}: ports-${__target}
121ports-${__target}:
122.for __i in ${PORTS_MODULES}
123	@${ECHO} "===> Ports module ${__i} (${__target})"
124	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/(re)?install/deinstall reinstall/}
125.endfor
126.endfor
127.endif
128
129.ORDER: kernel-install modules-install
130
131beforebuild: .PHONY
132kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA}
133
134kernel-cleandir: kernel-clean kernel-cleandepend
135
136kernel-clobber:
137	find . -maxdepth 1 ! -type d ! -name version -delete
138
139kernel-obj:
140
141.if !defined(NO_MODULES)
142modules: modules-all
143
144.if !defined(NO_MODULES_OBJ)
145modules-all modules-depend: modules-obj
146.endif
147.endif
148
149.if !defined(DEBUG)
150FULLKERNEL=	${KERNEL_KO}
151.else
152FULLKERNEL=	${KERNEL_KO}.full
153${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
154	${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
155	    ${FULLKERNEL} ${.TARGET}
156${KERNEL_KO}.debug: ${FULLKERNEL}
157	${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
158install.debug reinstall.debug: gdbinit
159	cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
160
161# Install gdbinit files for kernel debugging.
162gdbinit:
163	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
164	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
165	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
166.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
167	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
168	    ${.CURDIR}/gdbinit.machine
169.endif
170.endif
171
172${FULLKERNEL}: ${SYSTEM_DEP} vers.o
173	@rm -f ${.TARGET}
174	@echo linking ${.TARGET}
175	${SYSTEM_LD}
176.if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
177	@sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
178.endif
179.if ${MK_CTF} != "no"
180	@echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
181	@${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
182.endif
183.if !defined(DEBUG)
184	${OBJCOPY} --strip-debug ${.TARGET}
185.endif
186	${SYSTEM_LD_TAIL}
187
188OBJS_DEPEND_GUESS+=	offset.inc assym.inc vnode_if.h ${BEFORE_DEPEND:M*.h} \
189			${MFILES:T:S/.m$/.h/}
190
191.for mfile in ${MFILES}
192# XXX the low quality .m.o rules gnerated by config are normally used
193# instead of the .m.c rules here.
194${mfile:T:S/.m$/.c/}: ${mfile}
195	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
196${mfile:T:S/.m$/.h/}: ${mfile}
197	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
198.endfor
199
200kernel-clean:
201	rm -f *.o *.so *.pico *.ko *.s eddep errs \
202	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
203	    tags vers.c \
204	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
205	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
206	    ${CLEAN}
207
208# This is a hack.  BFD "optimizes" away dynamic mode if there are no
209# dynamic references.  We could probably do a '-Bforcedynamic' mode like
210# in the a.out ld.  For now, this works.
211HACK_EXTRA_FLAGS?= -shared
212hack.pico: Makefile
213	:> hack.c
214	${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.pico
215	rm -f hack.c
216
217offset.inc: $S/kern/genoffset.sh genoffset.o
218	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET}
219
220genoffset.o: $S/kern/genoffset.c
221	${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/kern/genoffset.c
222
223# genoffset_test.o is not actually used for anything - the point of compiling it
224# is to exercise the CTASSERT that checks that the offsets in the offset.inc
225# _lite struct(s) match those in the original(s).
226genoffset_test.o: $S/kern/genoffset.c offset.inc
227	${CC} -c ${CFLAGS:N-flto:N-fno-common} -DOFFSET_TEST \
228	    $S/kern/genoffset.c -o ${.TARGET}
229
230assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o
231	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
232
233genassym.o: $S/$M/$M/genassym.c  offset.inc
234	${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/$M/$M/genassym.c
235
236OBJS_DEPEND_GUESS+= opt_global.h
237genoffset.o genassym.o vers.o: opt_global.h
238
239.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
240_meta_filemon=	1
241.endif
242# Skip reading .depend when not needed to speed up tree-walks and simple
243# lookups.  For install, only do this if no other targets are specified.
244# Also skip generating or including .depend.* files if in meta+filemon mode
245# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used
246# for _meta_filemon but not for _SKIP_DEPEND.
247.if !defined(NO_SKIP_DEPEND) && \
248    ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \
249    ${.TARGETS:M*obj} == ${.TARGETS} || \
250    ${.TARGETS:M*clean*} == ${.TARGETS} || \
251    ${.TARGETS:M*install*} == ${.TARGETS})
252_SKIP_DEPEND=	1
253.endif
254.if defined(_SKIP_DEPEND) || defined(_meta_filemon)
255.MAKE.DEPENDFILE=	/dev/null
256.endif
257
258kernel-depend: .depend
259SRCS=	assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
260	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
261	${MFILES:T:S/.m$/.h/}
262DEPENDOBJS+=	${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
263DEPENDOBJS+=	${CLEAN:M*.o}
264DEPENDFILES=	${DEPENDOBJS:O:u:C/^/.depend./}
265.if ${MAKE_VERSION} < 20160220
266DEPEND_MP?=	-MP
267.endif
268.if defined(_SKIP_DEPEND)
269# Don't bother reading any .meta files
270${DEPENDOBJS}:	.NOMETA
271.depend:	.NOMETA
272# Unset these to avoid looping/statting on them later.
273.undef DEPENDOBJS
274.undef DEPENDFILES
275.endif	# defined(_SKIP_DEPEND)
276DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF.depend.${.TARGET}
277DEPEND_CFLAGS+=	-MT${.TARGET}
278.if !defined(_meta_filemon)
279.if !empty(DEPEND_CFLAGS)
280# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
281# as those are the only ones we will include.
282DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
283CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
284.endif
285.for __depend_obj in ${DEPENDFILES}
286.if ${MAKE_VERSION} < 20160220
287.sinclude "${.OBJDIR}/${__depend_obj}"
288.else
289.dinclude "${.OBJDIR}/${__depend_obj}"
290.endif
291.endfor
292.endif	# !defined(_meta_filemon)
293
294# Always run 'make depend' to generate dependencies early and to avoid the
295# need for manually running it.  For the kernel this is mostly a NOP since
296# all dependencies are correctly added or accounted for.  This is mostly to
297# ensure downstream uses of kernel-depend are handled.
298beforebuild: kernel-depend
299
300# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
301# For meta+filemon the .meta file is checked for since it is the dependency
302# file used.
303.for __obj in ${DEPENDOBJS:O:u}
304.if defined(_meta_filemon)
305_depfile=	${.OBJDIR}/${__obj}.meta
306.else
307_depfile=	${.OBJDIR}/.depend.${__obj}
308.endif
309.if !exists(${_depfile})
310.if ${SYSTEM_OBJS:M${__obj}}
311${__obj}: ${OBJS_DEPEND_GUESS}
312.endif
313${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
314.elif defined(_meta_filemon)
315# For meta mode we still need to know which file to depend on to avoid
316# ambiguous suffix transformation rules from .PATH.  Meta mode does not
317# use .depend files.  We really only need source files, not headers since
318# they are typically in SRCS/beforebuild already.  For target-specific
319# guesses do include headers though since they may not be in SRCS.
320.if ${SYSTEM_OBJS:M${__obj}}
321${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
322.endif
323${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
324.endif	# !exists(${_depfile})
325.endfor
326
327.NOPATH: .depend ${DEPENDFILES}
328
329.depend: .PRECIOUS ${SRCS}
330
331_ILINKS= machine
332.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
333_ILINKS+= ${MACHINE_CPUARCH}
334.endif
335.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
336_ILINKS+= x86
337.endif
338
339# Ensure that the link exists without depending on it when it exists.
340.for _link in ${_ILINKS}
341.if !exists(${.OBJDIR}/${_link})
342${SRCS} ${CLEAN:M*.o}: ${_link}
343.endif
344.endfor
345
346${_ILINKS}:
347	@case ${.TARGET} in \
348	machine) \
349		path=${S}/${MACHINE}/include ;; \
350	*) \
351		path=${S}/${.TARGET}/include ;; \
352	esac ; \
353	${ECHO} ${.TARGET} "->" $$path ; \
354	ln -fns $$path ${.TARGET}
355
356# .depend needs include links so we remove them only together.
357kernel-cleandepend: .PHONY
358	rm -f .depend .depend.* ${_ILINKS}
359
360kernel-tags:
361	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
362	sh $S/conf/systags.sh
363
364kernel-install: .PHONY
365	@if [ ! -f ${KERNEL_KO} ] ; then \
366		echo "You must build a kernel first." ; \
367		exit 1 ; \
368	fi
369.if exists(${DESTDIR}${KODIR})
370	-thiskernel=`sysctl -n kern.bootfile` ; \
371	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
372		chflags -R noschg ${DESTDIR}${KODIR} ; \
373		rm -rf ${DESTDIR}${KODIR} ; \
374		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
375	else \
376		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
377			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
378			rm -rf ${DESTDIR}${KODIR}.old ; \
379		fi ; \
380		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
381		if [ -n "${KERN_DEBUGDIR}" -a \
382		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
383			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
384			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
385		fi ; \
386		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
387	fi
388.endif
389	mkdir -p ${DESTDIR}${KODIR}
390	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
391.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
392	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
393	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
394.endif
395.if defined(KERNEL_EXTRA_INSTALL)
396	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
397.endif
398
399
400
401kernel-reinstall:
402	@-chflags -R noschg ${DESTDIR}${KODIR}
403	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
404.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
405	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
406.endif
407
408config.o env.o hints.o vers.o vnode_if.o:
409	${NORMAL_C}
410	${NORMAL_CTFCONVERT}
411
412.if ${MK_REPRODUCIBLE_BUILD} != "no"
413REPRO_FLAG="-R"
414.endif
415vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
416	MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT}
417
418vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
419	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
420
421vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
422    $S/kern/vnode_if.src
423vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
424	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
425vnode_if_newproto.h:
426	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
427vnode_if_typedef.h:
428	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
429
430.if ${MFS_IMAGE:Uno} != "no"
431.if empty(MD_ROOT_SIZE_CONFIGURED)
432# Generate an object file from the file system image to embed in the kernel
433# via linking. Make sure the contents are in the mfs section and rename the
434# start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
435# respectively.
436embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
437	${OBJCOPY} --input-target binary \
438	    --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
439	    --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
440	    ${MFS_IMAGE} ${.TARGET}
441	${OBJCOPY} \
442	    --rename-section .data=mfs,contents,alloc,load,readonly,data \
443	    --redefine-sym \
444		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
445	    --redefine-sym \
446		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
447	    --redefine-sym \
448		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
449	    ${.TARGET}
450.endif
451.endif
452
453# XXX strictly, everything depends on Makefile because changes to ${PROF}
454# only appear there, but we don't handle that.
455
456.include "kern.mk"
457