tools/build: Don't redefine open() for the linux bootstrap
[freebsd.git] / Makefile.inc1
1 #
2 # $FreeBSD$
3 #
4 # Make command line options:
5 #       -DNO_CLEANDIR run ${MAKE} clean, instead of ${MAKE} cleandir
6 #       -DNO_CLEAN do not clean at all
7 #       -DDB_FROM_SRC use the user/group databases in src/etc instead of
8 #           the system database when installing.
9 #       -DNO_SHARE do not go into share subdir
10 #       -DKERNFAST define NO_KERNEL{CONFIG,CLEAN,OBJ}
11 #       -DNO_KERNELCONFIG do not run config in ${MAKE} buildkernel
12 #       -DNO_KERNELCLEAN do not run ${MAKE} clean in ${MAKE} buildkernel
13 #       -DNO_KERNELOBJ do not run ${MAKE} obj in ${MAKE} buildkernel
14 #       -DNO_ROOT install without using root privilege
15 #       -DWITHOUT_CTF do not run the DTrace CTF conversion tools on built objects
16 #       LOCAL_DIRS="list of dirs" to add additional dirs to the SUBDIR list
17 #       LOCAL_ITOOLS="list of tools" to add additional tools to the ITOOLS list
18 #       LOCAL_LIB_DIRS="list of dirs" to add additional dirs to libraries target
19 #       LOCAL_MTREE="list of mtree files" to process to allow local directories
20 #           to be created before files are installed
21 #       LOCAL_LEGACY_DIRS="list of dirs" to add additional dirs to the legacy
22 #           target
23 #       LOCAL_BSTOOL_DIRS="list of dirs" to add additional dirs to the
24 #           bootstrap-tools target
25 #       LOCAL_TOOL_DIRS="list of dirs" to add additional dirs to the build-tools
26 #           target
27 #       LOCAL_XTOOL_DIRS="list of dirs" to add additional dirs to the
28 #           cross-tools target
29 #       METALOG="path to metadata log" to write permission and ownership
30 #           when NO_ROOT is set.  (default: ${DESTDIR}/${DISTDIR}/METALOG,
31 #           check /etc/make.conf for DISTDIR)
32 #       TARGET="machine" to crossbuild world for a different machine type
33 #       TARGET_ARCH= may be required when a TARGET supports multiple endians
34 #       BUILDENV_SHELL= shell to launch for the buildenv target (def:${SHELL})
35 #       WORLD_FLAGS= additional flags to pass to make(1) during buildworld
36 #       KERNEL_FLAGS= additional flags to pass to make(1) during buildkernel
37 #       SUBDIR_OVERRIDE="list of dirs" to build rather than everything.
38 #           All libraries and includes, and some build tools will still build.
39
40 #
41 # The intended user-driven targets are:
42 # buildworld  - rebuild *everything*, including glue to help do upgrades
43 # installworld- install everything built by "buildworld"
44 # checkworld  - run test suite on installed world
45 # doxygen     - build API documentation of the kernel
46 # update      - convenient way to update your source tree (eg: svn/svnup)
47 #
48 # Standard targets (not defined here) are documented in the makefiles in
49 # /usr/share/mk.  These include:
50 #               obj depend all install clean cleandepend cleanobj
51
52 .if !defined(TARGET) || !defined(TARGET_ARCH)
53 .error "Both TARGET and TARGET_ARCH must be defined."
54 .endif
55
56 .if make(showconfig) || make(test-system-*)
57 _MKSHOWCONFIG=  t
58 .endif
59
60 SRCDIR?=        ${.CURDIR}
61 LOCALBASE?=     /usr/local
62 TIME_ENV ?= time env
63
64 .include "share/mk/src.tools.mk"
65
66 # Cross toolchain changes must be in effect before bsd.compiler.mk
67 # so that gets the right CC, and pass CROSS_TOOLCHAIN to submakes.
68 .if defined(CROSS_TOOLCHAIN)
69 .if exists(${LOCALBASE}/share/toolchains/${CROSS_TOOLCHAIN}.mk)
70 .include "${LOCALBASE}/share/toolchains/${CROSS_TOOLCHAIN}.mk"
71 .elif exists(/usr/share/toolchains/${CROSS_TOOLCHAIN}.mk)
72 .include "/usr/share/toolchains/${CROSS_TOOLCHAIN}.mk"
73 .elif exists(${CROSS_TOOLCHAIN})
74 .include "${CROSS_TOOLCHAIN}"
75 .else
76 .error CROSS_TOOLCHAIN ${CROSS_TOOLCHAIN} not found
77 .endif
78 CROSSENV+=CROSS_TOOLCHAIN="${CROSS_TOOLCHAIN}"
79 .endif
80 .if defined(CROSS_TOOLCHAIN_PREFIX)
81 CROSS_COMPILER_PREFIX?=${CROSS_TOOLCHAIN_PREFIX}
82 .endif
83
84 XCOMPILERS=     CC CXX CPP
85 .for COMPILER in ${XCOMPILERS}
86 .if defined(CROSS_COMPILER_PREFIX)
87 X${COMPILER}?=  ${CROSS_COMPILER_PREFIX}${${COMPILER}}
88 .else
89 X${COMPILER}?=  ${${COMPILER}}
90 .endif
91 .endfor
92 # If a full path to an external cross compiler is given, don't build
93 # a cross compiler.
94 .if ${XCC:N${CCACHE_BIN}:M/*}
95 MK_CLANG_BOOTSTRAP=     no
96 .endif
97
98 # Pull in compiler metadata from buildworld/toolchain if possible to avoid
99 # running CC from bsd.compiler.mk.
100 .if make(installworld) || make(install) || make(distributeworld) || \
101     make(stageworld)
102 .-include "${OBJTOP}/toolchain-metadata.mk"
103 .if !defined(_LOADED_TOOLCHAIN_METADATA)
104 .error A build is required first.  You may have the wrong MAKEOBJDIRPREFIX set.
105 .endif
106 .endif
107
108 # Pull in COMPILER_TYPE and COMPILER_FREEBSD_VERSION early. Pull it from the
109 # tree to be friendlier to foreign OS builds. It's safe to do so unconditionally
110 # here since we will always have the right make, unlike in src/Makefile
111 # Don't include bsd.linker.mk yet until XBINUTILS is handled (after src.opts.mk)
112 _NO_INCLUDE_LINKERMK=   t
113 # We also want the X_COMPILER* variables if we are using an external toolchain.
114 _WANT_TOOLCHAIN_CROSS_VARS=     t
115 .include "share/mk/bsd.compiler.mk"
116 .undef _NO_INCLUDE_LINKERMK
117 .undef _WANT_TOOLCHAIN_CROSS_VARS
118 # src.opts.mk depends on COMPILER_FEATURES
119 .include "share/mk/src.opts.mk"
120
121 .if ${TARGET} == ${MACHINE}
122 TARGET_CPUTYPE?=${CPUTYPE}
123 .else
124 TARGET_CPUTYPE?=
125 .endif
126 .if !empty(TARGET_CPUTYPE)
127 _TARGET_CPUTYPE=${TARGET_CPUTYPE}
128 .else
129 _TARGET_CPUTYPE=dummy
130 .endif
131 .if ${TARGET} == "arm"
132 .if ${TARGET_ARCH:Marmv[67]*} != "" && ${TARGET_CPUTYPE:M*soft*} == ""
133 TARGET_ABI=     gnueabihf
134 .else
135 TARGET_ABI=     gnueabi
136 .endif
137 .endif
138 MACHINE_ABI?=   unknown
139 MACHINE_TRIPLE?=${MACHINE_ARCH:S/amd64/x86_64/:C/[hs]f$//:S/mipsn32/mips64/}-${MACHINE_ABI}-freebsd14.0
140 TARGET_ABI?=    unknown
141 TARGET_TRIPLE?= ${TARGET_ARCH:S/amd64/x86_64/:C/[hs]f$//:S/mipsn32/mips64/}-${TARGET_ABI}-freebsd14.0
142 KNOWN_ARCHES?=  aarch64/arm64 \
143                 amd64 \
144                 armv6/arm \
145                 armv7/arm \
146                 i386 \
147                 mips \
148                 mipsel/mips \
149                 mips64el/mips \
150                 mipsn32el/mips \
151                 mips64/mips \
152                 mipsn32/mips \
153                 mipshf/mips \
154                 mipselhf/mips \
155                 mips64elhf/mips \
156                 mips64hf/mips \
157                 powerpc \
158                 powerpc64/powerpc \
159                 powerpc64le/powerpc \
160                 powerpcspe/powerpc \
161                 riscv64/riscv \
162                 riscv64sf/riscv
163
164 .if ${TARGET} == ${TARGET_ARCH}
165 _t=             ${TARGET}
166 .else
167 _t=             ${TARGET_ARCH}/${TARGET}
168 .endif
169 .for _t in ${_t}
170 .if empty(KNOWN_ARCHES:M${_t})
171 .error Unknown target ${TARGET_ARCH}:${TARGET}.
172 .endif
173 .endfor
174
175 .if ${.MAKE.OS} != "FreeBSD"
176 CROSSBUILD_HOST=${.MAKE.OS}
177 .if ${.MAKE.OS} != "Linux" && ${.MAKE.OS} != "Darwin"
178 .warning "Unsupported crossbuild system: ${.MAKE.OS}. Build will probably fail!"
179 .endif
180 # We need to force NO_ROOT/DB_FROM_SRC builds when building on other operating
181 # systems since the BSD.foo.dist specs contain users and groups that do not
182 # exist by default on a Linux/MacOS system.
183 NO_ROOT:=       1
184 DB_FROM_SRC:=   1
185 .export NO_ROOT
186 .endif
187
188 # If all targets are disabled for system llvm then don't expect it to work
189 # for cross-builds.
190 .if !defined(TOOLS_PREFIX) && ${MK_LLVM_TARGET_ALL} == "no" && \
191     ${MACHINE} != ${TARGET} && ${MACHINE_ARCH} != ${TARGET_ARCH} && \
192     !make(showconfig)
193 MK_SYSTEM_COMPILER=     no
194 MK_SYSTEM_LINKER=       no
195 .endif
196
197 # Handle external binutils.
198 .if defined(CROSS_TOOLCHAIN_PREFIX)
199 CROSS_BINUTILS_PREFIX?=${CROSS_TOOLCHAIN_PREFIX}
200 .endif
201 XBINUTILS=      AS AR ELFCTL LD NM OBJCOPY RANLIB SIZE STRINGS STRIPBIN
202 .for BINUTIL in ${XBINUTILS}
203 .if defined(CROSS_BINUTILS_PREFIX) && \
204     exists(${CROSS_BINUTILS_PREFIX}/${${BINUTIL}})
205 X${BINUTIL}?=   ${CROSS_BINUTILS_PREFIX:C,/*$,,}/${${BINUTIL}}
206 .else
207 X${BINUTIL}?=   ${${BINUTIL}}
208 .endif
209 .endfor
210
211 # If a full path to an external linker is given, don't build lld.
212 .if ${XLD:M/*}
213 MK_LLD_BOOTSTRAP=       no
214 .endif
215
216 # We also want the X_LINKER* variables if we are using an external toolchain.
217 _WANT_TOOLCHAIN_CROSS_VARS=     t
218 .include "share/mk/bsd.linker.mk"
219 .undef _WANT_TOOLCHAIN_CROSS_VARS
220
221 # Begin WITH_SYSTEM_COMPILER / WITH_SYSTEM_LD
222
223 # WITH_SYSTEM_COMPILER - Pull in needed values and make a decision.
224
225 # Check if there is a local compiler that can satisfy as an external compiler.
226 # Which compiler is expected to be used?
227 .if ${MK_CLANG_BOOTSTRAP} == "yes"
228 WANT_COMPILER_TYPE=     clang
229 .else
230 WANT_COMPILER_TYPE=
231 .endif
232
233 .if !defined(WANT_COMPILER_FREEBSD_VERSION) && !make(showconfig) && \
234     !make(test-system-linker)
235 .if ${WANT_COMPILER_TYPE} == "clang"
236 WANT_COMPILER_FREEBSD_VERSION_FILE= lib/clang/freebsd_cc_version.h
237 WANT_COMPILER_FREEBSD_VERSION!= \
238         awk '$$2 == "FREEBSD_CC_VERSION" {printf("%d\n", $$3)}' \
239         ${SRCDIR}/${WANT_COMPILER_FREEBSD_VERSION_FILE} || echo unknown
240 WANT_COMPILER_VERSION_FILE= lib/clang/include/clang/Basic/Version.inc
241 WANT_COMPILER_VERSION!= \
242         awk '$$2 == "CLANG_VERSION" {split($$3, a, "."); print a[1] * 10000 + a[2] * 100 + a[3]}' \
243         ${SRCDIR}/${WANT_COMPILER_VERSION_FILE} || echo unknown
244 .endif
245 .export WANT_COMPILER_FREEBSD_VERSION WANT_COMPILER_VERSION
246 .endif  # !defined(WANT_COMPILER_FREEBSD_VERSION)
247
248 # It needs to be the same revision as we would build for the bootstrap.
249 # If the expected vs CC is different then we can't skip.
250 # GCC cannot be used for cross-arch yet.  For clang we pass -target later if
251 # TARGET_ARCH!=MACHINE_ARCH.
252 .if ${MK_SYSTEM_COMPILER} == "yes" && \
253     defined(WANT_COMPILER_FREEBSD_VERSION) && \
254     ${MK_CLANG_BOOTSTRAP} == "yes" && \
255     !make(xdev*) && \
256     ${X_COMPILER_TYPE} == ${WANT_COMPILER_TYPE} && \
257     (${X_COMPILER_TYPE} == "clang" || ${TARGET_ARCH} == ${MACHINE_ARCH}) && \
258     ${X_COMPILER_VERSION} == ${WANT_COMPILER_VERSION} && \
259     ${X_COMPILER_FREEBSD_VERSION} == ${WANT_COMPILER_FREEBSD_VERSION}
260 # Everything matches, disable the bootstrap compiler.
261 MK_CLANG_BOOTSTRAP=     no
262 USING_SYSTEM_COMPILER=  yes
263 .endif  # ${WANT_COMPILER_TYPE} == ${COMPILER_TYPE}
264
265 # WITH_SYSTEM_LD - Pull in needed values and make a decision.
266
267 # Check if there is a local linker that can satisfy as an external linker.
268 # Which linker is expected to be used?
269 .if ${MK_LLD_BOOTSTRAP} == "yes"
270 WANT_LINKER_TYPE=               lld
271 .else
272 WANT_LINKER_TYPE=
273 .endif
274
275 .if !defined(WANT_LINKER_FREEBSD_VERSION) && !make(showconfig) && \
276     !make(test-system-compiler)
277 .if ${WANT_LINKER_TYPE} == "lld"
278 WANT_LINKER_FREEBSD_VERSION_FILE= lib/clang/include/VCSVersion.inc
279 _WANT_LINKER_FREEBSD_VERSION!= \
280         awk '$$2 == "LLD_REVISION" {gsub(/"/, "", $$3); print $$3}' \
281         ${SRCDIR}/${WANT_LINKER_FREEBSD_VERSION_FILE} || echo unknown
282 WANT_LINKER_FREEBSD_VERSION=${_WANT_LINKER_FREEBSD_VERSION:C/.*-(.*)/\1/}
283 WANT_LINKER_VERSION_FILE= lib/clang/include/lld/Common/Version.inc
284 WANT_LINKER_VERSION!= \
285         awk '$$2 == "LLD_VERSION_STRING" { gsub("\"", "", $$3); split($$3, a, "."); print a[1] * 10000 + a[2] * 100 + a[3]}' \
286         ${SRCDIR}/${WANT_LINKER_VERSION_FILE} || echo unknown
287 .else
288 WANT_LINKER_FREEBSD_VERSION_FILE=
289 WANT_LINKER_FREEBSD_VERSION=
290 .endif
291 .export WANT_LINKER_FREEBSD_VERSION WANT_LINKER_VERSION
292 .endif  # !defined(WANT_LINKER_FREEBSD_VERSION)
293
294 .if ${MK_SYSTEM_LINKER} == "yes" && \
295     defined(WANT_LINKER_FREEBSD_VERSION) && \
296     (${MK_LLD_BOOTSTRAP} == "yes") && \
297     !make(xdev*) && \
298     ${X_LINKER_TYPE} == ${WANT_LINKER_TYPE} && \
299     ${X_LINKER_VERSION} == ${WANT_LINKER_VERSION} && \
300     ${X_LINKER_FREEBSD_VERSION} == ${WANT_LINKER_FREEBSD_VERSION}
301 # Everything matches, disable the bootstrap linker.
302 MK_LLD_BOOTSTRAP=       no
303 USING_SYSTEM_LINKER=    yes
304 .endif  # ${WANT_LINKER_TYPE} == ${LINKER_TYPE}
305
306 # WITH_SYSTEM_COMPILER / WITH_SYSTEM_LINKER - Handle defaults and debug.
307 USING_SYSTEM_COMPILER?= no
308 USING_SYSTEM_LINKER?=   no
309
310 TEST_SYSTEM_COMPILER_VARS= \
311         USING_SYSTEM_COMPILER MK_SYSTEM_COMPILER \
312         MK_CROSS_COMPILER MK_CLANG_BOOTSTRAP \
313         WANT_COMPILER_TYPE WANT_COMPILER_VERSION WANT_COMPILER_VERSION_FILE \
314         WANT_COMPILER_FREEBSD_VERSION WANT_COMPILER_FREEBSD_VERSION_FILE \
315         CC COMPILER_TYPE COMPILER_FEATURES COMPILER_VERSION \
316         COMPILER_FREEBSD_VERSION \
317         XCC X_COMPILER_TYPE X_COMPILER_FEATURES X_COMPILER_VERSION \
318         X_COMPILER_FREEBSD_VERSION
319 TEST_SYSTEM_LINKER_VARS= \
320         USING_SYSTEM_LINKER MK_SYSTEM_LINKER \
321         MK_LLD_BOOTSTRAP \
322         WANT_LINKER_TYPE WANT_LINKER_VERSION WANT_LINKER_VERSION_FILE \
323         WANT_LINKER_FREEBSD_VERSION WANT_LINKER_FREEBSD_VERSION_FILE \
324         LD LINKER_TYPE LINKER_FEATURES LINKER_VERSION \
325         LINKER_FREEBSD_VERSION \
326         XLD X_LINKER_TYPE X_LINKER_FEATURES X_LINKER_VERSION \
327         X_LINKER_FREEBSD_VERSION
328
329 .for _t in compiler linker
330 test-system-${_t}: .PHONY
331 .for v in ${TEST_SYSTEM_${_t:tu}_VARS}
332         ${_+_}@printf "%-35s= %s\n" "${v}" "${${v}}"
333 .endfor
334 .endfor
335 .if (make(buildworld) || make(buildkernel) || make(kernel-toolchain) || \
336     make(toolchain) || make(_cross-tools))
337 .if ${USING_SYSTEM_COMPILER} == "yes"
338 .info SYSTEM_COMPILER: Determined that CC=${CC} matches the source tree.  Not bootstrapping a cross-compiler.
339 .elif ${MK_CLANG_BOOTSTRAP} == "yes"
340 .info SYSTEM_COMPILER: libclang will be built for bootstrapping a cross-compiler.
341 .endif
342 .if ${USING_SYSTEM_LINKER} == "yes"
343 .info SYSTEM_LINKER: Determined that LD=${LD} matches the source tree.  Not bootstrapping a cross-linker.
344 .elif ${MK_LLD_BOOTSTRAP} == "yes"
345 .info SYSTEM_LINKER: libclang will be built for bootstrapping a cross-linker.
346 .endif
347 .endif
348
349 # End WITH_SYSTEM_COMPILER / WITH_SYSTEM_LD
350
351 # Store some compiler metadata for use in installworld where we don't
352 # want to invoke CC at all.
353 _TOOLCHAIN_METADATA_VARS=       COMPILER_VERSION \
354                                 COMPILER_TYPE \
355                                 COMPILER_FEATURES \
356                                 COMPILER_FREEBSD_VERSION \
357                                 COMPILER_RESOURCE_DIR \
358                                 LINKER_VERSION \
359                                 LINKER_FEATURES \
360                                 LINKER_TYPE \
361                                 LINKER_FREEBSD_VERSION
362 toolchain-metadata.mk: .PHONY .META
363         @: > ${.TARGET}
364         @echo ".info Using cached toolchain metadata from build at $$(hostname) on $$(date)" \
365             > ${.TARGET}
366         @echo "_LOADED_TOOLCHAIN_METADATA=t" >> ${.TARGET}
367 .for v in ${_TOOLCHAIN_METADATA_VARS}
368         @echo "${v}=${${v}}" >> ${.TARGET}
369         @echo "X_${v}=${X_${v}}" >> ${.TARGET}
370 .endfor
371         @echo ".export ${_TOOLCHAIN_METADATA_VARS}" >> ${.TARGET}
372         @echo ".export ${_TOOLCHAIN_METADATA_VARS:C,^,X_,}" >> ${.TARGET}
373
374
375 # We must do lib/ and libexec/ before bin/ in case of a mid-install error to
376 # keep the users system reasonably usable.  For static->dynamic root upgrades,
377 # we don't want to install a dynamic binary without rtld and the needed
378 # libraries.  More commonly, for dynamic root, we don't want to install a
379 # binary that requires a newer library version that hasn't been installed yet.
380 # This ordering is not a guarantee though.  The only guarantee of a working
381 # system here would require fine-grained ordering of all components based
382 # on their dependencies.
383 .if !empty(SUBDIR_OVERRIDE)
384 SUBDIR= ${SUBDIR_OVERRIDE}
385 .else
386 SUBDIR= lib libexec
387 # Add LOCAL_LIB_DIRS, but only if they will not be picked up as a SUBDIR
388 # of a LOCAL_DIRS directory.  This allows LOCAL_DIRS=foo and
389 # LOCAL_LIB_DIRS=foo/lib to behave as expected.
390 .for _DIR in ${LOCAL_DIRS:M*/} ${LOCAL_DIRS:N*/:S|$|/|}
391 _REDUNDANT_LIB_DIRS+=    ${LOCAL_LIB_DIRS:M${_DIR}*}
392 .endfor
393 .for _DIR in ${LOCAL_LIB_DIRS}
394 .if ${_DIR} == ".WAIT" || (empty(_REDUNDANT_LIB_DIRS:M${_DIR}) && exists(${.CURDIR}/${_DIR}/Makefile))
395 SUBDIR+=        ${_DIR}
396 .endif
397 .endfor
398 .if !defined(NO_ROOT) && (make(installworld) || make(install))
399 # Ensure libraries are installed before progressing.
400 SUBDIR+=.WAIT
401 .endif
402 SUBDIR+=bin
403 .if ${MK_CDDL} != "no"
404 SUBDIR+=cddl
405 .endif
406 SUBDIR+=gnu include
407 .if ${MK_KERBEROS} != "no"
408 SUBDIR+=kerberos5
409 .endif
410 .if ${MK_RESCUE} != "no"
411 SUBDIR+=rescue
412 .endif
413 SUBDIR+=sbin
414 .if ${MK_CRYPT} != "no"
415 SUBDIR+=secure
416 .endif
417 .if !defined(NO_SHARE)
418 SUBDIR+=share
419 .endif
420 .if ${MK_BOOT} != "no"
421 SUBDIR+=stand
422 .endif
423 SUBDIR+=sys usr.bin usr.sbin
424 .if ${MK_TESTS} != "no"
425 SUBDIR+=        tests
426 .endif
427
428 # Local directories are built in parallel with the base system directories.
429 # Users may insert a .WAIT directive at the beginning or elsewhere within
430 # the LOCAL_DIRS and LOCAL_LIB_DIRS lists as needed.
431 .for _DIR in ${LOCAL_DIRS}
432 .if ${_DIR} == ".WAIT" || exists(${.CURDIR}/${_DIR}/Makefile)
433 SUBDIR+=        ${_DIR}
434 .endif
435 .endfor
436
437 # We must do etc/ last as it hooks into building the man whatis file
438 # by calling 'makedb' in share/man.  This is only relevant for
439 # install/distribute so they build the whatis file after every manpage is
440 # installed.
441 .if make(installworld) || make(install)
442 SUBDIR+=.WAIT
443 .endif
444 SUBDIR+=etc
445
446 .endif  # !empty(SUBDIR_OVERRIDE)
447
448 .if defined(NOCLEAN)
449 .warning The src.conf WITHOUT_CLEAN option can now be used instead of NOCLEAN.
450 MK_CLEAN:=      no
451 .endif
452 .if defined(NO_CLEAN)
453 .info The src.conf WITHOUT_CLEAN option can now be used instead of NO_CLEAN.
454 MK_CLEAN:=      no
455 .endif
456 .if defined(NO_CLEANDIR)
457 CLEANDIR=       clean cleandepend
458 .else
459 CLEANDIR=       cleandir
460 .endif
461
462 .if defined(WORLDFAST)
463 MK_CLEAN:=      no
464 NO_OBJWALK=     t
465 .endif
466
467 .if ${MK_META_MODE} == "yes"
468 # If filemon is used then we can rely on the build being incremental-safe.
469 # The .meta files will also track the build command and rebuild should
470 # it change.
471 .if empty(.MAKE.MODE:Mnofilemon)
472 MK_CLEAN:=      no
473 .endif
474 .endif
475 .if defined(NO_OBJWALK) || ${MK_AUTO_OBJ} == "yes"
476 NO_OBJWALK=     t
477 NO_KERNELOBJ=   t
478 .endif
479 .if !defined(NO_OBJWALK)
480 _obj=           obj
481 .endif
482
483 LOCAL_TOOL_DIRS?=
484 PACKAGEDIR?=    ${DESTDIR}/${DISTDIR}
485
486 .if empty(SHELL:M*csh*)
487 BUILDENV_SHELL?=${SHELL}
488 .else
489 BUILDENV_SHELL?=/bin/sh
490 .endif
491
492 .if !defined(_MKSHOWCONFIG)
493 .if !defined(VCS_REVISION) || empty(VCS_REVISION)
494 .if !defined(SVNVERSION_CMD) || empty(SVNVERSION_CMD)
495 . for _D in ${PATH:S,:, ,g}
496 .  if exists(${_D}/svnversion)
497 SVNVERSION_CMD?=${_D}/svnversion
498 .  endif
499 .  if exists(${_D}/svnliteversion)
500 SVNVERSION_CMD?=${_D}/svnliteversion
501 .  endif
502 . endfor
503 .endif
504 _VCS_REVISION?= $$(eval ${SVNVERSION_CMD} ${SRCDIR})
505 . if !empty(_VCS_REVISION)
506 VCS_REVISION=   $$(echo r${_VCS_REVISION})
507 . endif
508 .export VCS_REVISION
509 .endif
510
511 .if !defined(GIT_CMD) || empty(GIT_CMD)
512 . for _P in /usr/bin /usr/local/bin
513 .  if exists(${_P}/git)
514 GIT_CMD=   ${_P}/git
515 .  endif
516 . endfor
517 .export GIT_CMD
518 .endif
519
520 .if !defined(OSRELDATE)
521 .if exists(/usr/include/osreldate.h)
522 OSRELDATE!=     awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
523                 /usr/include/osreldate.h
524 .else
525 OSRELDATE=      0
526 .endif
527 .export OSRELDATE
528 .endif
529
530 # Set VERSION for CTFMERGE to use via the default CTFFLAGS=-L VERSION.
531 .for _V in BRANCH REVISION
532 .if !defined(_${_V})
533 _${_V}!=        eval $$(awk '/^${_V}=/{print}' ${SRCTOP}/sys/conf/newvers.sh); echo $$${_V}
534 .export _${_V}
535 .endif
536 .endfor
537 .if !defined(SRCRELDATE)
538 SRCRELDATE!=    awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
539                 ${SRCDIR}/sys/sys/param.h
540 .export SRCRELDATE
541 .endif
542 .if !defined(VERSION)
543 VERSION=        FreeBSD ${_REVISION}-${_BRANCH:C/-p[0-9]+$//} ${TARGET_ARCH} ${SRCRELDATE}
544 .export VERSION
545 .endif
546
547 .if !defined(PKG_VERSION)
548 _STRTIMENOW=    %Y%m%d%H%M%S
549 _TIMENOW=       ${_STRTIMENOW:gmtime}
550 .if ${_BRANCH:MCURRENT*} || ${_BRANCH:MSTABLE*} || ${_BRANCH:MPRERELEASE*}
551 _REVISION:=     ${_REVISION:R}
552 EXTRA_REVISION= .snap${_TIMENOW}
553 .elif ${_BRANCH:MALPHA*}
554 EXTRA_REVISION= .a${_BRANCH:C/ALPHA([0-9]+).*/\1/}.${_TIMENOW}
555 .elif ${_BRANCH:MBETA*}
556 EXTRA_REVISION= .b${_BRANCH:C/BETA([0-9]+).*/\1/}.${_TIMENOW}
557 .elif ${_BRANCH:MRC*}
558 EXTRA_REVISION= .rc${_BRANCH:C/RC([0-9]+).*/\1/}.${_TIMENOW}
559 .elif ${_BRANCH:M*-p*}
560 EXTRA_REVISION= p${_BRANCH:C/.*-p([0-9]+$)/\1/}
561 .endif
562 PKG_VERSION:=   ${_REVISION}${EXTRA_REVISION:C/[[:space:]]//g}
563 .endif
564 .endif  # !defined(PKG_VERSION)
565
566 .if !defined(PKG_TIMESTAMP)
567 TIMEEPOCHNOW=           %s
568 SOURCE_DATE_EPOCH=      ${TIMEEPOCHNOW:gmtime}
569 .else
570 SOURCE_DATE_EPOCH=      ${PKG_TIMESTAMP}
571 .endif
572
573 PKG_NAME_PREFIX?=       FreeBSD
574 PKG_MAINTAINER?=        re@FreeBSD.org
575 PKG_WWW?=               https://www.FreeBSD.org
576 .export PKG_NAME_PREFIX
577 .export PKG_MAINTAINER
578 .export PKG_WWW
579
580 .if !defined(_MKSHOWCONFIG)
581 _CPUTYPE!=      MAKEFLAGS= CPUTYPE=${_TARGET_CPUTYPE} ${MAKE} -f /dev/null \
582                 -m ${.CURDIR}/share/mk MK_AUTO_OBJ=no -V CPUTYPE
583 .if ${_CPUTYPE} != ${_TARGET_CPUTYPE}
584 .error CPUTYPE global should be set with ?=.
585 .endif
586 .endif
587 .if make(buildworld)
588 BUILD_ARCH!=    uname -p
589 # On some Linux systems uname -p returns "unknown" so skip this check there.
590 # This check only exists to tell people to use TARGET_ARCH instead of
591 # MACHINE_ARCH so skipping it when crossbuilding on non-FreeBSD should be fine.
592 .if ${MACHINE_ARCH} != ${BUILD_ARCH} && ${.MAKE.OS} == "FreeBSD"
593 .error To cross-build, set TARGET_ARCH.
594 .endif
595 .endif
596 WORLDTMP?=      ${OBJTOP}/tmp
597 BPATH=          ${CCACHE_WRAPPER_PATH_PFX}${WORLDTMP}/legacy/usr/sbin:${WORLDTMP}/legacy/usr/bin:${WORLDTMP}/legacy/bin:${WORLDTMP}/legacy/usr/libexec
598 XPATH=          ${WORLDTMP}/bin:${WORLDTMP}/usr/sbin:${WORLDTMP}/usr/bin
599
600 # When building we want to find the cross tools before the host tools in ${BPATH}.
601 # We also need to add UNIVERSE_TOOLCHAIN_PATH so that we can find the shared
602 # toolchain files (clang, lld, etc.) during make universe/tinderbox
603 STRICTTMPPATH=  ${XPATH}:${BPATH}:${UNIVERSE_TOOLCHAIN_PATH}
604 # We should not be using tools from /usr/bin accidentally since this could cause
605 # the build to break on other systems that don't have that tool. For now we
606 # still allow using the old behaviour (inheriting $PATH) if
607 # BUILD_WITH_STRICT_TMPPATH is set to 0 but this will eventually be removed.
608
609 # Currently strict $PATH can cause build failures and does not work yet with
610 # USING_SYSTEM_LINKER/USING_SYSTEM_COMPILER. Once these issues have been
611 # resolved it will be turned on by default.
612 BUILD_WITH_STRICT_TMPPATH?=0
613 .if defined(CROSSBUILD_HOST)
614 # When building on non-FreeBSD we can't rely on the tools in /usr/bin being compatible
615 # with what FreeBSD expects. Therefore we only use tools from STRICTTMPPATH
616 # during the world build stage. We build most tools during the bootstrap-tools
617 # phase but symlink host tools that are known to work instead of building them
618 BUILD_WITH_STRICT_TMPPATH:=1
619 .endif
620 .if ${BUILD_WITH_STRICT_TMPPATH} != 0
621 TMPPATH=        ${STRICTTMPPATH}
622 .else
623 TMPPATH=        ${STRICTTMPPATH}:${PATH}
624 .endif
625
626 #
627 # Avoid running mktemp(1) unless actually needed.
628 # It may not be functional, e.g., due to new ABI
629 # when in the middle of installing over this system.
630 #
631 .if make(distributeworld) || make(installworld) || make(stageworld)
632 .if ${BUILD_WITH_STRICT_TMPPATH} != 0
633 MKTEMP=${WORLDTMP}/legacy/usr/bin/mktemp
634 .if !exists(${MKTEMP})
635 .error "mktemp binary doesn't exist in expected location: ${MKTEMP}"
636 .endif
637 .else
638 MKTEMP=mktemp
639 .endif
640 INSTALLTMP!=    ${MKTEMP} -d -u -t install
641 .endif
642
643 .if make(stagekernel) || make(distributekernel)
644 TAGS+=          kernel
645 PACKAGE=        kernel
646 .endif
647
648 #
649 # Building a world goes through the following stages
650 #
651 # 1. legacy stage [BMAKE]
652 #       This stage is responsible for creating compatibility
653 #       shims that are needed by the bootstrap-tools,
654 #       build-tools and cross-tools stages. These are generally
655 #       APIs that tools from one of those three stages need to
656 #       build that aren't present on the host.
657 # 1. bootstrap-tools stage [BMAKE]
658 #       This stage is responsible for creating programs that
659 #       are needed for backward compatibility reasons. They
660 #       are not built as cross-tools.
661 # 2. build-tools stage [TMAKE]
662 #       This stage is responsible for creating the object
663 #       tree and building any tools that are needed during
664 #       the build process. Some programs are listed during
665 #       this phase because they build binaries to generate
666 #       files needed to build these programs. This stage also
667 #       builds the 'build-tools' target rather than 'all'.
668 # 3. cross-tools stage [XMAKE]
669 #       This stage is responsible for creating any tools that
670 #       are needed for building the system. A cross-compiler is one
671 #       of them. This differs from build tools in two ways:
672 #       1. the 'all' target is built rather than 'build-tools'
673 #       2. these tools are installed into TMPPATH for stage 4.
674 # 4. world stage [WMAKE]
675 #       This stage actually builds the world.
676 # 5. install stage (optional) [IMAKE]
677 #       This stage installs a previously built world.
678 #
679
680 BOOTSTRAPPING?= 0
681 # Keep these in sync
682 MINIMUM_SUPPORTED_OSREL?= 1002501
683 MINIMUM_SUPPORTED_REL?= 10.3
684
685 # Common environment for world related stages
686 CROSSENV+=      \
687                 MACHINE_ARCH=${TARGET_ARCH} \
688                 MACHINE=${TARGET} \
689                 CPUTYPE=${TARGET_CPUTYPE}
690 .if ${MK_META_MODE} != "no"
691 # Don't rebuild build-tools targets during normal build.
692 CROSSENV+=      BUILD_TOOLS_META=.NOMETA
693 .endif
694 .if defined(TARGET_CFLAGS)
695 CROSSENV+=      ${TARGET_CFLAGS}
696 .endif
697 .if (${TARGET} != ${MACHINE} && !defined(WITH_LOCAL_MODULES)) || \
698     defined(WITHOUT_LOCAL_MODULES)
699 CROSSENV+=      LOCAL_MODULES=
700 .endif
701
702 BOOTSTRAPPING_OSRELDATE?=${OSRELDATE}
703
704 # bootstrap-tools stage
705 BMAKEENV=       INSTALL="sh ${.CURDIR}/tools/install.sh" \
706                 TOOLS_PREFIX=${TOOLS_PREFIX_UNDEF:U${WORLDTMP}} \
707                 PATH=${BPATH}:${PATH} \
708                 WORLDTMP=${WORLDTMP} \
709                 MAKEFLAGS="-m ${.CURDIR}/tools/build/mk ${.MAKEFLAGS}"
710 # need to keep this in sync with targets/pseudo/bootstrap-tools/Makefile
711 BSARGS=         DESTDIR= \
712                 OBJTOP='${WORLDTMP}/obj-tools' \
713                 OBJROOT='$${OBJTOP}/' \
714                 MAKEOBJDIRPREFIX= \
715                 BOOTSTRAPPING=${BOOTSTRAPPING_OSRELDATE} \
716                 BWPHASE=${.TARGET:C,^_,,} \
717                 SSP_CFLAGS= \
718                 MK_HTML=no NO_LINT=yes MK_MAN=no MK_MAN_UTILS=yes \
719                 -DNO_PIC MK_PROFILE=no -DNO_SHARED \
720                 -DNO_CPU_CFLAGS MK_WERROR=no MK_CTF=no \
721                 MK_CLANG_EXTRAS=no MK_CLANG_FORMAT=no MK_CLANG_FULL=no \
722                 MK_LLDB=no MK_RETPOLINE=no MK_TESTS=no \
723                 MK_INCLUDES=yes
724
725 BMAKE=          \
726                 ${TIME_ENV} ${BMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
727                 ${BSARGS}
728 .if empty(.MAKEOVERRIDES:MMK_LLVM_TARGET_ALL)
729 BMAKE+=         MK_LLVM_TARGET_ALL=no
730 .endif
731
732 # build-tools stage
733 TMAKE=          \
734                 ${TIME_ENV} ${BMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
735                 TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
736                 DESTDIR= \
737                 BOOTSTRAPPING=${BOOTSTRAPPING_OSRELDATE} \
738                 BWPHASE=${.TARGET:C,^_,,} \
739                 SSP_CFLAGS= \
740                 -DNO_LINT \
741                 -DNO_CPU_CFLAGS MK_WERROR=no MK_CTF=no \
742                 MK_CLANG_EXTRAS=no MK_CLANG_FORMAT=no MK_CLANG_FULL=no \
743                 MK_LLDB=no MK_RETPOLINE=no MK_TESTS=no
744
745 # cross-tools stage
746 # TOOLS_PREFIX set in BMAKE
747 XMAKE=          ${BMAKE} \
748                 TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
749                 MK_CLANG_IS_CC=${MK_CLANG_BOOTSTRAP} \
750                 MK_TESTS=no
751
752 # kernel-tools stage
753 KTMAKEENV=      INSTALL="sh ${.CURDIR}/tools/install.sh" \
754                 PATH=${BPATH}:${PATH} \
755                 WORLDTMP=${WORLDTMP} \
756                 MAKEFLAGS="-m ${.CURDIR}/tools/build/mk ${.MAKEFLAGS}"
757
758 KTMAKE=         ${TIME_ENV} \
759                 TOOLS_PREFIX=${TOOLS_PREFIX_UNDEF:U${WORLDTMP}} \
760                 ${KTMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
761                 DESTDIR= \
762                 OBJTOP='${WORLDTMP}/obj-kernel-tools' \
763                 OBJROOT='$${OBJTOP}/' \
764                 MAKEOBJDIRPREFIX= \
765                 BOOTSTRAPPING=${BOOTSTRAPPING_OSRELDATE} \
766                 SSP_CFLAGS= \
767                 MK_HTML=no -DNO_LINT MK_MAN=no \
768                 -DNO_PIC MK_PROFILE=no -DNO_SHARED \
769                 -DNO_CPU_CFLAGS MK_RETPOLINE=no MK_WERROR=no MK_CTF=no
770
771 # world stage
772 WMAKEENV=       ${CROSSENV} \
773                 INSTALL="${INSTALL_CMD} -U" \
774                 PATH=${TMPPATH} \
775                 SYSROOT=${WORLDTMP}
776
777 # make hierarchy
778 HMAKE=          PATH=${TMPPATH} ${MAKE} LOCAL_MTREE=${LOCAL_MTREE:Q}
779 .if defined(NO_ROOT)
780 HMAKE+=         PATH=${TMPPATH} METALOG=${METALOG} -DNO_ROOT
781 .endif
782
783 CROSSENV+=      CC="${XCC} ${XCFLAGS}" CXX="${XCXX} ${XCXXFLAGS} ${XCFLAGS}" \
784                 CPP="${XCPP} ${XCFLAGS}" \
785                 AS="${XAS}" AR="${XAR}" ELFCTL="${XELFCTL}" LD="${XLD}" \
786                 LLVM_LINK="${XLLVM_LINK}" NM=${XNM} OBJCOPY="${XOBJCOPY}" \
787                 RANLIB=${XRANLIB} STRINGS=${XSTRINGS} \
788                 SIZE="${XSIZE}" STRIPBIN="${XSTRIPBIN}"
789
790 .if defined(CROSS_BINUTILS_PREFIX) && exists(${CROSS_BINUTILS_PREFIX})
791 # In the case of xdev-build tools, CROSS_BINUTILS_PREFIX won't be a
792 # directory, but the compiler will look in the right place for its
793 # tools so we don't need to tell it where to look.
794 BFLAGS+=        -B${CROSS_BINUTILS_PREFIX}
795 .endif
796
797
798 # The internal bootstrap compiler has a default sysroot set by TOOLS_PREFIX
799 # and target set by TARGET/TARGET_ARCH.  However, there are several needs to
800 # always pass an explicit --sysroot and -target.
801 # - External compiler needs sysroot and target flags.
802 # - External ld needs sysroot.
803 # - To be clear about the use of a sysroot when using the internal compiler.
804 # - Easier debugging.
805 # - Allowing WITH_SYSTEM_COMPILER+WITH_META_MODE to work together due to
806 #   the flip-flopping build command when sometimes using external and
807 #   sometimes using internal.
808 # - Allow using lld which has no support for default paths.
809 .if !defined(CROSS_BINUTILS_PREFIX) || !exists(${CROSS_BINUTILS_PREFIX})
810 BFLAGS+=        -B${WORLDTMP}/usr/bin
811 .endif
812 .if ${WANT_COMPILER_TYPE} == gcc || \
813     (defined(X_COMPILER_TYPE) && ${X_COMPILER_TYPE} == gcc)
814 .elif ${WANT_COMPILER_TYPE} == clang || \
815     (defined(X_COMPILER_TYPE) && ${X_COMPILER_TYPE} == clang)
816 XCFLAGS+=       -target ${TARGET_TRIPLE}
817 .endif
818 XCFLAGS+=       --sysroot=${WORLDTMP}
819
820 .if !empty(BFLAGS)
821 XCFLAGS+=       ${BFLAGS}
822 .endif
823
824 .if ${MK_LIB32} == "yes"
825 _LIBCOMPAT= 32
826 .include "Makefile.libcompat"
827 .elif ${MK_LIBSOFT} == "yes"
828 _LIBCOMPAT= SOFT
829 .include "Makefile.libcompat"
830 .endif
831
832 # META_MODE normally ignores host file changes since every build updates
833 # timestamps (see NO_META_IGNORE_HOST in sys.mk).  There are known times
834 # when the ABI breaks though that we want to force rebuilding WORLDTMP
835 # to get updated host tools.
836 .if ${MK_META_MODE} == "yes" && ${MK_CLEAN} == "no" && \
837     !defined(NO_META_IGNORE_HOST) && !defined(NO_META_IGNORE_HOST_HEADERS) && \
838     !defined(_MKSHOWCONFIG)
839 # r318736 - ino64 major ABI breakage
840 META_MODE_BAD_ABI_VERS+=        1200031
841
842 .if !defined(OBJDIR_HOST_OSRELDATE)
843 .if exists(${OBJTOP}/host-osreldate.h)
844 OBJDIR_HOST_OSRELDATE!= \
845     awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
846     ${OBJTOP}/host-osreldate.h
847 .elif exists(${WORLDTMP}/usr/include/osreldate.h)
848 OBJDIR_HOST_OSRELDATE=  0
849 .endif
850 .export OBJDIR_HOST_OSRELDATE
851 .endif
852
853 # Note that this logic is the opposite of normal BOOTSTRAP handling.  We want
854 # to compare the WORLDTMP's OSRELDATE to the host's OSRELDATE.  If the WORLDTMP
855 # is older than the ABI-breakage OSRELDATE of the HOST then we rebuild.
856 .if defined(OBJDIR_HOST_OSRELDATE)
857 .for _ver in ${META_MODE_BAD_ABI_VERS}
858 .if ${OSRELDATE} >= ${_ver} && ${OBJDIR_HOST_OSRELDATE} < ${_ver}
859 _meta_mode_need_rebuild=        ${_ver}
860 .endif
861 .endfor
862 .if defined(_meta_mode_need_rebuild)
863 .info META_MODE: Rebuilding host tools due to ABI breakage in __FreeBSD_version ${_meta_mode_need_rebuild}.
864 NO_META_IGNORE_HOST_HEADERS=    1
865 .export NO_META_IGNORE_HOST_HEADERS
866 .endif  # defined(_meta_mode_need_rebuild)
867 .endif  # defined(OBJDIR_HOST_OSRELDATE)
868 .endif  # ${MK_META_MODE} == "yes" && ${MK_CLEAN} == "no" ...
869 # This is only used for META_MODE+filemon to track what the oldest
870 # __FreeBSD_version is in WORLDTMP.  This purposely does NOT have
871 # a make dependency on /usr/include/osreldate.h as the file should
872 # only be copied when it is missing or meta mode determines it has changed.
873 # Since host files are normally ignored without NO_META_IGNORE_HOST
874 # the file will never be updated unless that flag is specified.  This
875 # allows tracking the oldest osreldate to force rebuilds via
876 # META_MODE_BADABI_REVS above.
877 host-osreldate.h: # DO NOT ADD /usr/include/osreldate.h here
878 .if !defined(CROSSBUILD_HOST)
879         @cp -f /usr/include/osreldate.h ${.TARGET}
880 .else
881         @echo "#ifndef __FreeBSD_version" > ${.TARGET}
882         @echo "#define __FreeBSD_version ${OSRELDATE}" >> ${.TARGET}
883         @echo "#endif" >> ${.TARGET}
884 .endif
885
886 WMAKE=          ${TIME_ENV} ${WMAKEENV} ${MAKE} ${WORLD_FLAGS} -f Makefile.inc1 \
887                 BWPHASE=${.TARGET:C,^_,,} \
888                 DESTDIR=${WORLDTMP}
889
890 IMAKEENV=       ${CROSSENV}
891 IMAKE=          ${TIME_ENV} ${IMAKEENV} ${MAKE} -f Makefile.inc1 \
892                 ${IMAKE_INSTALL} ${IMAKE_MTREE}
893 .if empty(.MAKEFLAGS:M-n)
894 IMAKEENV+=      PATH=${STRICTTMPPATH}:${INSTALLTMP} \
895                 LD_LIBRARY_PATH=${INSTALLTMP} \
896                 PATH_LOCALE=${INSTALLTMP}/locale
897 IMAKE+=         __MAKE_SHELL=${INSTALLTMP}/sh
898 .else
899 IMAKEENV+=      PATH=${TMPPATH}:${INSTALLTMP}
900 .endif
901
902 # When generating install media, do not allow user and group information from
903 # the build host to affect the contents of the distribution.
904 .if make(distributeworld) || make(distrib-dirs) || make(distribution) || \
905     make(stageworld)
906 DB_FROM_SRC=    yes
907 .endif
908
909 .if defined(DB_FROM_SRC)
910 INSTALLFLAGS+=  -N ${.CURDIR}/etc
911 MTREEFLAGS+=    -N ${.CURDIR}/etc
912 .endif
913 _INSTALL_DDIR=  ${DESTDIR}/${DISTDIR}
914 INSTALL_DDIR=   ${_INSTALL_DDIR:S://:/:g:C:/$::}
915 .if defined(NO_ROOT)
916 METALOG?=       ${DESTDIR}/${DISTDIR}/METALOG
917 METALOG:=       ${METALOG:C,//+,/,g}
918 IMAKE+=         -DNO_ROOT METALOG=${METALOG}
919 METALOG_INSTALLFLAGS=   -U -M ${METALOG} -D ${INSTALL_DDIR}
920 INSTALLFLAGS+=  ${METALOG_INSTALLFLAGS}
921 CERTCTLFLAGS=   ${METALOG_INSTALLFLAGS}
922 MTREEFLAGS+=    -W
923 .endif
924 .if defined(BUILD_PKGS)
925 INSTALLFLAGS+=  -h sha256
926 .endif
927 .if defined(DB_FROM_SRC) || defined(NO_ROOT)
928 IMAKE_INSTALL=  INSTALL="${INSTALL_CMD} ${INSTALLFLAGS}"
929 IMAKE_MTREE=    MTREE_CMD="${MTREE_CMD} ${MTREEFLAGS}"
930 .endif
931 .if make(distributeworld)
932 CERTCTLDESTDIR= ${DESTDIR}/${DISTDIR}/base
933 .else
934 CERTCTLDESTDIR= ${DESTDIR}
935 .endif
936
937 DESTDIR_MTREEFLAGS=     -deU
938 # When creating worldtmp we don't need to set the directories as owned by root
939 # so we also pass -W
940 WORLDTMP_MTREEFLAGS=    -deUW
941 .if defined(NO_ROOT)
942 # When building with -DNO_ROOT we shouldn't be changing the directories
943 # that are created by mtree to be owned by root/wheel.
944 DESTDIR_MTREEFLAGS+=    -W
945 .endif
946 DISTR_MTREE=    ${MTREE_CMD}
947 .if ${BUILD_WITH_STRICT_TMPPATH} != 0
948 DISTR_MTREE=    ${WORLDTMP}/legacy/usr/sbin/mtree
949 .endif
950 WORLDTMP_MTREE= ${DISTR_MTREE} ${WORLDTMP_MTREEFLAGS}
951 DESTDIR_MTREE=  ${DISTR_MTREE} ${DESTDIR_MTREEFLAGS}
952
953 # kernel stage
954 KMAKEENV=       ${WMAKEENV:NSYSROOT=*}
955 KMAKE=          ${TIME_ENV} ${KMAKEENV} ${MAKE} ${.MAKEFLAGS} ${KERNEL_FLAGS} KERNEL=${INSTKERNNAME}
956
957 #
958 # buildworld
959 #
960 # Attempt to rebuild the entire system, with reasonable chance of
961 # success, regardless of how old your existing system is.
962 #
963 _sanity_check: .PHONY .MAKE
964 .if ${.CURDIR:C/[^,]//g} != ""
965 #       The m4 build of sendmail files doesn't like it if ',' is used
966 #       anywhere in the path of it's files.
967         @echo
968         @echo "*** Error: path to source tree contains a comma ','"
969         @echo
970         @false
971 .elif ${.CURDIR:M*\:*} != ""
972 #       Using ':' leaks into PATH and breaks finding cross-tools.
973         @echo
974         @echo "*** Error: path to source tree contains a colon ':'"
975         @echo
976         @false
977 .endif
978
979 # Our current approach to dependency tracking cannot cope with certain source
980 # tree changes, particularly with respect to removing source files and
981 # replacing generated files.  Handle these cases here in an ad-hoc fashion.
982 _cleanobj_fast_depend_hack: .PHONY
983         @echo ">>> Deleting stale dependencies...";
984         sh ${.CURDIR}/tools/build/depend-cleanup.sh ${OBJTOP}
985
986 _worldtmp: .PHONY
987         @echo
988         @echo "--------------------------------------------------------------"
989         @echo ">>> Rebuilding the temporary build tree"
990         @echo "--------------------------------------------------------------"
991 .if ${MK_CLEAN} == "yes"
992         rm -rf ${WORLDTMP}
993 .else
994 # Note: for delete-old we need to set $PATH to also include the host $PATH
995 # since otherwise a partial build with missing symlinks in ${WORLDTMP}/legacy/
996 # will fail to run due to missing binaries. $WMAKE sets PATH to only ${TMPPATH}
997 # so we remove that assingnment from $WMAKE and prepend the new $PATH
998         ${_+_}@if [ -e "${WORLDTMP}" ]; then \
999                 echo ">>> Deleting stale files in build tree..."; \
1000                 cd ${.CURDIR}; env PATH=${TMPPATH}:${PATH} ${WMAKE:NPATH=*} \
1001                     _NO_INCLUDE_COMPILERMK=t -DBATCH_DELETE_OLD_FILES delete-old \
1002                     delete-old-libs >/dev/null; \
1003         fi
1004         rm -rf ${WORLDTMP}/legacy/usr/include
1005 .if ${USING_SYSTEM_COMPILER} == "yes"
1006 .for cc in cc c++
1007         if [ -x ${WORLDTMP}/usr/bin/${cc} ]; then \
1008                 inum=$$(stat -f %i ${WORLDTMP}/usr/bin/${cc}); \
1009                 find ${WORLDTMP}/usr/bin -inum $${inum} -delete; \
1010         fi
1011 .endfor
1012 .endif  # ${USING_SYSTEM_COMPILER} == "yes"
1013 .if ${USING_SYSTEM_LINKER} == "yes"
1014         @rm -f ${WORLDTMP}/usr/bin/ld ${WORLDTMP}/usr/bin/ld.lld
1015 .endif  # ${USING_SYSTEM_LINKER} == "yes"
1016 .endif  # ${MK_CLEAN} == "yes"
1017         @mkdir -p ${WORLDTMP}
1018         @touch ${WORLDTMP}/${.TARGET}
1019 # We can't use mtree to create the worldtmp directories since it may not be
1020 # available on the target system (this happens e.g. when building on non-FreeBSD)
1021         cd ${.CURDIR}/tools/build; \
1022             ${MAKE} DIRPRFX=tools/build/ DESTDIR=${WORLDTMP}/legacy installdirs
1023 # In order to build without inheriting $PATH we need to add symlinks to the host
1024 # tools in $WORLDTMP for the tools that we don't build during bootstrap-tools
1025         cd ${.CURDIR}/tools/build; \
1026             ${MAKE} DIRPRFX=tools/build/ DESTDIR=${WORLDTMP}/legacy host-symlinks
1027
1028 _legacy:
1029         @echo
1030         @echo "--------------------------------------------------------------"
1031         @echo ">>> stage 1.1: legacy release compatibility shims"
1032         @echo "--------------------------------------------------------------"
1033         ${_+_}cd ${.CURDIR}; ${BMAKE} legacy
1034 _bootstrap-tools:
1035         @echo
1036         @echo "--------------------------------------------------------------"
1037         @echo ">>> stage 1.2: bootstrap tools"
1038         @echo "--------------------------------------------------------------"
1039         ${_+_}cd ${.CURDIR}; ${BMAKE} bootstrap-tools
1040         mkdir -p ${WORLDTMP}/usr ${WORLDTMP}/lib/casper ${WORLDTMP}/lib/geom \
1041             ${WORLDTMP}/bin
1042         ${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
1043             -p ${WORLDTMP}/usr >/dev/null
1044         ${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.include.dist \
1045             -p ${WORLDTMP}/usr/include >/dev/null
1046         ln -sf ${.CURDIR}/sys ${WORLDTMP}
1047 .if ${MK_DEBUG_FILES} != "no"
1048         ${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
1049             -p ${WORLDTMP}/usr/lib >/dev/null
1050 .endif
1051 .for _mtree in ${LOCAL_MTREE}
1052         ${WORLDTMP_MTREE} -f ${.CURDIR}/${_mtree} -p ${WORLDTMP} > /dev/null
1053 .endfor
1054 _cleanobj:
1055 .if ${MK_CLEAN} == "yes"
1056         @echo
1057         @echo "--------------------------------------------------------------"
1058         @echo ">>> stage 2.1: cleaning up the object tree"
1059         @echo "--------------------------------------------------------------"
1060         # Avoid including bsd.compiler.mk in clean and obj with _NO_INCLUDE_COMPILERMK
1061         # since the restricted $PATH might not contain a valid cc binary
1062         ${_+_}cd ${.CURDIR}; ${WMAKE} _NO_INCLUDE_COMPILERMK=t ${CLEANDIR}
1063 .if defined(_LIBCOMPAT)
1064         ${_+_}cd ${.CURDIR}; ${LIBCOMPATWMAKE} _NO_INCLUDE_COMPILERMK=t -f Makefile.inc1 ${CLEANDIR}
1065 .endif
1066 .else
1067         ${_+_}cd ${.CURDIR}; ${WMAKE} _NO_INCLUDE_COMPILERMK=t _cleanobj_fast_depend_hack
1068 .endif  # ${MK_CLEAN} == "yes"
1069 _obj:
1070         @echo
1071         @echo "--------------------------------------------------------------"
1072         @echo ">>> stage 2.2: rebuilding the object tree"
1073         @echo "--------------------------------------------------------------"
1074         ${_+_}cd ${.CURDIR}; ${WMAKE} _NO_INCLUDE_COMPILERMK=t obj
1075 _build-tools:
1076         @echo
1077         @echo "--------------------------------------------------------------"
1078         @echo ">>> stage 2.3: build tools"
1079         @echo "--------------------------------------------------------------"
1080         ${_+_}cd ${.CURDIR}; ${TMAKE} build-tools
1081 _cross-tools:
1082         @echo
1083         @echo "--------------------------------------------------------------"
1084         @echo ">>> stage 3: cross tools"
1085         @echo "--------------------------------------------------------------"
1086         @rm -f ${OBJTOP}/toolchain-metadata.mk
1087         ${_+_}cd ${.CURDIR}; ${XMAKE} cross-tools
1088         ${_+_}cd ${.CURDIR}; ${XMAKE} kernel-tools
1089 _build-metadata:
1090         @echo
1091         @echo "--------------------------------------------------------------"
1092         @echo ">>> stage 3.1: recording build metadata"
1093         @echo "--------------------------------------------------------------"
1094         ${_+_}cd ${.CURDIR}; ${WMAKE} toolchain-metadata.mk
1095         ${_+_}cd ${.CURDIR}; ${WMAKE} host-osreldate.h
1096 _includes:
1097         @echo
1098         @echo "--------------------------------------------------------------"
1099         @echo ">>> stage 4.1: building includes"
1100         @echo "--------------------------------------------------------------"
1101 # Special handling for SUBDIR_OVERRIDE in buildworld as they most likely need
1102 # headers from default SUBDIR.  Do SUBDIR_OVERRIDE includes last.
1103         ${_+_}cd ${.CURDIR}; ${WMAKE} SUBDIR_OVERRIDE= SHARED=symlinks \
1104             MK_INCLUDES=yes includes
1105 .if !empty(SUBDIR_OVERRIDE) && make(buildworld)
1106         ${_+_}cd ${.CURDIR}; ${WMAKE} MK_INCLUDES=yes SHARED=symlinks includes
1107 .endif
1108 _libraries:
1109         @echo
1110         @echo "--------------------------------------------------------------"
1111         @echo ">>> stage 4.2: building libraries"
1112         @echo "--------------------------------------------------------------"
1113         ${_+_}cd ${.CURDIR}; \
1114             ${WMAKE} -DNO_FSCHG MK_HTML=no -DNO_LINT MK_MAN=no \
1115             MK_PROFILE=no MK_TESTS=no MK_TESTS_SUPPORT=${MK_TESTS_SUPPORT} \
1116             libraries
1117 everything: .PHONY
1118         @echo
1119         @echo "--------------------------------------------------------------"
1120         @echo ">>> stage 4.4: building everything"
1121         @echo "--------------------------------------------------------------"
1122         ${_+_}cd ${.CURDIR}; _PARALLEL_SUBDIR_OK=1 ${WMAKE} all
1123
1124 WMAKE_TGTS=
1125 .if !defined(WORLDFAST)
1126 WMAKE_TGTS+=    _sanity_check _worldtmp _legacy
1127 .if empty(SUBDIR_OVERRIDE)
1128 WMAKE_TGTS+=    _bootstrap-tools
1129 .endif
1130 WMAKE_TGTS+=    _cleanobj
1131 .if !defined(NO_OBJWALK)
1132 WMAKE_TGTS+=    _obj
1133 .endif
1134 WMAKE_TGTS+=    _build-tools _cross-tools
1135 WMAKE_TGTS+=    _build-metadata
1136 WMAKE_TGTS+=    _includes
1137 .endif
1138 .if !defined(NO_LIBS)
1139 WMAKE_TGTS+=    _libraries
1140 .endif
1141 .if defined(_LIBCOMPAT) && empty(SUBDIR_OVERRIDE)
1142 WMAKE_TGTS+=    build${libcompat}
1143 .endif
1144 WMAKE_TGTS+=    everything
1145
1146 # record buildworld time in seconds
1147 .if make(buildworld)
1148 _BUILDWORLD_START!= date '+%s'
1149 .export _BUILDWORLD_START
1150 .endif
1151
1152 buildworld: buildworld_prologue ${WMAKE_TGTS} buildworld_epilogue .PHONY
1153 .ORDER: buildworld_prologue ${WMAKE_TGTS} buildworld_epilogue
1154
1155 _ncpu_cmd=sysctl -n hw.ncpu 2>/dev/null || nproc 2>/dev/null || echo unknown
1156
1157 buildworld_prologue: .PHONY
1158         @echo "--------------------------------------------------------------"
1159         @echo ">>> World build started on `LC_ALL=C date`"
1160         @echo "--------------------------------------------------------------"
1161
1162 buildworld_epilogue: .PHONY
1163         @echo
1164         @echo "--------------------------------------------------------------"
1165         @echo ">>> World build completed on `LC_ALL=C date`"
1166         @seconds=$$(($$(date '+%s') - ${_BUILDWORLD_START})); \
1167           echo -n ">>> World built in $$seconds seconds, "; \
1168           echo "ncpu: $$(${_ncpu_cmd})${.MAKE.JOBS:S/^/, make -j/}"
1169         @echo "--------------------------------------------------------------"
1170
1171 #
1172 # We need to have this as a target because the indirection between Makefile
1173 # and Makefile.inc1 causes the correct PATH to be used, rather than a
1174 # modification of the current environment's PATH.  In addition, we need
1175 # to quote multiword values.
1176 #
1177 buildenvvars: .PHONY
1178         @echo ${WMAKEENV:Q} ${.MAKE.EXPORTED:@v@$v=\"${$v}\"@}
1179
1180 .if ${.TARGETS:Mbuildenv}
1181 .if ${.MAKEFLAGS:M-j}
1182 .error The buildenv target is incompatible with -j
1183 .endif
1184 .endif
1185 BUILDENV_DIR?=  ${.CURDIR}
1186 #
1187 # Note: make will report any errors the shell reports. This can
1188 # be odd if the last command in an interactive shell generates an
1189 # error or is terminated by SIGINT. These reported errors look bad,
1190 # but are harmless. Allowing them also allows BUIDLENV_SHELL to
1191 # be a complex command whose status will be returned to the caller.
1192 # Some scripts in tools rely on this behavior to report build errors.
1193 #
1194 buildenv: .PHONY
1195         @echo Entering world for ${TARGET_ARCH}:${TARGET}
1196 .if ${BUILDENV_SHELL:M*zsh*}
1197         @echo For ZSH you must run: export CPUTYPE=${TARGET_CPUTYPE}
1198 .endif
1199         @cd ${BUILDENV_DIR} && env ${WMAKEENV} \
1200         INSTALL="${INSTALL_CMD} ${INSTALLFLAGS}" \
1201         MTREE_CMD="${MTREE_CMD} ${MTREEFLAGS}" BUILDENV=1 ${BUILDENV_SHELL}
1202
1203 TOOLCHAIN_TGTS= ${WMAKE_TGTS:Neverything:Nbuild${libcompat}}
1204 toolchain: ${TOOLCHAIN_TGTS} .PHONY
1205 KERNEL_TOOLCHAIN_TGTS=  ${TOOLCHAIN_TGTS:N_obj:N_cleanobj:N_includes:N_libraries}
1206 .if make(kernel-toolchain)
1207 .ORDER: ${KERNEL_TOOLCHAIN_TGTS}
1208 .endif
1209 kernel-toolchain: ${KERNEL_TOOLCHAIN_TGTS} .PHONY
1210
1211 #
1212 # installcheck
1213 #
1214 # Checks to be sure system is ready for installworld/installkernel.
1215 #
1216 installcheck: _installcheck_world _installcheck_kernel .PHONY
1217 _installcheck_world: .PHONY
1218         @echo "--------------------------------------------------------------"
1219         @echo ">>> Install check world"
1220         @echo "--------------------------------------------------------------"
1221 _installcheck_kernel: .PHONY
1222         @echo "--------------------------------------------------------------"
1223         @echo ">>> Install check kernel"
1224         @echo "--------------------------------------------------------------"
1225
1226 #
1227 # Require DESTDIR to be set if installing for a different architecture or
1228 # using the user/group database in the source tree.
1229 #
1230 .if ${TARGET_ARCH} != ${MACHINE_ARCH} || ${TARGET} != ${MACHINE} || \
1231     defined(DB_FROM_SRC)
1232 .if !make(distributeworld)
1233 _installcheck_world: __installcheck_DESTDIR
1234 _installcheck_kernel: __installcheck_DESTDIR
1235 __installcheck_DESTDIR: .PHONY
1236 .if !defined(DESTDIR) || empty(DESTDIR)
1237         @echo "ERROR: Please set DESTDIR!"; \
1238         false
1239 .endif
1240 .endif
1241 .endif
1242
1243 .if !defined(DB_FROM_SRC)
1244 #
1245 # Check for missing UIDs/GIDs.
1246 #
1247 CHECK_UIDS=     auditdistd
1248 CHECK_GIDS=     audit
1249 CHECK_UIDS+=    ntpd
1250 CHECK_GIDS+=    ntpd
1251 CHECK_UIDS+=    proxy
1252 CHECK_GIDS+=    proxy authpf
1253 CHECK_UIDS+=    smmsp
1254 CHECK_GIDS+=    smmsp
1255 CHECK_UIDS+=    unbound
1256 CHECK_GIDS+=    unbound
1257 _installcheck_world: __installcheck_UGID
1258 __installcheck_UGID: .PHONY
1259 .for uid in ${CHECK_UIDS}
1260         @if ! `id -u ${uid} >/dev/null 2>&1`; then \
1261                 echo "ERROR: Required ${uid} user is missing, see /usr/src/UPDATING."; \
1262                 false; \
1263         fi
1264 .endfor
1265 .for gid in ${CHECK_GIDS}
1266         @if ! `find / -prune -group ${gid} >/dev/null 2>&1`; then \
1267                 echo "ERROR: Required ${gid} group is missing, see /usr/src/UPDATING."; \
1268                 false; \
1269         fi
1270 .endfor
1271 .endif
1272 #
1273 # If installing over the running system (DESTDIR is / or unset) and the install
1274 # includes rescue, try running rescue from the objdir as a sanity check.  If
1275 # rescue is not functional (e.g., because it depends on a system call not
1276 # supported by the currently running kernel), abort the installation.
1277 #
1278 .if !make(distributeworld) && ${MK_RESCUE} != "no" && \
1279     (empty(DESTDIR) || ${DESTDIR} == "/") && empty(BYPASS_INSTALLCHECK_SH)
1280 _installcheck_world: __installcheck_sh_check
1281 __installcheck_sh_check: .PHONY
1282         @if [ "`${OBJTOP}/rescue/rescue/rescue sh -c 'echo OK'`" != \
1283             OK ]; then \
1284                 echo "rescue/sh check failed, installation aborted" >&2; \
1285                 false; \
1286         fi
1287 .endif
1288
1289 #
1290 # Required install tools to be saved in a scratch dir for safety.
1291 #
1292 .if ${MK_ZONEINFO} != "no"
1293 _zoneinfo=      zic tzsetup
1294 .endif
1295
1296 .if !defined(CROSSBUILD_HOST)
1297 _sysctl=sysctl
1298 .endif
1299
1300 ITOOLS= [ awk cap_mkdb cat chflags chmod chown cmp cp \
1301         date echo egrep find grep id install ${_install-info} \
1302         ln make mkdir mtree mv pwd_mkdb \
1303         rm sed services_mkdb sh sort strip ${_sysctl} test time true uname wc \
1304         ${_zoneinfo} ${LOCAL_ITOOLS}
1305
1306 # Needed for share/man
1307 .if ${MK_MAN_UTILS} != "no"
1308 ITOOLS+=makewhatis
1309 .endif
1310
1311 #
1312 # distributeworld
1313 #
1314 # Distributes everything compiled by a `buildworld'.
1315 #
1316 # installworld
1317 #
1318 # Installs everything compiled by a 'buildworld'.
1319 #
1320
1321 # Non-base distributions produced by the base system
1322 EXTRA_DISTRIBUTIONS=
1323 .if defined(_LIBCOMPAT)
1324 EXTRA_DISTRIBUTIONS+=   lib${libcompat}
1325 .endif
1326 .if ${MK_TESTS} != "no"
1327 EXTRA_DISTRIBUTIONS+=   tests
1328 .endif
1329
1330 DEBUG_DISTRIBUTIONS=
1331 .if ${MK_DEBUG_FILES} != "no"
1332 DEBUG_DISTRIBUTIONS+=   base ${EXTRA_DISTRIBUTIONS:S,tests,,}
1333 .endif
1334
1335 MTREE_MAGIC?=   mtree 2.0
1336
1337 distributeworld installworld stageworld: _installcheck_world .PHONY
1338         mkdir -p ${INSTALLTMP}
1339         progs=$$(for prog in ${ITOOLS}; do \
1340                 if progpath=`env PATH=${TMPPATH} which $$prog`; then \
1341                         echo $$progpath; \
1342                 else \
1343                         echo "Required tool $$prog not found in PATH ($$PATH)." >&2; \
1344                         exit 1; \
1345                 fi; \
1346             done); \
1347         if [ -z "${CROSSBUILD_HOST}" ] ; then \
1348                 libs=$$(ldd -f "%o %p\n" -f "%o %p\n" $$progs 2>/dev/null | sort -u | \
1349                     while read line; do \
1350                         set -- $$line; \
1351                         if [ "$$2 $$3" != "not found" ]; then \
1352                                 echo $$2; \
1353                         else \
1354                                 echo "Required library $$1 not found." >&2; \
1355                                 exit 1; \
1356                         fi; \
1357                     done); \
1358         fi; \
1359         cp $$libs $$progs ${INSTALLTMP}
1360         cp -R $${PATH_LOCALE:-"/usr/share/locale"} ${INSTALLTMP}/locale
1361 .if defined(NO_ROOT)
1362         -mkdir -p ${METALOG:H}
1363         echo "#${MTREE_MAGIC}" > ${METALOG}
1364 .endif
1365 .if make(distributeworld)
1366 .for dist in ${EXTRA_DISTRIBUTIONS}
1367         -mkdir ${DESTDIR}/${DISTDIR}/${dist}
1368         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.root.dist \
1369             -p ${DESTDIR}/${DISTDIR}/${dist} >/dev/null
1370         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
1371             -p ${DESTDIR}/${DISTDIR}/${dist}/usr >/dev/null
1372         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.include.dist \
1373             -p ${DESTDIR}/${DISTDIR}/${dist}/usr/include >/dev/null
1374 .if ${MK_DEBUG_FILES} != "no"
1375         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.debug.dist \
1376             -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib >/dev/null
1377 .endif
1378 .if defined(_LIBCOMPAT)
1379         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist \
1380             -p ${DESTDIR}/${DISTDIR}/${dist}/usr >/dev/null
1381 .if ${MK_DEBUG_FILES} != "no"
1382         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist \
1383             -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib/debug/usr >/dev/null
1384 .endif
1385 .endif
1386 .if ${MK_TESTS} != "no" && ${dist} == "tests"
1387         -mkdir -p ${DESTDIR}/${DISTDIR}/${dist}${TESTSBASE}
1388         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
1389             -p ${DESTDIR}/${DISTDIR}/${dist}${TESTSBASE} >/dev/null
1390 .if ${MK_DEBUG_FILES} != "no"
1391         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
1392             -p ${DESTDIR}/${DISTDIR}/${dist}/usr/lib/debug/${TESTSBASE} >/dev/null
1393 .endif
1394 .endif
1395 .if defined(NO_ROOT)
1396         ${IMAKEENV} ${DISTR_MTREE} -C -f ${.CURDIR}/etc/mtree/BSD.root.dist | \
1397             sed -e 's#^\./#./${dist}/#' >> ${METALOG}
1398         ${IMAKEENV} ${DISTR_MTREE} -C -f ${.CURDIR}/etc/mtree/BSD.usr.dist | \
1399             sed -e 's#^\./#./${dist}/usr/#' >> ${METALOG}
1400         ${IMAKEENV} ${DISTR_MTREE} -C -f ${.CURDIR}/etc/mtree/BSD.include.dist | \
1401             sed -e 's#^\./#./${dist}/usr/include/#' >> ${METALOG}
1402 .if defined(_LIBCOMPAT)
1403         ${IMAKEENV} ${DISTR_MTREE} -C -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist | \
1404             sed -e 's#^\./#./${dist}/usr/#' >> ${METALOG}
1405 .endif
1406 .endif
1407 .endfor
1408         -mkdir ${DESTDIR}/${DISTDIR}/base
1409         ${_+_}cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
1410             METALOG=${METALOG} ${IMAKE_INSTALL} ${IMAKE_MTREE} \
1411             DISTBASE=/base DESTDIR=${DESTDIR}/${DISTDIR}/base \
1412             LOCAL_MTREE=${LOCAL_MTREE:Q} distrib-dirs
1413         ${INSTALL_SYMLINK} ${INSTALLFLAGS} usr/src/sys ${INSTALL_DDIR}/base/sys
1414 .endif # make(distributeworld)
1415         ${_+_}cd ${.CURDIR}; ${IMAKE} re${.TARGET:S/world$//}; \
1416             ${IMAKEENV} rm -rf ${INSTALLTMP}
1417 .if make(distributeworld)
1418 .for dist in ${EXTRA_DISTRIBUTIONS}
1419         find ${DESTDIR}/${DISTDIR}/${dist} -mindepth 1 -type d -empty -delete
1420 .endfor
1421 .if defined(NO_ROOT)
1422 .for dist in base ${EXTRA_DISTRIBUTIONS}
1423         @# For each file that exists in this dist, print the corresponding
1424         @# line from the METALOG.  This relies on the fact that
1425         @# a line containing only the filename will sort immediately before
1426         @# the relevant mtree line.
1427         cd ${DESTDIR}/${DISTDIR}; \
1428         find ./${dist} | sort -u ${METALOG} - | \
1429         awk 'BEGIN { print "#${MTREE_MAGIC}" } !/ type=/ { file = $$1 } / type=/ { if ($$1 == file) { sub(/^\.\/${dist}\//, "./"); print } }' > \
1430         ${DESTDIR}/${DISTDIR}/${dist}.meta
1431 .endfor
1432 .for dist in ${DEBUG_DISTRIBUTIONS}
1433         @# For each file that exists in this dist, print the corresponding
1434         @# line from the METALOG.  This relies on the fact that
1435         @# a line containing only the filename will sort immediately before
1436         @# the relevant mtree line.
1437         cd ${DESTDIR}/${DISTDIR}; \
1438         find ./${dist}/usr/lib/debug | sort -u ${METALOG} - | \
1439         awk 'BEGIN { print "#${MTREE_MAGIC}" } !/ type=/ { file = $$1 } / type=/ { if ($$1 == file) { sub(/^\.\/${dist}\//, "./"); print } }' > \
1440         ${DESTDIR}/${DISTDIR}/${dist}.debug.meta
1441 .endfor
1442 .endif
1443 .endif # make(distributeworld)
1444 .if !make(packageworld) && ${MK_CAROOT} != "no"
1445         @if which openssl>/dev/null; then \
1446                 DESTDIR=${CERTCTLDESTDIR} PATH=${TMPPATH}:${PATH} \
1447                     sh ${SRCTOP}/usr.sbin/certctl/certctl.sh ${CERTCTLFLAGS} rehash \
1448         else \
1449                 echo "No openssl on the host, not rehashing certificates target -- /etc/ssl may not be populated."; \
1450         fi
1451 .endif
1452
1453 packageworld: .PHONY
1454 .for dist in base ${EXTRA_DISTRIBUTIONS}
1455 .if defined(NO_ROOT)
1456         ${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1457             tar cvf - --exclude usr/lib/debug \
1458             @${DESTDIR}/${DISTDIR}/${dist}.meta | \
1459             ${XZ_CMD} > ${PACKAGEDIR}/${dist}.txz
1460 .else
1461         ${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1462             tar cvf - --exclude usr/lib/debug . | \
1463             ${XZ_CMD} > ${PACKAGEDIR}/${dist}.txz
1464 .endif
1465 .endfor
1466
1467 .for dist in ${DEBUG_DISTRIBUTIONS}
1468 . if defined(NO_ROOT)
1469         ${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1470             tar cvf - @${DESTDIR}/${DISTDIR}/${dist}.debug.meta | \
1471             ${XZ_CMD} > ${PACKAGEDIR}/${dist}-dbg.txz
1472 . else
1473         ${_+_}cd ${DESTDIR}/${DISTDIR}/${dist}; \
1474             tar cvLf - usr/lib/debug | \
1475             ${XZ_CMD} > ${PACKAGEDIR}/${dist}-dbg.txz
1476 . endif
1477 .endfor
1478
1479 makeman: .PHONY
1480         ${_+_}cd ${.CURDIR}/tools/build/options; sh makeman > \
1481             ${.CURDIR}/share/man/man5/src.conf.5
1482
1483 # We can't assume here that ${TMPPATH} will include ${PATH} or /usr/libexec
1484 # because we may be building with a STRICTTMPPATH, so we explicitly include
1485 # /usr/libexec here for flua.  ${TMPPATH} still usefully includes anything else
1486 # we may need to function.
1487 _sysent_PATH=   ${TMPPATH}:/usr/libexec
1488 _sysent_dirs=   sys/kern
1489 _sysent_dirs+=  sys/compat/freebsd32
1490 _sysent_dirs+=  sys/compat/cloudabi32   \
1491                 sys/compat/cloudabi64
1492 _sysent_dirs+=  sys/amd64/linux         \
1493                 sys/amd64/linux32       \
1494                 sys/arm/linux           \
1495                 sys/arm64/linux         \
1496                 sys/i386/linux
1497
1498 sysent: .PHONY
1499 .for _dir in ${_sysent_dirs}
1500 sysent-${_dir}: .PHONY
1501         @echo "${MAKE} -C ${.CURDIR}/${_dir} sysent"
1502         ${_+_}@env PATH=${_sysent_PATH} ${MAKE} -C ${.CURDIR}/${_dir} sysent
1503
1504 sysent: sysent-${_dir}
1505 .endfor
1506
1507 #
1508 # reinstall
1509 #
1510 # If you have a build server, you can NFS mount the source and obj directories
1511 # and do a 'make reinstall' on the *client* to install new binaries from the
1512 # most recent server build.
1513 #
1514 restage reinstall: .MAKE .PHONY
1515         @echo "--------------------------------------------------------------"
1516         @echo ">>> Making hierarchy"
1517         @echo "--------------------------------------------------------------"
1518         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 \
1519             LOCAL_MTREE=${LOCAL_MTREE:Q} hierarchy
1520 .if make(restage)
1521         @echo "--------------------------------------------------------------"
1522         @echo ">>> Making distribution"
1523         @echo "--------------------------------------------------------------"
1524         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 \
1525             LOCAL_MTREE=${LOCAL_MTREE:Q} distribution
1526 .endif
1527         @echo
1528         @echo "--------------------------------------------------------------"
1529         @echo ">>> Installing everything started on `LC_ALL=C date`"
1530         @echo "--------------------------------------------------------------"
1531         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 install
1532 .if defined(_LIBCOMPAT)
1533         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 install${libcompat}
1534 .endif
1535         @echo "--------------------------------------------------------------"
1536         @echo ">>> Installing everything completed on `LC_ALL=C date`"
1537         @echo "--------------------------------------------------------------"
1538
1539 redistribute: .MAKE .PHONY
1540         @echo "--------------------------------------------------------------"
1541         @echo ">>> Distributing everything"
1542         @echo "--------------------------------------------------------------"
1543         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 distribute
1544 .if defined(_LIBCOMPAT)
1545         ${_+_}cd ${.CURDIR}; ${MAKE} -f Makefile.inc1 distribute${libcompat} \
1546             DISTRIBUTION=lib${libcompat}
1547 .endif
1548
1549 distrib-dirs distribution: .MAKE .PHONY
1550         ${_+_}cd ${.CURDIR}/etc; ${CROSSENV} PATH=${TMPPATH} ${MAKE} \
1551             ${IMAKE_INSTALL} ${IMAKE_MTREE} METALOG=${METALOG} ${.TARGET}
1552 .if make(distribution)
1553         ${_+_}cd ${.CURDIR}; ${CROSSENV} PATH=${TMPPATH} \
1554                 ${MAKE} -f Makefile.inc1 ${IMAKE_INSTALL} \
1555                 METALOG=${METALOG} MK_TESTS=no \
1556                 MK_TESTS_SUPPORT=${MK_TESTS_SUPPORT} installconfig
1557 .endif
1558
1559 #
1560 # buildkernel and installkernel
1561 #
1562 # Which kernels to build and/or install is specified by setting
1563 # KERNCONF. If not defined a GENERIC kernel is built/installed.
1564 # Only the existing (depending TARGET) config files are used
1565 # for building kernels and only the first of these is designated
1566 # as the one being installed.
1567 #
1568 # Note that we have to use TARGET instead of TARGET_ARCH when
1569 # we're in kernel-land. Since only TARGET_ARCH is (expected) to
1570 # be set to cross-build, we have to make sure TARGET is set
1571 # properly.
1572
1573 .if defined(KERNFAST)
1574 NO_KERNELCLEAN= t
1575 NO_KERNELCONFIG=        t
1576 NO_KERNELOBJ=           t
1577 # Shortcut for KERNCONF=Blah -DKERNFAST is now KERNFAST=Blah
1578 .if !defined(KERNCONF) && ${KERNFAST} != "1"
1579 KERNCONF=${KERNFAST}
1580 .endif
1581 .endif
1582 .if ${TARGET_ARCH} == "powerpc64"
1583 KERNCONF?=      GENERIC64
1584 .elif ${TARGET_ARCH} == "powerpc64le"
1585 KERNCONF?=      GENERIC64LE
1586 .elif ${TARGET_ARCH} == "powerpcspe"
1587 KERNCONF?=      MPC85XXSPE
1588 .else
1589 KERNCONF?=      GENERIC
1590 .endif
1591 INSTKERNNAME?=  kernel
1592
1593 KERNSRCDIR?=    ${.CURDIR}/sys
1594 KRNLCONFDIR=    ${KERNSRCDIR}/${TARGET}/conf
1595 KRNLOBJDIR=     ${OBJTOP}${KERNSRCDIR:C,^${.CURDIR},,}
1596 KERNCONFDIR?=   ${KRNLCONFDIR}
1597
1598 BUILDKERNELS=
1599 INSTALLKERNEL=
1600 .if defined(NO_INSTALLKERNEL)
1601 # All of the BUILDKERNELS loops start at index 1.
1602 BUILDKERNELS+= dummy
1603 .endif
1604 .for _kernel in ${KERNCONF}
1605 .if !defined(_MKSHOWCONFIG) && exists(${KERNCONFDIR}/${_kernel})
1606 BUILDKERNELS+=  ${_kernel}
1607 .if empty(INSTALLKERNEL) && !defined(NO_INSTALLKERNEL)
1608 INSTALLKERNEL= ${_kernel}
1609 .endif
1610 .else
1611 .if make(buildkernel)
1612 .error Missing KERNCONF ${KERNCONFDIR}/${_kernel}
1613 .endif
1614 .endif
1615 .endfor
1616
1617 _cleankernobj_fast_depend_hack: .PHONY
1618 # 20191009  r353340  removal of opensolaris_atomic.S (also r353381)
1619 .if ${MACHINE} != i386
1620 .for f in opensolaris_atomic
1621 .for m in opensolaris zfs
1622         @if [ -e "${KRNLOBJDIR}/${KERNCONF}/modules${SRCTOP}/sys/modules/${m}/.depend.${f}.o" ] && \
1623             grep -q ${f}.S "${KRNLOBJDIR}/${KERNCONF}/modules${SRCTOP}/sys/modules/${m}/.depend.${f}.o"; then \
1624                 echo "Removing stale dependencies for opensolaris_atomic"; \
1625                 rm -f ${KRNLOBJDIR}/${KERNCONF}/modules${SRCTOP}/sys/modules/${m}/.depend.${f}.*; \
1626         fi
1627 .endfor
1628 .endfor
1629 .endif
1630
1631 ${WMAKE_TGTS:N_worldtmp:Nbuild${libcompat}} ${.ALLTARGETS:M_*:N_worldtmp}: .MAKE .PHONY
1632
1633 # record kernel(s) build time in seconds
1634 .if make(buildkernel)
1635 _BUILDKERNEL_START!= date '+%s'
1636 .endif
1637
1638 #
1639 # buildkernel
1640 #
1641 # Builds all kernels defined by BUILDKERNELS.
1642 #
1643 buildkernel: .MAKE .PHONY
1644 .if empty(BUILDKERNELS:Ndummy)
1645         @echo "ERROR: Missing kernel configuration file(s) (${KERNCONF})."; \
1646         false
1647 .endif
1648         @echo
1649 .for _kernel in ${BUILDKERNELS:Ndummy}
1650         @echo "--------------------------------------------------------------"
1651         @echo ">>> Kernel build for ${_kernel} started on `LC_ALL=C date`"
1652         @echo "--------------------------------------------------------------"
1653         @echo "===> ${_kernel}"
1654         mkdir -p ${KRNLOBJDIR}
1655 .if !defined(NO_KERNELCONFIG)
1656         @echo
1657         @echo "--------------------------------------------------------------"
1658         @echo ">>> stage 1: configuring the kernel"
1659         @echo "--------------------------------------------------------------"
1660         cd ${KRNLCONFDIR}; \
1661                 PATH=${TMPPATH} \
1662                     config ${CONFIGARGS} -d ${KRNLOBJDIR}/${_kernel} \
1663                         -I '${KERNCONFDIR}' -I '${KRNLCONFDIR}' \
1664                         '${KERNCONFDIR}/${_kernel}'
1665 .endif
1666 .if ${MK_CLEAN} == "yes" && !defined(NO_KERNELCLEAN)
1667         @echo
1668         @echo "--------------------------------------------------------------"
1669         @echo ">>> stage 2.1: cleaning up the object tree"
1670         @echo "--------------------------------------------------------------"
1671         ${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} ${CLEANDIR}
1672 .else
1673         ${_+_}cd ${.CURDIR}; ${WMAKE} _cleankernobj_fast_depend_hack
1674 .endif
1675 .if !defined(NO_KERNELOBJ)
1676         @echo
1677         @echo "--------------------------------------------------------------"
1678         @echo ">>> stage 2.2: rebuilding the object tree"
1679         @echo "--------------------------------------------------------------"
1680         ${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} obj
1681 .endif
1682         @echo
1683         @echo "--------------------------------------------------------------"
1684         @echo ">>> stage 2.3: build tools"
1685         @echo "--------------------------------------------------------------"
1686         ${_+_}cd ${.CURDIR}; ${KTMAKE} kernel-tools
1687         @echo
1688         @echo "--------------------------------------------------------------"
1689         @echo ">>> stage 3.1: building everything"
1690         @echo "--------------------------------------------------------------"
1691         ${_+_}cd ${KRNLOBJDIR}/${_kernel}; ${KMAKE} all -DNO_MODULES_OBJ
1692         @echo "--------------------------------------------------------------"
1693         @echo ">>> Kernel build for ${_kernel} completed on `LC_ALL=C date`"
1694         @echo "--------------------------------------------------------------"
1695
1696 .endfor
1697         @seconds=$$(($$(date '+%s') - ${_BUILDKERNEL_START})); \
1698           echo -n ">>> Kernel(s) ${BUILDKERNELS} built in $$seconds seconds, "; \
1699           echo "ncpu: $$(${_ncpu_cmd})${.MAKE.JOBS:S/^/, make -j/}"
1700         @echo "--------------------------------------------------------------"
1701
1702 .if !make(packages) && !make(update-packages)
1703 NO_INSTALLEXTRAKERNELS?=        yes
1704 .else
1705 # packages/update-packages installs kernels to a staging directory then builds
1706 # packages from the result to be installed, typically to other systems.  It is
1707 # less surprising for these targets to honor KERNCONF if multiple kernels are
1708 # specified.
1709 NO_INSTALLEXTRAKERNELS?=        no
1710 .endif
1711
1712 #
1713 # installkernel, etc.
1714 #
1715 # Install the kernel defined by INSTALLKERNEL
1716 #
1717 installkernel installkernel.debug \
1718 reinstallkernel reinstallkernel.debug: _installcheck_kernel .PHONY
1719 .if !defined(NO_INSTALLKERNEL)
1720 .if empty(INSTALLKERNEL)
1721         @echo "ERROR: No kernel \"${KERNCONF}\" to install."; \
1722         false
1723 .endif
1724         @echo "--------------------------------------------------------------"
1725         @echo ">>> Installing kernel ${INSTALLKERNEL} on $$(LC_ALL=C date)"
1726         @echo "--------------------------------------------------------------"
1727         ${_+_}cd ${KRNLOBJDIR}/${INSTALLKERNEL}; \
1728             ${CROSSENV} PATH=${TMPPATH} \
1729             ${MAKE} ${IMAKE_INSTALL} KERNEL=${INSTKERNNAME} ${.TARGET:S/kernel//}
1730         @echo "--------------------------------------------------------------"
1731         @echo ">>> Installing kernel ${INSTALLKERNEL} completed on $$(LC_ALL=C date)"
1732         @echo "--------------------------------------------------------------"
1733 .endif
1734 .if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1735 .for _kernel in ${BUILDKERNELS:[2..-1]}
1736         @echo "--------------------------------------------------------------"
1737         @echo ">>> Installing kernel ${_kernel} $$(LC_ALL=C date)"
1738         @echo "--------------------------------------------------------------"
1739         ${_+_}cd ${KRNLOBJDIR}/${_kernel}; \
1740             ${CROSSENV} PATH=${TMPPATH} \
1741             ${MAKE} ${IMAKE_INSTALL} KERNEL=${INSTKERNNAME}.${_kernel} ${.TARGET:S/kernel//}
1742         @echo "--------------------------------------------------------------"
1743         @echo ">>> Installing kernel ${_kernel} completed on $$(LC_ALL=C date)"
1744         @echo "--------------------------------------------------------------"
1745 .endfor
1746 .endif
1747
1748 distributekernel distributekernel.debug: .PHONY
1749 .if !defined(NO_INSTALLKERNEL)
1750 .if empty(INSTALLKERNEL)
1751         @echo "ERROR: No kernel \"${KERNCONF}\" to install."; \
1752         false
1753 .endif
1754         mkdir -p ${DESTDIR}/${DISTDIR}
1755 .if defined(NO_ROOT)
1756         @echo "#${MTREE_MAGIC}" > ${DESTDIR}/${DISTDIR}/kernel.premeta
1757 .endif
1758         ${_+_}cd ${KRNLOBJDIR}/${INSTALLKERNEL}; \
1759             ${IMAKEENV} ${IMAKE_INSTALL:S/METALOG/kernel.premeta/} \
1760             ${IMAKE_MTREE} PATH=${TMPPATH} ${MAKE} KERNEL=${INSTKERNNAME} \
1761             DESTDIR=${INSTALL_DDIR}/kernel \
1762             ${.TARGET:S/distributekernel/install/}
1763 .if defined(NO_ROOT)
1764         @sed -e 's|^./kernel|.|' ${DESTDIR}/${DISTDIR}/kernel.premeta > \
1765             ${DESTDIR}/${DISTDIR}/kernel.meta
1766 .endif
1767 .endif
1768 .if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1769 .for _kernel in ${BUILDKERNELS:[2..-1]}
1770 .if defined(NO_ROOT)
1771         @echo "#${MTREE_MAGIC}" > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.premeta
1772 .endif
1773         ${_+_}cd ${KRNLOBJDIR}/${_kernel}; \
1774             ${IMAKEENV} ${IMAKE_INSTALL:S/METALOG/kernel.${_kernel}.premeta/} \
1775             ${IMAKE_MTREE} PATH=${TMPPATH} ${MAKE} \
1776             KERNEL=${INSTKERNNAME}.${_kernel} \
1777             DESTDIR=${INSTALL_DDIR}/kernel.${_kernel} \
1778             ${.TARGET:S/distributekernel/install/}
1779 .if defined(NO_ROOT)
1780         @sed -e "s|^./kernel.${_kernel}|.|" \
1781             ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.premeta > \
1782             ${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta
1783 .endif
1784 .endfor
1785 .endif
1786
1787 packagekernel: .PHONY
1788 .if defined(NO_ROOT)
1789 .if !defined(NO_INSTALLKERNEL)
1790         cd ${DESTDIR}/${DISTDIR}/kernel; \
1791             tar cvf - --exclude '*.debug' \
1792             @${DESTDIR}/${DISTDIR}/kernel.meta | \
1793             ${XZ_CMD} > ${PACKAGEDIR}/kernel.txz
1794 .endif
1795 .if ${MK_DEBUG_FILES} != "no"
1796         cd ${DESTDIR}/${DISTDIR}/kernel; \
1797             tar cvf - --include '*/*/*.debug' \
1798             @${DESTDIR}/${DISTDIR}/kernel.meta | \
1799             ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel-dbg.txz
1800 .endif
1801 .if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1802 .for _kernel in ${BUILDKERNELS:[2..-1]}
1803         cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1804             tar cvf - --exclude '*.debug' \
1805             @${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta | \
1806             ${XZ_CMD} > ${PACKAGEDIR}/kernel.${_kernel}.txz
1807 .if ${MK_DEBUG_FILES} != "no"
1808         cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1809             tar cvf - --include '*/*/*.debug' \
1810             @${DESTDIR}/${DISTDIR}/kernel.${_kernel}.meta | \
1811             ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}-dbg.txz
1812 .endif
1813 .endfor
1814 .endif
1815 .else
1816 .if !defined(NO_INSTALLKERNEL)
1817         cd ${DESTDIR}/${DISTDIR}/kernel; \
1818             tar cvf - --exclude '*.debug' . | \
1819             ${XZ_CMD} > ${PACKAGEDIR}/kernel.txz
1820 .endif
1821 .if ${MK_DEBUG_FILES} != "no"
1822         cd ${DESTDIR}/${DISTDIR}/kernel; \
1823             tar cvf - --include '*/*/*.debug' $$(eval find .) | \
1824             ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel-dbg.txz
1825 .endif
1826 .if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
1827 .for _kernel in ${BUILDKERNELS:[2..-1]}
1828         cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1829             tar cvf - --exclude '*.debug' . | \
1830             ${XZ_CMD} > ${PACKAGEDIR}/kernel.${_kernel}.txz
1831 .if ${MK_DEBUG_FILES} != "no"
1832         cd ${DESTDIR}/${DISTDIR}/kernel.${_kernel}; \
1833             tar cvf - --include '*/*/*.debug' $$(eval find .) | \
1834             ${XZ_CMD} > ${DESTDIR}/${DISTDIR}/kernel.${_kernel}-dbg.txz
1835 .endif
1836 .endfor
1837 .endif
1838 .endif
1839
1840 stagekernel: .PHONY
1841         ${_+_}${MAKE} -C ${.CURDIR} ${.MAKEFLAGS} distributekernel
1842
1843 PORTSDIR?=      /usr/ports
1844 WSTAGEDIR?=     ${OBJTOP}/worldstage
1845 KSTAGEDIR?=     ${OBJTOP}/kernelstage
1846 REPODIR?=       ${OBJROOT}repo
1847 PKG_FORMAT?=    txz
1848 PKG_REPO_SIGNING_KEY?=  # empty
1849 PKG_OUTPUT_DIR?=        ${PKG_VERSION}
1850
1851 .ORDER:         stage-packages create-packages
1852 .ORDER:         create-packages create-world-packages
1853 .ORDER:         create-packages create-kernel-packages
1854 .ORDER:         create-packages sign-packages
1855
1856 _pkgbootstrap: .PHONY
1857 .if make(*package*) && !exists(${LOCALBASE}/sbin/pkg)
1858         @env ASSUME_ALWAYS_YES=YES pkg bootstrap
1859 .endif
1860
1861 .if make(create-world-packages-jobs) || make(create-kernel-packages*) || make(real-update-packages) || make(sign-packages)
1862 PKG_ABI!=${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI
1863 .endif
1864 PKG_BIN_VERSION!=${PKG_CMD} --version | awk -F. '/^[0-9.]+$$/ {print $$1 * 10000 + $$2 * 100 + $$3}'
1865 .if ${PKG_BIN_VERSION} < 11700
1866 PKG_EXT=        ${PKG_FORMAT}
1867 .else
1868 PKG_EXT=        pkg
1869 .endif
1870
1871 .if !defined(PKG_VERSION_FROM) && make(real-update-packages)
1872 .if defined(PKG_ABI)
1873 .if exists(${REPODIR}/${PKG_ABI})
1874 PKG_VERSION_FROM!=/usr/bin/readlink ${REPODIR}/${PKG_ABI}/latest
1875 PKG_VERSION_FROM_DIR=   ${REPODIR}/${PKG_ABI}/${PKG_VERSION_FROM}
1876 .else
1877 PKG_VERSION_FROM=
1878 PKG_VERSION_FROM_DIR=
1879 .endif
1880 .endif
1881 .endif
1882
1883 PKGMAKEARGS+=   PKG_VERSION=${PKG_VERSION} \
1884                 NO_INSTALLEXTRAKERNELS=${NO_INSTALLEXTRAKERNELS}
1885
1886 packages: .PHONY
1887         ${_+_}${MAKE} -C ${.CURDIR} ${PKGMAKEARGS} real-packages
1888
1889 update-packages: .PHONY
1890         ${_+_}${MAKE} -C ${.CURDIR} ${PKGMAKEARGS} real-update-packages
1891
1892 package-pkg: .PHONY
1893         rm -rf /tmp/ports.${TARGET} || :
1894         env ${WMAKEENV:Q} SRCDIR=${.CURDIR} PORTSDIR=${PORTSDIR} REVISION=${_REVISION} \
1895                 PKG_CMD=${PKG_CMD} PKG_VERSION=${PKG_VERSION} REPODIR=${REPODIR} \
1896                 WSTAGEDIR=${WSTAGEDIR} \
1897                 sh ${.CURDIR}/release/scripts/make-pkg-package.sh
1898
1899 real-packages:  stage-packages create-packages sign-packages .PHONY
1900
1901 real-update-packages: stage-packages .PHONY
1902         ${_+_}${MAKE} -C ${.CURDIR} PKG_VERSION=${PKG_VERSION} create-packages
1903 .if empty(PKG_VERSION_FROM_DIR)
1904         @echo "==> Bootstrapping repository, not checking for new packages"
1905 .else
1906         @echo "==> Checking for new packages (comparing ${PKG_VERSION} to ${PKG_VERSION_FROM})"
1907         @for pkg in ${PKG_VERSION_FROM_DIR}/${PKG_NAME_PREFIX}-*; do \
1908           pkgname=$$(pkg query -F $${pkg} '%n' | sed 's/${PKG_NAME_PREFIX}-\(.*\)/\1/') ; \
1909           newpkgname=${PKG_NAME_PREFIX}-$${pkgname}-${PKG_VERSION}.${PKG_EXT} ; \
1910           oldsum=$$(pkg query -F $${pkg} '%X') ; \
1911           if [ ! -f ${REPODIR}/${PKG_ABI}/${PKG_VERSION}/$${newpkgname} ]; then \
1912             continue; \
1913           fi ; \
1914           newsum=$$(pkg query -F ${REPODIR}/${PKG_ABI}/${PKG_VERSION}/$${newpkgname} '%X') ; \
1915           if [ "$${oldsum}" == "$${newsum}" ]; then \
1916            echo "==> Keeping old ${PKG_NAME_PREFIX}-$${pkgname}-${PKG_VERSION_FROM}.${PKG_EXT}" ; \
1917            rm ${REPODIR}/${PKG_ABI}/${PKG_VERSION}/$${newpkgname} ; \
1918            cp $${pkg} ${REPODIR}/${PKG_ABI}/${PKG_VERSION} ; \
1919           else \
1920             echo "==> New package $${newpkgname}" ; \
1921           fi ; \
1922         done
1923 .endif
1924         ${_+_}@cd ${.CURDIR}; \
1925                 ${MAKE} -f Makefile.inc1 PKG_VERSION=${PKG_VERSION} sign-packages
1926
1927 stage-packages-world: .PHONY
1928         @mkdir -p ${WSTAGEDIR}
1929         ${_+_}@cd ${.CURDIR}; \
1930                 ${MAKE} DESTDIR=${WSTAGEDIR} -DNO_ROOT stageworld
1931
1932 stage-packages-kernel: .PHONY
1933         @mkdir -p ${KSTAGEDIR}
1934         ${_+_}@cd ${.CURDIR}; \
1935                 ${MAKE} DESTDIR=${KSTAGEDIR} -DNO_ROOT stagekernel
1936
1937 stage-packages: .PHONY stage-packages-world stage-packages-kernel
1938
1939 _repodir: .PHONY
1940         @mkdir -p ${REPODIR}
1941
1942 create-packages-world:  _pkgbootstrap _repodir .PHONY
1943         ${_+_}@cd ${.CURDIR}; \
1944                 ${MAKE} -f Makefile.inc1 \
1945                         DESTDIR=${WSTAGEDIR} \
1946                         PKG_VERSION=${PKG_VERSION} create-world-packages
1947
1948 create-packages-kernel: _pkgbootstrap _repodir .PHONY
1949         ${_+_}@cd ${.CURDIR}; \
1950                 ${MAKE} -f Makefile.inc1 \
1951                         DESTDIR=${KSTAGEDIR} \
1952                         PKG_VERSION=${PKG_VERSION} DISTDIR=kernel \
1953                         SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH} \
1954                         create-kernel-packages
1955
1956 create-packages: .PHONY create-packages-world create-packages-kernel
1957
1958 create-world-packages:  _pkgbootstrap .PHONY
1959         @rm -f ${WSTAGEDIR}/*.plist 2>/dev/null || :
1960         @cd ${WSTAGEDIR} ; \
1961                 env -i LC_COLLATE=C sort ${WSTAGEDIR}/${DISTDIR}/METALOG | \
1962                 awk -f ${SRCDIR}/release/scripts/mtree-to-plist.awk
1963         @for plist in ${WSTAGEDIR}/*.plist; do \
1964           plist=$${plist##*/} ; \
1965           pkgname=$${plist%.plist} ; \
1966           echo "_PKGS+= $${pkgname}" ; \
1967         done > ${WSTAGEDIR}/packages.mk
1968         ${_+_}@cd ${.CURDIR}; \
1969                 ${MAKE} -f Makefile.inc1 create-world-packages-jobs \
1970                 SOURCE_DATE_EPOCH=${SOURCE_DATE_EPOCH} \
1971                 .MAKE.JOB.PREFIX=
1972
1973 .if make(create-world-packages-jobs)
1974 .include "${WSTAGEDIR}/packages.mk"
1975 .endif
1976
1977 create-world-packages-jobs: .PHONY
1978 .for pkgname in ${_PKGS}
1979 create-world-packages-jobs: create-world-package-${pkgname}
1980 create-world-package-${pkgname}: .PHONY
1981         @sh ${SRCDIR}/release/packages/generate-ucl.sh -o ${pkgname} \
1982                 -s ${SRCDIR} -u ${WSTAGEDIR}/${pkgname}.ucl
1983         @awk -F\" ' \
1984                 /^name/ { printf("===> Creating %s-", $$2); next } \
1985                 /^version/ { print $$2; next } \
1986                 ' ${WSTAGEDIR}/${pkgname}.ucl
1987         @if [ "${pkgname}" == "runtime" ]; then \
1988                 sed -i '' -e "s/%VCS_REVISION%/${VCS_REVISION}/" ${WSTAGEDIR}/${pkgname}.ucl ; \
1989         fi
1990         ${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname -o ALLOW_BASE_SHLIBS=yes \
1991                 create -f ${PKG_FORMAT} -M ${WSTAGEDIR}/${pkgname}.ucl \
1992                 -p ${WSTAGEDIR}/${pkgname}.plist \
1993                 -r ${WSTAGEDIR} \
1994                 -o ${REPODIR}/${PKG_ABI}/${PKG_OUTPUT_DIR}
1995 .endfor
1996
1997 _default_flavor=        -default
1998 .if make(*package*) && exists(${KSTAGEDIR}/kernel.meta)
1999 . if ${MK_DEBUG_FILES} != "no"
2000 _debug=-dbg
2001 . endif
2002 create-kernel-packages: .PHONY
2003 . for flavor in "" ${_debug}
2004 create-kernel-packages: create-kernel-packages-flavor${flavor:C,^""$,${_default_flavor},}
2005 create-kernel-packages-flavor${flavor:C,^""$,${_default_flavor},}: _pkgbootstrap .PHONY
2006         @cd ${KSTAGEDIR}/${DISTDIR} ; \
2007         env -i LC_COLLATE=C sort ${KSTAGEDIR}/kernel.meta | \
2008         awk -f ${SRCDIR}/release/scripts/mtree-to-plist.awk \
2009                 -v kernel=yes -v _kernconf=${INSTALLKERNEL} ; \
2010         sed -e "s/%VERSION%/${PKG_VERSION}/" \
2011                 -e "s/%PKGNAME%/kernel-${INSTALLKERNEL:tl}${flavor}/" \
2012                 -e "s/%KERNELDIR%/kernel/" \
2013                 -e "s/%COMMENT%/FreeBSD ${INSTALLKERNEL} kernel ${flavor}/" \
2014                 -e "s/%DESC%/FreeBSD ${INSTALLKERNEL} kernel ${flavor}/" \
2015                 -e "s/ %VCS_REVISION%/${VCS_REVISION}/" \
2016                 -e "s/%PKG_NAME_PREFIX%/${PKG_NAME_PREFIX}/" \
2017                 -e "s/%PKG_MAINTAINER%/${PKG_MAINTAINER}/" \
2018                 -e "s|%PKG_WWW%|${PKG_WWW}|" \
2019                 ${SRCDIR}/release/packages/kernel.ucl \
2020                 > ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.ucl ; \
2021         awk -F\" ' \
2022                 /name/ { printf("===> Creating %s-", $$2); next } \
2023                 /version/ {print $$2; next } ' \
2024                 ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.ucl ; \
2025         ${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname -o ALLOW_BASE_SHLIBS=yes \
2026                 create -f ${PKG_FORMAT} \
2027                 -M ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.ucl \
2028                 -p ${KSTAGEDIR}/${DISTDIR}/kernel.${INSTALLKERNEL}${flavor}.plist \
2029                 -r ${KSTAGEDIR}/${DISTDIR} \
2030                 -o ${REPODIR}/${PKG_ABI}/${PKG_OUTPUT_DIR}
2031 . endfor
2032 .endif
2033 .if ${BUILDKERNELS:[#]} > 1 && ${NO_INSTALLEXTRAKERNELS} != "yes"
2034 . for _kernel in ${BUILDKERNELS:[2..-1]}
2035 .  if exists(${KSTAGEDIR}/kernel.${_kernel}.meta)
2036 .   if ${MK_DEBUG_FILES} != "no"
2037 _debug=-dbg
2038 .   endif
2039 .   for flavor in "" ${_debug}
2040 create-kernel-packages: create-kernel-packages-extra-flavor${flavor:C,^""$,${_default_flavor},}-${_kernel}
2041 create-kernel-packages-extra-flavor${flavor:C,^""$,${_default_flavor},}-${_kernel}: _pkgbootstrap .PHONY
2042         @cd ${KSTAGEDIR}/kernel.${_kernel} ; \
2043         env -i LC_COLLATE=C sort ${KSTAGEDIR}/kernel.${_kernel}.meta | \
2044         awk -f ${SRCDIR}/release/scripts/mtree-to-plist.awk \
2045                 -v kernel=yes -v _kernconf=${_kernel} ; \
2046         sed -e "s/%VERSION%/${PKG_VERSION}/" \
2047                 -e "s/%PKGNAME%/kernel-${_kernel:tl}${flavor}/" \
2048                 -e "s/%KERNELDIR%/kernel.${_kernel}/" \
2049                 -e "s/%COMMENT%/FreeBSD ${_kernel} kernel ${flavor}/" \
2050                 -e "s/%DESC%/FreeBSD ${_kernel} kernel ${flavor}/" \
2051                 -e "s/ %VCS_REVISION%/${VCS_REVISION}/" \
2052                 -e "s/%PKG_NAME_PREFIX%/${PKG_NAME_PREFIX}/" \
2053                 -e "s/%PKG_MAINTAINER%/${PKG_MAINTAINER}/" \
2054                 -e "s|%PKG_WWW%|${PKG_WWW}|" \
2055                 ${SRCDIR}/release/packages/kernel.ucl \
2056                 > ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.ucl ; \
2057         awk -F\" ' \
2058                 /name/ { printf("===> Creating %s-", $$2); next } \
2059                 /version/ {print $$2; next } ' \
2060                 ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.ucl ; \
2061         ${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname -o ALLOW_BASE_SHLIBS=yes \
2062                 create -f ${PKG_FORMAT} \
2063                 -M ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.ucl \
2064                 -p ${KSTAGEDIR}/kernel.${_kernel}/kernel.${_kernel}${flavor}.plist \
2065                 -r ${KSTAGEDIR}/kernel.${_kernel} \
2066                 -o ${REPODIR}/${PKG_ABI}/${PKG_OUTPUT_DIR}
2067 .   endfor
2068 .  endif
2069 . endfor
2070 .endif
2071
2072 sign-packages:  _pkgbootstrap .PHONY
2073         printf "version = 2;\n" > ${WSTAGEDIR}/meta
2074 .if ${PKG_BIN_VERSION} < 11700
2075         printf "packing_format = \"${PKG_FORMAT}\";\n" >> ${WSTAGEDIR}/meta
2076 .endif
2077         @[ -L "${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI)/latest" ] && \
2078                 unlink ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI)/latest ; \
2079         ${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname repo \
2080                 -m ${WSTAGEDIR}/meta \
2081                 -o ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI)/${PKG_VERSION} \
2082                 ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI)/${PKG_VERSION} \
2083                 ${PKG_REPO_SIGNING_KEY} ; \
2084         cd ${REPODIR}/$$(${PKG_CMD} -o ABI_FILE=${WSTAGEDIR}/usr/bin/uname config ABI); \
2085         ln -s ${PKG_OUTPUT_DIR} latest
2086
2087 #
2088 #
2089 # checkworld
2090 #
2091 # Run test suite on installed world.
2092 #
2093 checkworld: .PHONY
2094         @if [ ! -x "${LOCALBASE}/bin/kyua" ] && [ ! -x "/usr/bin/kyua" ]; then \
2095                 echo "You need kyua (devel/kyua) to run the test suite." | /usr/bin/fmt; \
2096                 exit 1; \
2097         fi
2098         ${_+_}PATH="$$PATH:${LOCALBASE}/bin" kyua test -k ${TESTSBASE}/Kyuafile
2099
2100 #
2101 #
2102 # doxygen
2103 #
2104 # Build the API documentation with doxygen
2105 #
2106 doxygen: .PHONY
2107         @if [ ! -x "${LOCALBASE}/bin/doxygen" ]; then \
2108                 echo "You need doxygen (devel/doxygen) to generate the API documentation of the kernel." | /usr/bin/fmt; \
2109                 exit 1; \
2110         fi
2111         ${_+_}cd ${.CURDIR}/tools/kerneldoc/subsys; ${MAKE} obj all
2112
2113 #
2114 # ------------------------------------------------------------------------
2115 #
2116 # From here onwards are utility targets used by the 'make world' and
2117 # related targets.  If your 'world' breaks, you may like to try to fix
2118 # the problem and manually run the following targets to attempt to
2119 # complete the build.  Beware, this is *not* guaranteed to work, you
2120 # need to have a pretty good grip on the current state of the system
2121 # to attempt to manually finish it.  If in doubt, 'make world' again.
2122 #
2123
2124 #
2125 # legacy: Build compatibility shims for the next three targets. This is a
2126 # minimal set of tools and shims necessary to compensate for older systems
2127 # which don't have the APIs required by the targets built in bootstrap-tools,
2128 # build-tools or cross-tools.
2129 #
2130 legacy: .PHONY
2131 .if ${BOOTSTRAPPING} < ${MINIMUM_SUPPORTED_OSREL} && ${BOOTSTRAPPING} != 0
2132         @echo "ERROR: Source upgrades from versions prior to ${MINIMUM_SUPPORTED_REL} are not supported."; \
2133         false
2134 .endif
2135
2136 .for _tool in \
2137   tools/build \
2138   ${LOCAL_LEGACY_DIRS}
2139         ${_+_}@${ECHODIR} "===> ${_tool} (obj,includes,all,install)"; \
2140             cd ${.CURDIR}/${_tool}; \
2141             if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
2142             ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${WORLDTMP}/legacy includes; \
2143             ${MAKE} DIRPRFX=${_tool}/ MK_INCLUDES=no all; \
2144             ${MAKE} DIRPRFX=${_tool}/ MK_INCLUDES=no \
2145                 DESTDIR=${WORLDTMP}/legacy install
2146 .endfor
2147
2148 #
2149 # bootstrap-tools: Build tools needed for compatibility. These are binaries that
2150 # are built to build other binaries in the system. However, the focus of these
2151 # binaries is usually quite narrow. Bootstrap tools use the host's compiler and
2152 # libraries, augmented by -legacy, in addition to the libraries built during
2153 # bootstrap-tools.
2154 #
2155 _bt=            _bootstrap-tools
2156
2157 # We want to run the build with only ${WORLDTMP} in $PATH to ensure we don't
2158 # accidentally run tools that are incompatible but happen to be in $PATH.
2159 # This is especially important when building on Linux/MacOS where many of the
2160 # programs used during the build accept different flags or generate different
2161 # output. On those platforms we only symlink the tools known to be compatible
2162 # (e.g. basic utilities such as mkdir) into ${WORLDTMP} and build all others
2163 # from the FreeBSD sources during the bootstrap-tools stage.
2164 # We want to build without the user's $PATH starting in the bootstrap-tools
2165 # phase so the tools used in that phase (ln, cp, etc) must have already been
2166 # linked to $WORLDTMP. The tools are listed in the _host_tools_to_symlink
2167 # variable in tools/build/Makefile and are linked during the legacy phase.
2168 # Since they could be Linux or MacOS binaries, too we must only use flags that
2169 # are portable across operating systems.
2170
2171 # If BOOTSTRAP_ALL_TOOLS is set we will build all the required tools from the
2172 # current source tree. Otherwise we create a symlink to the version found in
2173 # $PATH during the bootstrap-tools stage.
2174 # When building on non-FreeBSD systems we can't assume that the host binaries
2175 # accept compatible flags or produce compatible output. Therefore we force
2176 # BOOTSTRAP_ALL_TOOLS and just build the FreeBSD version of the binary.
2177 .if defined(CROSSBUILD_HOST)
2178 BOOTSTRAP_ALL_TOOLS:=   1
2179 .endif
2180 .if defined(BOOTSTRAP_ALL_TOOLS)
2181 # BOOTSTRAPPING will be set on the command line so we can't override it here.
2182 # Instead set BOOTSTRAPPING_OSRELDATE so that the value 0 is set ${BSARGS}
2183 BOOTSTRAPPING_OSRELDATE:=       0
2184 .endif
2185
2186 # libnv and libsbuf are requirements for config(8), which is an unconditional
2187 # bootstrap-tool.
2188 _config=usr.sbin/config lib/libnv lib/libsbuf
2189 ${_bt}-usr.sbin/config: ${_bt}-lib/libnv ${_bt}-lib/libsbuf
2190
2191 .if ${MK_GAMES} != "no"
2192 _strfile=       usr.bin/fortune/strfile
2193 .endif
2194
2195 # vtfontcvt is used to build font files for loader and to generate
2196 # C source for loader built in font (8x16.c).
2197 _vtfontcvt=     usr.bin/vtfontcvt
2198
2199 # If we are not building the bootstrap because BOOTSTRAPPING is sufficient
2200 # we symlink the host version to $WORLDTMP instead. By doing this we can also
2201 # detect when a bootstrap tool is being used without the required MK_FOO.
2202 # If you add a new bootstrap tool where we could also use the host version,
2203 # please ensure that you also add a .else case where you add the tool to the
2204 # _bootstrap_tools_links variable.
2205 .if ${BOOTSTRAPPING} < 1000033
2206 # Note: lex needs m4 to build but m4 also depends on lex (which needs m4 to
2207 # generate any files). To fix this cyclic dependency we can build a bootstrap
2208 # version of m4 (with pre-generated files) then use that to build the real m4.
2209 # We can't simply use the host m4 since e.g. the macOS version does not accept
2210 # the flags that are passed by lex.
2211 # For lex we also use the pre-gerated files since we would otherwise need to
2212 # build awk and sed first (which need lex to build)
2213 # TODO: add a _bootstrap_lex and then build the real lex afterwards
2214 _lex=           usr.bin/lex
2215 _m4=            tools/build/bootstrap-m4 usr.bin/m4
2216 ${_bt}-tools/build/bootstrap-m4: ${_bt}-usr.bin/lex ${_bt}-lib/libopenbsd ${_bt}-usr.bin/yacc
2217 ${_bt}-usr.bin/m4: ${_bt}-lib/libopenbsd ${_bt}-usr.bin/yacc ${_bt}-usr.bin/lex ${_bt}-tools/build/bootstrap-m4
2218 _bt_m4_depend=${_bt}-usr.bin/m4
2219 _bt_lex_depend=${_bt}-usr.bin/lex ${_bt_m4_depend}
2220 .else
2221 _bootstrap_tools_links+=m4 lex
2222 .endif
2223
2224 # ELF Tool Chain libraries are needed for ELF tools and dtrace tools.
2225 # r296685 fix cross-endian objcopy
2226 # r310724 fixed PR 215350, a crash in libdwarf with objects built by GCC 6.2.
2227 # r334881 added libdwarf constants used by ctfconvert.
2228 # r338478 fixed a crash in objcopy for mips64el objects
2229 # r339083 libelf: correct mips64el test to use ELF header
2230 # r348347 Add missing powerpc64 relocation support to libdwarf
2231 .if ${BOOTSTRAPPING} < 1300030
2232 _elftoolchain_libs= lib/libelf lib/libdwarf lib/libzstd
2233 ${_bt}-lib/libelf: ${_bt_m4_depend}
2234 ${_bt}-lib/libdwarf: ${_bt_m4_depend}
2235 .endif
2236
2237 # flua is required to regenerate syscall files.  It first appeared during the
2238 # 13.0-CURRENT cycle, thus needs to be built on -older releases and stable
2239 # branches.
2240 .if ${BOOTSTRAPPING} < 1300059
2241 ${_bt}-libexec/flua: ${_bt}-lib/liblua ${_bt}-lib/libucl
2242 _flua= lib/liblua lib/libucl libexec/flua
2243 .endif
2244
2245 # r245440 mtree -N support added
2246 # r313404 requires sha384.h for libnetbsd, added to libmd in r292782
2247 .if ${BOOTSTRAPPING} < 1100093
2248 _nmtree=        lib/libmd \
2249                 lib/libnetbsd \
2250                 usr.sbin/nmtree
2251
2252 ${_bt}-lib/libnetbsd: ${_bt}-lib/libmd
2253 ${_bt}-usr.sbin/nmtree: ${_bt}-lib/libnetbsd
2254 .else
2255 _bootstrap_tools_links+=mtree
2256 .endif
2257
2258 # r246097: log addition login.conf.db, passwd, pwd.db, and spwd.db with cat -l
2259 .if ${BOOTSTRAPPING} < 1000027
2260 _cat=           bin/cat
2261 .else
2262 _bootstrap_tools_links+=cat
2263 .endif
2264
2265 # r277259 crunchide: Correct 64-bit section header offset
2266 # r281674 crunchide: always include both 32- and 64-bit ELF support
2267 .if ${BOOTSTRAPPING} < 1100078
2268 _crunchide=     usr.sbin/crunch/crunchide
2269 .else
2270 _bootstrap_tools_links+=crunchide
2271 .endif
2272
2273 # 1300115: Higher WARNS fixes
2274 .if ${BOOTSTRAPPING} < 1202502 || \
2275         (${BOOTSTRAPPING} > 1300000 && ${BOOTSTRAPPING} < 1300131)
2276 _crunchgen=     usr.sbin/crunch/crunchgen
2277 .else
2278 _bootstrap_tools_links+=crunchgen
2279 .endif
2280
2281 # r296926 -P keymap search path, MFC to stable/10 in r298297
2282 # Note: kbdcontrol can not be bootstrapped on non-FreeBSD systems
2283 .if !defined(CROSSBUILD_HOST)
2284 .if (${BOOTSTRAPPING} < 1003501 || \
2285         (${BOOTSTRAPPING} >= 1100000 && ${BOOTSTRAPPING} < 1100103) || \
2286         (!exists(/usr/sbin/kbdcontrol)))
2287 _kbdcontrol=    usr.sbin/kbdcontrol
2288 .else
2289 _bootstrap_tools_links+=kbdcontrol
2290 .endif
2291 .endif
2292
2293 # 1300102: VHDX support
2294 .if ${BOOTSTRAPPING} < 1201520 || \
2295         (${BOOTSTRAPPING} > 1300000 && ${BOOTSTRAPPING} < 1300102)
2296 _mkimg= usr.bin/mkimg
2297 .else
2298 _bootstrap_tools_links+=mkimg
2299 .endif
2300
2301 _yacc=          usr.bin/yacc
2302
2303 .if ${MK_BSNMP} != "no"
2304 _gensnmptree=   usr.sbin/bsnmpd/gensnmptree
2305 .endif
2306
2307
2308 # We need to build tblgen when we're building clang or lld, either as
2309 # bootstrap tools, or as the part of the normal build.
2310 .if ${MK_CLANG_BOOTSTRAP} != "no" || ${MK_CLANG} != "no" || \
2311     ${MK_LLD_BOOTSTRAP} != "no" || ${MK_LLD} != "no" || \
2312     ${MK_LLDB} != "no"
2313 _clang_tblgen= \
2314         lib/clang/libllvmminimal \
2315         usr.bin/clang/llvm-tblgen
2316 .if ${MK_CLANG_BOOTSTRAP} != "no" || ${MK_CLANG} != "no"
2317 _clang_tblgen+= usr.bin/clang/clang-tblgen
2318 .endif
2319 .if ${MK_LLDB} != "no"
2320 _clang_tblgen+= usr.bin/clang/lldb-tblgen
2321 .endif
2322 ${_bt}-usr.bin/clang/clang-tblgen: ${_bt}-lib/clang/libllvmminimal
2323 ${_bt}-usr.bin/clang/llvm-tblgen: ${_bt}-lib/clang/libllvmminimal
2324 ${_bt}-usr.bin/clang/lldb-tblgen: ${_bt}-lib/clang/libllvmminimal
2325 .endif
2326
2327 .if ${MK_LOCALES} != "no"
2328 _localedef=     usr.bin/localedef
2329 ${_bt}-usr.bin/localedef: ${_bt}-usr.bin/yacc ${_bt_lex_depend}
2330 .endif
2331
2332 .if ${MK_KERBEROS} != "no"
2333 _kerberos5_bootstrap_tools= \
2334         kerberos5/tools/make-roken \
2335         kerberos5/lib/libroken \
2336         kerberos5/lib/libvers \
2337         kerberos5/tools/asn1_compile \
2338         kerberos5/tools/slc \
2339         usr.bin/compile_et
2340
2341 .ORDER: ${_kerberos5_bootstrap_tools:C/^/${_bt}-/g}
2342 .for _tool in ${_kerberos5_bootstrap_tools}
2343 ${_bt}-${_tool}: ${_bt}-usr.bin/yacc ${_bt_lex_depend}
2344 .endfor
2345 .endif
2346
2347 ${_bt}-usr.bin/mandoc: ${_bt}-lib/libopenbsd
2348
2349 # The tools listed in _basic_bootstrap_tools will generally not be
2350 # bootstrapped unless BOOTSTRAP_ALL_TOOL is set. However, when building on a
2351 # Linux or MacOS host the host versions are incompatible so we need to build
2352 # them from the source tree. Usually the link name will be the same as the subdir,
2353 # but some directories such as grep or test install multiple binaries. In that
2354 # case we use the _basic_bootstrap_tools_multilink variable which is a list of
2355 # subdirectory and comma-separated list of files.
2356 _basic_bootstrap_tools_multilink=usr.bin/grep grep,egrep,fgrep
2357 _basic_bootstrap_tools_multilink+=bin/test test,[
2358 # bootstrap tools needed by buildworld:
2359 _basic_bootstrap_tools+=usr.bin/cut bin/expr usr.bin/gencat usr.bin/join \
2360     usr.bin/mktemp bin/realpath bin/rmdir usr.bin/sed usr.bin/sort \
2361     usr.bin/truncate usr.bin/tsort
2362 # Some build scripts use nawk instead of awk (this happens at least in
2363 # cddl/contrib/opensolaris/lib/libdtrace/common/mknames.sh) so we need both awk
2364 # and nawk in ${WORLDTMP}/legacy/bin.
2365 _basic_bootstrap_tools_multilink+=usr.bin/awk awk,nawk
2366 # file2c is required for building usr.sbin/config:
2367 _basic_bootstrap_tools+=usr.bin/file2c
2368 # uuencode/uudecode required for share/tabset
2369 _basic_bootstrap_tools+=usr.bin/uuencode usr.bin/uudecode
2370 # xargs is required by mkioctls
2371 _basic_bootstrap_tools+=usr.bin/xargs
2372 # cap_mkdb is required for share/termcap:
2373 _basic_bootstrap_tools+=usr.bin/cap_mkdb
2374 # services_mkdb/pwd_mkdb are required for installworld:
2375 _basic_bootstrap_tools+=usr.sbin/services_mkdb usr.sbin/pwd_mkdb
2376 # ldd is required for installcheck (TODO: just always use /usr/bin/ldd instead?)
2377 .if !defined(CROSSBUILD_HOST)
2378 # ldd is only needed for updating the running system so we don't need to
2379 # bootstrap ldd on non-FreeBSD systems
2380 _basic_bootstrap_tools+=usr.bin/ldd
2381 .endif
2382 # sysctl/chflags are required for installkernel:
2383 .if !defined(CROSSBUILD_HOST)
2384 _basic_bootstrap_tools+=bin/chflags
2385 # Note: sysctl does not bootstrap on FreeBSD < 13 anymore, but that doesn't
2386 # matter since we don't need any of the new features for the build.
2387 _bootstrap_tools_links+=sysctl
2388 .else
2389 # When building on non-FreeBSD, install a fake chflags instead since the
2390 # version from the source tree cannot work. We also don't need sysctl since we
2391 # are install with -DNO_ROOT.
2392 _other_bootstrap_tools+=tools/build/cross-build/fake_chflags
2393 .endif
2394 # mkfifo is used by sys/conf/newvers.sh
2395 _basic_bootstrap_tools+=usr.bin/mkfifo
2396 # jot is needed for the mkimg tests
2397 _basic_bootstrap_tools+=usr.bin/jot
2398
2399 .if ${MK_BOOT} != "no"
2400 # md5 is used by boot/beri (and possibly others)
2401 _basic_bootstrap_tools+=sbin/md5
2402 .endif
2403
2404 .if ${MK_ZONEINFO} != "no"
2405 _basic_bootstrap_tools+=usr.sbin/zic usr.sbin/tzsetup
2406 .endif
2407
2408 .if defined(BOOTSTRAP_ALL_TOOLS)
2409 _other_bootstrap_tools+=${_basic_bootstrap_tools}
2410 .for _subdir _links in ${_basic_bootstrap_tools_multilink}
2411 _other_bootstrap_tools+=${_subdir}
2412 .endfor
2413 ${_bt}-usr.bin/awk: ${_bt_lex_depend} ${_bt}-usr.bin/yacc
2414 ${_bt}-bin/expr: ${_bt_lex_depend} ${_bt}-usr.bin/yacc
2415 # If we are bootstrapping file2c, we have to build it before config:
2416 ${_bt}-usr.sbin/config: ${_bt}-usr.bin/file2c ${_bt_lex_depend}
2417 # Note: no symlink to make/bmake in the !BOOTSTRAP_ALL_TOOLS case here since
2418 # the links to make/bmake make links will have already have been created in the
2419 # `make legacy` step. Not adding a link to make is important on non-FreeBSD
2420 # since "make" will usually point to GNU make there.
2421 _other_bootstrap_tools+=usr.bin/bmake
2422
2423 # Avoid dependency on host bz2 headers:
2424 _other_bootstrap_tools+=lib/libbz2
2425 ${_bt}-usr.bin/grep: ${_bt}-lib/libbz2
2426 .else
2427 # All tools in _basic_bootstrap_tools have the same name as the subdirectory
2428 # so we can use :T to get the name of the symlinks that we need to create.
2429 _bootstrap_tools_links+=${_basic_bootstrap_tools:T}
2430 .for _subdir _links in ${_basic_bootstrap_tools_multilink}
2431 _bootstrap_tools_links+=${_links:S/,/ /g}
2432 .endfor
2433 .endif  # defined(BOOTSTRAP_ALL_TOOLS)
2434
2435 # Link the tools that we need for building but don't need to bootstrap because
2436 # the host version is known to be compatible into ${WORLDTMP}/legacy
2437 # We do this before building any of the bootstrap tools in case they depend on
2438 # the presence of any of the links (e.g. as m4/lex/awk)
2439 ${_bt}-links: .PHONY
2440
2441 .for _tool in ${_bootstrap_tools_links}
2442 ${_bt}-link-${_tool}: .PHONY .MAKE
2443         @rm -f "${WORLDTMP}/legacy/bin/${_tool}"; \
2444         source_path=`which ${_tool}`; \
2445         if [ ! -e "$${source_path}" ] ; then \
2446                 echo "Cannot find host tool '${_tool}'"; false; \
2447         fi; \
2448         cp -pf "$${source_path}" "${WORLDTMP}/legacy/bin/${_tool}"
2449 ${_bt}-links: ${_bt}-link-${_tool}
2450 .endfor
2451
2452 bootstrap-tools: ${_bt}-links .PHONY
2453
2454 #       Please document (add comment) why something is in 'bootstrap-tools'.
2455 #       Try to bound the building of the bootstrap-tool to just the
2456 #       FreeBSD versions that need the tool built at this stage of the build.
2457 .for _tool in \
2458     ${_clang_tblgen} \
2459     ${_kerberos5_bootstrap_tools} \
2460     ${_strfile} \
2461     usr.bin/dtc \
2462     ${_cat} \
2463     ${_kbdcontrol} \
2464     ${_elftoolchain_libs} \
2465     usr.bin/lorder \
2466     lib/libopenbsd \
2467     usr.bin/mandoc \
2468     usr.bin/rpcgen \
2469     ${_yacc} \
2470     ${_m4} \
2471     ${_lex} \
2472     ${_other_bootstrap_tools} \
2473     usr.bin/xinstall \
2474     ${_gensnmptree} \
2475     ${_config} \
2476     ${_flua} \
2477     ${_crunchide} \
2478     ${_crunchgen} \
2479     ${_mkimg} \
2480     ${_nmtree} \
2481     ${_vtfontcvt} \
2482     ${_localedef} \
2483     ${LOCAL_BSTOOL_DIRS}
2484 ${_bt}-${_tool}: ${_bt}-links .PHONY .MAKE
2485         ${_+_}@${ECHODIR} "===> ${_tool} (obj,all,install)"; \
2486                 cd ${.CURDIR}/${_tool}; \
2487                 if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
2488                 if [ "${_tool}" = "usr.bin/lex" ]; then \
2489                         ${MAKE} DIRPRFX=${_tool}/ bootstrap; \
2490                 fi; \
2491                 ${MAKE} DIRPRFX=${_tool}/ all; \
2492                 ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${WORLDTMP}/legacy install
2493
2494 bootstrap-tools: ${_bt}-${_tool}
2495 .endfor
2496 .if target(${_bt}-lib/libmd)
2497 # If we are bootstrapping libmd (e.g. when building on macOS/Linux) add the
2498 # necessary dependencies:
2499 ${_bt}-usr.bin/sort: ${_bt}-lib/libmd
2500 ${_bt}-usr.bin/xinstall: ${_bt}-lib/libmd
2501 ${_bt}-sbin/md5: ${_bt}-lib/libmd
2502 .endif
2503
2504
2505 #
2506 # build-tools: Build special purpose build tools
2507 #
2508 .if !defined(NO_SHARE) && ${MK_SYSCONS} != "no"
2509 _share= share/syscons/scrnmaps
2510 .endif
2511
2512 .if ${MK_RESCUE} != "no"
2513 # rescue includes programs that have build-tools targets
2514 _rescue=rescue/rescue
2515 .endif
2516
2517 .if ${MK_TCSH} != "no"
2518 _tcsh=bin/csh
2519 .endif
2520 .if ${MK_FILE} != "no"
2521 _libmagic=lib/libmagic
2522 .endif
2523
2524 .if ${MK_PMC} != "no"
2525 _jevents=lib/libpmc/pmu-events
2526 .endif
2527
2528 # kernel-toolchain skips _cleanobj, so handle cleaning up previous
2529 # build-tools directories if needed.
2530 .if ${MK_CLEAN} == "yes" && make(kernel-toolchain)
2531 _bt_clean=      ${CLEANDIR}
2532 .endif
2533
2534 .for _tool in \
2535     ${_tcsh} \
2536     bin/sh \
2537     ${LOCAL_TOOL_DIRS} \
2538     ${_jevents} \
2539     lib/ncurses/ncurses \
2540     ${_rescue} \
2541     ${_share} \
2542     usr.bin/awk \
2543     ${_libmagic} \
2544     usr.bin/mkesdb_static \
2545     usr.bin/mkcsmapper_static \
2546     usr.bin/vi/catalog
2547 build-tools_${_tool}: .PHONY
2548         ${_+_}@${ECHODIR} "===> ${_tool} (${_bt_clean:D${_bt_clean},}obj,build-tools)"; \
2549                 cd ${.CURDIR}/${_tool}; \
2550                 if [ -n "${_bt_clean}" ]; then ${MAKE} DIRPRFX=${_tool}/ ${_bt_clean}; fi; \
2551                 if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
2552                 ${MAKE} DIRPRFX=${_tool}/ build-tools
2553 build-tools: build-tools_${_tool}
2554 .endfor
2555
2556 #
2557 # kernel-tools: Build kernel-building tools
2558 #
2559 kernel-tools: .PHONY
2560         mkdir -p ${WORLDTMP}/usr
2561         ${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
2562             -p ${WORLDTMP}/usr >/dev/null
2563
2564 #
2565 # cross-tools: All the tools needed to build the rest of the system after
2566 # we get done with the earlier stages. It is the last set of tools needed
2567 # to begin building the target binaries.
2568 #
2569 .if ${TARGET_ARCH} != ${MACHINE_ARCH} || ${BUILD_WITH_STRICT_TMPPATH} != 0
2570 .if ${TARGET_ARCH} == "amd64" || ${TARGET_ARCH} == "i386"
2571 _btxld=         usr.sbin/btxld
2572 .endif
2573 .endif
2574
2575 # Rebuild ctfconvert and ctfmerge to avoid difficult-to-diagnose failures
2576 # resulting from missing bug fixes or ELF Toolchain updates.
2577 .if ${MK_CDDL} != "no"
2578 _dtrace_tools= cddl/lib/libctf cddl/lib/libspl cddl/usr.bin/ctfconvert \
2579     cddl/usr.bin/ctfmerge
2580 .endif
2581
2582 # If we're given an XAS, don't build binutils.
2583 .if ${XAS:M/*} == ""
2584 .if ${MK_ELFTOOLCHAIN_BOOTSTRAP} != "no"
2585 _elftctools=    lib/libelftc \
2586                 lib/libpe \
2587                 usr.bin/elfctl \
2588                 usr.bin/objcopy \
2589                 usr.bin/nm \
2590                 usr.bin/size \
2591                 usr.bin/strings
2592 # These are not required by the build, but can be useful for developers who
2593 # cross-build on a FreeBSD 10 host:
2594 _elftctools+=   usr.bin/addr2line
2595 .endif
2596 .elif ${TARGET_ARCH} != ${MACHINE_ARCH} && ${MK_ELFTOOLCHAIN_BOOTSTRAP} != "no"
2597 # If cross-building with an external binutils we still need to build strip for
2598 # the target (for at least crunchide).
2599 _elftctools=    lib/libelftc \
2600                 lib/libpe \
2601                 usr.bin/elfctl \
2602                 usr.bin/objcopy
2603 .endif
2604
2605 .if ${MK_CLANG_BOOTSTRAP} != "no"
2606 _clang=         usr.bin/clang
2607 .endif
2608 .if ${MK_LLD_BOOTSTRAP} != "no"
2609 _lld=           usr.bin/clang/lld
2610 .endif
2611 .if ${MK_CLANG_BOOTSTRAP} != "no" || ${MK_LLD_BOOTSTRAP} != "no"
2612 _clang_libs=    lib/clang
2613 .endif
2614 .if ${MK_USB} != "no"
2615 _usb_tools=     stand/usb/tools
2616 .endif
2617
2618 .if ${BUILD_WITH_STRICT_TMPPATH} != 0 || defined(BOOTSTRAP_ALL_TOOLS)
2619 _ar=usr.bin/ar
2620 .endif
2621
2622 cross-tools: .MAKE .PHONY
2623 .for _tool in \
2624     ${LOCAL_XTOOL_DIRS} \
2625     ${_ar} \
2626     ${_clang_libs} \
2627     ${_clang} \
2628     ${_lld} \
2629     ${_elftctools} \
2630     ${_dtrace_tools} \
2631     ${_btxld} \
2632     ${_usb_tools}
2633         ${_+_}@${ECHODIR} "===> ${_tool} (obj,all,install)"; \
2634                 cd ${.CURDIR}/${_tool}; \
2635                 if [ -z "${NO_OBJWALK}" ]; then ${MAKE} DIRPRFX=${_tool}/ obj; fi; \
2636                 ${MAKE} DIRPRFX=${_tool}/ all; \
2637                 ${MAKE} DIRPRFX=${_tool}/ DESTDIR=${WORLDTMP} install
2638 .endfor
2639
2640 #
2641 # native-xtools is the current target for qemu-user cross builds of ports
2642 # via poudriere and the imgact_binmisc kernel module.
2643 # This target merely builds a toolchan/sysroot, then builds the tools it wants
2644 # with the options it wants in a special MAKEOBJDIRPREFIX, using the toolchain
2645 # already built.  It then installs the static tools to NXBDESTDIR for Poudriere
2646 # to pickup.
2647 #
2648 NXBOBJROOT=     ${OBJROOT}${MACHINE}.${MACHINE_ARCH}/nxb/
2649 NXBOBJTOP=      ${NXBOBJROOT}${NXB_TARGET}.${NXB_TARGET_ARCH}
2650 NXTP?=          /nxb-bin
2651 .if ${NXTP:N/*}
2652 .error NXTP variable should be an absolute path
2653 .endif
2654 NXBDESTDIR?=    ${DESTDIR}${NXTP}
2655
2656 # This is the list of tools to be built/installed as static and where
2657 # appropriate to build for the given TARGET.TARGET_ARCH.
2658 NXBDIRS+= \
2659     bin/cat \
2660     bin/chmod \
2661     bin/cp \
2662     ${_tcsh} \
2663     bin/echo \
2664     bin/expr \
2665     bin/hostname \
2666     bin/ln \
2667     bin/ls \
2668     bin/mkdir \
2669     bin/mv \
2670     bin/ps \
2671     bin/realpath \
2672     bin/rm \
2673     bin/rmdir \
2674     bin/sh \
2675     bin/sleep \
2676     sbin/md5 \
2677     sbin/sysctl \
2678     usr.bin/addr2line \
2679     usr.bin/ar \
2680     usr.bin/awk \
2681     usr.bin/basename \
2682     usr.bin/bmake \
2683     usr.bin/bzip2 \
2684     usr.bin/cmp \
2685     usr.bin/diff \
2686     usr.bin/dirname \
2687     usr.bin/objcopy \
2688     usr.bin/env \
2689     usr.bin/fetch \
2690     usr.bin/find \
2691     usr.bin/grep \
2692     usr.bin/gzip \
2693     usr.bin/head \
2694     usr.bin/id \
2695     usr.bin/lex \
2696     usr.bin/limits \
2697     usr.bin/lorder \
2698     usr.bin/mandoc \
2699     usr.bin/mktemp \
2700     usr.bin/mt \
2701     usr.bin/nm \
2702     usr.bin/patch \
2703     usr.bin/readelf \
2704     usr.bin/sed \
2705     usr.bin/size \
2706     usr.bin/sort \
2707     usr.bin/strings \
2708     usr.bin/tar \
2709     usr.bin/touch \
2710     usr.bin/tr \
2711     usr.bin/true \
2712     usr.bin/uniq \
2713     usr.bin/unzip \
2714     usr.bin/wc \
2715     usr.bin/xargs \
2716     usr.bin/xinstall \
2717     usr.bin/xz \
2718     usr.bin/yacc \
2719     usr.sbin/chown
2720
2721 SUBDIR_DEPEND_usr.bin/clang=    lib/clang
2722 .if ${MK_CLANG} != "no"
2723 NXBDIRS+=       lib/clang
2724 NXBDIRS+=       usr.bin/clang
2725 .endif
2726 # XXX: native-xtools passes along ${NXBDIRS} in SUBDIR_OVERRIDE that needs
2727 # to be evaluated after NXBDIRS is set.
2728 .if make(install) && !empty(SUBDIR_OVERRIDE)
2729 SUBDIR= ${SUBDIR_OVERRIDE}
2730 .endif
2731
2732 NXBMAKEARGS+= \
2733         OBJTOP=${NXBOBJTOP:Q} \
2734         OBJROOT=${NXBOBJROOT:Q} \
2735         MAKEOBJDIRPREFIX= \
2736         -DNO_SHARED \
2737         -DNO_CPU_CFLAGS \
2738         -DNO_PIC \
2739         SSP_CFLAGS= \
2740         MK_CASPER=no \
2741         MK_CLANG_EXTRAS=no \
2742         MK_CLANG_FORMAT=no \
2743         MK_CLANG_FULL=no \
2744         MK_CTF=no \
2745         MK_DEBUG_FILES=no \
2746         MK_HTML=no \
2747         MK_LLDB=no \
2748         MK_MAN=no \
2749         MK_MAN_UTILS=yes \
2750         MK_OFED=no \
2751         MK_OPENSSH=no \
2752         MK_PROFILE=no \
2753         MK_RETPOLINE=no \
2754         MK_SENDMAIL=no \
2755         MK_TESTS=no \
2756         MK_WERROR=no \
2757         MK_ZFS=no
2758
2759 .if make(native-xtools*) && \
2760     (!defined(NXB_TARGET) || !defined(NXB_TARGET_ARCH))
2761 .error Missing NXB_TARGET / NXB_TARGET_ARCH
2762 .endif
2763 # For 'toolchain' we want to produce native binaries that themselves generate
2764 # native binaries.
2765 NXBTMAKE=       ${NXBMAKEENV} ${MAKE} ${NXBMAKEARGS:N-DNO_PIC:N-DNO_SHARED} \
2766                 TARGET=${MACHINE} TARGET_ARCH=${MACHINE_ARCH}
2767 # For 'everything' we want to produce native binaries (hence -target to
2768 # be MACHINE) that themselves generate TARGET.TARGET_ARCH binaries.
2769 # TARGET/TARGET_ARCH are still passed along from user.
2770 #
2771 # Use the toolchain we create as an external toolchain.
2772 .if ${USING_SYSTEM_COMPILER} == "yes" || ${XCC:N${CCACHE_BIN}:M/*}
2773 NXBMAKE+=       XCC="${XCC}" \
2774                 XCXX="${XCXX}" \
2775                 XCPP="${XCPP}"
2776 .else
2777 NXBMAKE+=       XCC="${NXBOBJTOP}/tmp/usr/bin/cc" \
2778                 XCXX="${NXBOBJTOP}/tmp/usr/bin/c++" \
2779                 XCPP="${NXBOBJTOP}/tmp/usr/bin/cpp"
2780 .endif
2781 NXBMAKE+=       ${NXBMAKEENV} ${MAKE} -f Makefile.inc1 ${NXBMAKEARGS} \
2782                 MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH} \
2783                 TARGET=${NXB_TARGET} TARGET_ARCH=${NXB_TARGET_ARCH} \
2784                 TARGET_TRIPLE=${MACHINE_TRIPLE:Q}
2785 # NXBDIRS is improperly based on MACHINE rather than NXB_TARGET.  Need to
2786 # invoke a sub-make to reevaluate MK_CLANG, etc, for NXBDIRS.
2787 NXBMAKE+=       SUBDIR_OVERRIDE='$${NXBDIRS:M*}'
2788 # Need to avoid the -isystem logic when using clang as an external toolchain
2789 # even if the TARGET being built for wants GCC.
2790 NXBMAKE+=       WANT_COMPILER_TYPE='$${X_COMPILER_TYPE}'
2791 native-xtools: .PHONY
2792         ${_+_}cd ${.CURDIR}; ${NXBTMAKE} _cleanobj
2793         # Build the bootstrap/host/cross tools that produce native binaries
2794         ${_+_}cd ${.CURDIR}; ${NXBTMAKE} kernel-toolchain
2795         # Populate includes/libraries sysroot that produce native binaries.
2796         # This is split out from 'toolchain' above mostly so that target LLVM
2797         # libraries have a proper LLVM_DEFAULT_TARGET_TRIPLE without
2798         # polluting the cross-compiler build.  The LLVM/GCC libs are skipped
2799         # here to avoid the problem but are kept in 'toolchain' so that
2800         # needed build tools are built.
2801         ${_+_}cd ${.CURDIR}; ${NXBTMAKE} _includes MK_CLANG=no
2802         ${_+_}cd ${.CURDIR}; ${NXBTMAKE} _libraries MK_CLANG=no
2803 .if !defined(NO_OBJWALK)
2804         ${_+_}cd ${.CURDIR}; ${NXBMAKE} _obj
2805 .endif
2806         ${_+_}cd ${.CURDIR}; ${NXBMAKE} everything
2807         @echo ">> native-xtools done.  Use 'make native-xtools-install' to install to a given DESTDIR"
2808
2809 native-xtools-install: .PHONY
2810         mkdir -p ${NXBDESTDIR}/bin ${NXBDESTDIR}/sbin ${NXBDESTDIR}/usr
2811         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
2812             -p ${NXBDESTDIR}/usr >/dev/null
2813         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.include.dist \
2814             -p ${NXBDESTDIR}/usr/include >/dev/null
2815         ${_+_}cd ${.CURDIR}; ${NXBMAKE} \
2816             DESTDIR=${NXBDESTDIR} \
2817             -DNO_ROOT \
2818             install
2819
2820 #
2821 # hierarchy - ensure that all the needed directories are present
2822 #
2823 hierarchy hier: .MAKE .PHONY
2824         ${_+_}cd ${.CURDIR}/etc; ${HMAKE} distrib-dirs
2825
2826 #
2827 # libraries - build all libraries, and install them under ${DESTDIR}.
2828 #
2829 # The list of libraries with dependents (${_prebuild_libs}) and their
2830 # interdependencies (__L) are built automatically by the
2831 # ${.CURDIR}/tools/make_libdeps.sh script.
2832 #
2833 libraries: .MAKE .PHONY
2834         ${_+_}cd ${.CURDIR}; \
2835             ${MAKE} -f Makefile.inc1 _prereq_libs; \
2836             ${MAKE} -f Makefile.inc1 _startup_libs; \
2837             ${MAKE} -f Makefile.inc1 _prebuild_libs; \
2838             ${MAKE} -f Makefile.inc1 _generic_libs
2839
2840 #
2841 # static libgcc.a prerequisite for shared libc
2842 #
2843 _prereq_libs= lib/libcompiler_rt
2844 .if ${MK_SSP} != "no"
2845 _prereq_libs+= lib/libssp_nonshared
2846 .endif
2847
2848 # These dependencies are not automatically generated:
2849 #
2850 # lib/csu and lib/libc must be built before
2851 # all shared libraries for ELF.
2852 #
2853 _startup_libs=  lib/csu
2854 _startup_libs+= lib/libc
2855 _startup_libs+= lib/libc_nonshared
2856 .if ${MK_CXX} != "no"
2857 _startup_libs+= lib/libcxxrt
2858 .endif
2859
2860 _prereq_libs+=  lib/libgcc_eh lib/libgcc_s
2861 _startup_libs+= lib/libgcc_eh lib/libgcc_s
2862
2863 lib/libgcc_s__L: lib/libc__L
2864 lib/libgcc_s__L: lib/libc_nonshared__L
2865 .if ${MK_CXX} != "no"
2866 lib/libcxxrt__L: lib/libgcc_s__L
2867 .endif
2868
2869 _prebuild_libs= ${_kerberos5_lib_libasn1} \
2870                 ${_kerberos5_lib_libhdb} \
2871                 ${_kerberos5_lib_libheimbase} \
2872                 ${_kerberos5_lib_libheimntlm} \
2873                 ${_libsqlite3} \
2874                 ${_kerberos5_lib_libheimipcc} \
2875                 ${_kerberos5_lib_libhx509} ${_kerberos5_lib_libkrb5} \
2876                 ${_kerberos5_lib_libroken} \
2877                 ${_kerberos5_lib_libwind} \
2878                 lib/libbz2 ${_libcom_err} lib/libcrypt \
2879                 lib/libelf lib/libexpat \
2880                 lib/libfigpar \
2881                 ${_lib_libgssapi} \
2882                 lib/libjail \
2883                 lib/libkiconv lib/libkvm lib/liblzma lib/libmd lib/libnv \
2884                 lib/libzstd \
2885                 ${_lib_casper} \
2886                 lib/ncurses/ncurses \
2887                 lib/libopie lib/libpam/libpam lib/libthr \
2888                 ${_lib_libradius} lib/libsbuf lib/libtacplus \
2889                 lib/libgeom \
2890                 ${_cddl_lib_libumem} ${_cddl_lib_libnvpair} \
2891                 ${_cddl_lib_libuutil} \
2892                 ${_cddl_lib_libavl} \
2893                 ${_cddl_lib_libicp} \
2894                 ${_cddl_lib_libicp_rescue} \
2895                 ${_cddl_lib_libspl} \
2896                 ${_cddl_lib_libtpool} \
2897                 ${_cddl_lib_libzfs_core} ${_cddl_lib_libzfs} \
2898                 ${_cddl_lib_libzutil} \
2899                 ${_cddl_lib_libctf} ${_cddl_lib_libzfsbootenv} \
2900                 lib/libufs \
2901                 lib/libutil lib/libpjdlog ${_lib_libypclnt} lib/libz lib/msun \
2902                 ${_secure_lib_libcrypto} ${_secure_lib_libssl} \
2903                 ${_lib_libldns} ${_secure_lib_libssh}
2904
2905 .if ${MK_DIALOG} != "no"
2906 _prebuild_libs+= gnu/lib/libdialog
2907 gnu/lib/libdialog__L: lib/msun__L lib/ncurses/ncurses__L
2908 .endif
2909
2910 .if ${MK_GOOGLETEST} != "no"
2911 _prebuild_libs+= lib/libregex
2912 .endif
2913
2914 .if ${MK_CXX} != "no"
2915 _prebuild_libs+= lib/libc++
2916 .endif
2917
2918 lib/libgeom__L: lib/libexpat__L lib/libsbuf__L
2919 lib/libkvm__L: lib/libelf__L
2920
2921 .if ${MK_RADIUS_SUPPORT} != "no"
2922 _lib_libradius= lib/libradius
2923 .endif
2924
2925 .if ${MK_OFED} != "no"
2926 _prebuild_libs+= \
2927         lib/ofed/libibverbs \
2928         lib/ofed/libibmad \
2929         lib/ofed/libibumad \
2930         lib/ofed/complib \
2931         lib/ofed/libmlx5
2932
2933 lib/ofed/libibmad__L:   lib/ofed/libibumad__L
2934 lib/ofed/complib__L:    lib/libthr__L
2935 lib/ofed/libmlx5__L:    lib/ofed/libibverbs__L lib/libthr__L
2936 .endif
2937
2938 .if ${MK_CASPER} != "no"
2939 _lib_casper=    lib/libcasper
2940 .endif
2941
2942 lib/libpjdlog__L: lib/libutil__L
2943 lib/libcasper__L: lib/libnv__L
2944 lib/liblzma__L: lib/libmd__L lib/libthr__L
2945 lib/libzstd__L: lib/libthr__L
2946
2947 _generic_libs=  ${_cddl_lib} gnu/lib ${_kerberos5_lib} lib ${_secure_lib}
2948 .if ${MK_IPFILTER} != "no"
2949 _generic_libs+= sbin/ipf/libipf
2950 .endif
2951 .for _DIR in ${LOCAL_LIB_DIRS}
2952 .if ${_DIR} == ".WAIT"  || (empty(_generic_libs:M${_DIR}) && exists(${.CURDIR}/${_DIR}/Makefile))
2953 _generic_libs+= ${_DIR}
2954 .endif
2955 .endfor
2956
2957 lib/libopie__L lib/libtacplus__L: lib/libmd__L
2958
2959 .if ${MK_CDDL} != "no"
2960 _cddl_lib_libumem= cddl/lib/libumem
2961 _cddl_lib_libnvpair= cddl/lib/libnvpair
2962 _cddl_lib_libavl= cddl/lib/libavl
2963 _cddl_lib_libuutil= cddl/lib/libuutil
2964 _cddl_lib_libspl= cddl/lib/libspl
2965
2966 cddl/lib/libuutil__L: cddl/lib/libavl__L cddl/lib/libspl__L
2967
2968 .if ${MK_ZFS} != "no"
2969 _cddl_lib_libicp= cddl/lib/libicp
2970 _cddl_lib_libicp_rescue= cddl/lib/libicp_rescue
2971 _cddl_lib_libtpool= cddl/lib/libtpool
2972 _cddl_lib_libzutil= cddl/lib/libzutil
2973 _cddl_lib_libzfs_core= cddl/lib/libzfs_core
2974 _cddl_lib_libzfs= cddl/lib/libzfs
2975 _cddl_lib_libzfsbootenv= cddl/lib/libzfsbootenv
2976
2977 cddl/lib/libtpool__L: cddl/lib/libspl__L
2978
2979 cddl/lib/libzutil__L: cddl/lib/libavl__L cddl/lib/libtpool__L
2980
2981 cddl/lib/libzfs_core__L: cddl/lib/libnvpair__L
2982
2983 cddl/lib/libzfs__L: cddl/lib/libzfs_core__L lib/msun__L lib/libutil__L
2984 cddl/lib/libzfs__L: lib/libthr__L lib/libmd__L lib/libz__L cddl/lib/libumem__L
2985 cddl/lib/libzfs__L: cddl/lib/libuutil__L cddl/lib/libavl__L lib/libgeom__L
2986 cddl/lib/libzfs__L: cddl/lib/libnvpair__L cddl/lib/libzutil__L
2987 cddl/lib/libzfs__L: secure/lib/libcrypto__L
2988
2989 cddl/lib/libzfsbootenv__L: cddl/lib/libzfs__L
2990 lib/libbe__L: cddl/lib/libzfs__L cddl/lib/libzfsbootenv__L
2991 .endif
2992 _cddl_lib_libctf= cddl/lib/libctf
2993 _cddl_lib= cddl/lib
2994 cddl/lib/libctf__L: lib/libz__L cddl/lib/libspl__L
2995 .endif
2996 # cddl/lib/libdtrace requires lib/libproc and lib/librtld_db
2997 _prebuild_libs+=        lib/libprocstat lib/libproc lib/librtld_db
2998 lib/libprocstat__L: lib/libelf__L lib/libkvm__L lib/libutil__L
2999 lib/libproc__L: lib/libprocstat__L
3000 lib/librtld_db__L: lib/libprocstat__L
3001
3002 .if ${MK_CRYPT} != "no"
3003 .if ${MK_OPENSSL} != "no"
3004 _secure_lib_libcrypto= secure/lib/libcrypto
3005 _secure_lib_libssl= secure/lib/libssl
3006 lib/libradius__L secure/lib/libssl__L: secure/lib/libcrypto__L
3007 secure/lib/libcrypto__L: lib/libthr__L
3008 .if ${MK_LDNS} != "no"
3009 _lib_libldns= lib/libldns
3010 lib/libldns__L: secure/lib/libssl__L
3011 .endif
3012 .if ${MK_OPENSSH} != "no"
3013 _secure_lib_libssh= secure/lib/libssh
3014 secure/lib/libssh__L: lib/libz__L secure/lib/libcrypto__L lib/libcrypt__L
3015 .if ${MK_LDNS} != "no"
3016 secure/lib/libssh__L: lib/libldns__L
3017 .endif
3018 .if ${MK_GSSAPI} != "no" && ${MK_KERBEROS_SUPPORT} != "no"
3019 secure/lib/libssh__L: lib/libgssapi__L kerberos5/lib/libkrb5__L \
3020     kerberos5/lib/libhx509__L kerberos5/lib/libasn1__L lib/libcom_err__L \
3021     lib/libmd__L kerberos5/lib/libroken__L
3022 .endif
3023 .endif
3024 .endif
3025 _secure_lib=    secure/lib
3026 .endif
3027
3028 .if ${MK_KERBEROS} != "no"
3029 kerberos5/lib/libasn1__L: lib/libcom_err__L kerberos5/lib/libroken__L
3030 kerberos5/lib/libhdb__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
3031     kerberos5/lib/libkrb5__L kerberos5/lib/libroken__L \
3032     kerberos5/lib/libwind__L lib/libsqlite3__L
3033 kerberos5/lib/libheimntlm__L: secure/lib/libcrypto__L kerberos5/lib/libkrb5__L \
3034     kerberos5/lib/libroken__L lib/libcom_err__L
3035 kerberos5/lib/libhx509__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
3036     secure/lib/libcrypto__L kerberos5/lib/libroken__L kerberos5/lib/libwind__L
3037 kerberos5/lib/libkrb5__L: kerberos5/lib/libasn1__L lib/libcom_err__L \
3038     lib/libcrypt__L secure/lib/libcrypto__L kerberos5/lib/libhx509__L \
3039     kerberos5/lib/libroken__L kerberos5/lib/libwind__L \
3040     kerberos5/lib/libheimbase__L kerberos5/lib/libheimipcc__L
3041 kerberos5/lib/libroken__L: lib/libcrypt__L
3042 kerberos5/lib/libwind__L: kerberos5/lib/libroken__L lib/libcom_err__L
3043 kerberos5/lib/libheimbase__L: lib/libthr__L
3044 kerberos5/lib/libheimipcc__L: kerberos5/lib/libroken__L kerberos5/lib/libheimbase__L lib/libthr__L
3045 .endif
3046
3047 lib/libsqlite3__L: lib/libthr__L
3048
3049 .if ${MK_GSSAPI} != "no"
3050 _lib_libgssapi= lib/libgssapi
3051 .endif
3052
3053 .if ${MK_KERBEROS} != "no"
3054 _kerberos5_lib= kerberos5/lib
3055 _kerberos5_lib_libasn1= kerberos5/lib/libasn1
3056 _kerberos5_lib_libhdb= kerberos5/lib/libhdb
3057 _kerberos5_lib_libheimbase= kerberos5/lib/libheimbase
3058 _kerberos5_lib_libkrb5= kerberos5/lib/libkrb5
3059 _kerberos5_lib_libhx509= kerberos5/lib/libhx509
3060 _kerberos5_lib_libroken= kerberos5/lib/libroken
3061 _kerberos5_lib_libheimntlm= kerberos5/lib/libheimntlm
3062 _libsqlite3= lib/libsqlite3
3063 _kerberos5_lib_libheimipcc= kerberos5/lib/libheimipcc
3064 _kerberos5_lib_libwind= kerberos5/lib/libwind
3065 _libcom_err= lib/libcom_err
3066 .endif
3067
3068 .if ${MK_NIS} != "no"
3069 _lib_libypclnt= lib/libypclnt
3070 .endif
3071
3072 .if ${MK_OPENSSL} == "no"
3073 lib/libradius__L: lib/libmd__L
3074 .endif
3075
3076 lib/libproc__L: \
3077     ${_cddl_lib_libctf:D${_cddl_lib_libctf}__L} lib/libelf__L lib/librtld_db__L lib/libutil__L
3078 .if ${MK_CXX} != "no"
3079 lib/libproc__L: lib/libcxxrt__L
3080 .endif
3081
3082 .for _lib in ${_prereq_libs}
3083 ${_lib}__PL: .PHONY .MAKE
3084 .if !defined(_MKSHOWCONFIG) && exists(${.CURDIR}/${_lib})
3085         ${_+_}@${ECHODIR} "===> ${_lib} (obj,all,install)"; \
3086                 cd ${.CURDIR}/${_lib}; \
3087                 if [ -z "${NO_OBJWALK}" ]; then ${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ obj; fi; \
3088                 ${MAKE} MK_TESTS=no MK_PROFILE=no -DNO_PIC \
3089                     DIRPRFX=${_lib}/ all; \
3090                 ${MAKE} MK_TESTS=no MK_PROFILE=no -DNO_PIC \
3091                     DIRPRFX=${_lib}/ install
3092 .endif
3093 .endfor
3094
3095 .for _lib in ${_startup_libs} ${_prebuild_libs} ${_generic_libs}
3096 ${_lib}__L: .PHONY .MAKE
3097 .if !defined(_MKSHOWCONFIG) && exists(${.CURDIR}/${_lib})
3098         ${_+_}@${ECHODIR} "===> ${_lib} (obj,all,install)"; \
3099                 cd ${.CURDIR}/${_lib}; \
3100                 if [ -z "${NO_OBJWALK}" ]; then ${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ obj; fi; \
3101                 ${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ all; \
3102                 ${MAKE} MK_TESTS=no DIRPRFX=${_lib}/ install
3103 .endif
3104 .endfor
3105
3106 _prereq_libs: ${_prereq_libs:S/$/__PL/}
3107 _startup_libs: ${_startup_libs:S/$/__L/}
3108 _prebuild_libs: ${_prebuild_libs:S/$/__L/}
3109 _generic_libs: ${_generic_libs:S/$/__L/}
3110
3111 # Enable SUBDIR_PARALLEL when not calling 'make all', unless called from
3112 # 'everything' with _PARALLEL_SUBDIR_OK set.  This is because it is unlikely
3113 # that running 'make all' from the top-level, especially with a SUBDIR_OVERRIDE
3114 # or LOCAL_DIRS set, will have a reliable build if SUBDIRs are built in
3115 # parallel.  This is safe for the world stage of buildworld though since it has
3116 # already built libraries in a proper order and installed includes into
3117 # WORLDTMP. Special handling is done for SUBDIR ordering for 'install*' to
3118 # avoid trashing a system if it crashes mid-install.
3119 .if !make(all) || defined(_PARALLEL_SUBDIR_OK)
3120 SUBDIR_PARALLEL=
3121 .endif
3122
3123 .include <bsd.subdir.mk>
3124
3125 .if make(check-old) || make(check-old-dirs) || \
3126     make(check-old-files) || make(check-old-libs) || \
3127     make(delete-old) || make(delete-old-dirs) || \
3128     make(delete-old-files) || make(delete-old-libs)
3129
3130 #
3131 # check for / delete old files section
3132 #
3133
3134 .include "ObsoleteFiles.inc"
3135
3136 OLD_LIBS_MESSAGE="Please be sure no application still uses those libraries, \
3137 else you can not start such an application. Consult UPDATING for more \
3138 information regarding how to cope with the removal/revision bump of a \
3139 specific library."
3140
3141 .if !defined(BATCH_DELETE_OLD_FILES)
3142 RM_I=-i
3143 .else
3144 RM_I=-v
3145 .endif
3146
3147 delete-old-files: .PHONY
3148         @echo ">>> Removing old files (only deletes safe to delete libs)"
3149 # Ask for every old file if the user really wants to remove it.
3150 # It's annoying, but better safe than sorry.
3151 # NB: We cannot pass the list of OLD_FILES as a parameter because the
3152 # argument list will get too long. Using .for/.endfor make "loops" will make
3153 # the Makefile parser segfault.
3154         @exec 3<&0; \
3155         cd ${.CURDIR}; \
3156         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3157             -V OLD_FILES -V "OLD_FILES:Musr/share/*.gz:R" | xargs -n1 | sort | \
3158         while read file; do \
3159                 if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
3160                         chflags noschg "${DESTDIR}/$${file}" 2>/dev/null || true; \
3161                         rm ${RM_I} "${DESTDIR}/$${file}" <&3; \
3162                 fi; \
3163                 for ext in debug symbols; do \
3164                   if ! [ -e "${DESTDIR}/$${file}" ] && [ -f \
3165                       "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
3166                           rm ${RM_I} "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" \
3167                               <&3; \
3168                   fi; \
3169                 done; \
3170         done
3171 # Remove catpages without corresponding manpages.
3172         @exec 3<&0; \
3173         find ${DESTDIR}/usr/share/man/cat* ! -type d 2>/dev/null | sort | \
3174         sed -ep -e's:${DESTDIR}/usr/share/man/cat:${DESTDIR}/usr/share/man/man:' | \
3175         while read catpage; do \
3176                 read manpage; \
3177                 if [ ! -e "$${manpage}" ]; then \
3178                         rm ${RM_I} $${catpage} <&3; \
3179                 fi; \
3180         done
3181         @echo ">>> Old files removed"
3182
3183 check-old-files: .PHONY
3184         @echo ">>> Checking for old files"
3185         @cd ${.CURDIR}; \
3186         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3187             -V OLD_FILES -V "OLD_FILES:Musr/share/*.gz:R" | xargs -n1 | \
3188         while read file; do \
3189                 if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
3190                         echo "${DESTDIR}/$${file}"; \
3191                 fi; \
3192                 for ext in debug symbols; do \
3193                   if [ -f "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
3194                           echo "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}"; \
3195                   fi; \
3196                 done; \
3197         done | sort
3198 # Check for catpages without corresponding manpages.
3199         @find ${DESTDIR}/usr/share/man/cat* ! -type d 2>/dev/null | \
3200         sed -ep -e's:${DESTDIR}/usr/share/man/cat:${DESTDIR}/usr/share/man/man:' | \
3201         while read catpage; do \
3202                 read manpage; \
3203                 if [ ! -e "$${manpage}" ]; then \
3204                         echo $${catpage}; \
3205                 fi; \
3206         done | sort
3207
3208 delete-old-libs: .PHONY
3209         @echo ">>> Removing old libraries"
3210         @echo "${OLD_LIBS_MESSAGE}" | fmt
3211         @exec 3<&0; \
3212         cd ${.CURDIR}; \
3213         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3214             -V OLD_LIBS | xargs -n1 | sort | \
3215         while read file; do \
3216                 if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
3217                         chflags noschg "${DESTDIR}/$${file}" 2>/dev/null || true; \
3218                         rm ${RM_I} "${DESTDIR}/$${file}" <&3; \
3219                 fi; \
3220                 for ext in debug symbols; do \
3221                   if ! [ -e "${DESTDIR}/$${file}" ] && [ -f \
3222                       "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
3223                           rm ${RM_I} "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" \
3224                               <&3; \
3225                   fi; \
3226                 done; \
3227         done
3228         @echo ">>> Old libraries removed"
3229
3230 check-old-libs: .PHONY
3231         @echo ">>> Checking for old libraries"
3232         @cd ${.CURDIR}; \
3233         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3234             -V OLD_LIBS | xargs -n1 | \
3235         while read file; do \
3236                 if [ -f "${DESTDIR}/$${file}" -o -L "${DESTDIR}/$${file}" ]; then \
3237                         echo "${DESTDIR}/$${file}"; \
3238                 fi; \
3239                 for ext in debug symbols; do \
3240                   if [ -f "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}" ]; then \
3241                           echo "${DESTDIR}${DEBUGDIR}/$${file}.$${ext}"; \
3242                   fi; \
3243                 done; \
3244         done | sort
3245
3246 delete-old-dirs: .PHONY
3247         @echo ">>> Removing old directories"
3248         @cd ${.CURDIR}; \
3249         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3250             -V OLD_DIRS | xargs -n1 | sort -r | \
3251         while read dir; do \
3252                 if [ -d "${DESTDIR}/$${dir}" ]; then \
3253                         rmdir -v "${DESTDIR}/$${dir}" || true; \
3254                 elif [ -L "${DESTDIR}/$${dir}" ]; then \
3255                         echo "${DESTDIR}/$${dir} is a link, please remove everything manually."; \
3256                 fi; \
3257                 if [ -d "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
3258                         rmdir -v "${DESTDIR}${DEBUGDIR}/$${dir}" || true; \
3259                 elif [ -L "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
3260                         echo "${DESTDIR}${DEBUGDIR}/$${dir} is a link, please remove everything manually."; \
3261                 fi; \
3262         done
3263         @echo ">>> Old directories removed"
3264
3265 check-old-dirs: .PHONY
3266         @echo ">>> Checking for old directories"
3267         @cd ${.CURDIR}; \
3268         ${MAKE} -f ${.CURDIR}/Makefile.inc1 ${.MAKEFLAGS} ${.TARGET} \
3269             -V OLD_DIRS | xargs -n1 | sort -r | \
3270         while read dir; do \
3271                 if [ -d "${DESTDIR}/$${dir}" ]; then \
3272                         echo "${DESTDIR}/$${dir}"; \
3273                 elif [ -L "${DESTDIR}/$${dir}" ]; then \
3274                         echo "${DESTDIR}/$${dir} is a link, please remove everything manually."; \
3275                 fi; \
3276                 if [ -d "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
3277                         echo "${DESTDIR}${DEBUGDIR}/$${dir}"; \
3278                 elif [ -L "${DESTDIR}${DEBUGDIR}/$${dir}" ]; then \
3279                         echo "${DESTDIR}${DEBUGDIR}/$${dir} is a link, please remove everything manually."; \
3280                 fi; \
3281         done
3282
3283 delete-old: delete-old-files delete-old-dirs .PHONY
3284         @echo "To remove old libraries run '${MAKE_CMD} delete-old-libs'."
3285
3286 check-old: check-old-files check-old-libs check-old-dirs .PHONY
3287         @echo "To remove old files and directories run '${MAKE_CMD} delete-old'."
3288         @echo "To remove old libraries run '${MAKE_CMD} delete-old-libs'."
3289
3290 .endif
3291
3292 #
3293 # showconfig - show build configuration.
3294 #
3295 showconfig: .PHONY
3296         @(${MAKE} -n -f ${.CURDIR}/sys/conf/kern.opts.mk -V dummy -dg1 UPDATE_DEPENDFILE=no NO_OBJ=yes; \
3297           ${MAKE} -n -f ${.CURDIR}/share/mk/src.opts.mk -V dummy -dg1 UPDATE_DEPENDFILE=no NO_OBJ=yes) 2>&1 | grep ^MK_ | sort -u
3298
3299 .if !empty(KRNLOBJDIR) && !empty(KERNCONF)
3300 DTBOUTPUTPATH= ${KRNLOBJDIR}/${KERNCONF}/
3301
3302 .if !defined(FDT_DTS_FILE) || empty(FDT_DTS_FILE)
3303 .if !defined(_MKSHOWCONFIG) && exists(${KERNCONFDIR}/${KERNCONF})
3304 FDT_DTS_FILE!= awk 'BEGIN {FS="="} /^makeoptions[[:space:]]+FDT_DTS_FILE/ {print $$2}' \
3305         '${KERNCONFDIR}/${KERNCONF}' ; echo
3306 .endif
3307 .endif
3308
3309 .endif
3310
3311 .if !defined(DTBOUTPUTPATH) || !exists(${DTBOUTPUTPATH})
3312 DTBOUTPUTPATH= ${.CURDIR}
3313 .endif
3314
3315 #
3316 # Build 'standalone' Device Tree Blob
3317 #
3318 builddtb: .PHONY
3319         @PATH=${TMPPATH} MACHINE=${TARGET} \
3320         sh ${.CURDIR}/sys/tools/fdt/make_dtb.sh ${.CURDIR}/sys \
3321             "${FDT_DTS_FILE}" ${DTBOUTPUTPATH}
3322
3323 ###############
3324
3325 # cleanworld
3326 # In the following, the first 'rm' in a series will usually remove all
3327 # files and directories.  If it does not, then there are probably some
3328 # files with file flags set, so this unsets them and tries the 'rm' a
3329 # second time.  There are situations where this target will be cleaning
3330 # some directories via more than one method, but that duplication is
3331 # needed to correctly handle all the possible situations.  Removing all
3332 # files without file flags set in the first 'rm' instance saves time,
3333 # because 'chflags' will need to operate on fewer files afterwards.
3334 #
3335 # It is expected that BW_CANONICALOBJDIR == the CANONICALOBJDIR as would be
3336 # created by bsd.obj.mk, except that we don't want to .include that file
3337 # in this makefile.  We don't do a cleandir walk if MK_AUTO_OBJ is yes
3338 # since it is not possible for files to land in the wrong place.
3339 #
3340 .if make(cleanworld)
3341 BW_CANONICALOBJDIR:=${OBJTOP}/
3342 .elif make(cleanuniverse)
3343 BW_CANONICALOBJDIR:=${OBJROOT}
3344 .if ${MK_UNIFIED_OBJDIR} == "no"
3345 .error ${.TARGETS} only supported with WITH_UNIFIED_OBJDIR enabled.
3346 .endif
3347 .endif
3348 cleanworld cleanuniverse: .PHONY
3349 .if !empty(BW_CANONICALOBJDIR) && exists(${BW_CANONICALOBJDIR}) && \
3350     ${.CURDIR:tA} != ${BW_CANONICALOBJDIR:tA}
3351         -rm -rf ${BW_CANONICALOBJDIR}*
3352         -chflags -R 0 ${BW_CANONICALOBJDIR}
3353         rm -rf ${BW_CANONICALOBJDIR}*
3354 .endif
3355 .if make(cleanworld) && ${MK_AUTO_OBJ} == "no" && \
3356     (empty(BW_CANONICALOBJDIR) || ${.CURDIR:tA} == ${BW_CANONICALOBJDIR:tA})
3357 .if ${.CURDIR} == ${.OBJDIR} || ${.CURDIR}/obj == ${.OBJDIR}
3358         #   To be safe in this case, fall back to a 'make cleandir'
3359         ${_+_}@cd ${.CURDIR}; ${MAKE} cleandir
3360 .endif
3361 .endif
3362
3363 .if ${TARGET} == ${MACHINE} && ${TARGET_ARCH} == ${MACHINE_ARCH}
3364 XDEV_CPUTYPE?=${CPUTYPE}
3365 .else
3366 XDEV_CPUTYPE?=${TARGET_CPUTYPE}
3367 .endif
3368
3369 NOFUN=-DNO_FSCHG MK_HTML=no -DNO_LINT \
3370         MK_MAN=no MK_NLS=no MK_PROFILE=no \
3371         MK_KERBEROS=no MK_RESCUE=no MK_TESTS=no MK_WERROR=no \
3372         TARGET=${TARGET} TARGET_ARCH=${TARGET_ARCH} \
3373         CPUTYPE=${XDEV_CPUTYPE}
3374
3375 XDDIR=${TARGET_ARCH}-freebsd
3376 XDTP?=/usr/${XDDIR}
3377 .if ${XDTP:N/*}
3378 .error XDTP variable should be an absolute path
3379 .endif
3380
3381 CDBOBJROOT=     ${OBJROOT}${MACHINE}.${MACHINE_ARCH}/xdev/
3382 CDBOBJTOP=      ${CDBOBJROOT}${XDDIR}
3383 CDBENV= \
3384         INSTALL="sh ${.CURDIR}/tools/install.sh"
3385 CDENV= ${CDBENV} \
3386         TOOLS_PREFIX=${XDTP}
3387 CDMAKEARGS= \
3388         OBJTOP=${CDBOBJTOP:Q} \
3389         OBJROOT=${CDBOBJROOT:Q}
3390 CD2MAKEARGS= ${CDMAKEARGS}
3391
3392 .if ${WANT_COMPILER_TYPE} == gcc || \
3393     (defined(X_COMPILER_TYPE) && ${X_COMPILER_TYPE} == gcc)
3394 # GCC requires -isystem and -L when using a cross-compiler.  --sysroot
3395 # won't set header path and -L is used to ensure the base library path
3396 # is added before the port PREFIX library path.
3397 CD2CFLAGS+=     -isystem ${XDDESTDIR}/usr/include -L${XDDESTDIR}/usr/lib
3398 # GCC requires -B to find /usr/lib/crti.o when using a cross-compiler
3399 # combined with --sysroot.
3400 CD2CFLAGS+=     -B${XDDESTDIR}/usr/lib
3401 # Force using libc++ for external GCC.
3402 .if defined(X_COMPILER_TYPE) && \
3403     ${X_COMPILER_TYPE} == gcc && ${X_COMPILER_VERSION} >= 40800
3404 CD2CXXFLAGS+=   -isystem ${XDDESTDIR}/usr/include/c++/v1 -std=c++11 \
3405                 -nostdinc++
3406 .endif
3407 .endif
3408 CD2CFLAGS+=     --sysroot=${XDDESTDIR}/
3409 CD2ENV=${CDENV} CC="${CC} ${CD2CFLAGS}" CXX="${CXX} ${CD2CXXFLAGS} ${CD2CFLAGS}" \
3410         CPP="${CPP} ${CD2CFLAGS}" \
3411         MACHINE=${TARGET} MACHINE_ARCH=${TARGET_ARCH}
3412
3413 CDTMP=  ${OBJTOP}/${XDDIR}/tmp
3414 CDMAKE=${CDENV} PATH=${CDTMP}/usr/bin:${PATH} ${MAKE} ${CDMAKEARGS} ${NOFUN}
3415 CD2MAKE=${CD2ENV} PATH=${CDTMP}/usr/bin:${XDDESTDIR}/usr/bin:${PATH} \
3416         ${MAKE} ${CD2MAKEARGS} ${NOFUN}
3417 .if ${MK_META_MODE} != "no"
3418 # Don't rebuild build-tools targets during normal build.
3419 CD2MAKE+=       BUILD_TOOLS_META=.NOMETA
3420 .endif
3421 XDDESTDIR=${DESTDIR}${XDTP}
3422
3423 .ORDER: xdev-build xdev-install xdev-links
3424 xdev: xdev-build xdev-install .PHONY
3425
3426 .ORDER: _xb-worldtmp _xb-bootstrap-tools _xb-build-tools _xb-cross-tools
3427 xdev-build: _xb-worldtmp _xb-bootstrap-tools _xb-build-tools _xb-cross-tools .PHONY
3428
3429 _xb-worldtmp: .PHONY
3430         mkdir -p ${CDTMP}/usr
3431         ${WORLDTMP_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
3432             -p ${CDTMP}/usr >/dev/null
3433
3434 _xb-bootstrap-tools: .PHONY
3435 .for _tool in \
3436     ${_clang_tblgen} \
3437     ${_yacc}
3438         ${_+_}@${ECHODIR} "===> ${_tool} (obj,all,install)"; \
3439         cd ${.CURDIR}/${_tool}; \
3440         if [ -z "${NO_OBJWALK}" ]; then ${CDMAKE} DIRPRFX=${_tool}/ obj; fi; \
3441         ${CDMAKE} DIRPRFX=${_tool}/ all; \
3442         ${CDMAKE} DIRPRFX=${_tool}/ DESTDIR=${CDTMP} install
3443 .endfor
3444
3445 _xb-build-tools: .PHONY
3446         ${_+_}@cd ${.CURDIR}; \
3447         ${CDBENV} ${MAKE} ${CDMAKEARGS} -f Makefile.inc1 ${NOFUN} build-tools
3448
3449 XDEVDIRS= \
3450     ${_clang_libs} \
3451     ${_lld} \
3452     ${_elftctools} \
3453     usr.bin/ar \
3454     ${_clang}
3455
3456 _xb-cross-tools: .PHONY
3457 .for _tool in ${XDEVDIRS}
3458         ${_+_}@${ECHODIR} "===> xdev ${_tool} (obj,all)"; \
3459         cd ${.CURDIR}/${_tool}; \
3460         if [ -z "${NO_OBJWALK}" ]; then ${CDMAKE} DIRPRFX=${_tool}/ obj; fi; \
3461         ${CDMAKE} DIRPRFX=${_tool}/ all
3462 .endfor
3463
3464 _xi-mtree: .PHONY
3465         ${_+_}@${ECHODIR} "mtree populating ${XDDESTDIR}"
3466         mkdir -p ${XDDESTDIR}
3467         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.root.dist \
3468             -p ${XDDESTDIR} >/dev/null
3469         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.usr.dist \
3470             -p ${XDDESTDIR}/usr >/dev/null
3471         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.include.dist \
3472             -p ${XDDESTDIR}/usr/include >/dev/null
3473 .if defined(_LIBCOMPAT)
3474         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.lib${libcompat}.dist \
3475             -p ${XDDESTDIR}/usr >/dev/null
3476 .endif
3477 .if ${MK_TESTS} != "no"
3478         mkdir -p ${XDDESTDIR}${TESTSBASE}
3479         ${DESTDIR_MTREE} -f ${.CURDIR}/etc/mtree/BSD.tests.dist \
3480             -p ${XDDESTDIR}${TESTSBASE} >/dev/null
3481 .endif
3482
3483 .ORDER: xdev-build _xi-mtree _xi-cross-tools _xi-includes _xi-libraries
3484 xdev-install: xdev-build _xi-mtree _xi-cross-tools _xi-includes _xi-libraries .PHONY
3485
3486 _xi-cross-tools: .PHONY
3487         @echo "_xi-cross-tools"
3488 .for _tool in ${XDEVDIRS}
3489         ${_+_}@${ECHODIR} "===> xdev ${_tool} (install)"; \
3490         cd ${.CURDIR}/${_tool}; \
3491         ${CDMAKE} DIRPRFX=${_tool}/ install DESTDIR=${XDDESTDIR}
3492 .endfor
3493
3494 _xi-includes: .PHONY
3495 .if !defined(NO_OBJWALK)
3496         ${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 _obj \
3497                 DESTDIR=${XDDESTDIR}
3498 .endif
3499         ${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 includes \
3500                 DESTDIR=${XDDESTDIR}
3501
3502 _xi-libraries: .PHONY
3503         ${_+_}cd ${.CURDIR}; ${CD2MAKE} -f Makefile.inc1 libraries \
3504                 DESTDIR=${XDDESTDIR}
3505
3506 xdev-links: .PHONY
3507         ${_+_}cd ${XDDESTDIR}/usr/bin; \
3508         mkdir -p ../../../../usr/bin; \
3509                 for i in *; do \
3510                         ln -sf ../../${XDTP}/usr/bin/$$i \
3511                             ../../../../usr/bin/${XDDIR}-$$i; \
3512                         ln -sf ../../${XDTP}/usr/bin/$$i \
3513                             ../../../../usr/bin/${XDDIR}${_REVISION}-$$i; \
3514                 done