xref: /xnu-10063.121.3/makedefs/MakeInc.top (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa) !
1# -*- mode: makefile;-*-
2#
3# Copyright (C) 2010-2020 Apple Inc. All rights reserved.
4#
5# MakeInc.top is the top-level makefile for the xnu
6# build system. All the main XBS targets
7# (like "installhdrs") are defined here, as
8# well as globals that can be overridden on
9# the command-line by the user.
10#
11# This makefile's main purpose is to bootstrap
12# the user's intent ("build these 3 kernels")
13# into 3 single-architecture builds that each
14# invoke the recursive make build system.
15# As such, we have no knowledge of how to build
16# a kernel or perform actions other than
17# invoking a sub-make with a different
18# current directory, makefile, and target. One
19# side effect of this is that each
20# single-architecture build is responsible for
21# inserting its build products into the final
22# multi-architecture output files. To avoid
23# races, these aggregating stages for
24# "primary" build configs are done in serial.
25#
26
27export MakeInc_cmd=${VERSDIR}/makedefs/MakeInc.cmd
28
29include $(MakeInc_cmd)
30
31
32#
33# Architecture Configuration options
34#
35
36# Default to current kernel architecture
37
38ifneq ($(filter $(SUPPORTED_EMBEDDED_PLATFORMS),$(PLATFORM)),)
39     override DEFAULT_ARCH_CONFIG := ARM64
40else ifneq ($(filter $(SUPPORTED_SIMULATOR_PLATFORMS),$(PLATFORM)),)
41     override DEFAULT_ARCH_CONFIG := X86_64
42else
43     override DEFAULT_ARCH_CONFIG := X86_64
44endif
45
46# Accept either explicit ARCH_CONFIGS or XBS-style RC_ARCHS
47ifndef ARCH_CONFIGS
48ifdef RC_ARCHS
49ARCH_CONFIGS	:= $(shell printf "%s" "$(RC_ARCHS)" | $(TR) a-z A-Z | $(TR) " " "\n" | sort -u | $(TR) "\n" " ")
50else
51ARCH_CONFIGS	:= DEFAULT
52endif
53endif
54
55#
56# Kernel Configuration options
57#
58
59DEFAULT_PRODUCT_CONFIGS :=
60
61ifneq ($(filter $(RC_ProjectName),xnu_debug),)
62override DEFAULT_KERNEL_CONFIG := DEBUG
63else ifneq ($(filter $(RC_ProjectName),xnu_kasan),)
64override KERNEL_CONFIGS := KASAN
65else ifneq ($(filter $(SUPPORTED_EMBEDDED_PLATFORMS),$(PLATFORM)),)
66override DEFAULT_KERNEL_CONFIG := DEVELOPMENT
67else ifeq ($(PLATFORM),MacOSX)
68override DEFAULT_KERNEL_CONFIG := DEVELOPMENT
69else
70override DEFAULT_KERNEL_CONFIG := RELEASE
71endif
72
73# If KERNEL_CONFIGS is specified it should override default
74ifndef KERNEL_CONFIGS
75KERNEL_CONFIGS := DEFAULT
76endif
77
78# If PRODUCT_CONFIGS is specified it should override default
79ifndef PRODUCT_CONFIGS
80PRODUCT_CONFIGS := $(DEFAULT_PRODUCT_CONFIGS)
81endif
82
83#
84# Machine Configuration options
85#
86
87override DEFAULT_I386_MACHINE_CONFIG := NONE
88override DEFAULT_X86_64_MACHINE_CONFIG := NONE
89override DEFAULT_X86_64H_MACHINE_CONFIG := NONE
90
91ifneq ($(findstring _Sim,$(RC_ProjectName)),)
92override DEFAULT_ARM64_MACHINE_CONFIG	:= NONE
93else ifneq ($(findstring _host,$(RC_ProjectName)),)
94override DEFAULT_ARM64_MACHINE_CONFIG	:= NONE
95else
96override DEFAULT_ARM64_MACHINE_CONFIG	:= VMAPPLE
97endif
98
99# This is typically never specified (TARGET_CONFIGS is used)
100ifndef MACHINE_CONFIGS
101MACHINE_CONFIGS	:= DEFAULT
102endif
103
104#
105# Target configuration options.  NOTE - target configurations will
106# override ARCH_CONFIGS and KERNEL_CONFIGS and MACHINE_CONFIGS.
107#
108# Target configs come in groups of three parameters.  The first is the
109# kernel configuration, the second is the architecture configuration,
110# and the third is the machine configuration.  You may pass in as
111# many groups of configurations as you wish.  Each item passed in is
112# seperated by whitespace.
113#
114# Example:
115#	TARGET_CONFIGS="release ppc default debug i386 default release arm MX31ADS"
116# Parameters may be in upper or lower case (they are converted to upper).
117#
118# "default" parameter is a special case.  It means use the default value for
119# that parameter.  Here are the default values for each configuration:
120#
121# default kernel configuration = DEFAULT_KERNEL_CONFIG
122# default architecture configuration = system architecture where you are running make.
123
124ifneq ($(filter $(SUPPORTED_PLATFORMS),$(PLATFORM)),)
125
126# Defaults for "make all_embedded"
127ifeq ($(KERNEL_CONFIGS),DEFAULT)
128KERNEL_CONFIGS_EMBEDDED := RELEASE DEVELOPMENT
129else
130KERNEL_CONFIGS_EMBEDDED := $(KERNEL_CONFIGS)
131endif
132
133ifeq ($(ARCH_CONFIGS),DEFAULT)
134ARCH_CONFIGS_EMBEDDED := ARM64
135ARCH_CONFIGS_DESKTOP := X86_64
136else
137ARCH_CONFIGS_EMBEDDED := $(strip $(shell echo $(filter-out X86_64, $(ARCH_CONFIGS)) | $(TR) a-z A-Z))
138ARCH_CONFIGS_DESKTOP := $(strip $(shell echo $(filter X86_64, $(ARCH_CONFIGS)) | $(TR) a-z A-Z))
139endif
140
141# Find supported products from the device map
142ifneq ($(EMBEDDED_DEVICE_MAP),)
143DEVICEMAP_PRODUCTS_ARM64 := $(shell $(EMBEDDED_DEVICE_MAP) -db $(EDM_DBPATH)		\
144				-list -query 'SELECT DISTINCT TargetType		\
145					FROM Files					\
146					  INNER JOIN Manifests USING (manifestID)	\
147					  INNER JOIN Targets USING (Target)		\
148					WHERE (KernelMachOArchitecture LIKE "arm64"	\
149					   AND fileType in ("KernelCache", "RestoreKernelCache"))')
150DEVICEMAP_PRODUCTS_OSX_ARM64 := $(shell $(EMBEDDED_DEVICE_MAP) -db $(EDM_DBPATH)		\
151					-list -query 'SELECT DISTINCT TargetType		\
152					      	FROM Files					\
153					  	  INNER JOIN Manifests USING (manifestID)	\
154					  	  INNER JOIN Targets USING (Target)		\
155					    	WHERE (KernelMachOArchitecture LIKE "arm64"	\
156					   	  AND fileType in ("KernelCache", "RestoreKernelCache") \
157						  AND SDKPlatform == "macosx")')
158
159
160# Generate a list of mappings of the form "n75:arm;t8002" based on the device map
161DEVICEMAP_PRODUCT_SOC_MAPPINGS := $(shell $(EMBEDDED_DEVICE_MAP) -db $(EDM_DBPATH) -list -query SELECT DISTINCT TargetType, KernelMachOArchitecture, KernelPlatform FROM Targets | awk -F\| '{ if ($$2 ~ /arm64[a-z]?/) { print $$1 ":arm64;" $$3 ";" $$4} else { print $$1 ":" $$2 ";" $$3 ";" $$4} }' )
162
163# use embedded_device_map
164endif
165
166# Map a product like "n75" to "arm;t8002"
167# $(1) is a product name in lower case
168function_lookup_product = $(call function_substitute_word_with_replacement,	\
169				 $(1),						\
170				 $(DEVICEMAP_PRODUCT_SOC_MAPPINGS),		\
171				 unknown_arch_for_$(1);unknown_platform_for_$(1) \
172			   )
173
174ifneq ($(PLATFORM),MacOSX)
175ifneq ($(EMBEDDED_DEVICE_MAP),)
176# Generate a list of mappings for products that use a different platform for their kernel configuration than their true platform
177# of the form "n71m:arm64;s8000;s8003". The 4th element is the true SoC platform, which will get an on-disk copy, while the
178# kernel's recursive build system will build the 3rd element as the KernelPlatform
179DEVICEMAP_PRODUCT_SOC_ALIASES := $(shell $(EMBEDDED_DEVICE_MAP) -db $(EDM_DBPATH) -list -query SELECT DISTINCT TargetType, KernelMachOArchitecture, KernelPlatform, Platform FROM Targets WHERE KernelPlatform "!=" Platform | awk -F\| '{ if ($$2 ~ /arm64[a-z]?/) { print $$1 ":arm64;" $$3 ";" $$4} else { print $$1 ":" $$2 ";" $$3 ";" $$4} }' )
180
181endif
182endif
183
184function_lookup_product_alias = $(call function_substitute_word_with_replacement,	\
185					$(1),						\
186					$(DEVICEMAP_PRODUCT_SOC_ALIASES),		\
187				 )
188endif
189
190ifeq ($(PLATFORM),MacOSX)
191
192# Defaults for "make all_desktop"
193ifeq ($(KERNEL_CONFIGS),DEFAULT)
194KERNEL_CONFIGS_DESKTOP := RELEASE DEVELOPMENT
195else
196KERNEL_CONFIGS_DESKTOP := $(KERNEL_CONFIGS)
197endif
198
199endif
200
201ifndef TARGET_CONFIGS
202ifneq ($(PRODUCT_CONFIGS),)
203# generate TARGET_CONFIGS using KERNEL_CONFIGS and PRODUCT_CONFIGS
204TARGET_CONFIGS := $(foreach my_devicemap_config,$(foreach my_product_config,$(shell printf "%s" "$(PRODUCT_CONFIGS)" | $(TR) A-Z a-z),$(call function_lookup_product,$(my_product_config))),$(foreach my_kernel_config,$(KERNEL_CONFIGS),$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
205TARGET_CONFIGS_ALIASES := $(foreach my_devicemap_config,$(foreach my_product_config,$(shell printf "%s" "$(PRODUCT_CONFIGS)" | $(TR) A-Z a-z),$(call function_lookup_product_alias,$(my_product_config))),$(foreach my_kernel_config,$(KERNEL_CONFIGS),$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
206else ifneq ($(filter %_release_embedded,$(MAKECMDGOALS)),)
207# generate TARGET_CONFIGS for RELEASE kernel configs and products in the device map
208TARGET_CONFIGS := $(foreach my_devicemap_config,$(foreach my_arch_config,$(ARCH_CONFIGS_EMBEDDED),$(foreach my_product_config,$(DEVICEMAP_PRODUCTS_$(my_arch_config)),$(call function_lookup_product,$(my_product_config)))),$(foreach my_kernel_config,RELEASE,$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
209TARGET_CONFIGS += $(EXTRA_TARGET_CONFIGS_RELEASE)
210TARGET_CONFIGS_ALIASES := $(foreach my_devicemap_config,$(foreach my_arch_config,$(ARCH_CONFIGS_EMBEDDED),$(foreach my_product_config,$(DEVICEMAP_PRODUCTS_$(my_arch_config)),$(call function_lookup_product_alias,$(my_product_config)))),$(foreach my_kernel_config,RELEASE,$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
211else ifneq ($(filter %_development_embedded,$(MAKECMDGOALS)),)
212# generate TARGET_CONFIGS for DEVELOPMENT kernel configs and products in the device map
213TARGET_CONFIGS := $(foreach my_devicemap_config,$(foreach my_arch_config,$(ARCH_CONFIGS_EMBEDDED),$(foreach my_product_config,$(DEVICEMAP_PRODUCTS_$(my_arch_config)),$(call function_lookup_product,$(my_product_config)))),$(foreach my_kernel_config,DEVELOPMENT,$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
214TARGET_CONFIGS_ALIASES := $(foreach my_devicemap_config,$(foreach my_arch_config,$(ARCH_CONFIGS_EMBEDDED),$(foreach my_product_config,$(DEVICEMAP_PRODUCTS_$(my_arch_config)),$(call function_lookup_product_alias,$(my_product_config)))),$(foreach my_kernel_config,DEVELOPMENT,$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
215else ifneq ($(filter %_embedded,$(MAKECMDGOALS)),)
216# generate TARGET_CONFIGS for all kernel configs and products in the device map
217TARGET_CONFIGS := $(foreach my_devicemap_config,$(foreach my_arch_config,$(ARCH_CONFIGS_EMBEDDED),$(foreach my_product_config,$(DEVICEMAP_PRODUCTS_$(my_arch_config)),$(call function_lookup_product,$(my_product_config)))),$(foreach my_kernel_config,$(KERNEL_CONFIGS_EMBEDDED),$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
218TARGET_CONFIGS += $(foreach my_kernel_config,$(KERNEL_CONFIGS_EMBEDDED),$(EXTRA_TARGET_CONFIGS_$(my_kernel_config)))
219TARGET_CONFIGS_ALIASES := $(foreach my_devicemap_config,$(foreach my_arch_config,$(ARCH_CONFIGS_EMBEDDED),$(foreach my_product_config,$(DEVICEMAP_PRODUCTS_$(my_arch_config)),$(call function_lookup_product_alias,$(my_product_config)))),$(foreach my_kernel_config,$(KERNEL_CONFIGS_EMBEDDED),$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
220else ifeq (xnu_headers_driverkit,$(RC_ProjectName))
221# generate TARGET_CONFIGS for all kernel configs for B&I
222TARGET_CONFIGS = DEVELOPMENT arm64 DEFAULT DEVELOPMENT X86_64 DEFAULT
223else ifneq ($(filter %_desktop,$(MAKECMDGOALS)),)
224# generate TARGET_CONFIGS for all kernel configs for B&I
225TARGET_CONFIGS := $(foreach my_kern_config, $(KERNEL_CONFIGS_DESKTOP), $(foreach my_arch_config, $(ARCH_CONFIGS_DESKTOP), $(foreach my_machine_config, $(MACHINE_CONFIGS), $(my_kern_config) $(my_arch_config) $(my_machine_config))))
226TARGET_CONFIGS += $(foreach my_devicemap_config,$(foreach my_arch_config,$(ARCH_CONFIGS_EMBEDDED),$(foreach my_product_config,$(DEVICEMAP_PRODUCTS_OSX_$(my_arch_config)),$(call function_lookup_product,$(my_product_config)))),$(foreach my_kernel_config,$(KERNEL_CONFIGS_EMBEDDED),$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
227TARGET_CONFIGS += $(foreach my_kernel_config,$(KERNEL_CONFIGS_DESKTOP),$(EXTRA_TARGET_CONFIGS_$(my_kernel_config)))
228TARGET_CONFIGS_ALIASES := $(foreach my_devicemap_config,$(foreach my_arch_config,$(ARCH_CONFIGS_EMBEDDED),$(foreach my_product_config,$(DEVICEMAP_PRODUCTS_OSX_$(my_arch_config)),$(call function_lookup_product_alias,$(my_product_config)))),$(foreach my_kernel_config,$(KERNEL_CONFIGS_EMBEDDED),$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
229else
230# generate TARGET_CONFIGS using KERNEL_CONFIGS and ARCH_CONFIGS and MACHINE_CONFIGS (which defaults to "DEFAULT")
231TARGET_CONFIGS := $(foreach my_kern_config, $(KERNEL_CONFIGS), $(foreach my_arch_config, $(ARCH_CONFIGS), $(foreach my_machine_config, $(MACHINE_CONFIGS), $(my_kern_config) $(my_arch_config) $(my_machine_config))))
232TARGET_CONFIGS_ALIASES :=
233endif
234endif
235
236ifeq ($(TARGET_CONFIGS),)
237$(error No TARGET_CONFIGS specified)
238endif
239
240TARGET_CONFIGS_UC := $(strip $(shell printf "%s" "$(TARGET_CONFIGS)" | $(TR) a-z A-Z))
241TARGET_CONFIGS_ALIASES_UC := $(strip $(shell printf "%s" "$(TARGET_CONFIGS_ALIASES)" | $(TR) a-z A-Z))
242
243#
244# Build Configurations
245#
246# TARGET_CONFIGS is unwieldy for use in Makefiles. Convert them to
247# "build configurations" which are tuples joined by "^". For
248# example, "RELEASE I386 DEFAULT DEVELOPMENT ARM DEFAULT" becomes
249# "RELEASE^I386^NONE DEVELOPMENT^ARM^T8002", which can be looped
250# over trivially. PRIMARY_BUILD_CONFIGS is the first config
251# for each architecture, used primarily for machine-dependent recursion.
252# VARIANT_CONFIGS contains one build configuration for each combination
253# of kernel architecture and configuration. It is used for variant
254# installation recursion.
255
256BUILD_CONFIGS = $(call function_create_build_configs, $(TARGET_CONFIGS_UC))
257
258PRIMARY_ARCHS = $(strip $(sort $(foreach build_config, $(BUILD_CONFIGS), $(call function_extract_arch_config_from_build_config, $(build_config)))))
259PRIMARY_BUILD_CONFIGS = $(strip $(foreach arch, $(PRIMARY_ARCHS), $(firstword $(foreach build_config, $(BUILD_CONFIGS), $(if $(filter $(arch),$(call function_extract_arch_config_from_build_config, $(build_config))), $(build_config), )))))
260NON_PRIMARY_BUILD_CONFIGS = $(strip $(filter-out $(PRIMARY_BUILD_CONFIGS), $(BUILD_CONFIGS)))
261FIRST_BUILD_CONFIG = $(firstword $(BUILD_CONFIGS))
262
263# Build list of all kernel configurations present in all build configs.
264PRIMARY_KERNEL_CONFIGS = $(strip $(sort $(foreach build_config, $(BUILD_CONFIGS), $(call function_extract_kernel_config_from_build_config, $(build_config)))))
265
266# Pick one build configuration for each kernel configuration.
267# This will be used to populate System.kext variants in SYMROOT / DSTROOT.
268VARIANT_CONFIGS = $(strip \
269                    $(foreach kernel_config, $(PRIMARY_KERNEL_CONFIGS), \
270                      $(firstword $(call function_match_build_config_for_kernel_config, $(BUILD_CONFIGS), $(kernel_config)))))
271
272ifneq ($(TARGET_CONFIGS_ALIASES_UC),)
273ALIAS_CONFIGS = $(call function_create_alias_configs, $(TARGET_CONFIGS_ALIASES_UC))
274else
275ALIAS_CONFIGS =
276endif
277
278# $(warning PRIMARY_ARCHS is $(PRIMARY_ARCHS))
279# $(warning TARGET_CONFIGS is $(TARGET_CONFIGS))
280# $(warning BUILD_CONFIGS is $(BUILD_CONFIGS))
281# $(warning PRIMARY_BUILD_CONFIGS is $(PRIMARY_BUILD_CONFIGS))
282# $(warning NON_PRIMARY_BUILD_CONFIGS is $(NON_PRIMARY_BUILD_CONFIGS))
283# $(warning TARGET_CONFIGS_ALIASES is $(TARGET_CONFIGS_ALIASES))
284# $(warning ALIAS_CONFIGS is $(ALIAS_CONFIGS))
285# $(warning PRIMARY_KERNEL_CONFIGS is $(PRIMARY_KERNEL_CONFIGS))
286# $(warning VARIANT_CONFIGS is $(VARIANT_CONFIGS))
287
288MEMORY_SIZE := $(shell /usr/sbin/sysctl -n hw.memsize)
289
290# Assume LTO scaling by default, unless it is being explicitly passed on the command-line
291LARGE_BUILD_FOOTPRINT := $(if $(BUILD_LTO),$(BUILD_LTO),1)
292
293ifeq ($(LARGE_BUILD_FOOTPRINT),1)
294RAM_PER_KERNEL_BUILD := 4294967296
295else
296RAM_PER_KERNEL_BUILD := 268435456
297endif
298
299KERNEL_BUILDS_IN_PARALLEL := $(shell if [ $(MEMORY_SIZE) -le $$((1 * $(RAM_PER_KERNEL_BUILD))) ]; then echo 1; elif [ $(MEMORY_SIZE) -gt $$(($(SYSCTL_HW_PHYSICALCPU) * $(RAM_PER_KERNEL_BUILD))) ]; then echo $(SYSCTL_HW_PHYSICALCPU); else expr $(MEMORY_SIZE) / $(RAM_PER_KERNEL_BUILD); fi )
300# $(warning Building $(KERNEL_BUILDS_IN_PARALLEL) kernels in parallel)
301
302#
303# TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template
304#
305# $(1) is the name of the makefile target to invoke for the each build config
306#      after setting up the parallel hierarchy in the TARGET directory
307# $(2) is an optional suffix on the TARGET directory, which might even be
308#      "/.."
309# $(3) are any dependencies for the bootstrap target
310# $(4) are any dependencies that are expanded per-build config to another bootstrap target
311# $(5) is how many build configurations to build in parallel
312# $(6) is which build configs to build in parallel
313#
314# Since building many configurations in parallel may overwhelm the system,
315# we try to throttle behavior into more managable S "stripes" of N/S
316# configurations by generating sequential dependencies between configs
317# in each stripe. That ensures that only S kernel builds are occurring
318# at once at any point in time
319
320define TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template
321
322# Create a list of synthesized targets for each build config
323$(1)_bootstrap_target_list = $$(addprefix $(1)_bootstrap_,$(6))
324
325.PHONY: $$($(1)_bootstrap_target_list)
326
327$(1)_generated_stripe_dependencies = $$(call _function_generate_stripe_groupings,$(1),$(5),$(call reverse,$(6)))
328ifeq ($$(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
329$$(warning Generate makefile fragment: $$($(1)_generated_stripe_dependencies))
330endif
331$$(eval $$($(1)_generated_stripe_dependencies))
332
333$$($(1)_bootstrap_target_list): $(1)_bootstrap_% : $(1)_stripe_dep_for_% $$(addsuffix _bootstrap_%,$(4)) $(3)
334	$$(_v)$$(MKDIR) $${OBJROOT}/$$(call function_convert_build_config_to_objdir,$$(patsubst $(1)_bootstrap_%,%,$$@))$(2)
335	$$(_v)$${MAKE}													        \
336		-C $${OBJROOT}/$$(call function_convert_build_config_to_objdir,$$(patsubst $(1)_bootstrap_%,%,$$@))$(2)		\
337		-f $${SRCROOT}/Makefile												\
338		CURRENT_KERNEL_CONFIG=$$(call function_extract_kernel_config_from_build_config,$$(patsubst $(1)_bootstrap_%,%,$$@)) \
339		CURRENT_ARCH_CONFIG=$$(call function_extract_arch_config_from_build_config,$$(patsubst $(1)_bootstrap_%,%,$$@))	  \
340		CURRENT_MACHINE_CONFIG=$$(call function_extract_machine_config_from_build_config,$$(patsubst $(1)_bootstrap_%,%,$$@))	\
341		CURRENT_BUILD_CONFIG=$$(patsubst $(1)_bootstrap_%,%,$$@)							\
342		PRIMARY_BUILD_CONFIGS="$(PRIMARY_BUILD_CONFIGS)" BUILD_CONFIGS="$(BUILD_CONFIGS)"				\
343		SOURCE=$${SRCROOT}/												\
344		RELATIVE_SOURCE_PATH=.											\
345		TARGET=$${OBJROOT}/$$(call function_convert_build_config_to_objdir,$$(patsubst $(1)_bootstrap_%,%,$$@))$(2)/	\
346		OBJPATH=$${OBJROOT}/$$(call function_convert_build_config_to_objdir,$$(patsubst $(1)_bootstrap_%,%,$$@))	\
347		$(1)
348
349.PHONY: $(1)_bootstrap
350
351$(1)_bootstrap: $$($(1)_bootstrap_target_list)
352endef
353
354#
355# TOP_LEVEL_STRIPE_DEPENDENCY_template
356#
357# $(1) is the Makefile target we are building for
358# $(2) is the build config that must build first
359# $(3) is the build config that must build after $(2)
360
361define TOP_LEVEL_STRIPE_DEPENDENCY_template
362
363.PHONY: $(1)_stripe_dep_for_$(3)
364
365 $(1)_stripe_dep_for_$(3): $(if $(2),$(1)_bootstrap_$(2))
366
367endef
368
369# $(1) is the Makefile target we are building for
370# $(2) is the stripe size
371# $(3) is the list of the build configs in the current group
372# $(4) is the list of remaining build configs
373_function_generate_stripe_groupings_recursive = $(foreach stripe_index,$(call sequence,$(2)),$(if $(word $(stripe_index),$(4)),$(call TOP_LEVEL_STRIPE_DEPENDENCY_template,$(1),$(word $(stripe_index),$(3)),$(word $(stripe_index),$(4))))) $(if $(word $(call increment,$(2)),$(4)),$(call _function_generate_stripe_groupings_recursive,$(1),$(2),$(wordlist 1,$(2),$(4)),$(wordlist $(call increment,$(2)),$(words $(4)),$(4))))
374
375# $(1) is the Makefile target we are building for
376# $(2) is the stripe size
377# $(3) is the list of the build configs
378_function_generate_stripe_groupings = $(call _function_generate_stripe_groupings_recursive,$(1),$(2),,$(3))
379
380#
381# Setup pass for build system tools
382#
383
384generated_top_level_build_setup = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_setup,/..,,,1,$(FIRST_BUILD_CONFIG))
385ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
386$(warning Generate makefile fragment: $(generated_top_level_build_setup))
387endif
388$(eval $(generated_top_level_build_setup))
389
390.PHONY: setup
391
392# invalidate current kernel in $(SYMROOT). Timestamp must be +1 from a previous kernel build
393setup: build_setup_bootstrap
394	$(_v)$(TOUCH) $(OBJROOT)/.mach_kernel.timestamp.new
395    # -nt and -ot are evaluated differently by bash, dash, and zsh (and are not part of the POSIX specification).
396    # Explicitly specify what should happen when the right hand file doesn't exist.
397	$(_v)while [ \! $(OBJROOT)/.mach_kernel.timestamp.new -nt $(OBJROOT)/.mach_kernel.timestamp -a \( \! -e $(OBJROOT)/.mach_kernel.timestamp.new -o -e $(OBJROOT)/.mach_kernel.timestamp \) ]; do \
398		$(SLEEP) 1;					\
399		$(TOUCH) $(OBJROOT)/.mach_kernel.timestamp.new;	\
400	done
401	$(_v)$(MV) $(OBJROOT)/.mach_kernel.timestamp.new $(OBJROOT)/.mach_kernel.timestamp
402	$(_v)$(TOUCH) $(OBJROOT)/.symbolset.timestamp.new
403	$(_v)while [ $(OBJROOT)/.symbolset.timestamp.new -ot $(OBJROOT)/.symbolset.timestamp ]; do \
404		$(SLEEP) 1;					\
405		$(TOUCH) $(OBJROOT)/.symbolset.timestamp.new;	\
406	done
407	$(_v)$(MV) $(OBJROOT)/.symbolset.timestamp.new $(OBJROOT)/.symbolset.timestamp
408
409#
410# Install kernel header files
411#
412.PHONY: exporthdrs exporthdrs_mi exporthdrs_md
413
414exporthdrs: exporthdrs_mi exporthdrs_md
415
416#
417# Install machine independent kernel header files
418#
419
420generated_top_level_build_exporthdrs_mi = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_exporthdrs_mi,,setup,,1,$(FIRST_BUILD_CONFIG))
421ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
422$(warning Generate makefile fragment: $(generated_top_level_build_exporthdrs_mi))
423endif
424$(eval $(generated_top_level_build_exporthdrs_mi))
425
426exporthdrs_mi: build_exporthdrs_mi_bootstrap
427
428#
429# Install machine dependent kernel header files
430#
431
432generated_top_level_build_exporthdrs_md = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_exporthdrs_md,,setup,,$(KERNEL_BUILDS_IN_PARALLEL),$(PRIMARY_BUILD_CONFIGS))
433ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
434$(warning Generate makefile fragment: $(generated_top_level_build_exporthdrs_md))
435endif
436$(eval $(generated_top_level_build_exporthdrs_md))
437
438exporthdrs_md: build_exporthdrs_md_bootstrap
439
440#
441# Install kernel header files
442#
443
444.PHONY: installhdrs installhdrs_mi installhdrs_md
445
446ifneq ($(filter $(RC_ProjectName),xnu_debug),)
447installhdrs:
448	@:
449else ifneq ($(filter $(RC_ProjectName),xnu_kasan),)
450installhdrs:
451	@:
452else
453
454installhdrs: installhdrs_mi installhdrs_md
455endif
456
457.PHONY: installhdrs_embedded installhdrs_release_embedded installhdrs_development_embedded installhdrs_desktop
458
459installhdrs_embedded installhdrs_release_embedded installhdrs_desktop: installhdrs
460
461installhdrs_development_embedded:
462
463#
464# Install machine independent header files
465#
466
467generated_top_level_build_installhdrs_mi = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_installhdrs_mi,,setup,build_exporthdrs_mi,1,$(FIRST_BUILD_CONFIG))
468ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
469$(warning Generate makefile fragment: $(generated_top_level_build_installhdrs_mi))
470endif
471$(eval $(generated_top_level_build_installhdrs_mi))
472
473installhdrs_mi: build_installhdrs_mi_bootstrap
474
475#
476# Install machine dependent kernel header files
477#
478
479generated_top_level_build_installhdrs_md = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_installhdrs_md,,setup,build_exporthdrs_md,$(KERNEL_BUILDS_IN_PARALLEL),$(PRIMARY_BUILD_CONFIGS))
480ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
481$(warning Generate makefile fragment: $(generated_top_level_build_installhdrs_md))
482endif
483$(eval $(generated_top_level_build_installhdrs_md))
484
485installhdrs_md: build_installhdrs_md_bootstrap
486
487.PHONY: install_textfiles install_textfiles_mi install_textfiles_md
488
489install_textfiles: install_textfiles_mi install_textfiles_md
490
491#
492#  Install machine independent text files (man pages, dtrace scripts, etc.)
493#
494
495generated_top_level_textfiles_install_mi = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,textfiles_install_mi,,setup,,1,$(FIRST_BUILD_CONFIG))
496ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
497$(warning Generate makefile fragment: $(generated_top_level_textfiles_install_mi))
498endif
499$(eval $(generated_top_level_textfiles_install_mi))
500
501install_textfiles_mi: textfiles_install_mi_bootstrap
502
503#
504#  Install machine dependent text files (man pages, dtrace scripts, etc.)
505#
506
507generated_top_level_textfiles_install_md = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,textfiles_install_md,,setup,,$(KERNEL_BUILDS_IN_PARALLEL),$(PRIMARY_BUILD_CONFIGS))
508ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
509$(warning Generate makefile fragment: $(generated_top_level_textfiles_install_md))
510endif
511$(eval $(generated_top_level_textfiles_install_md))
512
513install_textfiles_md: textfiles_install_md_bootstrap
514
515#
516# Build all architectures for all Configuration/Architecture options
517#
518
519generated_top_level_build_all = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_all,,setup exporthdrs,,$(KERNEL_BUILDS_IN_PARALLEL),$(BUILD_CONFIGS))
520ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
521$(warning Generate makefile fragment: $(generated_top_level_build_all))
522endif
523$(eval $(generated_top_level_build_all))
524
525.PHONY: build
526
527build: build_all_bootstrap
528
529#
530# Post-process build results
531#
532
533generated_top_level_config_all = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,config_all,,setup,build_all,$(KERNEL_BUILDS_IN_PARALLEL),$(BUILD_CONFIGS))
534ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
535$(warning Generate makefile fragment: $(generated_top_level_config_all))
536endif
537$(eval $(generated_top_level_config_all))
538
539.PHONY: all config
540
541all config: config_all_bootstrap
542
543.PHONY: all_embedded all_release_embedded all_development_embedded all_desktop
544
545all_embedded all_release_embedded all_development_embedded all_desktop: all
546
547#
548# Install kernel files
549#
550
551generated_top_level_build_install_primary = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_install_primary,,setup,config_all,1,$(PRIMARY_BUILD_CONFIGS))
552ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
553$(warning Generate makefile fragment: $(generated_top_level_build_install_primary))
554endif
555$(eval $(generated_top_level_build_install_primary))
556
557.PHONY: install_primary
558
559install_primary: build_install_primary_bootstrap
560
561generated_top_level_build_install_non_primary = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_install_non_primary,,setup,config_all,$(KERNEL_BUILDS_IN_PARALLEL),$(NON_PRIMARY_BUILD_CONFIGS))
562ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
563$(warning Generate makefile fragment: $(generated_top_level_build_install_non_primary))
564endif
565$(eval $(generated_top_level_build_install_non_primary))
566
567.PHONY: install_non_primary
568
569install_non_primary: build_install_non_primary_bootstrap
570
571generated_top_level_config_install_primary = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,config_install_primary,,setup,config_all,1,$(PRIMARY_BUILD_CONFIGS))
572ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
573$(warning Generate makefile fragment: $(generated_top_level_config_install_primary))
574endif
575$(eval $(generated_top_level_config_install_primary))
576
577.PHONY: install_config_primary
578
579install_config_primary: config_install_primary_bootstrap
580
581# config_install_variant iterates through products from all BUILD_CONFIGS so depends on config_all having completed overall
582generated_top_level_config_install_variant = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,config_install_variant,,config_all_bootstrap,,1,$(VARIANT_CONFIGS))
583ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
584$(warning Generate makefile fragment: $(generated_top_level_config_install_variant))
585endif
586$(eval $(generated_top_level_config_install_variant))
587
588.PHONY: install_config_variant
589
590install_config_variant: config_install_variant_bootstrap
591
592# config_install iterates through products from all BUILD_CONFIGS so depends on config_all having completed overall
593generated_top_level_config_install = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,config_install,,config_all_bootstrap,,1,$(FIRST_BUILD_CONFIG))
594ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
595$(warning Generate makefile fragment: $(generated_top_level_config_install))
596endif
597$(eval $(generated_top_level_config_install))
598
599.PHONY: install_config final_touch_config_timestamps
600
601install_config: config_install_bootstrap install_config_primary install_config_variant final_touch_config_timestamps
602
603
604# Tell the next build the latest timestamp of any potential file in DSTROOT/SYMROOT
605final_touch_config_timestamps: config_install_bootstrap install_config_primary install_config_variant
606	$(_v)$(TOUCH) $(OBJROOT)/.symbolset.timestamp
607
608#
609# Aggregate install targets, which install everything appropriate for the current build alias/make target
610#
611
612.PHONY: install
613
614ifneq ($(filter $(RC_ProjectName),xnu_debug),)
615install: install_kernels
616else ifneq ($(filter $(RC_ProjectName),xnu_kasan),)
617install: install_config install_kernels
618else ifneq ($(filter $(RC_ProjectName),xnu_headers_Sim),)
619install: installhdrs
620else ifneq ($(filter $(RC_ProjectName),xnu_headers_host),)
621install: installhdrs
622export INSTALLHDRS_SKIP_HOST=YES
623export INSTALLHDRS_MODULEMAPS_SKIP_HOST=YES
624else ifneq ($(filter $(RC_ProjectName),xnu_headers_driverkit),)
625install: installhdrs_desktop
626else ifneq ($(filter $(RC_ProjectName),xnu_headers_exclavekit xnu_headers_exclavecore),)
627install: installhdrs_desktop
628export INSTALLHDRS_MODULEMAPS_SKIP_HOST=YES
629export INSTALLHDRS_SKIP_HOST=YES
630export EXPORTHDRS_SKIP_EXCLAVES=YES
631else
632
633install: installhdrs install_textfiles install_config install_kernels install_aliases
634endif
635
636.PHONY: install_embedded install_release_embedded install_development_embedded install_desktop
637
638# By default, all kernel files, headers, text files, and pseudo-kexts are installed
639install_embedded install_release_embedded install_desktop: install
640
641# These special configs only install the kernel files
642install_development_embedded: install_kernels install_aliases
643
644.PHONY: install_kernels final_touch_kernel_timestamps install_aliases
645
646install_kernels: build_install_primary_bootstrap build_install_non_primary_bootstrap final_touch_kernel_timestamps
647
648# Tell the next build the latest timestamp of any potential file in DSTROOT/SYMROOT
649final_touch_kernel_timestamps: build_install_primary_bootstrap build_install_non_primary_bootstrap
650	$(_v)$(TOUCH) $(OBJROOT)/.mach_kernel.timestamp
651	@echo "done building xnu"
652
653# Copy kernels that are aliases of another configuration
654generated_top_level_install_alias = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,install_alias,,install_kernels,,$(KERNEL_BUILDS_IN_PARALLEL),$(ALIAS_CONFIGS))
655ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
656$(warning Generate makefile fragment: $(generated_top_level_install_alias))
657endif
658$(eval $(generated_top_level_install_alias))
659
660install_aliases: install_alias_bootstrap
661
662#
663# Install source tree
664#
665.PHONY: installsrc
666
667
668ifeq ($(CREATE_BRANCH_PATCH),)
669installsrc:
670	@echo INSTALLSRC $(SRCROOT)
671	$(_v)$(MKDIR) $(SRCROOT)
672	$(_v)$(FIND) -x . \! \( \( -name BUILD -o -name .svn -o -name .git -o -name cscope.\* -o -name compile_commands.json -o -name \*~ \) -prune \) -print0 | $(PAX) -rw -p a -d0 $(SRCROOT)
673	$(_v)$(CHMOD) -R go+rX $(SRCROOT)
674endif
675
676###############
677# Open source #
678###############
679
680
681########
682# Misc #
683########
684
685#
686# Clean up source tree
687#
688.PHONY: clean
689
690CLEAN_RM_DIRS= $(OBJROOT) $(SYMROOT) $(DSTROOT) \
691		$(SRCROOT)/tools/test/BUILD \
692		$(SRCROOT)/tools/tests/darwintests/build \
693		$(SRCROOT)/tools/tests/testkext/build \
694		$(SRCROOT)/libkdd/build \
695		$(SRCROOT)/tools/tests/unit_tests/BUILD \
696		$(SRCROOT)/tools/tests/execperf/BUILD \
697		$(SRCROOT)/tools/tests/packetdrill/BUILD \
698		$(SRCROOT)/tools/tests/perf_index/BUILD
699
700CLEAN_ACTION_DIRS= $(SRCROOT)/tools/tests/MPMMTest \
701		$(SRCROOT)/tools/tests/TLBcoherency \
702		$(SRCROOT)/tools/tests/kqueue_tests \
703		$(SRCROOT)/tools/tests/mktimer \
704		$(SRCROOT)/tools/tests/zero-to-n \
705		$(SRCROOT)/tools/tests/personas
706
707clean:
708	@:
709	$(_v)rm -f cscope.* 2> /dev/null
710	$(_v)rm -f TAGS 2> /dev/null
711	$(_v)for cdir in $(CLEAN_RM_DIRS); do \
712		if [ -d $${cdir} ] ; then \
713			echo "Removing $${cdir}"; \
714			rm -fr $${cdir} 2> /dev/null  || true ; \
715		fi ; \
716	done
717
718	$(_v)for mcdir in $(CLEAN_ACTION_DIRS); do \
719		make -C $${mcdir} clean; \
720	done
721
722
723#
724# Build source file list for cscope database and tags
725#
726.PHONY: cscope.files
727
728cscope.files:
729	@echo "Building file list for cscope and tags"
730	@find . -name '*.h' -type f | grep -v ^..BUILD > _cscope.files 2> /dev/null
731	@find . -name '*.defs' -type f | grep -v ^..BUILD >> _cscope.files 2> /dev/null
732	@find . -name '*.c' -type f | grep -v ^..BUILD >> _cscope.files 2> /dev/null
733	@find . -name '*.cpp' -type f | grep -v ^..BUILD >> _cscope.files 2> /dev/null
734	@find . -name '*.s' -type f | grep -v ^..BUILD >> _cscope.files 2> /dev/null
735	@find . -name '*.h.template' -type f | grep -v ^..BUILD >> _cscope.files 2> /dev/null
736	@cat $(OBJROOT)/cscope.genhdrs/* >> _cscope.files 2> /dev/null || true
737	@echo -k -q -c > cscope.files 2> /dev/null
738	@sort -u < _cscope.files >> cscope.files 2> /dev/null
739	@rm -f _cscope.files _cscope.files2 2> /dev/null
740
741#
742# Build cscope database
743#
744cscope: cscope.files
745	@echo "Building cscope database"
746	@cscope -bvU 2> /dev/null
747
748#
749# Build tags
750#
751tags: cscope.files
752	@echo "Building ctags"
753	@-sed 1d cscope.files | xargs ctags -dtw 2> /dev/null || \
754		echo "Phantom files detected!" 2>&1 > /dev/null
755	@-[ -f TAGS ] || ${MAKE} -f $(firstword $(MAKEFILE_LIST)) TAGS
756
757TAGS: cscope.files
758	@echo "Building etags"
759	@-cat cscope.files | etags -l auto -S - 2> /dev/null
760	@rm -f cscope.files 2> /dev/null
761
762
763.PHONY: help
764
765help:
766	@cat README.md
767
768.PHONY: print_exports
769
770print_exports:
771	$(_v)printenv | sort
772
773generated_top_level_print_exports = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,print_exports,,,,1,$(FIRST_BUILD_CONFIG))
774ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
775$(warning Generate makefile fragment: $(generated_top_level_print_exports))
776endif
777$(eval $(generated_top_level_print_exports))
778
779.PHONY: print_exports_first_build_config
780
781print_exports_first_build_config: print_exports_bootstrap
782
783
784# vim: set ft=make:
785