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