xref: /xnu-8020.121.3/makedefs/MakeInc.cmd (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1# -*- mode: makefile;-*-
2#
3# Copyright (C) 1999-2020 Apple Inc. All rights reserved.
4#
5# MakeInc.cmd contains command paths for use during
6# the build, as well as make fragments and text
7# strings that may be evaluated as utility functions.
8#
9
10#
11# Commands for the build environment
12#
13
14#
15# Build Logging and Verbosity
16#
17
18ifeq ($(RC_XBS),YES)
19	VERBOSE = YES
20else
21	VERBOSE = NO
22endif
23
24BASH = bash
25
26ECHO = echo
27
28ERR = $(ECHO) > /dev/stderr
29PRINTF = printf
30
31QUIET ?= 0
32ifneq ($(QUIET),0)
33	PRINTF = printf > /dev/null
34	ifeq ($(VERBOSE),YES)
35		override VERBOSE = NO
36	endif
37endif
38
39# Helper functions for logging operations.
40LOG_PFX_LEN = 15
41LOG_PFX_LEN_ADJ = $(LOG_PFX_LEN)
42LOG = $(PRINTF) "$2%$4s$(Color0) $3%s$(Color0)\n" "$1"
43
44CONCISE ?= 0
45ifneq ($(CONCISE),0)
46	# Concise logging puts all logs on the same line (CSI K to clear and
47	# carriage return).
48	LOG = $(PRINTF) "$2%$4s$(Color0) $3%s$(Color0)\033[K\r" "$1"
49endif
50
51_LOG_COMP = $(call LOG,$1,$(ColorC),$(ColorF),$(LOG_PFX_LEN_ADJ))
52_LOG_HOST = $(call LOG,$1,$(ColorH),$(ColorF),$(LOG_PFX_LEN))
53_LOG_HOST_LINK = $(call LOG,$1,$(ColorH),$(ColorLF),$(LOG_PFX_LEN))
54
55# Special operations.
56LOG_LDFILELIST = $(call LOG,LDFILELIST,$(ColorL),$(ColorLF),$(LOG_PFX_LEN_ADJ))
57LOG_MIG = $(call LOG,MIG,$(ColorM),$(ColorF),$(LOG_PFX_LEN_ADJ))
58LOG_LD = $(call LOG,LD,$(ColorL),$(ColorF),$(LOG_PFX_LEN_ADJ))
59LOG_ALIGN = $(call LOG,--------->,$(Color0),$(Color0),$(LOG_PFX_LEN))
60
61# Compiling/machine-specific operations.
62LOG_CC = $(call _LOG_COMP,CC)
63LOG_CXX = $(call _LOG_COMP,C++)
64LOG_AS = $(call _LOG_COMP,AS)
65LOG_LTO = $(call _LOG_COMP,LTO)
66LOG_SYMBOLSET = $(call _LOG_COMP,SYMSET)
67LOG_SYMBOLSETPLIST = $(call _LOG_COMP,SYMSETPLIST)
68
69# Host-side operations.
70LOG_IIG = $(call _LOG_HOST,IIG)
71LOG_HOST_CC = $(call _LOG_HOST,CC)
72LOG_HOST_LD = $(call _LOG_HOST,LD)
73LOG_HOST_CODESIGN = $(call _LOG_HOST,CODESIGN)
74LOG_HOST_BISON = $(call _LOG_HOST,BISON)
75LOG_HOST_FLEX = $(call _LOG_HOST,FLEX)
76LOG_INSTALL = $(call _LOG_HOST,INSTALL)
77LOG_INSTALLVARIANT = $(call _LOG_HOST,INSTALLVARIANT)
78LOG_INSTALLSYM = $(call _LOG_HOST,INSTALLSYM)
79LOG_INSTALLHDR = $(call _LOG_HOST,INSTALLHDR)
80LOG_INSTALLMACROS = $(call _LOG_HOST,INSTALLMACROS)
81LOG_INSTALLPY = $(call _LOG_HOST,INSTALLPY)
82LOG_MAN = $(call _LOG_HOST,MAN)
83LOG_MANLINK = $(call _LOG_HOST,MANLINK)
84LOG_ALIAS = $(call _LOG_HOST,ALIAS)
85LOG_STRIP = $(call _LOG_HOST,STRIP)
86LOG_DSYMUTIL = $(call _LOG_HOST,DSYMUTIL)
87LOG_LIBTOOL = $(call _LOG_HOST,LIBTOOL)
88LOG_FILEPREP = $(call _LOG_HOST,FILEPREP)
89
90# Host-side linking operations.
91LOG_GENASSYM = $(call _LOG_HOST_LINK,GENASSYM)
92LOG_GENERATE= $(call _LOG_HOST_LINK,GENERATE)
93LOG_CTFCONVERT = $(call _LOG_HOST_LINK,CTFCONVERT)
94LOG_CTFMERGE = $(call _LOG_HOST_LINK,CTFMERGE)
95LOG_CTFINSERT = $(call _LOG_HOST_LINK,CTFINSERT)
96LOG_DSYMUTIL = $(call _LOG_HOST_LINK,DSYMUTIL)
97LOG_SUPPORTED_KPI = $(call _LOG_HOST_LINK,SUPPORTED_KPI)
98
99ifeq ($(VERBOSE),YES)
100	_v =
101	_vstdout =
102	_vstderr =
103	XCRUN = /usr/bin/xcrun -verbose
104else
105	_v = @
106	_vstdout = > /dev/null
107	_vstderr = 2&> /dev/null
108	XCRUN = /usr/bin/xcrun
109endif
110
111VERBOSE_GENERATED_MAKE_FRAGMENTS = NO
112
113#
114# Defaults
115#
116
117SDKROOT ?= macosx
118HOST_SDKROOT ?= macosx
119
120# SDKROOT may be passed as a shorthand like "iphoneos.internal". We
121# must resolve these to a full path and override SDKROOT.
122
123ifeq ($(origin SDKROOT_RESOLVED),undefined)
124export SDKROOT_RESOLVED := $(shell $(XCRUN) -sdk $(SDKROOT) -show-sdk-path)
125ifeq ($(strip $(SDKROOT)_$(SDKROOT_RESOLVED)),/_)
126export SDKROOT_RESOLVED := /
127endif
128endif
129override SDKROOT = $(SDKROOT_RESOLVED)
130
131ifeq ($(origin HOST_SDKROOT_RESOLVED),undefined)
132export HOST_SDKROOT_RESOLVED := $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -show-sdk-path)
133ifeq ($(strip $(HOST_SDKROOT_RESOLVED)),)
134export HOST_SDKROOT_RESOLVED := /
135endif
136endif
137override HOST_SDKROOT = $(HOST_SDKROOT_RESOLVED)
138
139ifeq ($(origin SDKVERSION),undefined)
140     export SDKVERSION := $(shell $(XCRUN) -sdk $(SDKROOT) -show-sdk-version)
141endif
142
143ifeq ($(origin PLATFORM),undefined)
144	export PLATFORMPATH := $(shell $(XCRUN) -sdk $(SDKROOT) -show-sdk-platform-path)
145	export PLATFORM := $(shell echo $(PLATFORMPATH) | sed 's,^.*/\([^/]*\)\.platform$$,\1,')
146	ifeq ($(PLATFORM),)
147		export PLATFORM := MacOSX
148	else ifeq ($(shell echo $(PLATFORM) | tr A-Z a-z),watchos)
149		export PLATFORM := WatchOS
150	endif
151endif
152
153ifeq ($(PLATFORM),DriverKit)
154	ifeq ($(origin COHORT_SDKROOT_RESOLVED),undefined)
155		SDK_NAME = $(notdir $(SDKROOT))
156		export COHORT_NAME := $(shell echo $(SDK_NAME) | sed -e 's|DriverKit.\([a-zA-Z]*\)$(SDKVERSION)\([.Internal]*\).sdk|\1\2|g' | tr A-Z a-z)
157		export COHORT_SDKROOT_RESOLVED := $(shell $(XCRUN) -sdk $(COHORT_NAME) -show-sdk-path)
158		ifeq ($(strip $(COHORT_SDKROOT_RESOLVED)),)
159		export COHORT_SDKROOT_RESOLVED := $(SDKROOT_RESOLVED)
160		endif
161	endif
162	override COHORT_SDKROOT = $(COHORT_SDKROOT_RESOLVED)
163	export PLATFORMPATH = $(shell $(XCRUN) -sdk $(COHORT_SDKROOT) -show-sdk-platform-path)
164	export PLATFORM := DriverKit
165	export DRIVERKIT ?= 1
166	export DRIVERKITROOT ?= /System/DriverKit
167	export DRIVERKITRUNTIMEROOT = $(DRIVERKITROOT)/Runtime
168else
169	override COHORT_SDKROOT = $(SDKROOT)
170endif
171
172ifeq ($(PLATFORM),MacOSX)
173	ifeq (DriverKit,$(shell echo $(SDKROOT_RESOLVED) | sed 's|^.*/\([^./1-9]*\)\(\.[^./1-9]*\)\{0,1\}[1-9][^/]*\.sdk$$|\1|'))
174		export PLATFORM := DriverKit
175		export DRIVERKIT ?= 1
176		export DRIVERKITROOT ?= /System/DriverKit
177		export DRIVERKITRUNTIMEROOT = $(DRIVERKITROOT)/Runtime
178	endif
179endif
180
181# CC/CXX get defined by make(1) by default, so we can't check them
182# against the empty string to see if they haven't been set
183ifeq ($(origin CC),default)
184	export CC := $(shell $(XCRUN) -sdk $(SDKROOT) -find clang)
185endif
186ifeq ($(origin CXX),default)
187	export CXX := $(shell $(XCRUN) -sdk $(SDKROOT) -find clang++)
188endif
189ifeq ($(origin MIG),undefined)
190	export MIG := $(shell $(XCRUN) -sdk $(SDKROOT) -find mig)
191endif
192ifeq ($(origin MIGCOM),undefined)
193	export MIGCOM := $(shell $(XCRUN) -sdk $(SDKROOT) -find migcom)
194endif
195ifeq ($(origin MIGCC),undefined)
196	export MIGCC := $(CC)
197endif
198ifeq ($(origin IIG),undefined)
199	export IIG := $(shell $(XCRUN) -sdk $(SDKROOT) -find iig)
200endif
201ifeq ($(origin STRIP),undefined)
202	export STRIP := $(shell $(XCRUN) -sdk $(SDKROOT) -find strip)
203endif
204ifeq ($(origin LIPO),undefined)
205	export LIPO := $(shell $(XCRUN) -sdk $(SDKROOT) -find lipo)
206endif
207ifeq ($(origin LIBTOOL),undefined)
208	export LIBTOOL := $(shell $(XCRUN) -sdk $(SDKROOT) -find libtool)
209endif
210ifeq ($(origin OTOOL),undefined)
211	export OTOOL := $(shell $(XCRUN) -sdk $(SDKROOT) -find otool)
212endif
213ifeq ($(origin NM),undefined)
214	export NM := $(shell $(XCRUN) -sdk $(SDKROOT) -find nm)
215endif
216ifeq ($(origin UNIFDEF),undefined)
217	export UNIFDEF := $(shell $(XCRUN) -sdk $(SDKROOT) -find unifdef)
218endif
219ifeq ($(origin DSYMUTIL),undefined)
220	export DSYMUTIL := $(shell $(XCRUN) -sdk $(SDKROOT) -find dsymutil)
221endif
222ifeq ($(origin NMEDIT),undefined)
223	export NMEDIT := $(shell $(XCRUN) -sdk $(SDKROOT) -find nmedit)
224endif
225ifeq ($(origin GIT),undefined)
226	export GIT := $(shell $(XCRUN) -sdk $(SDKROOT) -find git)
227endif
228ifeq ($(origin SCAN_BUILD),undefined)
229	export SCAN_BUILD := $(shell $(XCRUN) -sdk $(SDKROOT) -find scan-build 2> /dev/null)
230endif
231ifeq ($(origin CTFINSERT),undefined)
232	export CTFINSERT := $(shell $(XCRUN) -sdk $(SDKROOT) -find ctf_insert 2> /dev/null)
233endif
234ifeq ($(origin CTFCONVERT),undefined)
235	export CTFCONVERT := $(shell $(XCRUN) -sdk $(SDKROOT) -find ctfconvert 2> /dev/null)
236endif
237ifeq ($(origin CTFMERGE),undefined)
238	export CTFMERGE := $(shell $(XCRUN) -sdk $(SDKROOT) -find ctfmerge 2> /dev/null)
239	ifeq (,$(wildcard $(CTFMERGE)))
240		export DO_CTFMERGE := 0
241	endif
242endif
243
244#
245# Platform options
246#
247SUPPORTED_EMBEDDED_PLATFORMS := iPhoneOS iPhoneOSNano tvOS AppleTVOS WatchOS BridgeOS
248SUPPORTED_SIMULATOR_PLATFORMS := iPhoneSimulator iPhoneNanoSimulator tvSimulator AppleTVSimulator WatchSimulator
249
250
251SUPPORTED_PLATFORMS := MacOSX DriverKit $(SUPPORTED_SIMULATOR_PLATFORMS) $(SUPPORTED_EMBEDDED_PLATFORMS)
252
253# Platform-specific tools
254EDM_DBPATH ?= $(PLATFORMPATH)/usr/local/standalone/firmware/device_map.db
255
256# Scripts or tools we build ourselves
257#
258# setsegname - Rename segments in a Mach-O object file
259# kextsymboltool - Create kext pseudo-kext Mach-O kexts binaries
260# decomment - Strip out comments to detect whether a file is comments-only
261# installfile - Atomically copy files, esp. when multiple architectures
262#               are trying to install the same target header
263# replacecontents - Write contents to a file and update modtime *only* if
264#               contents differ
265#
266SEG_HACK = $(OBJROOT)/SETUP/setsegname/setsegname
267KEXT_CREATE_SYMBOL_SET = $(OBJROOT)/SETUP/kextsymboltool/kextsymboltool
268DECOMMENT = $(OBJROOT)/SETUP/decomment/decomment
269NEWVERS = $(SRCROOT)/config/newvers.pl
270INSTALL = $(OBJROOT)/SETUP/installfile/installfile
271REPLACECONTENTS = $(OBJROOT)/SETUP/replacecontents/replacecontents
272JSONCOMPILATIONDB = $(OBJROOT)/SETUP/json_compilation_db/json_compilation_db
273
274# Standard BSD tools
275RM = /bin/rm -f
276RMDIR = /bin/rmdir
277CP = /bin/cp
278MV = /bin/mv
279LN = /bin/ln -fs
280CAT = /bin/cat
281MKDIR = /bin/mkdir -p
282CHMOD = /bin/chmod
283FIND = /usr/bin/find
284XARGS = /usr/bin/xargs
285PAX = /bin/pax
286BASENAME = /usr/bin/basename
287DIRNAME = /usr/bin/dirname
288TR = /usr/bin/tr
289TOUCH = /usr/bin/touch
290SLEEP = /bin/sleep
291AWK = /usr/bin/awk
292SED = /usr/bin/sed
293PLUTIL = /usr/bin/plutil
294PATCH = /usr/bin/patch
295GREP = /usr/bin/grep
296
297#
298# Command to generate host binaries. Intentionally not
299# $(CC), which controls the target compiler
300#
301ifeq ($(origin HOST_OS_VERSION),undefined)
302	export HOST_OS_VERSION	:= $(shell sw_vers -productVersion)
303endif
304ifeq ($(origin HOST_CC),undefined)
305	export HOST_CC		:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find clang)
306endif
307ifeq ($(origin HOST_FLEX),undefined)
308	export HOST_FLEX	:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find flex)
309endif
310ifeq ($(origin HOST_BISON),undefined)
311	export HOST_BISON	:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find bison)
312endif
313ifeq ($(origin HOST_GM4),undefined)
314	export HOST_GM4		:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find gm4)
315endif
316ifeq ($(origin HOST_CODESIGN),undefined)
317	export HOST_CODESIGN	:= /usr/bin/codesign
318endif
319ifeq ($(origin HOST_CODESIGN_ALLOCATE),undefined)
320	export HOST_CODESIGN_ALLOCATE	:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find codesign_allocate)
321endif
322
323#
324# The following variables are functions invoked with "call", and thus
325# behave similarly to externally compiled commands
326#
327
328# $(1) is an expanded kernel config from a TARGET_CONFIGS_UC tuple
329# $(2) is an expanded arch config from a TARGET_CONFIGS_UC tuple
330# $(3) is an expanded machine config from a TARGET_CONFIGS_UC tuple
331_function_create_build_configs_join = $(strip $(1))^$(strip $(2))^$(strip $(3))
332
333# $(1) is an un-expanded kernel config from a TARGET_CONFIGS_UC tuple
334# $(2) is an un-expanded arch config from a TARGET_CONFIGS_UC tuple
335# $(3) is an un-expanded machine config from a TARGET_CONFIGS_UC tuple
336_function_create_build_configs_do_expand =          $(call _function_create_build_configs_join, \
337							   $(if $(filter DEFAULT,$(1)), \
338								$(DEFAULT_KERNEL_CONFIG), \
339								$(1) \
340							    ), \
341							   $(if $(filter DEFAULT,$(2)), \
342								$(DEFAULT_ARCH_CONFIG), \
343								$(2) \
344							    ), \
345							   $(if $(filter DEFAULT,$(3)), \
346								$(if $(filter DEFAULT,$(2)), \
347								     $(DEFAULT_$(DEFAULT_ARCH_CONFIG)_MACHINE_CONFIG), \
348								     $(DEFAULT_$(strip $(2))_MACHINE_CONFIG) \
349								), \
350								$(3) \
351							    ) \
352						     )
353
354# $(1) is an un-expanded TARGET_CONFIGS_UC list, which must be consumed
355#      3 elements at a time
356function_create_build_configs = $(sort \
357					$(strip \
358						 $(call _function_create_build_configs_do_expand, \
359							$(word 1,$(1)), \
360							$(word 2,$(1)), \
361							$(word 3,$(1)), \
362						  ) \
363						 $(if $(word 4,$(1)), \
364						      $(call function_create_build_configs, \
365							     $(wordlist 4,$(words $(1)),$(1)) \
366						       ), \
367						  ) \
368					  ) \
369				   )
370
371# Similar to build configs, but alias configs are a 4-tuple
372
373# $(1) is an expanded kernel config from a TARGET_CONFIGS_ALIASES_UC tuple
374# $(2) is an expanded arch config from a TARGET_CONFIGS_ALIASES_UC tuple
375# $(3) is an expanded kernel machine config from a TARGET_CONFIGS_ALIASES_UC tuple
376# $(4) is an expanded SoC platform config from a TARGET_CONFIGS_ALIASES_UC tuple,
377#      which should be an alias of $(3)
378_function_create_alias_configs_join = $(strip $(1))^$(strip $(2))^$(strip $(3))^$(strip $(4))
379
380_function_create_alias_configs_do_expand =	    $(call _function_create_alias_configs_join, \
381							   $(if $(filter DEFAULT,$(1)), \
382							        $(DEFAULT_KERNEL_CONFIG), \
383								$(1) \
384							    ), \
385							   $(if $(filter DEFAULT,$(2)), \
386								$(DEFAULT_ARCH_CONFIG), \
387								$(2) \
388							    ), \
389							   $(3), \
390							   $(4) \
391						     )
392
393function_create_alias_configs = $(sort \
394					$(strip \
395						 $(call _function_create_alias_configs_do_expand, \
396							$(word 1,$(1)), \
397							$(word 2,$(1)), \
398							$(word 3,$(1)), \
399							$(word 4,$(1)), \
400						  ) \
401						 $(if $(word 5,$(1)), \
402						      $(call function_create_alias_configs, \
403							     $(wordlist 5,$(words $(1)),$(1)) \
404						       ), \
405						  ) \
406					 ) \
407				 )
408
409# $(1) is a fully-expanded kernel config
410# $(2) is a fully-expanded arch config
411# $(3) is a fully-expanded machine config. "NONE" is not represented in the objdir path
412function_convert_target_config_uc_to_objdir = $(if $(filter NONE,$(3)),$(strip $(1))_$(strip $(2)),$(strip $(1))_$(strip $(2))_$(strip $(3)))
413
414# $(1) is a fully-expanded build config (like "RELEASE^X86_64^NONE")
415function_convert_build_config_to_objdir = $(call function_convert_target_config_uc_to_objdir, \
416						 $(word 1,$(subst ^, ,$(1))), \
417						 $(word 2,$(subst ^, ,$(1))), \
418						 $(word 3,$(subst ^, ,$(1))) \
419					   )
420
421# $(1) is a fully-expanded build config (like "RELEASE^X86_64^NONE")
422function_extract_kernel_config_from_build_config  = $(word 1,$(subst ^, ,$(1)))
423function_extract_arch_config_from_build_config    = $(word 2,$(subst ^, ,$(1)))
424function_extract_machine_config_from_build_config = $(word 3,$(subst ^, ,$(1)))
425
426#
427# Returns build config if both architecture and kernel configuration match.
428#
429#   $(1) - list of build configs
430#   $(1) - architecture
431#   $(2) - kernel configuration
432
433function_match_build_config_for_architecture_and_kernel_config = $(strip \
434			    $(foreach build_config, $(1), \
435			      $(if \
436				$(and \
437				  $(filter $(2), $(call function_extract_arch_config_from_build_config, $(build_config))), \
438				  $(filter $(3), $(call function_extract_kernel_config_from_build_config, $(build_config)))), \
439			      $(build_config), )))
440
441#
442# Returns build config if kernel configuration matches.
443#
444#   $(1) - list of build configs
445#   $(2) - kernel configuration
446
447function_match_build_config_for_kernel_config = $(strip \
448			    $(foreach build_config, $(1), \
449			      $(if \
450				  $(filter $(2), $(call function_extract_kernel_config_from_build_config, $(build_config))), \
451			      $(build_config), )))
452
453# $(1) is an input word
454# $(2) is a list of colon-separate potential substitutions like "FOO:BAR BAZ:QUX"
455# $(3) is a fallback if no substitutions were made
456function_substitute_word_with_replacement = $(strip $(if $(2),								\
457							 $(if $(filter $(word 1,$(subst :, ,$(word 1,$(2)))),$(1)),	\
458							      $(word 2,$(subst :, ,$(word 1,$(2)))),		\
459							      $(call function_substitute_word_with_replacement,$(1),$(wordlist 2,$(words $(2)),$(2)),$(3))), \
460							 $(3)								\
461						     )									\
462					     )
463
464# You can't assign a variable to an empty space without these
465# shenanigans
466empty :=
467space := $(empty) $(empty)
468
469# Arithmetic
470# $(1) is the number to increment
471NUM32 = x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
472increment = $(words x $(wordlist 1,$(1),$(NUM32)))
473decrement = $(words $(wordlist 2,$(1),$(NUM32)))
474
475# Create a sequence from 1 to $(1)
476# F(N) = if N > 0: return F(N-1) + "N" else: return ""
477sequence = $(if $(wordlist 1,$(1),$(NUM32)),$(call sequence,$(call decrement,$(1))) $(1),)
478
479# Reverse a list of words in $(1)
480reverse = $(if $(word 2,$(1)),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(word 1,$(1))
481
482# vim: set ft=make:
483