1#	$NetBSD: Makefile,v 1.342 2025/09/23 20:12:05 rillig Exp $
2
3#
4# This is the top-level makefile for building NetBSD. For an outline of
5# how to build a snapshot or release, as well as other release engineering
6# information, see http://www.NetBSD.org/developers/releng/index.html
7#
8# Not everything you can set or do is documented in this makefile. In
9# particular, you should review the files in /usr/share/mk (especially
10# bsd.README) for general information on building programs and writing
11# Makefiles within this structure, and see the comments in src/etc/Makefile
12# for further information on installation and release set options.
13#
14# Variables listed below can be set on the make command line (highest
15# priority), in /etc/mk.conf (middle priority), or in the environment
16# (lowest priority).
17#
18# Variables:
19#   DESTDIR is the target directory for installation of the compiled
20#	software. It defaults to /. Note that programs are built against
21#	libraries installed in DESTDIR.
22#   MKMAN, if `no', will prevent building of manual pages.
23#   MKOBJDIRS, if not `no', will build object directories at
24#	an appropriate point in a build.
25#   MKSHARE, if `no', will prevent building and installing
26#	anything in /usr/share.
27#   MKUPDATE, if not `no', will avoid a `make cleandir' at the start of
28#	`make build', as well as having the effects listed in mk.conf(5).
29#   NOCLEANDIR, if defined, will avoid a `make cleandir' at the start
30#	of the `make build'.
31#   NOINCLUDES will avoid the `make includes' usually done by `make build'.
32#   NOBINARIES will not build binaries, only includes and libraries
33#
34#   See mk.conf(5) for more details.
35#
36#
37# Targets:
38#   build:
39#	Builds a full release of NetBSD in DESTDIR, except for the
40#	/etc configuration files.
41#	If BUILD_DONE is set, this is an empty target.
42#   distribution:
43#	Builds a full release of NetBSD in DESTDIR, including the /etc
44#	configuration files.
45#   buildworld:
46#	As per `make distribution', except that it ensures that DESTDIR
47#	is not the root directory.
48#   installworld:
49#	Install the distribution from DESTDIR to INSTALLWORLDDIR (which
50#	defaults to the root directory).  Ensures that INSTALLWORLDDIR
51#	is not the root directory if cross compiling.
52#   release:
53#	Does a `make distribution', and then tars up the DESTDIR files
54#	into ${RELEASEDIR}/${RELEASEMACHINEDIR}, in release(7) format.
55#	(See etc/Makefile for more information on this.)
56#   regression-tests:
57#	Runs the regression tests in "regress" on this host.
58#   sets:
59#	Populate ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/sets
60#	from ${DESTDIR}
61#   sourcesets:
62#	Populate ${RELEASEDIR}/source/sets from ${NETBSDSRCDIR}
63#   syspkgs:
64#	Populate ${RELEASEDIR}/${RELEASEMACHINEDIR}/binary/syspkgs
65#	from ${DESTDIR}
66#   iso-image:
67#	Create CD-ROM image in RELEASEDIR/images.
68#	RELEASEDIR must already have been populated by `make release'
69#	or equivalent.
70#   iso-image-source:
71#	Create CD-ROM image with source in RELEASEDIR/images.
72#	RELEASEDIR must already have been populated by
73#	`make release sourcesets' or equivalent.
74#   live-image:
75#	Create bootable live image for emulators or USB stick etc.
76#	in RELEASEDIR/liveimage.
77#	RELEASEDIR must already have been populated by `make release'
78#	or equivalent.
79#   install-image:
80#	Create bootable installation image for USB stick etc.
81#	in RELEASEDIR/installimage.
82#	RELEASEDIR must already have been populated by `make release'
83#	or equivalent.
84#
85# Targets invoked by `make build,' in order:
86#   cleandir:        cleans the tree.
87#   do-top-obj:      creates the top level object directory.
88#   do-tools-obj:    creates object directories for the host toolchain.
89#   do-tools:        builds host toolchain.
90#   params:          create params file with various make(1) parameters.
91#   show-params:     show various make(1) parameters.
92#   obj:             creates object directories.
93#   do-distrib-dirs: creates the distribution directories.
94#   includes:        installs include files.
95#   do-lib:          builds and installs prerequisites from lib.
96#   do-compat-lib:   builds and installs prerequisites from compat/lib
97#                    if ${MKCOMPAT} != "no".
98#   do-x11:          builds and installs X11 tools and libraries
99#                    from src/external/mit/xorg if ${MKX11} != "no".
100#   do-build:        builds and installs the entire system.
101#   do-obsolete:     installs the obsolete sets (for the postinstall-* targets).
102#
103
104.if ${.MAKEFLAGS:M${.CURDIR}/share/mk} == ""
105.MAKEFLAGS: -m ${.CURDIR}/share/mk
106.endif
107
108#
109# If _SRC_TOP_OBJ_ gets set here, we will end up with a directory that may
110# not be the top level objdir, because "make obj" can happen in the *middle*
111# of "make build" (long after <bsd.own.mk> is calculated it).  So, pre-set
112# _SRC_TOP_OBJ_ here so it will not be added to ${.MAKEOVERRIDES}.
113#
114_SRC_TOP_OBJ_=
115
116.include <bsd.own.mk>
117
118#
119# Sanity check: make sure that "make build" is not invoked simultaneously
120# with a standard recursive target.
121#
122
123.if make(build) || make(release) || make(snapshot)
124.for targ in ${TARGETS:Nobj:Ncleandir}
125.if make(${targ}) && !target(.BEGIN)
126.BEGIN:
127	@echo 'BUILD ABORTED: "make build" and "make ${targ}" are mutually exclusive.'
128	@false
129.endif
130.endfor
131.endif
132
133#
134# _SUBDIR is used to set SUBDIR, after removing directories that have
135# BUILD_${dir}=no, or that have no ${dir}/Makefile.
136#
137_SUBDIR=	tools .WAIT lib
138.if ${MKLLVM} != "no"
139_SUBDIR+=	external/bsd/compiler_rt
140.endif
141_SUBDIR+=	include external crypto/external bin
142_SUBDIR+=	games libexec sbin usr.bin
143_SUBDIR+=	usr.sbin share sys etc tests compat
144_SUBDIR+=	.WAIT rescue .WAIT distrib regress
145
146.for dir in ${_SUBDIR}
147.if "${dir}" == ".WAIT" \
148	|| (${BUILD_${dir}:Uyes} != "no" && exists(${dir}/Makefile))
149SUBDIR+=	${dir}
150.endif
151.endfor
152
153.if exists(regress)
154regression-tests: .PHONY .MAKE
155	@echo Running regression tests...
156	${MAKEDIRTARGET} regress regress
157.endif
158
159.if ${MKUNPRIVED} != "no"
160NOPOSTINSTALL=	# defined
161.endif
162
163afterinstall: .PHONY .MAKE
164.if ${MKMAN} != "no"
165	${MAKEDIRTARGET} share/man makedb
166.endif
167.if (${MKUNPRIVED} != "no" && ${MKINFO} != "no")
168	${MAKEDIRTARGET} external/gpl2/texinfo/bin/install-info infodir-meta
169.endif
170.if !defined(NOPOSTINSTALL)
171	${MAKEDIRTARGET} . postinstall-check
172.endif
173
174_POSTINSTALL=	${:!cd ${.CURDIR}/usr.sbin/postinstall && \
175			${MAKE} -B -v .OBJDIR!}/postinstall  \
176		-m ${MACHINE} -a ${MACHINE_ARCH}
177_POSTINSTALL_ENV= \
178	AWK=${TOOL_AWK:Q}		\
179	DB=${TOOL_DB:Q}			\
180	HOST_SH=${HOST_SH:Q}		\
181	MAKE=${MAKE:Q}			\
182	PWD_MKDB=${TOOL_PWD_MKDB:Q}	\
183	SED=${TOOL_SED:Q}		\
184	STAT=${TOOL_STAT:Q}
185
186.if ${MKX11} != "no"
187_POSTINSTALL_X11=-x ${X11SRCDIR:Q}
188.endif
189
190postinstall-check: .PHONY
191	@echo "   === Post installation checks ==="
192	${_POSTINSTALL_ENV} ${HOST_SH} ${_POSTINSTALL} -s ${.CURDIR} ${_POSTINSTALL_X11} -d ${DESTDIR:C/^\/$//}/ check; if [ $$? -gt 1 ]; then exit 1; fi
193	@echo "   ================================"
194
195postinstall-fix: .NOTMAIN .PHONY
196	@echo "   === Post installation fixes ==="
197	${_POSTINSTALL_ENV} ${HOST_SH} ${_POSTINSTALL} -s ${.CURDIR} ${_POSTINSTALL_X11} -d ${DESTDIR:C/^\/$//}/ fix
198	@echo "   ==============================="
199
200postinstall-fix-obsolete: .NOTMAIN .PHONY
201	@echo "   === Removing obsolete files ==="
202	${_POSTINSTALL_ENV} ${HOST_SH} ${_POSTINSTALL} -s ${.CURDIR} ${_POSTINSTALL_X11} -d ${DESTDIR:C/^\/$//}/ fix obsolete
203	@echo "   ==============================="
204
205postinstall-fix-obsolete_stand: .NOTMAIN .PHONY
206	@echo "   === Removing obsolete files ==="
207	${_POSTINSTALL_ENV} ${HOST_SH} ${_POSTINSTALL} -s ${.CURDIR} ${_POSTINSTALL_X11} -d ${DESTDIR:C/^\/$//}/ fix obsolete_stand
208	${_POSTINSTALL_ENV} ${HOST_SH} ${_POSTINSTALL} -s ${.CURDIR} ${_POSTINSTALL_X11} -d ${DESTDIR:C/^\/$//}/ fix obsolete_stand_debug
209	@echo "   ==============================="
210
211
212#
213# Targets (in order!) called by "make build".
214#
215BUILDTARGETS+=	check-tools
216.if ${MKUPDATE} == "no" && !defined(NOCLEANDIR)
217BUILDTARGETS+=	cleandir
218.endif
219.if ${MKOBJDIRS} != "no"
220BUILDTARGETS+=	do-top-obj
221.endif
222.if ${USETOOLS} == "yes"	# {
223.if ${MKOBJDIRS} != "no"
224BUILDTARGETS+=	do-tools-obj
225.endif
226BUILDTARGETS+=	do-tools
227.endif # USETOOLS		# }
228BUILDTARGETS+=	params
229.if ${MKOBJDIRS} != "no"
230BUILDTARGETS+=	obj
231.endif
232BUILDTARGETS+=	clean_METALOG
233.if !defined(NODISTRIBDIRS)
234BUILDTARGETS+=	do-distrib-dirs
235.endif
236.if !defined(NOINCLUDES)
237BUILDTARGETS+=	includes
238.endif
239BUILDTARGETS+=	do-lib
240BUILDTARGETS+=	do-compat-lib
241.if ${MKLLVM} != "no"
242BUILDTARGETS+=	do-sanitizer
243.if ${MKSANITIZER:Uno} == "yes"
244BUILDTARGETS+=	do-sanitizer-tools
245.endif
246.endif
247.if ${MKX11} != "no"
248BUILDTARGETS+=	do-x11
249.endif
250.if !defined(NOBINARIES)
251BUILDTARGETS+=	do-build
252BUILDTARGETS+=	do-obsolete
253.endif
254
255#
256# Enforce proper ordering of some rules.
257#
258
259.ORDER:		${BUILDTARGETS}
260includes-lib:	.PHONY includes-include includes-sys
261
262#
263# Record the values of variables that might affect the build.
264# If no values have changed, avoid updating the timestamp
265# of the params file.
266#
267# This is referenced by _NETBSD_VERSION_DEPENDS in <bsd.own.mk>.
268#
269.include "${NETBSDSRCDIR}/etc/Makefile.params"
270CLEANDIRFILES+= params
271params: .EXEC
272	${_MKMSG_CREATE} params
273	@${:D make}${PRINT_PARAMS} >${.TARGET}.new
274	@if cmp -s ${.TARGET}.new ${.TARGET} > /dev/null 2>&1; then \
275		: "params is unchanged" ; \
276		rm ${.TARGET}.new ; \
277	else \
278		: "params has changed or is new" ; \
279		mv ${.TARGET}.new ${.TARGET} ; \
280	fi
281
282#
283# Display current make(1) parameters
284#
285show-params: .PHONY .MAKE
286	@${PRINT_PARAMS}
287
288#
289# Build the system and install into DESTDIR.
290#
291
292START_TIME!=	date
293
294build: .PHONY .MAKE
295.if defined(BUILD_DONE)
296	@echo "Build already installed into ${DESTDIR}"
297.else
298	@echo "Build started at: ${START_TIME}"
299.for tgt in ${BUILDTARGETS}
300	${MAKEDIRTARGET} . ${tgt}
301.endfor
302	${MAKEDIRTARGET} etc install-etc-release
303	@echo   "Build started at:  ${START_TIME}"
304	@printf "Build finished at: " && date
305.endif
306
307#
308# Build a full distribution, but not a release (i.e. no sets into
309# ${RELEASEDIR}).  "buildworld" enforces a build to ${DESTDIR} != /
310#
311
312distribution buildworld: .PHONY .MAKE
313.if make(buildworld) && \
314    (!defined(DESTDIR) || ${DESTDIR} == "" || ${DESTDIR} == "/")
315	@echo "Won't make ${.TARGET} with DESTDIR=/"
316	@false
317.endif
318	${MAKEDIRTARGET} . build NOPOSTINSTALL=1
319	${MAKEDIRTARGET} etc distribution INSTALL_DONE=1
320.if defined(DESTDIR) && ${DESTDIR} != "" && ${DESTDIR} != "/"
321	${MAKEDIRTARGET} . postinstall-fix-obsolete
322	${MAKEDIRTARGET} . postinstall-fix-obsolete_stand
323	${:D make}${MAKEDIRTARGET} distrib/sets checkflist
324.endif
325	@echo   "make ${.TARGET} started at:  ${START_TIME}"
326	@printf "make ${.TARGET} finished at: " && date
327
328#
329# Install the distribution from $DESTDIR to $INSTALLWORLDDIR (defaults to `/')
330# If installing to /, ensures that the host's operating system is NetBSD and
331# the host's `uname -m` == ${MACHINE}.
332#
333
334HOST_UNAME_S!=	uname -s
335HOST_UNAME_M!=	uname -m
336
337installworld: .PHONY .MAKE
338.if (!defined(DESTDIR) || ${DESTDIR} == "" || ${DESTDIR} == "/")
339	@echo "Can't make ${.TARGET} to DESTDIR=/"
340	@false
341.endif
342.if !defined(INSTALLWORLDDIR) || \
343    ${INSTALLWORLDDIR} == "" || ${INSTALLWORLDDIR} == "/"
344.if (${HOST_UNAME_S} != "NetBSD")
345	@echo "Won't cross-make ${.TARGET} from ${HOST_UNAME_S} to NetBSD with INSTALLWORLDDIR=/"
346	@false
347.endif
348.if (${HOST_UNAME_M} != ${MACHINE})
349	@echo "Won't cross-make ${.TARGET} from ${HOST_UNAME_M} to ${MACHINE} with INSTALLWORLDDIR=/"
350	@false
351.endif
352.endif
353	${MAKEDIRTARGET} distrib/sets installsets \
354		INSTALLDIR=${INSTALLWORLDDIR:U/} INSTALLSETS=${INSTALLSETS:Q}
355	${MAKEDIRTARGET} . postinstall-check DESTDIR=${INSTALLWORLDDIR}
356	@echo   "make ${.TARGET} started at:  ${START_TIME}"
357	@printf "make ${.TARGET} finished at: " && date
358
359#
360# Install modules from $DESTDIR to $INSTALLMODULESDIR
361#
362installmodules: .PHONY .MAKE
363.if (!defined(DESTDIR) || ${DESTDIR} == "" || ${DESTDIR} == "/")
364	@echo "Can't make ${.TARGET} to DESTDIR=/"
365	@false
366.endif
367.if !defined(INSTALLMODULESDIR) || \
368    ${INSTALLMODULESDIR} == "" || ${INSTALLMODULESDIR} == "/"
369.if (${HOST_UNAME_S} != "NetBSD")
370	@echo "Won't cross-make ${.TARGET} from ${HOST_UNAME_S} to NetBSD with INSTALLMODULESDIR=/"
371	@false
372.endif
373.if (${HOST_UNAME_M} != ${MACHINE})
374	@echo "Won't cross-make ${.TARGET} from ${HOST_UNAME_M} to ${MACHINE} with INSTALLMODULESDIR=/"
375	@false
376.endif
377.endif
378	${MAKEDIRTARGET} sys/modules install DESTDIR=${INSTALLMODULESDIR:U/}
379	@echo   "make ${.TARGET} started at:  ${START_TIME}"
380	@printf "make ${.TARGET} finished at: " && date
381
382#
383# Create sets from $DESTDIR or $NETBSDSRCDIR into $RELEASEDIR
384#
385
386.for tgt in sets sourcesets syspkgs
387${tgt}: .PHONY .MAKE
388	${MAKEDIRTARGET} distrib/sets ${tgt}
389.endfor
390
391#
392# Build a release or snapshot (implies "make distribution").  Note that
393# in this case, the set lists will be checked before the tar files
394# are made.
395#
396
397release snapshot: .PHONY .MAKE
398	${MAKEDIRTARGET} . distribution
399	${MAKEDIRTARGET} etc release DISTRIBUTION_DONE=1
400	@echo   "make ${.TARGET} started at:  ${START_TIME}"
401	@printf "make ${.TARGET} finished at: " && date
402
403#
404# Create a CD-ROM image.
405#
406
407iso-image: .PHONY .MAKE
408	${MAKEDIRTARGET} distrib iso_image
409	${MAKEDIRTARGET} etc iso-image
410	@echo   "make ${.TARGET} started at:  ${START_TIME}"
411	@printf "make ${.TARGET} finished at: " && date
412
413iso-image-source: .PHONY .MAKE
414	${MAKEDIRTARGET} distrib iso_image CDSOURCE=true
415	${MAKEDIRTARGET} etc iso-image
416	@echo   "make ${.TARGET} started at:  ${START_TIME}"
417	@printf "make ${.TARGET} finished at: " && date
418
419#
420# Create bootable live images.
421#
422
423live-image: .PHONY .MAKE
424	${MAKEDIRTARGET} etc live-image
425	@echo   "make ${.TARGET} started at:  ${START_TIME}"
426	@printf "make ${.TARGET} finished at: " && date
427
428#
429# Create bootable installation images.
430#
431
432install-image: .PHONY .MAKE
433	${MAKEDIRTARGET} etc install-image
434	@echo   "make ${.TARGET} started at:  ${START_TIME}"
435	@printf "make ${.TARGET} finished at: " && date
436
437#
438# Special components of the "make build" process.
439#
440
441check-tools: .PHONY
442.if ${TOOLCHAIN_MISSING} != "no" && !defined(EXTERNAL_TOOLCHAIN)
443	@echo '*** WARNING:  Building on MACHINE=${MACHINE} with missing toolchain.'
444	@echo '*** May result in a failed build or corrupt binaries!'
445.elif defined(EXTERNAL_TOOLCHAIN)
446	@echo '*** Using external toolchain rooted at ${EXTERNAL_TOOLCHAIN}.'
447.endif
448
449# Delete or sanitise a leftover METALOG from a previous build.
450clean_METALOG: .PHONY .MAKE
451.if ${MKUPDATE} != "no"
452	${MAKEDIRTARGET} distrib/sets clean_METALOG
453.endif
454
455do-distrib-dirs: .PHONY .MAKE
456.if !defined(DESTDIR) || ${DESTDIR} == ""
457	${MAKEDIRTARGET} etc distrib-dirs DESTDIR=/
458.else
459	${MAKEDIRTARGET} etc distrib-dirs DESTDIR=${DESTDIR}
460.endif
461
462.for targ in cleandir obj includes
463do-${targ}: .PHONY ${targ}
464	@true
465.endfor
466
467do-tools: .PHONY .MAKE
468	${MAKEDIRTARGET} tools build_install
469
470do-lib: .PHONY .MAKE
471	${MAKEDIRTARGET} lib build_install
472
473do-compat-lib: .PHONY .MAKE
474	${MAKEDIRTARGET} compat build_install BOOTSTRAP_SUBDIRS="../../../lib"
475
476do-sanitizer: .PHONY .MAKE
477	${MAKEDIRTARGET} external/bsd/compiler_rt build_install
478
479do-sanitizer-tools: .PHONY .MAKE
480.if !exists(${TOOLDIR}/lib/clang) && ${HAVE_LLVM:Uno} == "yes"
481	mkdir -p ${TOOLDIR}/lib/clang
482	cd ${DESTDIR}/usr/lib/clang && \
483		${TOOL_PAX} -rw . ${TOOLDIR}/lib/clang
484.endif
485
486do-top-obj: .PHONY .MAKE
487	${MAKEDIRTARGET} . obj NOSUBDIR=
488
489do-tools-obj: .PHONY .MAKE
490	${MAKEDIRTARGET} tools obj
491
492do-build: .PHONY .MAKE
493.for targ in dependall install
494	${MAKEDIRTARGET} . ${targ} BUILD_tools=no BUILD_lib=no
495.endfor
496
497do-x11: .PHONY .MAKE
498.if ${MKX11} != "no"
499	${MAKEDIRTARGET} external/mit/xorg/tools all
500	${MAKEDIRTARGET} external/mit/xorg/lib build_install
501.if ${MKCOMPATX11} != "no"
502	${MAKEDIRTARGET} compat build_install BOOTSTRAP_SUBDIRS="../../../external/mit/xorg/lib"
503.endif
504.else
505	@echo "MKX11 is not enabled"
506	@false
507.endif
508
509do-obsolete: .PHONY .MAKE
510	${MAKEDIRTARGET} etc install-obsolete-lists
511
512#
513# Speedup stubs for some subtrees that don't need to run these rules.
514# (Tells <bsd.subdir.mk> not to recurse for them.)
515#
516
517.for dir in bin etc distrib games libexec regress sbin usr.bin usr.sbin tools
518includes-${dir}: .PHONY
519	@true
520.endfor
521.for dir in etc distrib regress
522install-${dir}: .PHONY
523	@true
524.endfor
525
526#
527# XXX this needs to change when distrib Makefiles are recursion compliant
528# XXX many distrib subdirs need "cd etc && make snap_pre snap_kern" first...
529#
530dependall-distrib depend-distrib all-distrib: .PHONY
531	@true
532
533.include <bsd.obj.mk>
534.include <bsd.kernobj.mk>
535.include <bsd.subdir.mk>
536.include <bsd.clean.mk>
537