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 10.4 osx cc_kext \
78	asan_osx profile_osx
79endif
80
81# On Linux, include a library which has all the runtime functions.
82ifeq ($(OS),Linux)
83RuntimeDirs += linux
84RuntimeLibrary.linux.Configs :=
85
86# We currently only try to generate runtime libraries on x86.
87ifeq ($(ARCH),x86)
88RuntimeLibrary.linux.Configs += \
89	full-i386 profile-i386 asan-i386
90endif
91ifeq ($(ARCH),x86_64)
92RuntimeLibrary.linux.Configs += \
93	full-x86_64 profile-x86_64 asan-x86_64 tsan-x86_64
94endif
95
96endif
97
98####
99# The build rules below are designed to be generic and should only need to be
100# modified based on changes in the compiler-rt layout or build system.
101####
102
103# Rule to build the compiler-rt libraries we need.
104#
105# We build all the libraries in a single shot to avoid recursive make as much as
106# possible.
107BuildRuntimeLibraries:
108	$(Verb) $(MAKE) -C $(COMPILERRT_SRC_ROOT) \
109	  ProjSrcRoot=$(COMPILERRT_SRC_ROOT) \
110	  ProjObjRoot=$(PROJ_OBJ_DIR) \
111	  CC="$(ToolDir)/clang" \
112	  $(RuntimeDirs:%=clang_%)
113.PHONY: BuildRuntimeLibraries
114CleanRuntimeLibraries:
115	$(Verb) $(MAKE) -C $(COMPILERRT_SRC_ROOT) \
116	  ProjSrcRoot=$(COMPILERRT_SRC_ROOT) \
117	  ProjObjRoot=$(PROJ_OBJ_DIR) \
118	  clean
119.PHONY: CleanRuntimeLibraries
120
121$(PROJ_resources_lib):
122	$(Verb) $(MKDIR) $@
123
124# Expand rules for copying/installing each individual library. We can't use
125# implicit rules here because we need to match against multiple things.
126define RuntimeLibraryTemplate
127$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a: BuildRuntimeLibraries
128	@true
129.PRECIOUS: $(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a
130
131# Rule to copy the libraries to their resource directory location.
132$(ResourceLibDir)/$1/libclang_rt.%.a: \
133		$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a \
134		$(ResourceLibDir)/$1/.dir
135	$(Echo) Copying runtime library $1/$$* to build dir
136	$(Verb) cp $(PROJ_OBJ_DIR)/clang_$1/$$*/libcompiler_rt.a $$@
137RuntimeLibrary.$1: \
138		$(RuntimeLibrary.$1.Configs:%=$(ResourceLibDir)/$1/libclang_rt.%.a)
139.PHONY: RuntimeLibrary.$1
140
141$(PROJ_resources_lib)/$1: $(PROJ_resources_lib)
142	$(Verb) $(MKDIR) $$@
143
144$(PROJ_resources_lib)/$1/libclang_rt.%.a: \
145		$(ResourceLibDir)/$1/libclang_rt.%.a | $(PROJ_resources_lib)/$1
146	$(Echo) Installing compiler runtime library: $1/$$*
147	$(Verb) $(DataInstall) $$< $(PROJ_resources_lib)/$1
148
149# Rule to install runtime libraries.
150RuntimeLibraryInstall.$1: \
151		$(RuntimeLibrary.$1.Configs:%=$(PROJ_resources_lib)/$1/libclang_rt.%.a)
152.PHONY: RuntimeLibraryInstall.$1
153endef
154$(foreach lib,$(RuntimeDirs), $(eval $(call RuntimeLibraryTemplate,$(lib))))
155
156# Hook into the standard Makefile rules.
157all-local:: $(RuntimeDirs:%=RuntimeLibrary.%)
158install-local:: $(RuntimeDirs:%=RuntimeLibraryInstall.%)
159clean-local:: CleanRuntimeLibraries
160
161endif
162endif
163endif
164