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