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