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