1# -*- mode: makefile;-*-
2#
3# Copyright (C) 1999-2012 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# Verbosity
15##
16ifeq ($(RC_XBS),YES)
17VERBOSE = YES
18else
19VERBOSE = NO
20endif
21ifeq ($(VERBOSE),YES)
22_v =
23_vstdout =
24else
25_v = @
26_vstdout = > /dev/null
27endif
28
29ifeq ($(VERBOSE),YES)
30	XCRUN = /usr/bin/xcrun -verbose
31else
32	XCRUN = /usr/bin/xcrun
33endif
34
35SDKROOT ?= /
36HOST_SDKROOT ?= macosx
37HOST_SPARSE_SDKROOT ?= /
38
39# SDKROOT may be passed as a shorthand like "iphoneos.internal". We
40# must resolve these to a full path and override SDKROOT.
41
42ifeq ($(SDKROOT_RESOLVED),)
43export SDKROOT_RESOLVED := $(shell $(XCRUN) -sdk $(SDKROOT) -show-sdk-path)
44ifeq ($(strip $(SDKROOT)_$(SDKROOT_RESOLVED)),/_)
45export SDKROOT_RESOLVED := /
46endif
47endif
48override SDKROOT = $(SDKROOT_RESOLVED)
49
50ifeq ($(HOST_SDKROOT_RESOLVED),)
51export HOST_SDKROOT_RESOLVED := $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -show-sdk-path)
52endif
53override HOST_SDKROOT = $(HOST_SDKROOT_RESOLVED)
54
55ifeq ($(PLATFORM),)
56	export PLATFORMPATH := $(shell $(XCRUN) -sdk $(SDKROOT) -show-sdk-platform-path)
57	export PLATFORM := $(shell echo $(PLATFORMPATH) | sed 's,^.*/\([^/]*\)\.platform$$,\1,')
58	ifeq ($(PLATFORM),)
59		export PLATFORM := MacOSX
60	endif
61endif
62
63ifeq ($(SDKVERSION),)
64     export SDKVERSION := $(shell $(XCRUN) -sdk $(SDKROOT) -show-sdk-version)
65endif
66
67ifeq ($(PLATFORM),iPhoneOS)
68	ifeq ($(HOST_SPARSE_SDKROOT),/)
69		export HOST_SPARSE_SDKROOT := $(shell $(XCRUN) -sdk iphonehost.internal -show-sdk-path)
70	endif
71endif
72
73# CC/CXX get defined by make(1) by default, so we can't check them
74# against the empty string to see if they haven't been set
75ifeq ($(origin CC),default)
76	export CC := $(shell $(XCRUN) -sdk $(SDKROOT) -find clang)
77endif
78ifeq ($(origin CXX),default)
79	export CXX := $(shell $(XCRUN) -sdk $(SDKROOT) -find clang++)
80endif
81ifeq ($(MIG),)
82	export MIG := $(shell $(XCRUN) -sdk $(SDKROOT) -find mig)
83endif
84ifeq ($(MIGCOM),)
85	export MIGCOM := $(shell $(XCRUN) -sdk $(SDKROOT) -find migcom)
86endif
87ifeq ($(MIGCC),)
88	export MIGCC := $(CC)
89endif
90ifeq ($(STRIP),)
91	export STRIP := $(shell $(XCRUN) -sdk $(SDKROOT) -find strip)
92endif
93ifeq ($(LIPO),)
94	export LIPO := $(shell $(XCRUN) -sdk $(SDKROOT) -find lipo)
95endif
96ifeq ($(LIBTOOL),)
97	export LIBTOOL := $(shell $(XCRUN) -sdk $(SDKROOT) -find libtool)
98endif
99ifeq ($(NM),)
100	export NM := $(shell $(XCRUN) -sdk $(SDKROOT) -find nm)
101endif
102ifeq ($(UNIFDEF),)
103	export UNIFDEF := $(shell $(XCRUN) -sdk $(SDKROOT) -find unifdef)
104endif
105ifeq ($(DSYMUTIL),)
106	export DSYMUTIL := $(shell $(XCRUN) -sdk $(SDKROOT) -find dsymutil)
107endif
108ifeq ($(CTFCONVERT),)
109	export CTFCONVERT := $(shell $(XCRUN) -sdk $(SDKROOT) -find ctfconvert)
110endif
111ifeq ($(CTFMERGE),)
112	export CTFMERGE :=  $(shell $(XCRUN) -sdk $(SDKROOT) -find ctfmerge)
113endif
114ifeq ($(CTFINSERT),)
115	export CTFINSERT := $(shell $(XCRUN) -sdk $(SDKROOT) -find ctf_insert)
116endif
117ifeq ($(NMEDIT),)
118	export NMEDIT := $(shell $(XCRUN) -sdk $(SDKROOT) -find nmedit)
119endif
120
121# Platform-specific tools
122ifeq (iPhoneOS,$(PLATFORM))
123ifeq ($(EMBEDDED_DEVICE_MAP),)
124	export EMBEDDED_DEVICE_MAP := $(shell $(XCRUN) -sdk $(SDKROOT) -find embedded_device_map)
125endif
126EDM_DBPATH = $(PLATFORMPATH)/usr/local/standalone/firmware/device_map.db
127endif
128
129# Scripts or tools we build ourselves
130SEG_HACK = $(OBJROOT)/SETUP/setsegname/setsegname
131KEXT_CREATE_SYMBOL_SET = $(OBJROOT)/SETUP/kextsymboltool/kextsymboltool
132DECOMMENT = $(OBJROOT)/SETUP/decomment/decomment
133NEWVERS = $(SRCROOT)/config/newvers.pl
134INSTALL = $(OBJROOT)/SETUP/installfile/installfile
135
136# Standard BSD tools
137RM = /bin/rm -f
138RMDIR = /bin/rmdir
139CP = /bin/cp
140MV = /bin/mv
141LN = /bin/ln -fs
142CAT = /bin/cat
143MKDIR = /bin/mkdir -p
144FIND = /usr/bin/find
145XARGS = /usr/bin/xargs
146TAR = /usr/bin/gnutar
147BASENAME = /usr/bin/basename
148DIRNAME = /usr/bin/dirname
149TR = /usr/bin/tr
150TOUCH = /usr/bin/touch
151AWK = /usr/bin/awk
152SED = /usr/bin/sed
153ECHO = /bin/echo
154PLUTIL = /usr/bin/plutil
155
156#
157# Command to generate host binaries. Intentionally not
158# $(CC), which controls the target compiler
159#
160ifeq ($(HOST_OS_VERSION),)
161	export HOST_OS_VERSION	:= $(shell sw_vers -productVersion)
162endif
163ifeq ($(HOST_CC),)
164	export HOST_CC		:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find clang)
165endif
166ifeq ($(HOST_FLEX),)
167	export HOST_FLEX	:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find flex)
168endif
169ifeq ($(HOST_BISON),)
170	export HOST_BISON	:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find bison)
171endif
172ifeq ($(HOST_GM4),)
173	export HOST_GM4		:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find gm4)
174endif
175ifeq ($(HOST_CODESIGN),)
176	export HOST_CODESIGN	:= /usr/bin/codesign
177endif
178ifeq ($(HOST_CODESIGN_ALLOCATE),)
179	export HOST_CODESIGN_ALLOCATE	:= $(shell $(XCRUN) -sdk $(HOST_SDKROOT) -find codesign_allocate)
180endif
181
182#
183# The following variables are functions invoked with "call", and thus
184# behave similarly to externally compiled commands
185#
186
187# $(1) is an expanded kernel config from a TARGET_CONFIGS_UC tuple
188# $(2) is an expanded arch config from a TARGET_CONFIGS_UC tuple
189# $(3) is an expanded machine config from a TARGET_CONFIGS_UC tuple
190_function_create_build_configs_join = $(strip $(1))^$(strip $(2))^$(strip $(3))
191
192# $(1) is an un-expanded kernel config from a TARGET_CONFIGS_UC tuple
193# $(2) is an un-expanded arch config from a TARGET_CONFIGS_UC tuple
194# $(3) is an un-expanded machine config from a TARGET_CONFIGS_UC tuple
195_function_create_build_configs_do_expand =          $(call _function_create_build_configs_join, \
196							   $(if $(filter DEFAULT,$(1)), \
197							   	$(DEFAULT_KERNEL_CONFIG), \
198								$(1) \
199							    ), \
200							   $(if $(filter DEFAULT,$(2)), \
201							   	$(DEFAULT_ARCH_CONFIG), \
202								$(2) \
203							    ), \
204							   $(if $(filter DEFAULT,$(3)), \
205							   	$(if $(filter DEFAULT,$(2)), \
206							   	     $(DEFAULT_$(DEFAULT_ARCH_CONFIG)_MACHINE_CONFIG), \
207								     $(DEFAULT_$(strip $(2))_MACHINE_CONFIG) \
208							    	), \
209								$(3) \
210							    ) \
211						     )
212
213# $(1) is an un-expanded TARGET_CONFIGS_UC list, which must be consumed
214#      3 elements at a time
215function_create_build_configs = $(sort \
216					$(strip \
217				       	 	 $(call _function_create_build_configs_do_expand, \
218						 	$(word 1,$(1)), \
219						 	$(word 2,$(1)), \
220						 	$(word 3,$(1)), \
221						  ) \
222						 $(if $(word 4,$(1)), \
223						      $(call function_create_build_configs, \
224							     $(wordlist 4,$(words $(1)),$(1)) \
225						       ), \
226						       \
227						  ) \
228					  ) \
229				   )
230
231# $(1) is a fully-expanded kernel config
232# $(2) is a fully-expanded arch config
233# $(3) is a fully-expanded machine config. "NONE" is not represented in the objdir path
234function_convert_target_config_uc_to_objdir = $(if $(filter NONE,$(3)),$(strip $(1))_$(strip $(2)),$(strip $(1))_$(strip $(2))_$(strip $(3)))
235
236# $(1) is a fully-expanded build config (like "RELEASE^X86_64^NONE")
237function_convert_build_config_to_objdir = $(call function_convert_target_config_uc_to_objdir, \
238					  	 $(word 1,$(subst ^, ,$(1))), \
239						 $(word 2,$(subst ^, ,$(1))), \
240						 $(word 3,$(subst ^, ,$(1))) \
241					   )
242
243# $(1) is a fully-expanded build config (like "RELEASE^X86_64^NONE")
244function_extract_kernel_config_from_build_config  = $(word 1,$(subst ^, ,$(1)))
245function_extract_arch_config_from_build_config    = $(word 2,$(subst ^, ,$(1)))
246function_extract_machine_config_from_build_config = $(word 3,$(subst ^, ,$(1)))
247
248# $(1) is an input word
249# $(2) is a list of colon-separate potential substitutions like "FOO:BAR BAZ:QUX"
250# $(3) is a fallback if no substitutions were made
251function_substitute_word_with_replacement = $(strip $(if $(2),								\
252							 $(if $(filter $(word 1,$(subst :, ,$(word 1,$(2)))),$(1)),	\
253							      $(word 2,$(subst :, ,$(word 1,$(2)))),		\
254							      $(call function_substitute_word_with_replacement,$(1),$(wordlist 2,$(words $(2)),$(2)),$(3))), \
255							 $(3)								\
256						     )									\
257					     )
258
259# You can't assign a variable to an empty space without these
260# shenanigans
261empty :=
262space := $(empty) $(empty)
263
264# Arithmetic
265# $(1) is the number to increment
266NUM16 = x x x x x x x x x x x x x x x x 
267increment = $(words x $(wordlist 1,$(1),$(NUM16)))
268
269# vim: set ft=make:
270