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