xref: /xnu-8796.101.5/makedefs/MakeInc.cmd (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
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
243ifeq ($(origin CTFDUMP),undefined)
244	export CTFDUMP := $(shell $(XCRUN) -sdk $(SDKROOT) -find ctfdump 2> /dev/null)
245endif
246
247#
248# Platform options
249#
250SUPPORTED_EMBEDDED_PLATFORMS := iPhoneOS iPhoneOSNano tvOS AppleTVOS WatchOS BridgeOS
251SUPPORTED_SIMULATOR_PLATFORMS := iPhoneSimulator iPhoneNanoSimulator tvSimulator AppleTVSimulator WatchSimulator
252
253
254SUPPORTED_PLATFORMS := MacOSX DriverKit $(SUPPORTED_SIMULATOR_PLATFORMS) $(SUPPORTED_EMBEDDED_PLATFORMS)
255
256# Platform-specific tools
257EDM_DBPATH ?= $(PLATFORMPATH)/usr/local/standalone/firmware/device_map.db
258
259# Scripts or tools we build ourselves
260#
261# setsegname - Rename segments in a Mach-O object file
262# kextsymboltool - Create kext pseudo-kext Mach-O kexts binaries
263# decomment - Strip out comments to detect whether a file is comments-only
264# installfile - Atomically copy files, esp. when multiple architectures
265#               are trying to install the same target header
266# replacecontents - Write contents to a file and update modtime *only* if
267#               contents differ
268#
269SEG_HACK = $(OBJROOT)/SETUP/setsegname/setsegname
270KEXT_CREATE_SYMBOL_SET = $(OBJROOT)/SETUP/kextsymboltool/kextsymboltool
271DECOMMENT = $(OBJROOT)/SETUP/decomment/decomment
272NEWVERS = $(SRCROOT)/config/newvers.pl
273INSTALL = $(OBJROOT)/SETUP/installfile/installfile
274REPLACECONTENTS = $(OBJROOT)/SETUP/replacecontents/replacecontents
275JSONCOMPILATIONDB = $(OBJROOT)/SETUP/json_compilation_db/json_compilation_db
276
277# Standard BSD tools
278RM = /bin/rm -f
279RMDIR = /bin/rmdir
280CP = /bin/cp
281MV = /bin/mv
282LN = /bin/ln -fs
283CAT = /bin/cat
284MKDIR = /bin/mkdir -p
285CHMOD = /bin/chmod
286FIND = /usr/bin/find
287XARGS = /usr/bin/xargs
288PAX = /bin/pax
289BASENAME = /usr/bin/basename
290DIRNAME = /usr/bin/dirname
291TR = /usr/bin/tr
292TOUCH = /usr/bin/touch
293SLEEP = /bin/sleep
294AWK = /usr/bin/awk
295SED = /usr/bin/sed
296PLUTIL = /usr/bin/plutil
297PATCH = /usr/bin/patch
298GREP = /usr/bin/grep
299
300#
301# Command to generate host binaries. Intentionally not
302# $(CC), which controls the target compiler
303#
304ifeq ($(origin HOST_OS_VERSION),undefined)
305	export HOST_OS_VERSION	:= $(shell sw_vers -productVersion)
306endif
307ifeq ($(origin HOST_CC),undefined)
308	export HOST_CC		:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find clang)
309endif
310ifeq ($(origin HOST_FLEX),undefined)
311	export HOST_FLEX	:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find flex)
312endif
313ifeq ($(origin HOST_BISON),undefined)
314	export HOST_BISON	:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find bison)
315endif
316ifeq ($(origin HOST_GM4),undefined)
317	export HOST_GM4		:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find gm4)
318endif
319ifeq ($(origin HOST_CODESIGN),undefined)
320	export HOST_CODESIGN	:= /usr/bin/codesign
321endif
322ifeq ($(origin HOST_CODESIGN_ALLOCATE),undefined)
323	export HOST_CODESIGN_ALLOCATE	:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find codesign_allocate)
324endif
325
326#
327# The following variables are functions invoked with "call", and thus
328# behave similarly to externally compiled commands
329#
330
331# $(1) is an expanded kernel config from a TARGET_CONFIGS_UC tuple
332# $(2) is an expanded arch config from a TARGET_CONFIGS_UC tuple
333# $(3) is an expanded machine config from a TARGET_CONFIGS_UC tuple
334_function_create_build_configs_join = $(strip $(1))^$(strip $(2))^$(strip $(3))
335
336# $(1) is an un-expanded kernel config from a TARGET_CONFIGS_UC tuple
337# $(2) is an un-expanded arch config from a TARGET_CONFIGS_UC tuple
338# $(3) is an un-expanded machine config from a TARGET_CONFIGS_UC tuple
339_function_create_build_configs_do_expand =          $(call _function_create_build_configs_join, \
340							   $(if $(filter DEFAULT,$(1)), \
341								$(DEFAULT_KERNEL_CONFIG), \
342								$(1) \
343							    ), \
344							   $(if $(filter DEFAULT,$(2)), \
345								$(DEFAULT_ARCH_CONFIG), \
346								$(2) \
347							    ), \
348							   $(if $(filter DEFAULT,$(3)), \
349								$(if $(filter DEFAULT,$(2)), \
350								     $(DEFAULT_$(DEFAULT_ARCH_CONFIG)_MACHINE_CONFIG), \
351								     $(DEFAULT_$(strip $(2))_MACHINE_CONFIG) \
352								), \
353								$(3) \
354							    ) \
355						     )
356
357# $(1) is an un-expanded TARGET_CONFIGS_UC list, which must be consumed
358#      3 elements at a time
359function_create_build_configs = $(sort \
360					$(strip \
361						 $(call _function_create_build_configs_do_expand, \
362							$(word 1,$(1)), \
363							$(word 2,$(1)), \
364							$(word 3,$(1)), \
365						  ) \
366						 $(if $(word 4,$(1)), \
367						      $(call function_create_build_configs, \
368							     $(wordlist 4,$(words $(1)),$(1)) \
369						       ), \
370						  ) \
371					  ) \
372				   )
373
374# Similar to build configs, but alias configs are a 4-tuple
375
376# $(1) is an expanded kernel config from a TARGET_CONFIGS_ALIASES_UC tuple
377# $(2) is an expanded arch config from a TARGET_CONFIGS_ALIASES_UC tuple
378# $(3) is an expanded kernel machine config from a TARGET_CONFIGS_ALIASES_UC tuple
379# $(4) is an expanded SoC platform config from a TARGET_CONFIGS_ALIASES_UC tuple,
380#      which should be an alias of $(3)
381_function_create_alias_configs_join = $(strip $(1))^$(strip $(2))^$(strip $(3))^$(strip $(4))
382
383_function_create_alias_configs_do_expand =	    $(call _function_create_alias_configs_join, \
384							   $(if $(filter DEFAULT,$(1)), \
385							        $(DEFAULT_KERNEL_CONFIG), \
386								$(1) \
387							    ), \
388							   $(if $(filter DEFAULT,$(2)), \
389								$(DEFAULT_ARCH_CONFIG), \
390								$(2) \
391							    ), \
392							   $(3), \
393							   $(4) \
394						     )
395
396function_create_alias_configs = $(sort \
397					$(strip \
398						 $(call _function_create_alias_configs_do_expand, \
399							$(word 1,$(1)), \
400							$(word 2,$(1)), \
401							$(word 3,$(1)), \
402							$(word 4,$(1)), \
403						  ) \
404						 $(if $(word 5,$(1)), \
405						      $(call function_create_alias_configs, \
406							     $(wordlist 5,$(words $(1)),$(1)) \
407						       ), \
408						  ) \
409					 ) \
410				 )
411
412# $(1) is a fully-expanded kernel config
413# $(2) is a fully-expanded arch config
414# $(3) is a fully-expanded machine config. "NONE" is not represented in the objdir path
415function_convert_target_config_uc_to_objdir = $(if $(filter NONE,$(3)),$(strip $(1))_$(strip $(2)),$(strip $(1))_$(strip $(2))_$(strip $(3)))
416
417# $(1) is a fully-expanded build config (like "RELEASE^X86_64^NONE")
418function_convert_build_config_to_objdir = $(call function_convert_target_config_uc_to_objdir, \
419						 $(word 1,$(subst ^, ,$(1))), \
420						 $(word 2,$(subst ^, ,$(1))), \
421						 $(word 3,$(subst ^, ,$(1))) \
422					   )
423
424# $(1) is a fully-expanded build config (like "RELEASE^X86_64^NONE")
425function_extract_kernel_config_from_build_config  = $(word 1,$(subst ^, ,$(1)))
426function_extract_arch_config_from_build_config    = $(word 2,$(subst ^, ,$(1)))
427function_extract_machine_config_from_build_config = $(word 3,$(subst ^, ,$(1)))
428
429#
430# Returns build config if both architecture and kernel configuration match.
431#
432#   $(1) - list of build configs
433#   $(1) - architecture
434#   $(2) - kernel configuration
435
436function_match_build_config_for_architecture_and_kernel_config = $(strip \
437			    $(foreach build_config, $(1), \
438			      $(if \
439				$(and \
440				  $(filter $(2), $(call function_extract_arch_config_from_build_config, $(build_config))), \
441				  $(filter $(3), $(call function_extract_kernel_config_from_build_config, $(build_config)))), \
442			      $(build_config), )))
443
444#
445# Returns build config if kernel configuration matches.
446#
447#   $(1) - list of build configs
448#   $(2) - kernel configuration
449
450function_match_build_config_for_kernel_config = $(strip \
451			    $(foreach build_config, $(1), \
452			      $(if \
453				  $(filter $(2), $(call function_extract_kernel_config_from_build_config, $(build_config))), \
454			      $(build_config), )))
455
456# $(1) is an input word
457# $(2) is a list of colon-separate potential substitutions like "FOO:BAR BAZ:QUX"
458# $(3) is a fallback if no substitutions were made
459function_substitute_word_with_replacement = $(strip $(if $(2),								\
460							 $(if $(filter $(word 1,$(subst :, ,$(word 1,$(2)))),$(1)),	\
461							      $(word 2,$(subst :, ,$(word 1,$(2)))),		\
462							      $(call function_substitute_word_with_replacement,$(1),$(wordlist 2,$(words $(2)),$(2)),$(3))), \
463							 $(3)								\
464						     )									\
465					     )
466
467# You can't assign a variable to an empty space without these
468# shenanigans
469empty :=
470space := $(empty) $(empty)
471
472# Arithmetic
473# $(1) is the number to increment
474NUM32 = 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
475increment = $(words x $(wordlist 1,$(1),$(NUM32)))
476decrement = $(words $(wordlist 2,$(1),$(NUM32)))
477
478# Create a sequence from 1 to $(1)
479# F(N) = if N > 0: return F(N-1) + "N" else: return ""
480sequence = $(if $(wordlist 1,$(1),$(NUM32)),$(call sequence,$(call decrement,$(1))) $(1),)
481
482# Reverse a list of words in $(1)
483reverse = $(if $(word 2,$(1)),$(call reverse,$(wordlist 2,$(words $(1)),$(1)))) $(word 1,$(1))
484
485# vim: set ft=make:
486