xref: /xnu-12377.81.4/makedefs/MakeInc.top (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
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", or potentially multiple "soc;platform" strings, if the product has multiple platforms.
167# $(1) is a product name in lower case
168function_lookup_product = $(call function_parse_product_configs,$(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 ifeq (xnu_headers_driverkit,$(RC_ProjectName))
207# generate TARGET_CONFIGS for all kernel configs for B&I
208TARGET_CONFIGS = DEVELOPMENT arm64 DEFAULT DEVELOPMENT X86_64 DEFAULT
209else ifneq ($(filter %_release_embedded,$(MAKECMDGOALS))$(filter %_release_embedded_nohdrs,$(MAKECMDGOALS)),)
210# generate TARGET_CONFIGS for RELEASE kernel configs and products in the device map
211TARGET_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))))
212TARGET_CONFIGS += $(EXTRA_TARGET_CONFIGS_RELEASE)
213TARGET_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))))
214else ifneq ($(filter %_development_embedded,$(MAKECMDGOALS)),)
215# generate TARGET_CONFIGS for DEVELOPMENT kernel configs and products in the device map
216TARGET_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))))
217TARGET_CONFIGS += $(EXTRA_TARGET_CONFIGS_DEVELOPMENT)
218TARGET_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))))
219else ifneq ($(filter %_embedded,$(MAKECMDGOALS)),)
220# generate TARGET_CONFIGS for all kernel configs and products in the device map
221TARGET_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))))
222TARGET_CONFIGS += $(foreach my_kernel_config,$(KERNEL_CONFIGS_EMBEDDED),$(EXTRA_TARGET_CONFIGS_$(my_kernel_config)))
223TARGET_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))))
224else ifneq ($(filter %_release_desktop,$(MAKECMDGOALS))$(filter %_release_desktop_nohdrs,$(MAKECMDGOALS)),)
225# generate TARGET_CONFIGS for B&I release builds
226TARGET_CONFIGS := $(foreach my_kern_config, RELEASE, $(foreach my_arch_config, $(ARCH_CONFIGS_DESKTOP), $(foreach my_machine_config, $(MACHINE_CONFIGS), $(my_kern_config) $(my_arch_config) $(my_machine_config))))
227TARGET_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,RELEASE,$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
228TARGET_CONFIGS += $(EXTRA_TARGET_CONFIGS_RELEASE)
229TARGET_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,RELEASE,$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
230else ifneq ($(filter %_development_desktop,$(MAKECMDGOALS)),)
231# generate TARGET_CONFIGS for B&I development builds
232TARGET_CONFIGS := $(foreach my_kern_config, DEVELOPMENT, $(foreach my_arch_config, $(ARCH_CONFIGS_DESKTOP), $(foreach my_machine_config, $(MACHINE_CONFIGS), $(my_kern_config) $(my_arch_config) $(my_machine_config))))
233TARGET_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,DEVELOPMENT,$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
234TARGET_CONFIGS += $(EXTRA_TARGET_CONFIGS_DEVELOPMENT)
235TARGET_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,DEVELOPMENT,$(my_kernel_config) $(subst ;, ,$(my_devicemap_config))))
236else ifneq ($(filter %_desktop,$(MAKECMDGOALS)),)
237# generate TARGET_CONFIGS for all kernel configs for B&I
238TARGET_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))))
239TARGET_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))))
240TARGET_CONFIGS += $(foreach my_kernel_config,$(KERNEL_CONFIGS_DESKTOP),$(EXTRA_TARGET_CONFIGS_$(my_kernel_config)))
241TARGET_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))))
242else
243# generate TARGET_CONFIGS using KERNEL_CONFIGS and ARCH_CONFIGS and MACHINE_CONFIGS (which defaults to "DEFAULT")
244TARGET_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))))
245TARGET_CONFIGS_ALIASES :=
246endif
247endif
248
249ifeq ($(TARGET_CONFIGS),)
250$(error No TARGET_CONFIGS specified)
251endif
252
253TARGET_CONFIGS_UC := $(strip $(shell printf "%s" "$(TARGET_CONFIGS)" | $(TR) a-z A-Z))
254TARGET_CONFIGS_ALIASES_UC := $(strip $(shell printf "%s" "$(TARGET_CONFIGS_ALIASES)" | $(TR) a-z A-Z))
255
256#
257# Build Configurations
258#
259# TARGET_CONFIGS is unwieldy for use in Makefiles. Convert them to
260# "build configurations" which are tuples joined by "^". For
261# example, "RELEASE I386 DEFAULT DEVELOPMENT ARM DEFAULT" becomes
262# "RELEASE^I386^NONE DEVELOPMENT^ARM^T8002", which can be looped
263# over trivially. PRIMARY_BUILD_CONFIGS is the first config
264# for each architecture, used primarily for machine-dependent recursion.
265# VARIANT_CONFIGS contains one build configuration for each combination
266# of kernel architecture and configuration. It is used for variant
267# installation recursion.
268
269BUILD_CONFIGS = $(call function_create_build_configs, $(TARGET_CONFIGS_UC))
270
271PRIMARY_ARCHS = $(strip $(sort $(foreach build_config, $(BUILD_CONFIGS), $(call function_extract_arch_config_from_build_config, $(build_config)))))
272PRIMARY_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), )))))
273NON_PRIMARY_BUILD_CONFIGS = $(strip $(filter-out $(PRIMARY_BUILD_CONFIGS), $(BUILD_CONFIGS)))
274FIRST_BUILD_CONFIG = $(firstword $(BUILD_CONFIGS))
275
276# Build list of all kernel configurations present in all build configs.
277PRIMARY_KERNEL_CONFIGS = $(strip $(sort $(foreach build_config, $(BUILD_CONFIGS), $(call function_extract_kernel_config_from_build_config, $(build_config)))))
278
279# Pick one build configuration for each kernel configuration.
280# This will be used to populate System.kext variants in SYMROOT / DSTROOT.
281VARIANT_CONFIGS = $(strip \
282                    $(foreach kernel_config, $(PRIMARY_KERNEL_CONFIGS), \
283                      $(firstword $(call function_match_build_config_for_kernel_config, $(BUILD_CONFIGS), $(kernel_config)))))
284
285ifneq ($(TARGET_CONFIGS_ALIASES_UC),)
286ALIAS_CONFIGS = $(call function_create_alias_configs, $(TARGET_CONFIGS_ALIASES_UC))
287else
288ALIAS_CONFIGS =
289endif
290
291# $(warning PRIMARY_ARCHS is $(PRIMARY_ARCHS))
292# $(warning TARGET_CONFIGS is $(TARGET_CONFIGS))
293# $(warning BUILD_CONFIGS is $(BUILD_CONFIGS))
294# $(warning PRIMARY_BUILD_CONFIGS is $(PRIMARY_BUILD_CONFIGS))
295# $(warning NON_PRIMARY_BUILD_CONFIGS is $(NON_PRIMARY_BUILD_CONFIGS))
296# $(warning TARGET_CONFIGS_ALIASES is $(TARGET_CONFIGS_ALIASES))
297# $(warning ALIAS_CONFIGS is $(ALIAS_CONFIGS))
298# $(warning PRIMARY_KERNEL_CONFIGS is $(PRIMARY_KERNEL_CONFIGS))
299# $(warning VARIANT_CONFIGS is $(VARIANT_CONFIGS))
300
301MEMORY_SIZE := $(shell /usr/sbin/sysctl -n hw.memsize)
302
303# Assume LTO scaling by default, unless it is being explicitly passed on the command-line
304LARGE_BUILD_FOOTPRINT := $(if $(BUILD_LTO),$(BUILD_LTO),1)
305
306ifeq ($(LARGE_BUILD_FOOTPRINT),1)
307RAM_PER_KERNEL_BUILD := 4294967296
308else
309RAM_PER_KERNEL_BUILD := 268435456
310endif
311
312KERNEL_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 )
313# $(warning Building $(KERNEL_BUILDS_IN_PARALLEL) kernels in parallel)
314
315#
316# TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template
317#
318# $(1) is the name of the makefile target to invoke for the each build config
319#      after setting up the parallel hierarchy in the TARGET directory
320# $(2) is an optional suffix on the TARGET directory, which might even be
321#      "/.."
322# $(3) are any dependencies for the bootstrap target
323# $(4) are any dependencies that are expanded per-build config to another bootstrap target
324# $(5) is how many build configurations to build in parallel
325# $(6) is which build configs to build in parallel
326#
327# Since building many configurations in parallel may overwhelm the system,
328# we try to throttle behavior into more managable S "stripes" of N/S
329# configurations by generating sequential dependencies between configs
330# in each stripe. That ensures that only S kernel builds are occurring
331# at once at any point in time
332
333define TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template
334
335# Create a list of synthesized targets for each build config
336$(1)_bootstrap_target_list = $$(addprefix $(1)_bootstrap_,$(6))
337
338.PHONY: $$($(1)_bootstrap_target_list)
339
340$(1)_generated_stripe_dependencies = $$(call _function_generate_stripe_groupings,$(1),$(5),$(call reverse,$(6)))
341ifeq ($$(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
342$$(warning Generate makefile fragment: $$($(1)_generated_stripe_dependencies))
343endif
344$$(eval $$($(1)_generated_stripe_dependencies))
345
346$$($(1)_bootstrap_target_list): $(1)_bootstrap_% : $(1)_stripe_dep_for_% $$(addsuffix _bootstrap_%,$(4)) $(3)
347	$$(_v)$$(MKDIR) $${OBJROOT}/$$(call function_convert_build_config_to_objdir,$$(patsubst $(1)_bootstrap_%,%,$$@))$(2)
348	$$(_v)$${MAKE}													        \
349		-C $${OBJROOT}/$$(call function_convert_build_config_to_objdir,$$(patsubst $(1)_bootstrap_%,%,$$@))$(2)		\
350		-f $${SRCROOT}/Makefile												\
351		CURRENT_KERNEL_CONFIG=$$(call function_extract_kernel_config_from_build_config,$$(patsubst $(1)_bootstrap_%,%,$$@)) \
352		CURRENT_ARCH_CONFIG=$$(call function_extract_arch_config_from_build_config,$$(patsubst $(1)_bootstrap_%,%,$$@))	  \
353		CURRENT_MACHINE_CONFIG=$$(call function_extract_machine_config_from_build_config,$$(patsubst $(1)_bootstrap_%,%,$$@))	\
354		CURRENT_BUILD_CONFIG=$$(patsubst $(1)_bootstrap_%,%,$$@)							\
355		PRIMARY_BUILD_CONFIGS="$(PRIMARY_BUILD_CONFIGS)" BUILD_CONFIGS="$(BUILD_CONFIGS)"				\
356		SOURCE=$${SRCROOT}/												\
357		RELATIVE_SOURCE_PATH=.											\
358		TARGET=$${OBJROOT}/$$(call function_convert_build_config_to_objdir,$$(patsubst $(1)_bootstrap_%,%,$$@))$(2)/	\
359		OBJPATH=$${OBJROOT}/$$(call function_convert_build_config_to_objdir,$$(patsubst $(1)_bootstrap_%,%,$$@))	\
360		$(1)
361
362.PHONY: $(1)_bootstrap
363
364$(1)_bootstrap: $$($(1)_bootstrap_target_list)
365endef
366
367#
368# TOP_LEVEL_STRIPE_DEPENDENCY_template
369#
370# $(1) is the Makefile target we are building for
371# $(2) is the build config that must build first
372# $(3) is the build config that must build after $(2)
373
374define TOP_LEVEL_STRIPE_DEPENDENCY_template
375
376.PHONY: $(1)_stripe_dep_for_$(3)
377
378 $(1)_stripe_dep_for_$(3): $(if $(2),$(1)_bootstrap_$(2))
379
380endef
381
382# $(1) is the Makefile target we are building for
383# $(2) is the stripe size
384# $(3) is the list of the build configs in the current group
385# $(4) is the list of remaining build configs
386_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))))
387
388# $(1) is the Makefile target we are building for
389# $(2) is the stripe size
390# $(3) is the list of the build configs
391_function_generate_stripe_groupings = $(call _function_generate_stripe_groupings_recursive,$(1),$(2),,$(3))
392
393#
394# Setup pass for build system tools
395#
396
397generated_top_level_build_setup = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_setup,/..,,,1,$(FIRST_BUILD_CONFIG))
398ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
399$(warning Generate makefile fragment: $(generated_top_level_build_setup))
400endif
401$(eval $(generated_top_level_build_setup))
402
403.PHONY: setup
404
405# invalidate current kernel in $(SYMROOT). Timestamp must be +1 from a previous kernel build
406setup: build_setup_bootstrap
407	$(_v)$(TOUCH) $(OBJROOT)/.mach_kernel.timestamp.new
408    # -nt and -ot are evaluated differently by bash, dash, and zsh (and are not part of the POSIX specification).
409    # Explicitly specify what should happen when the right hand file doesn't exist.
410	$(_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 \
411		$(SLEEP) 1;					\
412		$(TOUCH) $(OBJROOT)/.mach_kernel.timestamp.new;	\
413	done
414	$(_v)$(MV) $(OBJROOT)/.mach_kernel.timestamp.new $(OBJROOT)/.mach_kernel.timestamp
415	$(_v)$(TOUCH) $(OBJROOT)/.symbolset.timestamp.new
416	$(_v)while [ $(OBJROOT)/.symbolset.timestamp.new -ot $(OBJROOT)/.symbolset.timestamp ]; do \
417		$(SLEEP) 1;					\
418		$(TOUCH) $(OBJROOT)/.symbolset.timestamp.new;	\
419	done
420	$(_v)$(MV) $(OBJROOT)/.symbolset.timestamp.new $(OBJROOT)/.symbolset.timestamp
421
422#
423# Install kernel header files
424#
425.PHONY: exporthdrs exporthdrs_mi exporthdrs_md
426
427exporthdrs: exporthdrs_mi exporthdrs_md
428
429#
430# Install machine independent kernel header files
431#
432
433generated_top_level_build_exporthdrs_mi = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_exporthdrs_mi,,setup,,1,$(FIRST_BUILD_CONFIG))
434ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
435$(warning Generate makefile fragment: $(generated_top_level_build_exporthdrs_mi))
436endif
437$(eval $(generated_top_level_build_exporthdrs_mi))
438
439exporthdrs_mi: build_exporthdrs_mi_bootstrap
440
441#
442# Install machine dependent kernel header files
443#
444
445generated_top_level_build_exporthdrs_md = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_exporthdrs_md,,setup,,$(KERNEL_BUILDS_IN_PARALLEL),$(PRIMARY_BUILD_CONFIGS))
446ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
447$(warning Generate makefile fragment: $(generated_top_level_build_exporthdrs_md))
448endif
449$(eval $(generated_top_level_build_exporthdrs_md))
450
451exporthdrs_md: build_exporthdrs_md_bootstrap
452
453#
454# Install kernel header files
455#
456
457.PHONY: installhdrs installhdrs_mi installhdrs_md
458
459ifneq ($(filter $(RC_ProjectName),xnu_debug),)
460installhdrs:
461	@:
462else ifneq ($(filter $(RC_ProjectName),xnu_kasan),)
463installhdrs:
464	@:
465else
466
467installhdrs: installhdrs_mi installhdrs_md
468endif
469
470.PHONY: installhdrs_embedded installhdrs_release_embedded installhdrs_development_embedded
471.PHONY: installhdrs_desktop installhdrs_release_desktop installhdrs_development_desktop
472
473installhdrs_embedded installhdrs_release_embedded: installhdrs
474installhdrs_desktop installhdrs_release_desktop: installhdrs
475
476installhdrs_development_embedded:
477installhdrs_development_desktop:
478
479#
480# Install machine independent header files
481#
482
483generated_top_level_build_installhdrs_mi = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_installhdrs_mi,,setup,build_exporthdrs_mi,1,$(FIRST_BUILD_CONFIG))
484ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
485$(warning Generate makefile fragment: $(generated_top_level_build_installhdrs_mi))
486endif
487$(eval $(generated_top_level_build_installhdrs_mi))
488
489installhdrs_mi: build_installhdrs_mi_bootstrap
490
491#
492# Install machine dependent kernel header files
493#
494
495generated_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))
496ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
497$(warning Generate makefile fragment: $(generated_top_level_build_installhdrs_md))
498endif
499$(eval $(generated_top_level_build_installhdrs_md))
500
501installhdrs_md: build_installhdrs_md_bootstrap
502
503.PHONY: install_textfiles install_textfiles_mi install_textfiles_md
504
505install_textfiles: install_textfiles_mi install_textfiles_md
506
507#
508#  Install machine independent text files (man pages, dtrace scripts, etc.)
509#
510
511generated_top_level_textfiles_install_mi = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,textfiles_install_mi,,setup,,1,$(FIRST_BUILD_CONFIG))
512ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
513$(warning Generate makefile fragment: $(generated_top_level_textfiles_install_mi))
514endif
515$(eval $(generated_top_level_textfiles_install_mi))
516
517install_textfiles_mi: textfiles_install_mi_bootstrap
518
519#
520#  Install machine dependent text files (man pages, dtrace scripts, etc.)
521#
522
523generated_top_level_textfiles_install_md = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,textfiles_install_md,,setup,,$(KERNEL_BUILDS_IN_PARALLEL),$(PRIMARY_BUILD_CONFIGS))
524ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
525$(warning Generate makefile fragment: $(generated_top_level_textfiles_install_md))
526endif
527$(eval $(generated_top_level_textfiles_install_md))
528
529install_textfiles_md: textfiles_install_md_bootstrap
530
531#
532# Build all architectures for all Configuration/Architecture options
533#
534
535generated_top_level_build_all = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_all,,setup exporthdrs,,$(KERNEL_BUILDS_IN_PARALLEL),$(BUILD_CONFIGS))
536ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
537$(warning Generate makefile fragment: $(generated_top_level_build_all))
538endif
539$(eval $(generated_top_level_build_all))
540
541.PHONY: build
542
543build: build_all_bootstrap
544
545#
546# Post-process build results
547#
548
549generated_top_level_config_all = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,config_all,,setup,build_all,$(KERNEL_BUILDS_IN_PARALLEL),$(BUILD_CONFIGS))
550ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
551$(warning Generate makefile fragment: $(generated_top_level_config_all))
552endif
553$(eval $(generated_top_level_config_all))
554
555.PHONY: all config
556
557all config: config_all_bootstrap
558
559.PHONY: all_embedded all_release_embedded all_development_embedded
560.PHONY: all_desktop all_release_desktop all_development_desktop
561
562all_embedded all_release_embedded all_development_embedded: all
563all_desktop all_release_desktop all_development_desktop: all
564
565#
566# Install kernel files
567#
568
569generated_top_level_build_install_primary = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,build_install_primary,,setup,config_all,1,$(PRIMARY_BUILD_CONFIGS))
570ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
571$(warning Generate makefile fragment: $(generated_top_level_build_install_primary))
572endif
573$(eval $(generated_top_level_build_install_primary))
574
575.PHONY: install_primary
576
577install_primary: build_install_primary_bootstrap
578
579generated_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))
580ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
581$(warning Generate makefile fragment: $(generated_top_level_build_install_non_primary))
582endif
583$(eval $(generated_top_level_build_install_non_primary))
584
585.PHONY: install_non_primary
586
587install_non_primary: build_install_non_primary_bootstrap
588
589generated_top_level_config_install_primary = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,config_install_primary,,setup,config_all,1,$(PRIMARY_BUILD_CONFIGS))
590ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
591$(warning Generate makefile fragment: $(generated_top_level_config_install_primary))
592endif
593$(eval $(generated_top_level_config_install_primary))
594
595.PHONY: install_config_primary
596
597install_config_primary: config_install_primary_bootstrap
598
599# config_install_variant iterates through products from all BUILD_CONFIGS so depends on config_all having completed overall
600generated_top_level_config_install_variant = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,config_install_variant,,config_all_bootstrap,,1,$(VARIANT_CONFIGS))
601ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
602$(warning Generate makefile fragment: $(generated_top_level_config_install_variant))
603endif
604$(eval $(generated_top_level_config_install_variant))
605
606.PHONY: install_config_variant
607
608install_config_variant: config_install_variant_bootstrap
609
610# config_install iterates through products from all BUILD_CONFIGS so depends on config_all having completed overall
611generated_top_level_config_install = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,config_install,,config_all_bootstrap,,1,$(FIRST_BUILD_CONFIG))
612ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
613$(warning Generate makefile fragment: $(generated_top_level_config_install))
614endif
615$(eval $(generated_top_level_config_install))
616
617.PHONY: install_config final_touch_config_timestamps
618
619install_config: config_install_bootstrap install_config_primary install_config_variant final_touch_config_timestamps
620
621
622# Tell the next build the latest timestamp of any potential file in DSTROOT/SYMROOT
623final_touch_config_timestamps: config_install_bootstrap install_config_primary install_config_variant
624	$(_v)$(TOUCH) $(OBJROOT)/.symbolset.timestamp
625
626#
627# Aggregate install targets, which install everything appropriate for the current build alias/make target
628#
629
630.PHONY: install install_nohdrs
631
632ifneq ($(filter $(RC_ProjectName),xnu_debug),)
633install: install_kernels
634else ifneq ($(filter $(RC_ProjectName),xnu_kasan),)
635install: install_config install_kernels
636else ifneq ($(filter $(RC_ProjectName),xnu_headers_Sim),)
637install: installhdrs
638else ifneq ($(filter $(RC_ProjectName),xnu_headers_host),)
639install: installhdrs
640export INSTALLHDRS_SKIP_HOST=YES
641export INSTALLHDRS_MODULEMAPS_SKIP_HOST=YES
642else ifneq ($(filter $(RC_ProjectName),xnu_headers_driverkit),)
643install: installhdrs_desktop
644else ifneq ($(filter $(RC_ProjectName),xnu_headers_exclavekit xnu_headers_exclavecore),)
645install: installhdrs_desktop
646export INSTALLHDRS_MODULEMAPS_SKIP_HOST=YES
647export INSTALLHDRS_SKIP_HOST=YES
648export EXPORTHDRS_SKIP_EXCLAVES=YES
649else
650
651install_nohdrs: install_textfiles install_config install_kernels install_aliases
652install: installhdrs install_nohdrs
653endif
654
655.PHONY: install_embedded install_release_embedded install_development_embedded install_release_embedded_nohdrs
656.PHONY: install_desktop install_release_desktop install_development_desktop install_release_desktop_nohdrs
657
658# By default, all kernel files, headers, text files, and pseudo-kexts are installed
659install_embedded install_release_embedded install_desktop install_release_desktop: install
660
661# These special configs only install the kernel files
662install_development_embedded install_development_desktop: install_kernels install_aliases
663
664# These install release kernels, text files, and pseudo-kexts, but no headers
665install_release_embedded_nohdrs install_release_desktop_nohdrs: install_nohdrs
666
667.PHONY: install_kernels final_touch_kernel_timestamps install_aliases
668
669install_kernels: build_install_primary_bootstrap build_install_non_primary_bootstrap final_touch_kernel_timestamps
670
671# Tell the next build the latest timestamp of any potential file in DSTROOT/SYMROOT
672final_touch_kernel_timestamps: build_install_primary_bootstrap build_install_non_primary_bootstrap
673	$(_v)$(TOUCH) $(OBJROOT)/.mach_kernel.timestamp
674	@echo "done building xnu"
675
676# Copy kernels that are aliases of another configuration
677generated_top_level_install_alias = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,install_alias,,install_kernels,,$(KERNEL_BUILDS_IN_PARALLEL),$(ALIAS_CONFIGS))
678ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
679$(warning Generate makefile fragment: $(generated_top_level_install_alias))
680endif
681$(eval $(generated_top_level_install_alias))
682
683install_aliases: install_alias_bootstrap
684
685#
686# Install source tree
687#
688.PHONY: installsrc
689
690
691ifeq ($(CREATE_BRANCH_PATCH),)
692installsrc:
693	@echo INSTALLSRC $(SRCROOT)
694	$(_v)$(MKDIR) $(SRCROOT)
695	$(_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)
696	$(_v)$(CHMOD) -R go+rX $(SRCROOT)
697endif
698
699###############
700# Open source #
701###############
702
703
704########
705# Misc #
706########
707
708#
709# Clean up source tree
710#
711.PHONY: clean
712
713CLEAN_RM_DIRS= $(OBJROOT) $(SYMROOT) $(DSTROOT) \
714		$(SRCROOT)/tools/test/BUILD \
715		$(SRCROOT)/tools/tests/darwintests/build \
716		$(SRCROOT)/tools/tests/testkext/build \
717		$(SRCROOT)/libkdd/build \
718		$(SRCROOT)/tools/tests/unit_tests/BUILD \
719		$(SRCROOT)/tools/tests/execperf/BUILD \
720		$(SRCROOT)/tools/tests/packetdrill/BUILD \
721		$(SRCROOT)/tools/tests/perf_index/BUILD
722
723CLEAN_ACTION_DIRS= $(SRCROOT)/tools/tests/MPMMTest \
724		$(SRCROOT)/tools/tests/TLBcoherency \
725		$(SRCROOT)/tools/tests/kqueue_tests \
726		$(SRCROOT)/tools/tests/mktimer \
727		$(SRCROOT)/tools/tests/zero-to-n \
728		$(SRCROOT)/tools/tests/personas
729
730clean:
731	@:
732	$(_v)rm -f cscope.* 2> /dev/null
733	$(_v)rm -f TAGS 2> /dev/null
734	$(_v)for cdir in $(CLEAN_RM_DIRS); do \
735		if [ -d $${cdir} ] ; then \
736			echo "Removing $${cdir}"; \
737			rm -fr $${cdir} 2> /dev/null  || true ; \
738		fi ; \
739	done
740
741	$(_v)for mcdir in $(CLEAN_ACTION_DIRS); do \
742		make -C $${mcdir} clean; \
743	done
744
745
746#
747# Build source file list for cscope database and tags
748#
749.PHONY: cscope.files
750
751cscope.files:
752	@echo "Building file list for cscope and tags"
753	@find . -name '*.h' -type f | grep -v ^..BUILD > _cscope.files 2> /dev/null
754	@find . -name '*.defs' -type f | grep -v ^..BUILD >> _cscope.files 2> /dev/null
755	@find . -name '*.c' -type f | grep -v ^..BUILD >> _cscope.files 2> /dev/null
756	@find . -name '*.cpp' -type f | grep -v ^..BUILD >> _cscope.files 2> /dev/null
757	@find . -name '*.s' -type f | grep -v ^..BUILD >> _cscope.files 2> /dev/null
758	@find . -name '*.h.template' -type f | grep -v ^..BUILD >> _cscope.files 2> /dev/null
759	@cat $(OBJROOT)/cscope.genhdrs/* >> _cscope.files 2> /dev/null || true
760	@echo -k -q -c > cscope.files 2> /dev/null
761	@sort -u < _cscope.files >> cscope.files 2> /dev/null
762	@rm -f _cscope.files _cscope.files2 2> /dev/null
763
764#
765# Build cscope database
766#
767cscope: cscope.files
768	@echo "Building cscope database"
769	@cscope -bvU 2> /dev/null
770
771#
772# Build tags
773#
774tags: cscope.files
775	@echo "Building ctags"
776	@-sed 1d cscope.files | xargs ctags -dtw 2> /dev/null || \
777		echo "Phantom files detected!" 2>&1 > /dev/null
778	@-[ -f TAGS ] || ${MAKE} -f $(firstword $(MAKEFILE_LIST)) TAGS
779
780TAGS: cscope.files
781	@echo "Building etags"
782	@-cat cscope.files | etags -l auto -S - 2> /dev/null
783	@rm -f cscope.files 2> /dev/null
784
785
786.PHONY: help
787
788help:
789	@cat README.md
790
791.PHONY: print_exports
792
793print_exports:
794	$(_v)printenv | sort
795
796generated_top_level_print_exports = $(call TOP_LEVEL_EACH_BUILD_CONFIG_BOOTSTRAP_template,print_exports,,,,1,$(FIRST_BUILD_CONFIG))
797ifeq ($(VERBOSE_GENERATED_MAKE_FRAGMENTS),YES)
798$(warning Generate makefile fragment: $(generated_top_level_print_exports))
799endif
800$(eval $(generated_top_level_print_exports))
801
802.PHONY: print_exports_first_build_config
803
804print_exports_first_build_config: print_exports_bootstrap
805
806
807# vim: set ft=make:
808