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