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