1##===- clang/runtime/compiler-rt/Makefile ------------------*- Makefile -*-===##
2#
3#                     The LLVM Compiler Infrastructure
4#
5# This file is distributed under the University of Illinois Open Source
6# License. See LICENSE.TXT for details.
7#
8##===----------------------------------------------------------------------===##
9#
10# This file defines support for building the Clang runtime libraries (which are
11# implemented by compiler-rt) and placing them in the proper locations in the
12# Clang resources directory (i.e., where the driver expects them).
13#
14##===----------------------------------------------------------------------===##
15
16CLANG_LEVEL := ../..
17include $(CLANG_LEVEL)/Makefile
18
19CLANG_VERSION := $(word 3,$(shell grep "CLANG_VERSION " \
20	$(PROJ_OBJ_DIR)/$(CLANG_LEVEL)/include/clang/Basic/Version.inc))
21
22ResourceDir := $(PROJ_OBJ_ROOT)/$(BuildMode)/lib/clang/$(CLANG_VERSION)
23PROJ_resources := $(DESTDIR)$(PROJ_prefix)/lib/clang/$(CLANG_VERSION)
24
25ResourceLibDir := $(ResourceDir)/lib
26ResourceIncludeDir := $(ResourceDir)/include
27PROJ_resources_lib := $(PROJ_resources)/lib
28PROJ_resources_include := $(PROJ_resources)/include
29
30# Initialize a variable to use for extra flags to pass to the
31# compiler-rt make process.
32COMPILERRT_MAKE_FLAGS :=
33
34# Expect compiler-rt to be in llvm/projects/compiler-rt
35COMPILERRT_SRC_ROOT := $(LLVM_SRC_ROOT)/projects/compiler-rt
36
37# We don't currently support building runtime libraries when we are
38# cross-compiling. The issue is that we really want to be set up so that the
39# available compiler targets are independent of the current build.
40#
41# Since we have to build the runtime libraries for the target, it requires we
42# have a cross compiler from the build machine to the target. Although in the
43# case where for the current build (host == target), we do have such a cross
44# compiler, but not defined in a way that is easy for us to reuse. Regardless,
45# that also wouldn't help for other possible compiler configurations.
46#
47# Thus, the simple set up we currently use is to assume that we will be using
48# the just built Clang to compile the compiler-rt libraries. As we grow better
49# cross compilation support inside Clang and tool support in LLVM, this makes it
50# easier for us to achieve the goal of having the compiler targets be easily
51# selected at configure time. However, this design does currently preclude the
52# building of compiler-rt libraries when the Clang itself is being cross
53# compiled.
54#
55# There are three possible solutions:
56#  1. Require building a build-target version of Clang when cross compiling. This
57#     is simplest, but als greatly increases the build time of cross builds.
58#
59#  2. Require cross builds have a build-target version of Clang available for
60#     use. This is a reasonable compromise on #1, as the compiler-rt libraries
61#     are simple enough that there is not a strong desire to ensure they are
62#     built with the exact version of Clang being used. Similarly, as Clang
63#     becomes a better cross compiler it is also increasingly more likely that
64#     the cross compiler being used will already be a version of Clang.
65#
66#  3. Come up with an alternate mechanism to define all the toolchain
67#     information that compiler-rt would need to build libraries for all the
68#     requested targets. This might be a simple short term solution, but is
69#     likely to be unwieldly and irritating to maintain in the long term.
70ifneq ($(LLVM_CROSS_COMPILING),1)
71ifneq ($(CLANG_NO_RUNTIME),1)
72ifeq ($(shell test -d $(COMPILERRT_SRC_ROOT) && echo OK),OK)
73
74# Select the compiler-rt configuration to use, and install directory.
75#
76# FIXME: Eventually, we want some kind of configure support for this. We want to
77# build/install runtime libraries for as many targets as clang was configured to
78# support.
79RuntimeDirs :=
80ifeq ($(OS),Darwin)
81RuntimeDirs += darwin darwin_embedded
82RuntimeLibrary.darwin.Configs := \
83	eprintf.a 10.4.a osx.a ios.a cc_kext.a cc_kext_ios5.a \
84	asan_osx_dynamic.dylib \
85	profile_osx.a profile_ios.a \
86	ubsan_osx.a
87RuntimeLibrary.darwin_embedded.Configs := \
88	soft_static.a hard_static.a soft_pic.a hard_pic.a
89
90# Support building compiler-rt with relocatable SDKs.
91#
92# This will cause make to put SDKROOT in the environment, and since we
93# are using the built Clang to build compiler-rt, it to pick up that
94# location as the default value for the include system root.
95ACTIVE_SDK_PATH := $(shell xcrun --show-sdk-path 2> /dev/null)
96ifneq ($(ACTIVE_SDK_PATH),)
97COMPILERRT_MAKE_FLAGS := SDKROOT=$(ACTIVE_SDK_PATH)
98endif
99IOSSIM_SDK_PATH := $(shell xcrun --show-sdk-path -sdk iphonesimulator 2> /dev/null)
100ifneq ($(IOSSIM_SDK_PATH),)
101RuntimeLibrary.darwin.Configs += asan_iossim_dynamic.dylib
102endif
103
104endif
105
106# On Linux, include a library which has all the runtime functions.
107ifeq ($(OS),Linux)
108RuntimeDirs += linux
109RuntimeLibrary.linux.Configs :=
110
111# TryCompile compiler source flags
112# Returns exit code of running a compiler invocation.
113TryCompile = \
114  $(shell \
115    cflags=""; \
116    for flag in $(3); do \
117      cflags="$$cflags $$flag"; \
118    done; \
119    $(1) $$cflags $(2) -o /dev/null > /dev/null 2> /dev/null ; \
120    echo $$?)
121
122# We try to build 32-bit runtimes both on 32-bit hosts and 64-bit hosts.
123Runtime32BitConfigs = \
124	full-i386.a profile-i386.a san-i386.a asan-i386.a ubsan-i386.a \
125	ubsan_cxx-i386.a
126
127# We currently only try to generate runtime libraries on x86.
128ifeq ($(ARCH),x86)
129RuntimeLibrary.linux.Configs += $(Runtime32BitConfigs)
130endif
131
132ifeq ($(ARCH),x86_64)
133RuntimeLibrary.linux.Configs += \
134	full-x86_64.a profile-x86_64.a san-x86_64.a asan-x86_64.a \
135	tsan-x86_64.a msan-x86_64.a ubsan-x86_64.a ubsan_cxx-x86_64.a \
136	dfsan-x86_64.a lsan-x86_64.a
137# We need to build 32-bit ASan/UBsan libraries on 64-bit platform, and add them
138# to the list of runtime libraries to make
139# "clang -fsanitize=(address|undefined) -m32" work.
140# We check that Clang can produce working 32-bit binaries by compiling a simple
141# executable.
142test_source = $(LLVM_SRC_ROOT)/tools/clang/runtime/compiler-rt/clang_linux_test_input.c
143ifeq ($(call TryCompile,$(ToolDir)/clang,$(test_source),-m32),0)
144RuntimeLibrary.linux.Configs += $(Runtime32BitConfigs)
145endif
146ifneq ($(LLVM_ANDROID_TOOLCHAIN_DIR),)
147RuntimeLibrary.linux.Configs += asan-arm-android.so
148endif
149endif
150
151endif
152
153####
154# The build rules below are designed to be generic and should only need to be
155# modified based on changes in the compiler-rt layout or build system.
156####
157
158# Rule to build the compiler-rt libraries we need.
159#
160# We build all the libraries in a single shot to avoid recursive make as much as
161# possible.
162BuildRuntimeLibraries:
163	$(Verb) $(MAKE) -C $(COMPILERRT_SRC_ROOT) \
164	  ProjSrcRoot=$(COMPILERRT_SRC_ROOT) \
165	  ProjObjRoot=$(PROJ_OBJ_DIR) \
166	  CC="$(ToolDir)/clang" \
167	  LLVM_ANDROID_TOOLCHAIN_DIR="$(LLVM_ANDROID_TOOLCHAIN_DIR)" \
168	  $(COMPILERRT_MAKE_FLAGS) \
169	  $(RuntimeDirs:%=clang_%)
170.PHONY: BuildRuntimeLibraries
171CleanRuntimeLibraries:
172	$(Verb) $(MAKE) -C $(COMPILERRT_SRC_ROOT) \
173	  ProjSrcRoot=$(COMPILERRT_SRC_ROOT) \
174	  ProjObjRoot=$(PROJ_OBJ_DIR) \
175	  $(COMPILERRT_MAKE_FLAGS) \
176	  clean
177.PHONY: CleanRuntimeLibraries
178RuntimeHeader: $(ResourceIncludeDir)/sanitizer
179
180$(PROJ_resources_lib):
181	$(Verb) $(MKDIR) $@
182
183$(ResourceIncludeDir):
184	$(Verb) $(MKDIR) $@
185
186$(ResourceIncludeDir)/sanitizer: $(ResourceIncludeDir)
187	$(Verb) $(MKDIR) $@
188	$(Verb) cp $(COMPILERRT_SRC_ROOT)/include/sanitizer/*.h $@
189
190# Expand rules for copying/installing each individual library. We can't use
191# implicit rules here because we need to match against multiple things.
192define RuntimeLibraryTemplate
193$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a: BuildRuntimeLibraries
194	@true
195$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.so: BuildRuntimeLibraries
196	@true
197$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.dylib: BuildRuntimeLibraries
198	@true
199.PRECIOUS: $(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a
200
201# Rule to copy the libraries to their resource directory location.
202$(ResourceLibDir)/$1/libclang_rt.%.a: \
203		$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a \
204		$(ResourceLibDir)/$1/.dir
205	$(Echo) Copying runtime library $1/$$* to build dir
206	$(Verb) cp $(PROJ_OBJ_DIR)/clang_$1/$$*/libcompiler_rt.a $$@
207$(ResourceLibDir)/$1/libclang_rt.%.so: \
208		$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.so \
209		$(ResourceLibDir)/$1/.dir
210	$(Echo) Copying runtime library $1/$$* to build dir
211	$(Verb) cp $(PROJ_OBJ_DIR)/clang_$1/$$*/libcompiler_rt.so $$@
212$(ResourceLibDir)/$1/libclang_rt.%.dylib: \
213		$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.dylib \
214		$(ResourceLibDir)/$1/.dir
215	$(Echo) Copying runtime library $1/$$* to build dir
216	$(Verb) cp $(PROJ_OBJ_DIR)/clang_$1/$$*/libcompiler_rt.dylib $$@
217	$(Echo) Fixing LC_ID_DYLIB of $$@
218	$(Verb) install_name_tool $$@ -id $$@
219RuntimeLibrary.$1: \
220		$(RuntimeLibrary.$1.Configs:%=$(ResourceLibDir)/$1/libclang_rt.%)
221.PHONY: RuntimeLibrary.$1
222
223$(PROJ_resources_lib)/$1: $(PROJ_resources_lib)
224	$(Verb) $(MKDIR) $$@
225
226$(PROJ_resources_lib)/$1/libclang_rt.%.a: \
227		$(ResourceLibDir)/$1/libclang_rt.%.a | $(PROJ_resources_lib)/$1
228	$(Echo) Installing compiler runtime library: $1/$$*
229	$(Verb) $(DataInstall) $$< $(PROJ_resources_lib)/$1
230$(PROJ_resources_lib)/$1/libclang_rt.%.so: \
231		$(ResourceLibDir)/$1/libclang_rt.%.so | $(PROJ_resources_lib)/$1
232	$(Echo) Installing compiler runtime library: $1/$$*
233	$(Verb) $(DataInstall) $$< $(PROJ_resources_lib)/$1
234$(PROJ_resources_lib)/$1/libclang_rt.%.dylib: \
235		$(ResourceLibDir)/$1/libclang_rt.%.dylib | $(PROJ_resources_lib)/$1
236	$(Echo) Installing compiler runtime library: $1/$$*
237	$(Verb) $(DataInstall) $$< $(PROJ_resources_lib)/$1
238
239# Rule to install runtime libraries.
240RuntimeLibraryInstall.$1: \
241		$(RuntimeLibrary.$1.Configs:%=$(PROJ_resources_lib)/$1/libclang_rt.%)
242.PHONY: RuntimeLibraryInstall.$1
243endef
244$(foreach lib,$(RuntimeDirs), $(eval $(call RuntimeLibraryTemplate,$(lib))))
245
246$(PROJ_resources_include):
247	$(Verb) $(MKDIR) $@
248
249$(PROJ_resources_include)/sanitizer: $(ResourceIncludeDir)/sanitizer $(PROJ_resources_include)
250	$(Verb) $(MKDIR) $@
251	$(Echo) Installing compiler runtime headers
252	$(Verb) $(DataInstall) $(ResourceIncludeDir)/sanitizer/* \
253                               $(PROJ_resources_include)/sanitizer
254
255RuntimeHeaderInstall: $(PROJ_resources_include)/sanitizer
256.PHONY: RuntimeHeaderInstall
257
258# Hook into the standard Makefile rules.
259all-local:: $(RuntimeDirs:%=RuntimeLibrary.%) RuntimeHeader
260install-local:: $(RuntimeDirs:%=RuntimeLibraryInstall.%) RuntimeHeaderInstall
261clean-local:: CleanRuntimeLibraries
262
263endif
264endif
265endif
266