Makefile revision 651f13cea278ec967336033dd032faef0e9fc2ec
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 unwieldy 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 macho_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
87
88RuntimeLibrary.macho_embedded.Configs := \
89	hard_static.a hard_pic.a
90ifneq (,$(findstring ARM,$(TARGETS_TO_BUILD)))
91RuntimeLibrary.macho_embedded.Configs += \
92	soft_static.a soft_pic.a
93endif
94
95# Support building compiler-rt with relocatable SDKs.
96#
97# This will cause make to put SDKROOT in the environment, and since we
98# are using the built Clang to build compiler-rt, it to pick up that
99# location as the default value for the include system root.
100ACTIVE_SDK_PATH := $(shell xcrun --show-sdk-path 2> /dev/null)
101ifneq ($(ACTIVE_SDK_PATH),)
102COMPILERRT_MAKE_FLAGS := SDKROOT=$(ACTIVE_SDK_PATH)
103endif
104IOSSIM_SDK_PATH := $(shell xcrun --show-sdk-path -sdk iphonesimulator 2> /dev/null)
105ifneq ($(IOSSIM_SDK_PATH),)
106RuntimeLibrary.darwin.Configs += asan_iossim_dynamic.dylib
107endif
108
109endif
110
111# On Linux, include a library which has all the runtime functions.
112ifeq ($(OS),Linux)
113RuntimeDirs += linux
114RuntimeLibrary.linux.Configs :=
115
116# TryCompile compiler source flags
117# Returns exit code of running a compiler invocation.
118TryCompile = \
119  $(shell \
120    cflags=""; \
121    for flag in $(3); do \
122      cflags="$$cflags $$flag"; \
123    done; \
124    $(1) $$cflags $(2) -o /dev/null > /dev/null 2> /dev/null ; \
125    echo $$?)
126
127# We try to build 32-bit runtimes both on 32-bit hosts and 64-bit hosts.
128Runtime32BitConfigs = \
129	full-i386.a profile-i386.a san-i386.a asan-i386.a ubsan-i386.a \
130	ubsan_cxx-i386.a
131
132# We currently only try to generate runtime libraries on x86.
133ifeq ($(ARCH),x86)
134RuntimeLibrary.linux.Configs += $(Runtime32BitConfigs)
135endif
136
137ifeq ($(ARCH),x86_64)
138RuntimeLibrary.linux.Configs += \
139	full-x86_64.a profile-x86_64.a san-x86_64.a asan-x86_64.a \
140	tsan-x86_64.a msan-x86_64.a ubsan-x86_64.a ubsan_cxx-x86_64.a \
141	dfsan-x86_64.a lsan-x86_64.a
142# We need to build 32-bit ASan/UBsan libraries on 64-bit platform, and add them
143# to the list of runtime libraries to make
144# "clang -fsanitize=(address|undefined) -m32" work.
145# We check that Clang can produce working 32-bit binaries by compiling a simple
146# executable.
147test_source = $(LLVM_SRC_ROOT)/tools/clang/runtime/compiler-rt/clang_linux_test_input.c
148ifeq ($(call TryCompile,$(ToolDir)/clang,$(test_source),-m32),0)
149RuntimeLibrary.linux.Configs += $(Runtime32BitConfigs)
150endif
151ifneq ($(LLVM_ANDROID_TOOLCHAIN_DIR),)
152RuntimeLibrary.linux.Configs += asan-arm-android.so
153endif
154endif
155
156endif
157
158####
159# The build rules below are designed to be generic and should only need to be
160# modified based on changes in the compiler-rt layout or build system.
161####
162
163# Rule to build the compiler-rt libraries we need.
164#
165# We build all the libraries in a single shot to avoid recursive make as much as
166# possible.
167BuildRuntimeLibraries:
168	$(Verb) $(MAKE) -C $(COMPILERRT_SRC_ROOT) \
169	  ProjSrcRoot=$(COMPILERRT_SRC_ROOT) \
170	  ProjObjRoot=$(PROJ_OBJ_DIR) \
171	  CC="$(ToolDir)/clang" \
172	  LLVM_ANDROID_TOOLCHAIN_DIR="$(LLVM_ANDROID_TOOLCHAIN_DIR)" \
173	  $(COMPILERRT_MAKE_FLAGS) \
174	  $(RuntimeDirs:%=clang_%)
175.PHONY: BuildRuntimeLibraries
176CleanRuntimeLibraries:
177	$(Verb) $(MAKE) -C $(COMPILERRT_SRC_ROOT) \
178	  ProjSrcRoot=$(COMPILERRT_SRC_ROOT) \
179	  ProjObjRoot=$(PROJ_OBJ_DIR) \
180	  $(COMPILERRT_MAKE_FLAGS) \
181	  clean
182.PHONY: CleanRuntimeLibraries
183RuntimeHeader: $(ResourceIncludeDir)/sanitizer
184
185$(PROJ_resources_lib):
186	$(Verb) $(MKDIR) $@
187
188$(ResourceIncludeDir):
189	$(Verb) $(MKDIR) $@
190
191$(ResourceIncludeDir)/sanitizer: $(ResourceIncludeDir)
192	$(Verb) $(MKDIR) $@
193	$(Verb) cp $(COMPILERRT_SRC_ROOT)/include/sanitizer/*.h $@
194
195# Expand rules for copying/installing each individual library. We can't use
196# implicit rules here because we need to match against multiple things.
197define RuntimeLibraryTemplate
198$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a: BuildRuntimeLibraries
199	@true
200$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.so: BuildRuntimeLibraries
201	@true
202$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.dylib: BuildRuntimeLibraries
203	@true
204.PRECIOUS: $(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a
205
206# Rule to copy the libraries to their resource directory location.
207$(ResourceLibDir)/$1/libclang_rt.%.a: \
208		$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a \
209		$(ResourceLibDir)/$1/.dir
210	$(Echo) Copying runtime library $1/$$* to build dir
211	$(Verb) cp $(PROJ_OBJ_DIR)/clang_$1/$$*/libcompiler_rt.a $$@
212$(ResourceLibDir)/$1/libclang_rt.%.so: \
213		$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.so \
214		$(ResourceLibDir)/$1/.dir
215	$(Echo) Copying runtime library $1/$$* to build dir
216	$(Verb) cp $(PROJ_OBJ_DIR)/clang_$1/$$*/libcompiler_rt.so $$@
217$(ResourceLibDir)/$1/libclang_rt.%.dylib: \
218		$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.dylib \
219		$(ResourceLibDir)/$1/.dir
220	$(Echo) Copying runtime library $1/$$* to build dir
221	$(Verb) cp $(PROJ_OBJ_DIR)/clang_$1/$$*/libcompiler_rt.dylib $$@
222	$(Echo) Fixing LC_ID_DYLIB of $$@
223	$(Verb) install_name_tool $$@ -id $$@
224RuntimeLibrary.$1: \
225		$(RuntimeLibrary.$1.Configs:%=$(ResourceLibDir)/$1/libclang_rt.%)
226.PHONY: RuntimeLibrary.$1
227
228$(PROJ_resources_lib)/$1: $(PROJ_resources_lib)
229	$(Verb) $(MKDIR) $$@
230
231$(PROJ_resources_lib)/$1/libclang_rt.%.a: \
232		$(ResourceLibDir)/$1/libclang_rt.%.a | $(PROJ_resources_lib)/$1
233	$(Echo) Installing compiler runtime library: $1/$$*
234	$(Verb) $(DataInstall) $$< $(PROJ_resources_lib)/$1
235$(PROJ_resources_lib)/$1/libclang_rt.%.so: \
236		$(ResourceLibDir)/$1/libclang_rt.%.so | $(PROJ_resources_lib)/$1
237	$(Echo) Installing compiler runtime library: $1/$$*
238	$(Verb) $(DataInstall) $$< $(PROJ_resources_lib)/$1
239$(PROJ_resources_lib)/$1/libclang_rt.%.dylib: \
240		$(ResourceLibDir)/$1/libclang_rt.%.dylib | $(PROJ_resources_lib)/$1
241	$(Echo) Installing compiler runtime library: $1/$$*
242	$(Verb) $(DataInstall) $$< $(PROJ_resources_lib)/$1
243
244# Rule to install runtime libraries.
245RuntimeLibraryInstall.$1: \
246		$(RuntimeLibrary.$1.Configs:%=$(PROJ_resources_lib)/$1/libclang_rt.%)
247.PHONY: RuntimeLibraryInstall.$1
248endef
249$(foreach lib,$(RuntimeDirs), $(eval $(call RuntimeLibraryTemplate,$(lib))))
250
251$(PROJ_resources_include):
252	$(Verb) $(MKDIR) $@
253
254$(PROJ_resources_include)/sanitizer: $(ResourceIncludeDir)/sanitizer $(PROJ_resources_include)
255	$(Verb) $(MKDIR) $@
256	$(Echo) Installing compiler runtime headers
257	$(Verb) $(DataInstall) $(ResourceIncludeDir)/sanitizer/* \
258                               $(PROJ_resources_include)/sanitizer
259
260RuntimeHeaderInstall: $(PROJ_resources_include)/sanitizer
261.PHONY: RuntimeHeaderInstall
262
263# Hook into the standard Makefile rules.
264all-local:: $(RuntimeDirs:%=RuntimeLibrary.%) RuntimeHeader
265install-local:: $(RuntimeDirs:%=RuntimeLibraryInstall.%) RuntimeHeaderInstall
266clean-local:: CleanRuntimeLibraries
267
268endif
269endif
270endif
271