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