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