]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/conf/kern.post.mk
MFC r345519: DTS/DTSO makeoptions
[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)
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
126 .if !defined(NO_MODULES_OBJ)
127 modules-all modules-depend: modules-obj
128 .endif
129 .endif
130
131 .if !defined(DEBUG)
132 FULLKERNEL=     ${KERNEL_KO}
133 .else
134 FULLKERNEL=     ${KERNEL_KO}.full
135 ${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
136         ${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
137             ${FULLKERNEL} ${.TARGET}
138 ${KERNEL_KO}.debug: ${FULLKERNEL}
139         ${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
140 install.debug reinstall.debug: gdbinit
141         cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
142
143 # Install gdbinit files for kernel debugging.
144 gdbinit:
145         grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
146             sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
147         cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
148 .if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
149         cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
150             ${.CURDIR}/gdbinit.machine
151 .endif
152 .endif
153
154 ${FULLKERNEL}: ${SYSTEM_DEP} vers.o
155         @rm -f ${.TARGET}
156         @echo linking ${.TARGET}
157         ${SYSTEM_LD}
158 .if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
159         @sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
160 .endif
161 .if ${MK_CTF} != "no"
162         @echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
163         @${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
164 .endif
165 .if !defined(DEBUG)
166         ${OBJCOPY} --strip-debug ${.TARGET}
167 .endif
168         ${SYSTEM_LD_TAIL}
169
170 OBJS_DEPEND_GUESS+=     offset.inc assym.inc vnode_if.h ${BEFORE_DEPEND:M*.h} \
171                         ${MFILES:T:S/.m$/.h/}
172
173 .for mfile in ${MFILES}
174 # XXX the low quality .m.o rules gnerated by config are normally used
175 # instead of the .m.c rules here.
176 ${mfile:T:S/.m$/.c/}: ${mfile}
177         ${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
178 ${mfile:T:S/.m$/.h/}: ${mfile}
179         ${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
180 .endfor
181
182 kernel-clean:
183         rm -f *.o *.so *.pico *.ko *.s eddep errs \
184             ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
185             tags vers.c \
186             vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
187             ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
188             ${CLEAN}
189
190 # This is a hack.  BFD "optimizes" away dynamic mode if there are no
191 # dynamic references.  We could probably do a '-Bforcedynamic' mode like
192 # in the a.out ld.  For now, this works.
193 HACK_EXTRA_FLAGS?= -shared
194 hack.pico: Makefile
195         :> hack.c
196         ${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.pico
197         rm -f hack.c
198
199 offset.inc: $S/kern/genoffset.sh genoffset.o
200         NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET}
201
202 genoffset.o: $S/kern/genoffset.c
203         ${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/kern/genoffset.c
204
205 # genoffset_test.o is not actually used for anything - the point of compiling it
206 # is to exercise the CTASSERT that checks that the offsets in the offset.inc
207 # _lite struct(s) match those in the original(s). 
208 genoffset_test.o: $S/kern/genoffset.c offset.inc
209         ${CC} -c ${CFLAGS:N-flto:N-fno-common} -DOFFSET_TEST \
210             $S/kern/genoffset.c -o ${.TARGET}
211
212 assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o
213         NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
214
215 genassym.o: $S/$M/$M/genassym.c  offset.inc
216         ${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/$M/$M/genassym.c
217
218 OBJS_DEPEND_GUESS+= opt_global.h
219 genoffset.o genassym.o vers.o: opt_global.h
220
221 .if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
222 _meta_filemon=  1
223 .endif
224 # Skip reading .depend when not needed to speed up tree-walks and simple
225 # lookups.  For install, only do this if no other targets are specified.
226 # Also skip generating or including .depend.* files if in meta+filemon mode
227 # since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used
228 # for _meta_filemon but not for _SKIP_DEPEND.
229 .if !defined(NO_SKIP_DEPEND) && \
230     ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \
231     ${.TARGETS:M*obj} == ${.TARGETS} || \
232     ${.TARGETS:M*clean*} == ${.TARGETS} || \
233     ${.TARGETS:M*install*} == ${.TARGETS})
234 _SKIP_DEPEND=   1
235 .endif
236 .if defined(_SKIP_DEPEND) || defined(_meta_filemon)
237 .MAKE.DEPENDFILE=       /dev/null
238 .endif
239
240 kernel-depend: .depend
241 SRCS=   assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
242         ${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
243         ${MFILES:T:S/.m$/.h/}
244 DEPENDOBJS+=    ${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
245 DEPENDOBJS+=    ${CLEAN:M*.o}
246 DEPENDFILES=    ${DEPENDOBJS:O:u:C/^/.depend./}
247 .if ${MAKE_VERSION} < 20160220
248 DEPEND_MP?=     -MP
249 .endif
250 .if defined(_SKIP_DEPEND)
251 # Don't bother reading any .meta files
252 ${DEPENDOBJS}:  .NOMETA
253 .depend:        .NOMETA
254 # Unset these to avoid looping/statting on them later.
255 .undef DEPENDOBJS
256 .undef DEPENDFILES
257 .endif  # defined(_SKIP_DEPEND)
258 DEPEND_CFLAGS+= -MD ${DEPEND_MP} -MF.depend.${.TARGET}
259 DEPEND_CFLAGS+= -MT${.TARGET}
260 .if !defined(_meta_filemon)
261 .if !empty(DEPEND_CFLAGS)
262 # Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
263 # as those are the only ones we will include.
264 DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
265 CFLAGS+=        ${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
266 .endif
267 .for __depend_obj in ${DEPENDFILES}
268 .if ${MAKE_VERSION} < 20160220
269 .sinclude "${.OBJDIR}/${__depend_obj}"
270 .else
271 .dinclude "${.OBJDIR}/${__depend_obj}"
272 .endif
273 .endfor
274 .endif  # !defined(_meta_filemon)
275
276 # Always run 'make depend' to generate dependencies early and to avoid the
277 # need for manually running it.  For the kernel this is mostly a NOP since
278 # all dependencies are correctly added or accounted for.  This is mostly to
279 # ensure downstream uses of kernel-depend are handled.
280 beforebuild: kernel-depend
281
282 # Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
283 # For meta+filemon the .meta file is checked for since it is the dependency
284 # file used.
285 .for __obj in ${DEPENDOBJS:O:u}
286 .if defined(_meta_filemon)
287 _depfile=       ${.OBJDIR}/${__obj}.meta
288 .else
289 _depfile=       ${.OBJDIR}/.depend.${__obj}
290 .endif
291 .if !exists(${_depfile})
292 .if ${SYSTEM_OBJS:M${__obj}}
293 ${__obj}: ${OBJS_DEPEND_GUESS}
294 .endif
295 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
296 .elif defined(_meta_filemon)
297 # For meta mode we still need to know which file to depend on to avoid
298 # ambiguous suffix transformation rules from .PATH.  Meta mode does not
299 # use .depend files.  We really only need source files, not headers since
300 # they are typically in SRCS/beforebuild already.  For target-specific
301 # guesses do include headers though since they may not be in SRCS.
302 .if ${SYSTEM_OBJS:M${__obj}}
303 ${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
304 .endif
305 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
306 .endif  # !exists(${_depfile})
307 .endfor
308
309 .NOPATH: .depend ${DEPENDFILES}
310
311 .depend: .PRECIOUS ${SRCS}
312
313 _ILINKS= machine
314 .if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
315 _ILINKS+= ${MACHINE_CPUARCH}
316 .endif
317 .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
318 _ILINKS+= x86
319 .endif
320
321 # Ensure that the link exists without depending on it when it exists.
322 .for _link in ${_ILINKS}
323 .if !exists(${.OBJDIR}/${_link})
324 ${SRCS} ${CLEAN:M*.o}: ${_link}
325 .endif
326 .endfor
327
328 ${_ILINKS}:
329         @case ${.TARGET} in \
330         machine) \
331                 path=${S}/${MACHINE}/include ;; \
332         *) \
333                 path=${S}/${.TARGET}/include ;; \
334         esac ; \
335         ${ECHO} ${.TARGET} "->" $$path ; \
336         ln -fns $$path ${.TARGET}
337
338 # .depend needs include links so we remove them only together.
339 kernel-cleandepend: .PHONY
340         rm -f .depend .depend.* ${_ILINKS}
341
342 kernel-tags:
343         @[ -f .depend ] || { echo "you must make depend first"; exit 1; }
344         sh $S/conf/systags.sh
345
346 kernel-install: .PHONY
347         @if [ ! -f ${KERNEL_KO} ] ; then \
348                 echo "You must build a kernel first." ; \
349                 exit 1 ; \
350         fi
351 .if exists(${DESTDIR}${KODIR})
352         -thiskernel=`sysctl -n kern.bootfile` ; \
353         if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
354                 chflags -R noschg ${DESTDIR}${KODIR} ; \
355                 rm -rf ${DESTDIR}${KODIR} ; \
356                 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
357         else \
358                 if [ -d ${DESTDIR}${KODIR}.old ] ; then \
359                         chflags -R noschg ${DESTDIR}${KODIR}.old ; \
360                         rm -rf ${DESTDIR}${KODIR}.old ; \
361                 fi ; \
362                 mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
363                 if [ -n "${KERN_DEBUGDIR}" -a \
364                      -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
365                         rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
366                         mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
367                 fi ; \
368                 sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
369         fi
370 .endif
371         mkdir -p ${DESTDIR}${KODIR}
372         ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
373 .if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
374         mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
375         ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
376 .endif
377 .if defined(KERNEL_EXTRA_INSTALL)
378         ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
379 .endif
380
381
382
383 kernel-reinstall:
384         @-chflags -R noschg ${DESTDIR}${KODIR}
385         ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
386 .if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
387         ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
388 .endif
389
390 config.o env.o hints.o vers.o vnode_if.o:
391         ${NORMAL_C}
392         ${NORMAL_CTFCONVERT}
393
394 .if ${MK_REPRODUCIBLE_BUILD} != "no"
395 REPRO_FLAG="-R"
396 .endif
397 vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
398         MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT}
399
400 vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
401         ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
402
403 vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
404     $S/kern/vnode_if.src
405 vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
406         ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
407 vnode_if_newproto.h:
408         ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
409 vnode_if_typedef.h:
410         ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
411
412 .if ${MFS_IMAGE:Uno} != "no"
413 .if empty(MD_ROOT_SIZE_CONFIGURED)
414 # Generate an object file from the file system image to embed in the kernel
415 # via linking. Make sure the contents are in the mfs section and rename the
416 # start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
417 # respectively.
418 embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
419         ${OBJCOPY} --input-target binary \
420             --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
421             --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
422             ${MFS_IMAGE} ${.TARGET}
423         ${OBJCOPY} \
424             --rename-section .data=mfs,contents,alloc,load,readonly,data \
425             --redefine-sym \
426                 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
427             --redefine-sym \
428                 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
429             --redefine-sym \
430                 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
431             ${.TARGET}
432 .endif
433 .endif
434
435 # XXX strictly, everything depends on Makefile because changes to ${PROF}
436 # only appear there, but we don't handle that.
437
438 .include "kern.mk"