xref: /freebsd/sys/conf/kern.post.mk (revision 724b4bfdf1306e4f2c451b6d146fe0fe0353b2c8)
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 ${MFS_IMAGE}
125	@rm -f ${.TARGET}
126	@echo linking ${.TARGET}
127	${SYSTEM_LD}
128.if ${MK_CTF} != "no"
129	${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
130.endif
131.if !defined(DEBUG)
132	${OBJCOPY} --strip-debug ${.TARGET}
133.endif
134	${SYSTEM_LD_TAIL}
135.if defined(MFS_IMAGE)
136	sh ${S}/tools/embed_mfs.sh ${FULLKERNEL} ${MFS_IMAGE}
137.endif
138
139.if !exists(${.OBJDIR}/.depend)
140${SYSTEM_OBJS}: assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} ${MFILES:T:S/.m$/.h/}
141.endif
142
143LNFILES=	${CFILES:T:S/.c$/.ln/}
144
145.for mfile in ${MFILES}
146# XXX the low quality .m.o rules gnerated by config are normally used
147# instead of the .m.c rules here.
148${mfile:T:S/.m$/.c/}: ${mfile}
149	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
150${mfile:T:S/.m$/.h/}: ${mfile}
151	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
152.endfor
153
154kernel-clean:
155	rm -f *.o *.so *.So *.ko *.s eddep errs \
156	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.symbols \
157	    linterrs tags vers.c \
158	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
159	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
160	    ${CLEAN}
161
162lint: ${LNFILES}
163	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC} 2>&1 | \
164	    tee -a linterrs
165
166# This is a hack.  BFD "optimizes" away dynamic mode if there are no
167# dynamic references.  We could probably do a '-Bforcedynamic' mode like
168# in the a.out ld.  For now, this works.
169HACK_EXTRA_FLAGS?= -shared
170hack.So: Makefile
171	:> hack.c
172	${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.So
173	rm -f hack.c
174
175# This rule stops ./assym.s in .depend from causing problems.
176./assym.s: assym.s
177
178assym.s: $S/kern/genassym.sh genassym.o
179	NM='${NM}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
180
181genassym.o: $S/$M/$M/genassym.c
182	${CC} -c ${CFLAGS:N-fno-common} $S/$M/$M/genassym.c
183
184${SYSTEM_OBJS} genassym.o vers.o: opt_global.h
185
186# We have "special" -I include paths for opensolaris/zfs files in 'depend'.
187CFILES_NOZFS=	${CFILES:N*/opensolaris/*}
188SFILES_NOZFS=	${SFILES:N*/opensolaris/*}
189CFILES_ZFS=	${CFILES:M*/opensolaris/*}
190SFILES_ZFS=	${SFILES:M*/opensolaris/*}
191
192kernel-depend: .depend
193# The argument list can be very long, so use make -V and xargs to
194# pass it to mkdep.
195SRCS=	assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
196	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
197	${MFILES:T:S/.m$/.h/}
198.depend: .PRECIOUS ${SRCS}
199	rm -f .newdep
200	${MAKE} -V CFILES_NOZFS -V SYSTEM_CFILES -V GEN_CFILES | \
201	    MKDEP_CPP="${CC} -E" CC="${CC}" xargs mkdep -a -f .newdep ${CFLAGS}
202	${MAKE} -V CFILES_ZFS | \
203	    MKDEP_CPP="${CC} -E" CC="${CC}" xargs mkdep -a -f .newdep ${ZFS_CFLAGS}
204	${MAKE} -V SFILES_NOZFS | \
205	    MKDEP_CPP="${CC} -E" xargs mkdep -a -f .newdep ${ASM_CFLAGS}
206	${MAKE} -V SFILES_ZFS | \
207	    MKDEP_CPP="${CC} -E" xargs mkdep -a -f .newdep ${ZFS_ASM_CFLAGS}
208	rm -f .depend
209	mv .newdep .depend
210
211_ILINKS= machine
212.if ${MACHINE} != ${MACHINE_CPUARCH}
213_ILINKS+= ${MACHINE_CPUARCH}
214.endif
215.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
216_ILINKS+= x86
217.endif
218
219# Ensure that the link exists without depending on it when it exists.
220.for _link in ${_ILINKS}
221.if !exists(${.OBJDIR}/${_link})
222${SRCS}: ${_link}
223.endif
224.endfor
225
226${_ILINKS}:
227	@case ${.TARGET} in \
228	machine) \
229		path=${S}/${MACHINE}/include ;; \
230	*) \
231		path=${S}/${.TARGET}/include ;; \
232	esac ; \
233	${ECHO} ${.TARGET} "->" $$path ; \
234	ln -s $$path ${.TARGET}
235
236# .depend needs include links so we remove them only together.
237kernel-cleandepend:
238	rm -f .depend ${_ILINKS}
239
240kernel-tags:
241	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
242	sh $S/conf/systags.sh
243
244kernel-install:
245	@if [ ! -f ${KERNEL_KO} ] ; then \
246		echo "You must build a kernel first." ; \
247		exit 1 ; \
248	fi
249.if exists(${DESTDIR}${KODIR})
250	-thiskernel=`sysctl -n kern.bootfile` ; \
251	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
252		chflags -R noschg ${DESTDIR}${KODIR} ; \
253		rm -rf ${DESTDIR}${KODIR} ; \
254	else \
255		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
256			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
257			rm -rf ${DESTDIR}${KODIR}.old ; \
258		fi ; \
259		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
260		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
261	fi
262.endif
263	mkdir -p ${DESTDIR}${KODIR}
264	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}
265.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
266	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.symbols ${DESTDIR}${KODIR}
267.endif
268.if defined(KERNEL_EXTRA_INSTALL)
269	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}
270.endif
271
272
273
274kernel-reinstall:
275	@-chflags -R noschg ${DESTDIR}${KODIR}
276	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}
277.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
278	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.symbols ${DESTDIR}${KODIR}
279.endif
280
281config.o env.o hints.o vers.o vnode_if.o:
282	${NORMAL_C}
283	${NORMAL_CTFCONVERT}
284
285config.ln env.ln hints.ln vers.ln vnode_if.ln:
286	${NORMAL_LINT}
287
288vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
289	MAKE=${MAKE} sh $S/conf/newvers.sh ${KERN_IDENT}
290
291vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
292	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
293
294vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
295    $S/kern/vnode_if.src
296vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
297	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
298vnode_if_newproto.h:
299	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
300vnode_if_typedef.h:
301	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
302
303# XXX strictly, everything depends on Makefile because changes to ${PROF}
304# only appear there, but we don't handle that.
305
306.include "kern.mk"
307