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 # Expect compiler-rt to be in llvm/projects/compiler-rt
     31 COMPILERRT_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 unwieldy and irritating to maintain in the long term.
     66 ifneq ($(LLVM_CROSS_COMPILING),1)
     67 ifneq ($(CLANG_NO_RUNTIME),1)
     68 ifeq ($(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.
     75 RuntimeDirs :=
     76 ifeq ($(OS),Darwin)
     77 RuntimeDirs += darwin macho_embedded
     78 RuntimeLibrary.darwin.Configs := \
     79 	eprintf.a 10.4.a osx.a cc_kext.a \
     80 	asan_osx_dynamic.dylib \
     81 	profile_osx.a \
     82 	ubsan_osx_dynamic.dylib
     83 
     84 IOS_SDK := $(shell xcrun --show-sdk-path -sdk iphoneos 2> /dev/null)
     85 IOSSIM_SDK := $(shell xcrun --show-sdk-path -sdk iphonesimulator 2> /dev/null)
     86 
     87 ifneq ($(IOS_SDK)$(IOSSIM_SDK),)
     88 RuntimeLibrary.darwin.Configs += ios.a profile_ios.a
     89 endif
     90 
     91 ifneq ($(IOS_SDK),)
     92 ifneq (,$(filter ARM AARCH64,$(TARGETS_TO_BUILD)))
     93 RuntimeLibrary.darwin.Configs += cc_kext_ios.a
     94 endif
     95 endif
     96 
     97 ifneq ($(IOSSIM_SDK),)
     98 RuntimeLibrary.darwin.Configs += asan_iossim_dynamic.dylib \
     99                                  ubsan_iossim_dynamic.dylib
    100 endif
    101 
    102 RuntimeLibrary.macho_embedded.Configs := \
    103 	hard_static.a hard_pic.a
    104 ifneq (,$(findstring ARM,$(TARGETS_TO_BUILD)))
    105 RuntimeLibrary.macho_embedded.Configs += \
    106 	soft_static.a soft_pic.a
    107 endif
    108 endif
    109 
    110 # On Linux, include a library which has all the runtime functions.
    111 ifeq ($(OS),Linux)
    112 RuntimeDirs += linux
    113 RuntimeLibrary.linux.Configs :=
    114 
    115 # TryCompile compiler source flags
    116 # Returns exit code of running a compiler invocation.
    117 TryCompile = \
    118   $(shell \
    119     cflags=""; \
    120     for flag in $(3); do \
    121       cflags="$$cflags $$flag"; \
    122     done; \
    123     $(1) $$cflags $(2) -o /dev/null > /dev/null 2> /dev/null ; \
    124     echo $$?)
    125 
    126 # We try to build 32-bit runtimes both on 32-bit hosts and 64-bit hosts.
    127 Runtime32BitConfigs = \
    128 	builtins-i386.a profile-i386.a
    129 
    130 # We currently only try to generate runtime libraries on x86.
    131 ifeq ($(ARCH),x86)
    132 RuntimeLibrary.linux.Configs += $(Runtime32BitConfigs)
    133 endif
    134 
    135 ifeq ($(ARCH),x86_64)
    136 RuntimeLibrary.linux.Configs += \
    137 	builtins-x86_64.a profile-x86_64.a
    138 # We need to build 32-bit libraries on 64-bit platform, and add them
    139 # to the list of runtime libraries to make "clang -m32" work.
    140 # We check that Clang can produce working 32-bit binaries by compiling a simple
    141 # executable.
    142 test_source = $(LLVM_SRC_ROOT)/tools/clang/runtime/compiler-rt/clang_linux_test_input.c
    143 ifeq ($(call TryCompile,$(ToolDir)/clang,$(test_source),-m32),0)
    144 RuntimeLibrary.linux.Configs += $(Runtime32BitConfigs)
    145 endif
    146 endif
    147 
    148 endif
    149 
    150 ####
    151 # The build rules below are designed to be generic and should only need to be
    152 # modified based on changes in the compiler-rt layout or build system.
    153 ####
    154 
    155 # Rule to build the compiler-rt libraries we need.
    156 #
    157 # We build all the libraries in a single shot to avoid recursive make as much as
    158 # possible.
    159 BuildRuntimeLibraries:
    160 	$(Verb) $(MAKE) -C $(COMPILERRT_SRC_ROOT) \
    161 	  ProjSrcRoot=$(COMPILERRT_SRC_ROOT) \
    162 	  ProjObjRoot=$(PROJ_OBJ_DIR) \
    163 	  CC="$(ToolDir)/clang" \
    164 	  VERBOSE=$(VERBOSE) \
    165 	  $(RuntimeDirs:%=clang_%)
    166 .PHONY: BuildRuntimeLibraries
    167 CleanRuntimeLibraries:
    168 	$(Verb) $(MAKE) -C $(COMPILERRT_SRC_ROOT) \
    169 	  ProjSrcRoot=$(COMPILERRT_SRC_ROOT) \
    170 	  ProjObjRoot=$(PROJ_OBJ_DIR) \
    171 	  VERBOSE=$(VERBOSE) \
    172 	  clean
    173 .PHONY: CleanRuntimeLibraries
    174 RuntimeHeader: $(ResourceIncludeDir)/sanitizer
    175 
    176 $(PROJ_resources_lib):
    177 	$(Verb) $(MKDIR) $@
    178 
    179 $(ResourceIncludeDir):
    180 	$(Verb) $(MKDIR) $@
    181 
    182 $(ResourceIncludeDir)/sanitizer: $(ResourceIncludeDir)
    183 	$(Verb) $(MKDIR) $@
    184 	$(Verb) cp $(COMPILERRT_SRC_ROOT)/include/sanitizer/*.h $@
    185 
    186 # Expand rules for copying/installing each individual library. We can't use
    187 # implicit rules here because we need to match against multiple things.
    188 define RuntimeLibraryTemplate
    189 $(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a: BuildRuntimeLibraries
    190 	@true
    191 $(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.so: BuildRuntimeLibraries
    192 	@true
    193 $(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.dylib: BuildRuntimeLibraries
    194 	@true
    195 .PRECIOUS: $(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a
    196 
    197 # Rule to copy the libraries to their resource directory location.
    198 $(ResourceLibDir)/$1/libclang_rt.%.a: \
    199 		$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.a \
    200 		$(ResourceLibDir)/$1/.dir
    201 	$(Echo) Copying runtime library $1/$$* to build dir
    202 	$(Verb) cp $(PROJ_OBJ_DIR)/clang_$1/$$*/libcompiler_rt.a $$@
    203 $(ResourceLibDir)/$1/libclang_rt.%.so: \
    204 		$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.so \
    205 		$(ResourceLibDir)/$1/.dir
    206 	$(Echo) Copying runtime library $1/$$* to build dir
    207 	$(Verb) cp $(PROJ_OBJ_DIR)/clang_$1/$$*/libcompiler_rt.so $$@
    208 $(ResourceLibDir)/$1/libclang_rt.%.dylib: \
    209 		$(PROJ_OBJ_DIR)/clang_$1/%/libcompiler_rt.dylib \
    210 		$(ResourceLibDir)/$1/.dir
    211 	$(Echo) Copying runtime library $1/$$* to build dir
    212 	$(Verb) cp $(PROJ_OBJ_DIR)/clang_$1/$$*/libcompiler_rt.dylib $$@
    213 RuntimeLibrary.$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.
    234 RuntimeLibraryInstall.$1: \
    235 		$(RuntimeLibrary.$1.Configs:%=$(PROJ_resources_lib)/$1/libclang_rt.%)
    236 .PHONY: RuntimeLibraryInstall.$1
    237 endef
    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 
    249 RuntimeHeaderInstall: $(PROJ_resources_include)/sanitizer
    250 .PHONY: RuntimeHeaderInstall
    251 
    252 # Hook into the standard Makefile rules.
    253 all-local:: $(RuntimeDirs:%=RuntimeLibrary.%) RuntimeHeader
    254 install-local:: $(RuntimeDirs:%=RuntimeLibraryInstall.%) RuntimeHeaderInstall
    255 clean-local:: CleanRuntimeLibraries
    256 
    257 endif
    258 endif
    259 endif
    260