xref: /freebsd/sys/conf/kern.post.mk (revision 5ca34122ecdd5abc62bdae39663fec9ac8523d87)
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.MAIN: all
27
28.for target in all clean cleandepend cleandir clobber depend install \
29    obj reinstall tags
30${target}: kernel-${target}
31.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
32${target}: modules-${target}
33modules-${target}:
34	cd $S/modules; ${MKMODULESENV} ${MAKE} \
35	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
36.endif
37.endfor
38
39# Handle ports (as defined by the user) that build kernel modules
40.if !defined(NO_MODULES) && defined(PORTS_MODULES)
41#
42# The ports tree needs some environment variables defined to match the new kernel
43#
44# Ports search for some dependencies in PATH, so add the location of the installed files
45LOCALBASE?=	/usr/local
46# SRC_BASE is how the ports tree refers to the location of the base source files
47.if !defined(SRC_BASE)
48SRC_BASE!=	realpath "${SYSDIR:H}/"
49.endif
50# OSVERSION is used by some ports to determine build options
51.if !defined(OSRELDATE)
52# Definition copied from src/Makefile.inc1
53OSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
54		    ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
55.endif
56# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
57WRKDIRPREFIX?=	${MAKEOBJDIRPREFIX}${SRC_BASE}/sys/${KERNCONF}
58PORTSMODULESENV=\
59	PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
60	SRC_BASE=${SRC_BASE} \
61	OSVERSION=${OSRELDATE} \
62	WRKDIRPREFIX=${WRKDIRPREFIX}
63
64# The WRKDIR needs to be cleaned before building, and trying to change the target
65# with a :C pattern below results in install -> instclean
66all:
67.for __i in ${PORTS_MODULES}
68	@${ECHO} "===> Ports module ${__i} (all)"
69	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean all
70.endfor
71
72.for __target in install reinstall clean
73${__target}: ports-${__target}
74ports-${__target}:
75.for __i in ${PORTS_MODULES}
76	@${ECHO} "===> Ports module ${__i} (${__target})"
77	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/install/deinstall reinstall/:C/reinstall/deinstall reinstall/}
78.endfor
79.endfor
80.endif
81
82.ORDER: kernel-install modules-install
83
84kernel-all: ${KERNEL_KO} ${KERNEL_EXTRA}
85
86kernel-cleandir: kernel-clean kernel-cleandepend
87
88kernel-clobber:
89	find . -maxdepth 1 ! -type d ! -name version -delete
90
91kernel-obj:
92
93.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
94modules: modules-all
95
96.if !defined(NO_MODULES_OBJ)
97modules-all modules-depend: modules-obj
98.endif
99.endif
100
101.if !defined(DEBUG)
102FULLKERNEL=	${KERNEL_KO}
103.else
104FULLKERNEL=	${KERNEL_KO}.debug
105${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.symbols
106	${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.symbols\
107	    ${FULLKERNEL} ${.TARGET}
108${KERNEL_KO}.symbols: ${FULLKERNEL}
109	${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
110install.debug reinstall.debug: gdbinit
111	cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
112
113# Install gdbinit files for kernel debugging.
114gdbinit:
115	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
116	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
117	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
118.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
119	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
120	    ${.CURDIR}/gdbinit.machine
121.endif
122.endif
123
124${FULLKERNEL}: ${SYSTEM_DEP} vers.o
125	@rm -f ${.TARGET}
126	@echo linking ${.TARGET}
127	${SYSTEM_LD}
128.if ${MK_CTF} != "no"
129	@echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
130	@${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
131.endif
132.if !defined(DEBUG)
133	${OBJCOPY} --strip-debug ${.TARGET}
134.endif
135	${SYSTEM_LD_TAIL}
136
137.if !exists(${.OBJDIR}/.depend)
138${SYSTEM_OBJS}: assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} ${MFILES:T:S/.m$/.h/}
139.endif
140
141LNFILES=	${CFILES:T:S/.c$/.ln/}
142
143.for mfile in ${MFILES}
144# XXX the low quality .m.o rules gnerated by config are normally used
145# instead of the .m.c rules here.
146${mfile:T:S/.m$/.c/}: ${mfile}
147	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
148${mfile:T:S/.m$/.h/}: ${mfile}
149	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
150.endfor
151
152kernel-clean:
153	rm -f *.o *.so *.So *.ko *.s eddep errs \
154	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.symbols \
155	    linterrs tags vers.c \
156	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
157	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
158	    ${CLEAN}
159
160lint: ${LNFILES}
161	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC} 2>&1 | \
162	    tee -a linterrs
163
164# This is a hack.  BFD "optimizes" away dynamic mode if there are no
165# dynamic references.  We could probably do a '-Bforcedynamic' mode like
166# in the a.out ld.  For now, this works.
167HACK_EXTRA_FLAGS?= -shared
168hack.So: Makefile
169	:> hack.c
170	${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.So
171	rm -f hack.c
172
173# This rule stops ./assym.s in .depend from causing problems.
174./assym.s: assym.s
175
176assym.s: $S/kern/genassym.sh genassym.o
177	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
178
179genassym.o: $S/$M/$M/genassym.c
180	${CC} -c ${CFLAGS:N-fno-common} $S/$M/$M/genassym.c
181
182${SYSTEM_OBJS} genassym.o vers.o: opt_global.h
183
184# We have "special" -I include paths for zfs/dtrace files in 'depend'.
185CFILES_NOCDDL=	${CFILES:N*/cddl/*:N*fs/nfsclient/nfs_clkdtrace*}
186SFILES_NOCDDL=	${SFILES:N*/cddl/*}
187CFILES_CDDL=	${CFILES:M*/cddl/*}
188SFILES_CDDL=	${SFILES:M*/cddl/*}
189
190kernel-depend: .depend
191# The argument list can be very long, so use make -V and xargs to
192# pass it to mkdep.
193SRCS=	assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
194	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
195	${MFILES:T:S/.m$/.h/}
196.depend: .PRECIOUS ${SRCS}
197	rm -f .newdep
198	${MAKE} -V CFILES_NOCDDL -V SYSTEM_CFILES -V GEN_CFILES | \
199	    MKDEP_CPP="${CC} -E" CC="${CC}" xargs mkdep -a -f .newdep ${CFLAGS}
200	${MAKE} -V CFILES_CDDL | \
201	    MKDEP_CPP="${CC} -E" CC="${CC}" xargs mkdep -a -f .newdep ${ZFS_CFLAGS} ${FBT_CFLAGS} ${DTRACE_CFLAGS}
202	${MAKE} -V SFILES_NOCDDL | \
203	    MKDEP_CPP="${CC} -E" xargs mkdep -a -f .newdep ${ASM_CFLAGS}
204	${MAKE} -V SFILES_CDDL | \
205	    MKDEP_CPP="${CC} -E" xargs mkdep -a -f .newdep ${ZFS_ASM_CFLAGS}
206	rm -f .depend
207	mv .newdep .depend
208
209_ILINKS= machine
210.if ${MACHINE} != ${MACHINE_CPUARCH}
211_ILINKS+= ${MACHINE_CPUARCH}
212.endif
213.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
214_ILINKS+= x86
215.endif
216
217# Ensure that the link exists without depending on it when it exists.
218.for _link in ${_ILINKS}
219.if !exists(${.OBJDIR}/${_link})
220${SRCS} ${CLEAN:M*.o}: ${_link}
221.endif
222.endfor
223
224${_ILINKS}:
225	@case ${.TARGET} in \
226	machine) \
227		path=${S}/${MACHINE}/include ;; \
228	*) \
229		path=${S}/${.TARGET}/include ;; \
230	esac ; \
231	${ECHO} ${.TARGET} "->" $$path ; \
232	ln -s $$path ${.TARGET}
233
234# .depend needs include links so we remove them only together.
235kernel-cleandepend:
236	rm -f .depend ${_ILINKS}
237
238kernel-tags:
239	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
240	sh $S/conf/systags.sh
241
242kernel-install:
243	@if [ ! -f ${KERNEL_KO} ] ; then \
244		echo "You must build a kernel first." ; \
245		exit 1 ; \
246	fi
247.if exists(${DESTDIR}${KODIR})
248	-thiskernel=`sysctl -n kern.bootfile` ; \
249	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
250		chflags -R noschg ${DESTDIR}${KODIR} ; \
251		rm -rf ${DESTDIR}${KODIR} ; \
252	else \
253		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
254			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
255			rm -rf ${DESTDIR}${KODIR}.old ; \
256		fi ; \
257		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
258		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
259	fi
260.endif
261	mkdir -p ${DESTDIR}${KODIR}
262	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}
263.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
264	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.symbols ${DESTDIR}${KODIR}
265.endif
266.if defined(KERNEL_EXTRA_INSTALL)
267	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}
268.endif
269
270
271
272kernel-reinstall:
273	@-chflags -R noschg ${DESTDIR}${KODIR}
274	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}
275.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
276	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.symbols ${DESTDIR}${KODIR}
277.endif
278
279config.o env.o hints.o vers.o vnode_if.o:
280	${NORMAL_C}
281	${NORMAL_CTFCONVERT}
282
283config.ln env.ln hints.ln vers.ln vnode_if.ln:
284	${NORMAL_LINT}
285
286vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
287	MAKE=${MAKE} sh $S/conf/newvers.sh ${KERN_IDENT}
288
289vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
290	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
291
292vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
293    $S/kern/vnode_if.src
294vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
295	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
296vnode_if_newproto.h:
297	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
298vnode_if_typedef.h:
299	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
300
301.if ${MFS_IMAGE:Uno} != "no"
302# Generate an object file from the file system image to embed in the kernel
303# via linking. Make sure the contents are in the mfs section and rename the
304# start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
305# respectively.
306embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
307	${OBJCOPY} --input-target binary \
308	    --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
309	    --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
310	    ${MFS_IMAGE} ${.TARGET}
311	${OBJCOPY} \
312	    --rename-section .data=mfs,contents,alloc,load,readonly,data \
313	    --redefine-sym \
314		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
315	    --redefine-sym \
316		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
317	    --redefine-sym \
318		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
319	    ${.TARGET}
320.endif
321
322# XXX strictly, everything depends on Makefile because changes to ${PROF}
323# only appear there, but we don't handle that.
324
325.include "kern.mk"
326