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