1# These are the functions which clang needs when it is targeting a previous
2# version of the OS. The issue is that the backend may use functions which were
3# not present in the libgcc that shipped on the platform. In such cases, we link
4# with a version of the library which contains private_extern definitions of all
5# the extra functions which might be referenced.
6
7Description := Static runtime libraries for clang/Darwin.
8
9# A function that ensures we don't try to build for architectures that we
10# don't have working toolchains for.
11CheckArches = \
12  $(shell \
13    result=""; \
14    for arch in $(1); do \
15      if $(CC) -arch $$arch -c \
16          -integrated-as \
17          $(ProjSrcRoot)/make/platform/clang_darwin_test_input.c \
18          -isysroot $(ProjSrcRoot)/SDKs/darwin \
19          -o /dev/null > /dev/null 2> /dev/null; then \
20        if $(LD) -v 2>&1 | grep "configured to support" \
21           | tr ' ' '\n' | grep "^$$arch$$" >/dev/null 2>/dev/null; then \
22          result="$$result$$arch "; \
23        else \
24          printf 1>&2 \
25            "warning: clang_darwin.mk: dropping arch '$$arch' from lib '$(2)'"; \
26          printf 1>&2 " (ld does not support it)\n"; \
27        fi; \
28      else \
29        printf 1>&2 \
30          "warning: clang_darwin.mk: dropping arch '$$arch' from lib '$(2)'"; \
31        printf 1>&2 " (clang does not support it)\n"; \
32      fi; \
33    done; \
34    echo $$result)
35
36XCRun = \
37  $(shell \
38    result=`xcrun -find $(1) 2> /dev/null`; \
39    if [ "$$?" != "0" ]; then result=$(1); fi; \
40    echo $$result)
41XCRunSdkPath = \
42  $(shell \
43    result=`xcrun --sdk $(1) --show-sdk-path 2> /dev/null`; \
44    if [ "$$?" != "0" ]; then result=""; fi; \
45    echo $$result)
46###
47
48CC       := $(call XCRun,clang)
49LD       := $(shell $(CC) -print-prog-name=ld)
50AR       := $(call XCRun,ar)
51RANLIB   := $(call XCRun,ranlib)
52STRIP    := $(call XCRun,strip)
53LIPO     := $(call XCRun,lipo)
54DSYMUTIL := $(call XCRun,dsymutil)
55
56Configs :=
57UniversalArchs :=
58
59# Configuration solely for providing access to an eprintf symbol, which may
60# still be referenced from Darwin system headers. This symbol is only ever
61# needed on i386.
62Configs += eprintf
63UniversalArchs.eprintf := $(call CheckArches,i386,eprintf)
64
65# Configuration for targeting 10.4. We need a few functions missing from
66# libgcc_s.10.4.dylib. We only build x86 slices since clang doesn't really
67# support targeting PowerPC.
68Configs += 10.4
69UniversalArchs.10.4 := $(call CheckArches,i386 x86_64,10.4)
70
71# Configuration for targeting iOS for a couple of functions that didn't
72# make it into libSystem.
73Configs += ios
74UniversalArchs.ios := $(call CheckArches,i386 x86_64 x86_64h armv7,ios)
75
76# Configuration for targeting OSX. These functions may not be in libSystem
77# so we should provide our own.
78Configs += osx
79UniversalArchs.osx := $(call CheckArches,i386 x86_64 x86_64h,osx)
80
81# Configuration for use with kernel/kexts.
82Configs += cc_kext
83UniversalArchs.cc_kext := $(call CheckArches,armv7 i386 x86_64 x86_64h,cc_kext)
84
85# Configuration for use with kernel/kexts for iOS 5.0 and earlier (which used 
86# a different code generation strategy).
87Configs += cc_kext_ios5
88UniversalArchs.cc_kext_ios5 := $(call CheckArches,x86_64 x86_64h armv7,cc_kext_ios5)
89
90# Configurations which define the profiling support functions.
91Configs += profile_osx
92UniversalArchs.profile_osx := $(call CheckArches,i386 x86_64 x86_64h,profile_osx)
93Configs += profile_ios
94UniversalArchs.profile_ios := $(call CheckArches,i386 x86_64 x86_64h armv7,profile_ios)
95
96# Configurations which define the ASAN support functions.
97Configs += asan_osx_dynamic
98UniversalArchs.asan_osx_dynamic := $(call CheckArches,i386 x86_64 x86_64h,asan_osx_dynamic)
99
100IOSSIM_SDK_PATH := $(call XCRunSdkPath,iphonesimulator)
101ifneq ($(IOSSIM_SDK_PATH),)
102Configs += asan_iossim_dynamic
103UniversalArchs.asan_iossim_dynamic := $(call CheckArches,i386 x86_64 x86_64h,asan_iossim_dynamic)
104endif
105
106Configs += ubsan_osx
107UniversalArchs.ubsan_osx := $(call CheckArches,i386 x86_64 x86_64h,ubsan_osx)
108
109# Darwin 10.6 has a bug in cctools that makes it unable to use ranlib on our ARM
110# object files. If we are on that platform, strip out all ARM archs. We still
111# build the libraries themselves so that Clang can find them where it expects
112# them, even though they might not have an expected slice.
113ifneq ($(shell test -x /usr/bin/sw_vers && sw_vers -productVersion | grep 10.6),)
114UniversalArchs.ios := $(filter-out armv7, $(UniversalArchs.ios))
115UniversalArchs.cc_kext := $(filter-out armv7, $(UniversalArchs.cc_kext))
116UniversalArchs.cc_kext_ios5 := $(filter-out armv7, $(UniversalArchs.cc_kext_ios5))
117UniversalArchs.profile_ios := $(filter-out armv7, $(UniversalArchs.profile_ios))
118endif
119
120# If RC_SUPPORTED_ARCHS is defined, treat it as a list of the architectures we
121# are intended to support and limit what we try to build to that.
122#
123# We make sure to remove empty configs if we end up dropping all the requested
124# archs for a particular config.
125ifneq ($(RC_SUPPORTED_ARCHS),)
126$(foreach config,$(Configs),\
127  $(call Set,UniversalArchs.$(config),\
128	$(filter $(RC_SUPPORTED_ARCHS),$(UniversalArchs.$(config))))\
129  $(if $(UniversalArchs.$(config)),,\
130	$(call Set,Configs,$(filter-out $(config),$(Configs)))))
131endif
132
133###
134
135# Forcibly strip off any -arch, as that totally breaks our universal support.
136override CC := $(subst -arch ,-arch_,$(CC))
137override CC := $(patsubst -arch_%,,$(CC))
138
139CFLAGS := -Wall -Werror -O3 -fomit-frame-pointer
140
141# Always set deployment target arguments for every build, these libraries should
142# never depend on the environmental overrides. We simply set them to minimum
143# supported deployment target -- nothing in the compiler-rt libraries should
144# actually depend on the deployment target.
145OSX_DEPLOYMENT_ARGS := -mmacosx-version-min=10.4
146IOS_DEPLOYMENT_ARGS := -mios-version-min=1.0
147IOS6_DEPLOYMENT_ARGS := -mios-version-min=6.0
148IOSSIM_DEPLOYMENT_ARGS := -mios-simulator-version-min=1.0
149
150# Use our stub SDK as the sysroot to support more portable building.
151OSX_DEPLOYMENT_ARGS += -isysroot $(ProjSrcRoot)/SDKs/darwin
152IOS_DEPLOYMENT_ARGS += -isysroot $(ProjSrcRoot)/SDKs/darwin
153IOS6_DEPLOYMENT_ARGS += -isysroot $(ProjSrcRoot)/SDKs/darwin
154IOSSIM_DEPLOYMENT_ARGS += -isysroot $(ProjSrcRoot)/SDKs/darwin
155
156CFLAGS.eprintf		:= $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
157CFLAGS.10.4		:= $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
158# FIXME: We can't build ASAN with our stub SDK yet.
159CFLAGS.asan_osx_dynamic := \
160	$(CFLAGS) -mmacosx-version-min=10.6 -fno-builtin \
161	-gline-tables-only \
162	-DMAC_INTERPOSE_FUNCTIONS=1
163
164CFLAGS.asan_iossim_dynamic := \
165	$(CFLAGS) -mios-simulator-version-min=7.0 \
166        -isysroot $(IOSSIM_SDK_PATH) \
167        -fno-builtin \
168	-gline-tables-only \
169	-DMAC_INTERPOSE_FUNCTIONS=1
170
171CFLAGS.ubsan_osx := $(CFLAGS) -mmacosx-version-min=10.6 -fno-builtin
172
173CFLAGS.ios.i386		:= $(CFLAGS) $(IOSSIM_DEPLOYMENT_ARGS)
174CFLAGS.ios.x86_64	:= $(CFLAGS) $(IOSSIM_DEPLOYMENT_ARGS)
175CFLAGS.ios.x86_64h	:= $(CFLAGS) $(IOSSIM_DEPLOYMENT_ARGS)
176CFLAGS.ios.armv7	:= $(CFLAGS) $(IOS_DEPLOYMENT_ARGS)
177CFLAGS.ios.armv7k	:= $(CFLAGS) $(IOS_DEPLOYMENT_ARGS)
178CFLAGS.ios.armv7s	:= $(CFLAGS) $(IOS_DEPLOYMENT_ARGS)
179CFLAGS.osx.i386		:= $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
180CFLAGS.osx.x86_64	:= $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
181CFLAGS.osx.x86_64h	:= $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
182CFLAGS.cc_kext.i386	:= $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
183CFLAGS.cc_kext.x86_64	:= $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
184CFLAGS.cc_kext.x86_64h	:= $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
185CFLAGS.cc_kext.armv7	:= $(CFLAGS) $(IOS6_DEPLOYMENT_ARGS)
186CFLAGS.cc_kext.armv7k	:= $(CFLAGS) $(IOS6_DEPLOYMENT_ARGS)
187CFLAGS.cc_kext.armv7s	:= $(CFLAGS) $(IOS6_DEPLOYMENT_ARGS)
188CFLAGS.cc_kext_ios5.armv7  := $(CFLAGS) $(IOS_DEPLOYMENT_ARGS)
189CFLAGS.cc_kext_ios5.armv7k := $(CFLAGS) $(IOS_DEPLOYMENT_ARGS)
190CFLAGS.cc_kext_ios5.armv7s := $(CFLAGS) $(IOS_DEPLOYMENT_ARGS)
191CFLAGS.profile_osx.i386    := $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
192CFLAGS.profile_osx.x86_64  := $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
193CFLAGS.profile_osx.x86_64h := $(CFLAGS) $(OSX_DEPLOYMENT_ARGS)
194CFLAGS.profile_ios.i386    := $(CFLAGS) $(IOSSIM_DEPLOYMENT_ARGS)
195CFLAGS.profile_ios.x86_64  := $(CFLAGS) $(IOSSIM_DEPLOYMENT_ARGS)
196CFLAGS.profile_ios.x86_64h := $(CFLAGS) $(IOSSIM_DEPLOYMENT_ARGS)
197CFLAGS.profile_ios.armv7  := $(CFLAGS) $(IOS_DEPLOYMENT_ARGS)
198CFLAGS.profile_ios.armv7k := $(CFLAGS) $(IOS_DEPLOYMENT_ARGS)
199CFLAGS.profile_ios.armv7s := $(CFLAGS) $(IOS_DEPLOYMENT_ARGS)
200
201# Configure the asan_osx_dynamic library to be built shared.
202SHARED_LIBRARY.asan_osx_dynamic := 1
203LDFLAGS.asan_osx_dynamic := -lstdc++ -undefined dynamic_lookup
204
205# Configure the asan_iossim_dynamic library to be built shared.
206SHARED_LIBRARY.asan_iossim_dynamic := 1
207# configure+make uses Clang, so we're using isysroot instead of --sysroot
208# or -Wl,-syslibroot.
209LDFLAGS.asan_iossim_dynamic := -undefined dynamic_lookup \
210  -Wl,-ios_simulator_version_min,7.0.0 \
211  -mios-simulator-version-min=7.0 -isysroot $(IOSSIM_SDK_PATH)
212
213FUNCTIONS.eprintf := eprintf
214FUNCTIONS.10.4 := eprintf floatundidf floatundisf floatundixf
215
216FUNCTIONS.ios	    := divmodsi4 udivmodsi4 mulosi4 mulodi4 muloti4
217# On x86, the divmod functions reference divsi.
218FUNCTIONS.ios.i386    := $(FUNCTIONS.ios) \
219                         divsi3 udivsi3
220FUNCTIONS.ios.x86_64  := $(FUNCTIONS.ios.i386)
221FUNCTIONS.ios.x86_64h := $(FUNCTIONS.ios.x86_64)
222
223FUNCTIONS.osx	:= mulosi4 mulodi4 muloti4
224
225FUNCTIONS.profile_osx := GCDAProfiling InstrProfiling InstrProfilingBuffer \
226                         InstrProfilingFile InstrProfilingPlatformDarwin \
227                         InstrProfilingRuntime
228FUNCTIONS.profile_ios := $(FUNCTIONS.profile_osx)
229
230FUNCTIONS.asan_osx_dynamic := $(AsanFunctions) $(AsanCXXFunctions) \
231                              $(InterceptionFunctions) \
232                              $(SanitizerCommonFunctions) \
233	                      $(AsanDynamicFunctions)
234
235FUNCTIONS.asan_iossim_dynamic := $(AsanFunctions) $(AsanCXXFunctions) \
236                                 $(InterceptionFunctions) \
237                                 $(SanitizerCommonFunctions) \
238	                         $(AsanDynamicFunctions)
239
240FUNCTIONS.ubsan_osx := $(UbsanFunctions) $(UbsanCXXFunctions) \
241                       $(SanitizerCommonFunctions)
242
243CCKEXT_COMMON_FUNCTIONS := \
244	absvdi2 \
245	absvsi2 \
246	addvdi3 \
247	addvsi3 \
248	ashldi3 \
249	ashrdi3 \
250	bswapdi2 \
251	bswapsi2 \
252	clzdi2 \
253	clzsi2 \
254	cmpdi2 \
255	ctzdi2 \
256	ctzsi2 \
257	divdc3 \
258	divdi3 \
259	divsc3 \
260	divmodsi4 \
261	udivmodsi4 \
262	do_global_dtors \
263	eprintf \
264	ffsdi2 \
265	fixdfdi \
266	fixsfdi \
267	fixunsdfdi \
268	fixunsdfsi \
269	fixunssfdi \
270	fixunssfsi \
271	floatdidf \
272	floatdisf \
273	floatundidf \
274	floatundisf \
275	gcc_bcmp \
276	lshrdi3 \
277	moddi3 \
278	muldc3 \
279	muldi3 \
280	mulsc3 \
281	mulvdi3 \
282	mulvsi3 \
283	negdi2 \
284	negvdi2 \
285	negvsi2 \
286	paritydi2 \
287	paritysi2 \
288	popcountdi2 \
289	popcountsi2 \
290	powidf2 \
291	powisf2 \
292	subvdi3 \
293	subvsi3 \
294	ucmpdi2 \
295	udiv_w_sdiv \
296	udivdi3 \
297	udivmoddi4 \
298	umoddi3
299
300CCKEXT_ARM_FUNCTIONS := $(CCKEXT_COMMON_FUNCTIONS) \
301	adddf3 \
302	addsf3 \
303	aeabi_cdcmpeq \
304	aeabi_cdrcmple \
305	aeabi_cfcmpeq \
306	aeabi_cfrcmple \
307	aeabi_dcmpeq \
308	aeabi_dcmpge \
309	aeabi_dcmpgt \
310	aeabi_dcmple \
311	aeabi_dcmplt \
312	aeabi_drsub \
313	aeabi_fcmpeq \
314	aeabi_fcmpge \
315	aeabi_fcmpgt \
316	aeabi_fcmple \
317	aeabi_fcmplt \
318	aeabi_frsub \
319	aeabi_idivmod \
320	aeabi_uidivmod \
321	cmpdf2 \
322	cmpsf2 \
323	div0 \
324	divdf3 \
325	divsf3 \
326	divsi3 \
327	extendsfdf2 \
328	ffssi2 \
329	fixdfsi \
330	fixsfsi \
331	floatsidf \
332	floatsisf \
333	floatunsidf \
334	floatunsisf \
335	comparedf2 \
336	comparesf2 \
337	modsi3 \
338	muldf3 \
339	mulsf3 \
340	negdf2 \
341	negsf2 \
342	subdf3 \
343	subsf3 \
344	switch16 \
345	switch32 \
346	switch8 \
347	switchu8 \
348	truncdfsf2 \
349	udivsi3 \
350	umodsi3 \
351	unorddf2 \
352	unordsf2
353
354CCKEXT_ARMVFP_FUNCTIONS := $(CCKEXT_ARM_FUNCTIONS) \
355	adddf3vfp \
356	addsf3vfp \
357	divdf3vfp \
358	divsf3vfp \
359	eqdf2vfp \
360	eqsf2vfp \
361	extendsfdf2vfp \
362	fixdfsivfp \
363	fixsfsivfp \
364	fixunsdfsivfp \
365	fixunssfsivfp \
366	floatsidfvfp \
367	floatsisfvfp \
368	floatunssidfvfp \
369	floatunssisfvfp \
370	gedf2vfp \
371	gesf2vfp \
372	gtdf2vfp \
373	gtsf2vfp \
374	ledf2vfp \
375	lesf2vfp \
376	ltdf2vfp \
377	ltsf2vfp \
378	muldf3vfp \
379	mulsf3vfp \
380	nedf2vfp \
381	nesf2vfp \
382	subdf3vfp \
383	subsf3vfp \
384	truncdfsf2vfp \
385	unorddf2vfp \
386	unordsf2vfp
387
388FUNCTIONS.cc_kext.armv7 := $(CCKEXT_ARMVFP_FUNCTIONS)
389FUNCTIONS.cc_kext.armv7k := $(CCKEXT_ARMVFP_FUNCTIONS)
390FUNCTIONS.cc_kext.armv7s := $(CCKEXT_ARMVFP_FUNCTIONS)
391FUNCTIONS.cc_kext_ios5.armv7 := $(CCKEXT_ARMVFP_FUNCTIONS)
392FUNCTIONS.cc_kext_ios5.armv7k := $(CCKEXT_ARMVFP_FUNCTIONS)
393FUNCTIONS.cc_kext_ios5.armv7s := $(CCKEXT_ARMVFP_FUNCTIONS)
394
395CCKEXT_X86_FUNCTIONS := $(CCKEXT_COMMON_FUNCTIONS) \
396	divxc3 \
397	fixunsxfdi \
398	fixunsxfsi \
399	fixxfdi \
400	floatdixf \
401	floatundixf \
402	mulxc3 \
403	powixf2
404
405FUNCTIONS.cc_kext.i386 := $(CCKEXT_X86_FUNCTIONS) \
406	ffssi2 \
407	i686.get_pc_thunk.eax \
408	i686.get_pc_thunk.ebp \
409	i686.get_pc_thunk.ebx \
410	i686.get_pc_thunk.ecx \
411	i686.get_pc_thunk.edi \
412	i686.get_pc_thunk.edx \
413	i686.get_pc_thunk.esi
414
415FUNCTIONS.cc_kext.x86_64 := $(CCKEXT_X86_FUNCTIONS) \
416	absvti2 \
417	addvti3 \
418	ashlti3 \
419	ashrti3 \
420	clzti2 \
421	cmpti2 \
422	ctzti2 \
423	divti3 \
424	ffsti2 \
425	fixdfti \
426	fixsfti \
427	fixunsdfti \
428	fixunssfti \
429	fixunsxfti \
430	fixxfti \
431	floattidf \
432	floattisf \
433	floattixf \
434	floatuntidf \
435	floatuntisf \
436	floatuntixf \
437	lshrti3 \
438	modti3 \
439	multi3 \
440	mulvti3 \
441	negti2 \
442	negvti2 \
443	parityti2 \
444	popcountti2 \
445	subvti3 \
446	ucmpti2 \
447	udivmodti4 \
448	udivti3 \
449	umodti3
450
451FUNCTIONS.cc_kext.x86_64h := $(FUNCTIONS.cc_kext.x86_64)
452
453# FIXME: Currently, compiler-rt is missing implementations for a number of the
454# functions that need to go into libcc_kext.a. Filter them out for now.
455CCKEXT_MISSING_FUNCTIONS := \
456	cmpdf2 cmpsf2 div0 \
457	ffssi2 \
458	udiv_w_sdiv unorddf2 unordsf2 bswapdi2 \
459	bswapsi2 \
460	gcc_bcmp \
461	do_global_dtors \
462	i686.get_pc_thunk.eax i686.get_pc_thunk.ebp i686.get_pc_thunk.ebx \
463	i686.get_pc_thunk.ecx i686.get_pc_thunk.edi i686.get_pc_thunk.edx \
464	i686.get_pc_thunk.esi \
465	aeabi_cdcmpeq aeabi_cdrcmple aeabi_cfcmpeq aeabi_cfrcmple aeabi_dcmpeq \
466	aeabi_dcmpge aeabi_dcmpgt aeabi_dcmple aeabi_dcmplt aeabi_drsub aeabi_fcmpeq \
467	aeabi_fcmpge aeabi_fcmpgt aeabi_fcmple aeabi_fcmplt aeabi_frsub aeabi_idivmod \
468	aeabi_uidivmod
469
470FUNCTIONS.cc_kext.armv7 := \
471	$(filter-out $(CCKEXT_MISSING_FUNCTIONS),$(FUNCTIONS.cc_kext.armv7))
472FUNCTIONS.cc_kext.armv7k := \
473	$(filter-out $(CCKEXT_MISSING_FUNCTIONS),$(FUNCTIONS.cc_kext.armv7k))
474FUNCTIONS.cc_kext.armv7s := \
475	$(filter-out $(CCKEXT_MISSING_FUNCTIONS),$(FUNCTIONS.cc_kext.armv7s))
476FUNCTIONS.cc_kext_ios5.armv7 := \
477	$(filter-out $(CCKEXT_MISSING_FUNCTIONS),$(FUNCTIONS.cc_kext_ios5.armv7))
478FUNCTIONS.cc_kext_ios5.armv7k := \
479	$(filter-out $(CCKEXT_MISSING_FUNCTIONS),$(FUNCTIONS.cc_kext_ios5.armv7k))
480FUNCTIONS.cc_kext_ios5.armv7s := \
481	$(filter-out $(CCKEXT_MISSING_FUNCTIONS),$(FUNCTIONS.cc_kext_ios5.armv7s))
482FUNCTIONS.cc_kext.i386 := \
483	$(filter-out $(CCKEXT_MISSING_FUNCTIONS),$(FUNCTIONS.cc_kext.i386))
484FUNCTIONS.cc_kext.x86_64 := \
485	$(filter-out $(CCKEXT_MISSING_FUNCTIONS),$(FUNCTIONS.cc_kext.x86_64))
486FUNCTIONS.cc_kext.x86_64h := \
487	$(filter-out $(CCKEXT_MISSING_FUNCTIONS),$(FUNCTIONS.cc_kext.x86_64h))
488
489KERNEL_USE.cc_kext := 1
490KERNEL_USE.cc_kext_ios5 := 1
491
492VISIBILITY_HIDDEN := 1
493
494SHARED_LIBRARY_SUFFIX := dylib
495