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