Home | History | Annotate | Download | only in compiler-rt
      1 # CMake build for CompilerRT.
      2 #
      3 # This build assumes that CompilerRT is checked out into the
      4 # 'projects/compiler-rt' inside of an LLVM tree, it is not a stand-alone build
      5 # system.
      6 #
      7 # An important constraint of the build is that it only produces libraries
      8 # based on the ability of the host toolchain to target various platforms.
      9 
     10 include(LLVMParseArguments)
     11 
     12 # The CompilerRT build system requires CMake version 2.8.8 or higher in order
     13 # to use its support for building convenience "libraries" as a collection of
     14 # .o files. This is particularly useful in producing larger, more complex
     15 # runtime libraries.
     16 cmake_minimum_required(VERSION 2.8.8)
     17 
     18 # Compute the Clang version from the LLVM version.
     19 # FIXME: We should be able to reuse CLANG_VERSION variable calculated
     20 #        in Clang cmake files, instead of copying the rules here.
     21 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
     22        ${PACKAGE_VERSION})
     23 # Setup the paths where compiler-rt runtimes and headers should be stored.
     24 set(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
     25 string(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
     26 set(CLANG_RESOURCE_DIR ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION})
     27 set(COMPILER_RT_LIBRARY_OUTPUT_DIR ${CLANG_RESOURCE_DIR}/lib/${LIBCLANG_OS_DIR})
     28 set(COMPILER_RT_LIBRARY_INSTALL_DIR
     29   ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR})
     30 
     31 # Add path for custom modules
     32 set(CMAKE_MODULE_PATH
     33   ${CMAKE_MODULE_PATH}
     34   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
     35   )
     36 include(AddCompilerRT)
     37 
     38 set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
     39 set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
     40 # Setup custom SDK sysroots.
     41 set(COMPILER_RT_DARWIN_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/darwin)
     42 set(COMPILER_RT_LINUX_SDK_SYSROOT ${COMPILER_RT_SOURCE_DIR}/SDKs/linux)
     43 
     44 # Detect whether the current target platform is 32-bit or 64-bit, and setup
     45 # the correct commandline flags needed to attempt to target 32-bit and 64-bit.
     46 if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
     47     NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
     48   message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
     49 endif()
     50 set(TARGET_64_BIT_CFLAGS "-m64")
     51 set(TARGET_32_BIT_CFLAGS "-m32")
     52 
     53 # List of architectures we can target.
     54 set(COMPILER_RT_SUPPORTED_ARCH)
     55 
     56 function(get_target_flags_for_arch arch out_var)
     57   list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
     58   if(ARCH_INDEX EQUAL -1)
     59     message(FATAL_ERROR "Unsupported architecture: ${arch}")
     60   else()
     61     set(${out_var} ${TARGET_${arch}_CFLAGS} PARENT_SCOPE)
     62   endif()
     63 endfunction()
     64 
     65 # Try to compile a very simple source file to ensure we can target the given
     66 # platform. We use the results of these tests to build only the various target
     67 # runtime libraries supported by our current compilers cross-compiling
     68 # abilities.
     69 set(SIMPLE_SOURCE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple.c)
     70 file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\nint main() {}")
     71 
     72 # test_target_arch(<arch> <target flags...>)
     73 # Sets the target flags for a given architecture and determines if this
     74 # architecture is supported by trying to build a simple file.
     75 macro(test_target_arch arch)
     76   set(TARGET_${arch}_CFLAGS ${ARGN})
     77   try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
     78               COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
     79               CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_${arch}_CFLAGS}")
     80   if(${CAN_TARGET_${arch}})
     81     list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
     82   endif()
     83 endmacro()
     84 
     85 if("${LLVM_NATIVE_ARCH}" STREQUAL "X86")
     86   test_target_arch(x86_64 ${TARGET_64_BIT_CFLAGS})
     87   test_target_arch(i386 ${TARGET_32_BIT_CFLAGS})
     88 elseif("${LLVM_NATIVE_ARCH}" STREQUAL "PowerPC")
     89   test_target_arch(powerpc64 ${TARGET_64_BIT_CFLAGS})
     90 endif()
     91 
     92 # We only support running instrumented tests when we're not cross compiling
     93 # and target a unix-like system. On Android we define the rules for building
     94 # unit tests, but don't execute them.
     95 if("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND UNIX AND NOT ANDROID)
     96   option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
     97 else()
     98   option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
     99 endif()
    100 
    101 # Check if compiler-rt is built with libc++.
    102 find_flag_in_string("${CMAKE_CXX_FLAGS}" "-stdlib=libc++"
    103                     COMPILER_RT_USES_LIBCXX)
    104 
    105 function(filter_available_targets out_var)
    106   set(archs)
    107   foreach(arch ${ARGN})
    108     list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
    109     if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
    110       list(APPEND archs ${arch})
    111     endif()
    112   endforeach()
    113   set(${out_var} ${archs} PARENT_SCOPE)
    114 endfunction()
    115 
    116 # Provide some common commmandline flags for Sanitizer runtimes.
    117 set(SANITIZER_COMMON_CFLAGS
    118   -fPIC
    119   -fno-builtin
    120   -fno-exceptions
    121   -fomit-frame-pointer
    122   -funwind-tables
    123   -fno-stack-protector
    124   -Wno-gnu  # Variadic macros with 0 arguments for ...
    125   -O3
    126   )
    127 if(NOT WIN32)
    128   list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden)
    129 endif()
    130 # Build sanitizer runtimes with debug info.
    131 check_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
    132 if(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
    133   list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
    134 else()
    135   list(APPEND SANITIZER_COMMON_CFLAGS -g)
    136 endif()
    137 # Warnings suppressions.
    138 check_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
    139 if(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
    140   list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
    141 endif()
    142 check_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
    143 if(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
    144   list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
    145 endif()
    146 # Sanitizer may not have libstdc++, so we can have problems with virtual
    147 # destructors.
    148 check_cxx_compiler_flag(-Wno-non-virtual-dtor SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
    149 if (SUPPORTS_NO_NON_VIRTUAL_DTOR_FLAG)
    150   list(APPEND SANITIZER_COMMON_CFLAGS -Wno-non-virtual-dtor)
    151 endif()
    152 
    153 # Setup min Mac OS X version.
    154 if(APPLE)
    155   if(COMPILER_RT_USES_LIBCXX)
    156     set(SANITIZER_MIN_OSX_VERSION 10.7)
    157   else()
    158     set(SANITIZER_MIN_OSX_VERSION 10.6)
    159   endif()
    160   list(APPEND SANITIZER_COMMON_CFLAGS
    161     -mmacosx-version-min=${SANITIZER_MIN_OSX_VERSION})
    162 endif()
    163 
    164 # Architectures supported by Sanitizer runtimes. Specific sanitizers may
    165 # support only subset of these (e.g. TSan works on x86_64 only).
    166 filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH
    167   x86_64 i386 powerpc64)
    168 
    169 # Add the public header's directory to the includes for all of compiler-rt.
    170 include_directories(include)
    171 add_subdirectory(include)
    172 
    173 set(SANITIZER_COMMON_LIT_TEST_DEPS
    174   clang clang-headers FileCheck count not llvm-nm llvm-symbolizer
    175   compiler-rt-headers)
    176 
    177 add_subdirectory(lib)
    178 
    179 if(LLVM_INCLUDE_TESTS)
    180   # Currently the tests have not been ported to CMake, so disable this
    181   # directory.
    182   #
    183   #add_subdirectory(test)
    184 endif()
    185