CMakeLists.txt revision 02dcc63dde3106c24999040768a5eb52f99fedda
1d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# CMake build for CompilerRT.
2d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth#
3d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# This build assumes that CompilerRT is checked out into the
4d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# 'projects/compiler-rt' inside of an LLVM tree, it is not a stand-alone build
5d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# system.
6d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth#
7d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# An important constraint of the build is that it only produces libraries
8d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# based on the ability of the host toolchain to target various platforms.
9d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth
10d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthinclude(LLVMParseArguments)
11d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth
12a765ffcef39dc37bd6d7e597286fd76fbeb1b9faChandler Carruth# The CompilerRT build system requires CMake version 2.8.8 or higher in order
13a765ffcef39dc37bd6d7e597286fd76fbeb1b9faChandler Carruth# to use its support for building convenience "libraries" as a collection of
14a765ffcef39dc37bd6d7e597286fd76fbeb1b9faChandler Carruth# .o files. This is particularly useful in producing larger, more complex
15a765ffcef39dc37bd6d7e597286fd76fbeb1b9faChandler Carruth# runtime libraries.
16a765ffcef39dc37bd6d7e597286fd76fbeb1b9faChandler Carruthcmake_minimum_required(VERSION 2.8.8)
17a765ffcef39dc37bd6d7e597286fd76fbeb1b9faChandler Carruth
1802dcc63dde3106c24999040768a5eb52f99feddaAlexey Samsonov# Add path for custom modules
1902dcc63dde3106c24999040768a5eb52f99feddaAlexey Samsonovset(CMAKE_MODULE_PATH
2002dcc63dde3106c24999040768a5eb52f99feddaAlexey Samsonov  ${CMAKE_MODULE_PATH}
2102dcc63dde3106c24999040768a5eb52f99feddaAlexey Samsonov  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
2202dcc63dde3106c24999040768a5eb52f99feddaAlexey Samsonov  )
2302dcc63dde3106c24999040768a5eb52f99feddaAlexey Samsonov
2402dcc63dde3106c24999040768a5eb52f99feddaAlexey Samsonovset(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
2502dcc63dde3106c24999040768a5eb52f99feddaAlexey Samsonov
26d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# FIXME: Below we assume that the target build of LLVM/Clang is x86, which is
27d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# not at all valid. Much of this can be fixed just by switching to use
28d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# a just-built-clang binary for the compiles.
29d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth
30d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# Detect whether the current target platform is 32-bit or 64-bit, and setup
31d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
32bd1700415c7f5c6c9b9d562b6fee3414360c3dc2Alexey Samsonovif(CMAKE_SIZEOF_VOID_P EQUAL 4 OR LLVM_BUILD_32_BITS)
33d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth  set(TARGET_X86_64_CFLAGS "-m64")
34d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth  set(TARGET_I386_CFLAGS "")
35d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthelse()
36d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth  if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
37d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth    message(FATAL_ERROR "Please use a sane architecture with 4 or 8 byte pointers.")
38d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth  endif()
39d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth  set(TARGET_X86_64_CFLAGS "")
40d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth  set(TARGET_I386_CFLAGS "-m32")
41d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthendif()
42d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth
43d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# Try to compile a very simple source file to ensure we can target the given
44d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# platform. We use the results of these tests to build only the various target
45d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# runtime libraries supported by our current compilers cross-compiling
46d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth# abilities.
47d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthset(SIMPLE_SOURCE64 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple64.c)
48d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthfile(WRITE ${SIMPLE_SOURCE64} "#include <stdlib.h>\nint main() {}")
49d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthtry_compile(CAN_TARGET_X86_64 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE64}
50d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth            COMPILE_DEFINITIONS "${TARGET_X86_64_CFLAGS}"
51d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth            CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_X86_64_CFLAGS}")
52d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth
53d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthset(SIMPLE_SOURCE32 ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/simple32.c)
54d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthfile(WRITE ${SIMPLE_SOURCE32} "#include <stdlib.h>\nint main() {}")
55d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthtry_compile(CAN_TARGET_I386 ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE32}
56d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth            COMPILE_DEFINITIONS "${TARGET_I386_CFLAGS}"
57d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth            CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${TARGET_I386_CFLAGS}")
58d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth
59fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonovfunction(filter_available_targets out_var)
60fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonov  set(archs)
61fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonov  foreach(arch ${ARGN})
62fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonov    if(${arch} STREQUAL "x86_64" AND CAN_TARGET_X86_64)
63fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonov      list(APPEND archs ${arch})
64fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonov    elseif (${arch} STREQUAL "i386" AND CAN_TARGET_I386)
65fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonov      list(APPEND archs ${arch})
66fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonov    endif()
67fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonov  endforeach()
68fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonov  set(${out_var} ${archs} PARENT_SCOPE)
69fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonovendfunction()
70fe51abb4949ad9976168bf53edb52c20016b9a0dAlexey Samsonov
7160ab090deed8213613643729a7921ef0915ac704Chandler Carruth# Provide some common commmandline flags for Sanitizer runtimes.
7260ab090deed8213613643729a7921ef0915ac704Chandler Carruthset(SANITIZER_COMMON_CFLAGS
7360ab090deed8213613643729a7921ef0915ac704Chandler Carruth  -fPIC
740f7d4a4ad69ad20053d8b78c611853e778bd465aAlexey Samsonov  -fno-builtin
7560ab090deed8213613643729a7921ef0915ac704Chandler Carruth  -fno-exceptions
760f7d4a4ad69ad20053d8b78c611853e778bd465aAlexey Samsonov  -fomit-frame-pointer
7760ab090deed8213613643729a7921ef0915ac704Chandler Carruth  -funwind-tables
780f7d4a4ad69ad20053d8b78c611853e778bd465aAlexey Samsonov  -O3
7960ab090deed8213613643729a7921ef0915ac704Chandler Carruth  )
80b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonovif(NOT WIN32)
81b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonov  list(APPEND SANITIZER_COMMON_CFLAGS -fvisibility=hidden)
82b46941a1d23012491a7a8a52718cacbde3c19ba1Alexey Samsonovendif()
83275ca01c181dd04ac504971e300ccc92d334a61dAlexey Samsonov# Build sanitizer runtimes with debug info.
84275ca01c181dd04ac504971e300ccc92d334a61dAlexey Samsonovcheck_cxx_compiler_flag(-gline-tables-only SUPPORTS_GLINE_TABLES_ONLY_FLAG)
85275ca01c181dd04ac504971e300ccc92d334a61dAlexey Samsonovif(SUPPORTS_GLINE_TABLES_ONLY_FLAG)
86275ca01c181dd04ac504971e300ccc92d334a61dAlexey Samsonov  list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
87275ca01c181dd04ac504971e300ccc92d334a61dAlexey Samsonovelse()
88275ca01c181dd04ac504971e300ccc92d334a61dAlexey Samsonov  list(APPEND SANITIZER_COMMON_CFLAGS -g)
89275ca01c181dd04ac504971e300ccc92d334a61dAlexey Samsonovendif()
90275ca01c181dd04ac504971e300ccc92d334a61dAlexey Samsonov# Warnings suppressions.
91721460bdcbc29605381ee7658e6d0c233da0da8dAlexey Samsonovcheck_cxx_compiler_flag(-Wno-variadic-macros SUPPORTS_NO_VARIADIC_MACROS_FLAG)
9260ab090deed8213613643729a7921ef0915ac704Chandler Carruthif(SUPPORTS_NO_VARIADIC_MACROS_FLAG)
9360ab090deed8213613643729a7921ef0915ac704Chandler Carruth  list(APPEND SANITIZER_COMMON_CFLAGS -Wno-variadic-macros)
9460ab090deed8213613643729a7921ef0915ac704Chandler Carruthendif()
95721460bdcbc29605381ee7658e6d0c233da0da8dAlexey Samsonovcheck_cxx_compiler_flag(-Wno-c99-extensions SUPPORTS_NO_C99_EXTENSIONS_FLAG)
9660ab090deed8213613643729a7921ef0915ac704Chandler Carruthif(SUPPORTS_NO_C99_EXTENSIONS_FLAG)
9760ab090deed8213613643729a7921ef0915ac704Chandler Carruth  list(APPEND SANITIZER_COMMON_CFLAGS -Wno-c99-extensions)
9860ab090deed8213613643729a7921ef0915ac704Chandler Carruthendif()
990f7d4a4ad69ad20053d8b78c611853e778bd465aAlexey Samsonovif(APPLE)
1000f7d4a4ad69ad20053d8b78c611853e778bd465aAlexey Samsonov  list(APPEND SANITIZER_COMMON_CFLAGS -mmacosx-version-min=10.5)
1010f7d4a4ad69ad20053d8b78c611853e778bd465aAlexey Samsonovendif()
10260ab090deed8213613643729a7921ef0915ac704Chandler Carruth
103984f6cf119e26ec25463d58235d613bfea114127Chandler Carruth# Because compiler-rt spends a lot of time setting up custom compile flags,
104984f6cf119e26ec25463d58235d613bfea114127Chandler Carruth# define a handy helper function for it. The compile flags setting in CMake
105984f6cf119e26ec25463d58235d613bfea114127Chandler Carruth# has serious issues that make its syntax challenging at best.
106984f6cf119e26ec25463d58235d613bfea114127Chandler Carruthfunction(set_target_compile_flags target)
107984f6cf119e26ec25463d58235d613bfea114127Chandler Carruth  foreach(arg ${ARGN})
108984f6cf119e26ec25463d58235d613bfea114127Chandler Carruth    set(argstring "${argstring} ${arg}")
109984f6cf119e26ec25463d58235d613bfea114127Chandler Carruth  endforeach()
110984f6cf119e26ec25463d58235d613bfea114127Chandler Carruth  set_property(TARGET ${target} PROPERTY COMPILE_FLAGS "${argstring}")
111984f6cf119e26ec25463d58235d613bfea114127Chandler Carruthendfunction()
112984f6cf119e26ec25463d58235d613bfea114127Chandler Carruth
11334fc56c43f60be0b85a0e3ce30658fd98951f0b8Evgeniy Stepanovfunction(set_target_link_flags target)
11434fc56c43f60be0b85a0e3ce30658fd98951f0b8Evgeniy Stepanov  foreach(arg ${ARGN})
11534fc56c43f60be0b85a0e3ce30658fd98951f0b8Evgeniy Stepanov    set(argstring "${argstring} ${arg}")
11634fc56c43f60be0b85a0e3ce30658fd98951f0b8Evgeniy Stepanov  endforeach()
11734fc56c43f60be0b85a0e3ce30658fd98951f0b8Evgeniy Stepanov  set_property(TARGET ${target} PROPERTY LINK_FLAGS "${argstring}")
11834fc56c43f60be0b85a0e3ce30658fd98951f0b8Evgeniy Stepanovendfunction()
11934fc56c43f60be0b85a0e3ce30658fd98951f0b8Evgeniy Stepanov
12070a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov# Compute the Clang version from the LLVM version.
12170a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov# FIXME: We should be able to reuse CLANG_VERSION variable calculated
12270a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov#        in Clang cmake files, instead of copying the rules here.
12370a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonovstring(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
12470a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov       ${PACKAGE_VERSION})
12570a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov# Setup the paths where compiler-rt runtimes and headers should be stored.
1267e72e45e3b725ef29b82fd4386c6571d64f9b6ccEvgeniy Stepanovset(LIBCLANG_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
12770a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonovstring(TOLOWER ${CMAKE_SYSTEM_NAME} LIBCLANG_OS_DIR)
12870a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov
12970a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov# Install compiler-rt headers.
13070a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonovinstall(DIRECTORY include/
13170a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov  DESTINATION ${LIBCLANG_INSTALL_PATH}/include
13270a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov  FILES_MATCHING
13370a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov  PATTERN "*.h"
13470a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov  PATTERN ".svn" EXCLUDE
13570a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov  )
13670a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov
13770a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov# Call add_clang_compiler_rt_libraries to make sure that targets are built
13870a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov# and installed in the directories where Clang driver expects to find them.
13970a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonovmacro(add_clang_compiler_rt_libraries)
14070a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov  # Setup output directories so that clang in build tree works.
14170a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov  set_target_properties(${ARGN} PROPERTIES
14270a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov    ARCHIVE_OUTPUT_DIRECTORY
14370a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov      ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}
14470a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov    LIBRARY_OUTPUT_DIRECTORY
14570a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov      ${LLVM_BINARY_DIR}/lib/clang/${CLANG_VERSION}/lib/${LIBCLANG_OS_DIR}
14670a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov    )
14770a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov  # Add installation command.
14870a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov  install(TARGETS ${ARGN}
14970a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov    ARCHIVE DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}
15070a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov    LIBRARY DESTINATION ${LIBCLANG_INSTALL_PATH}/lib/${LIBCLANG_OS_DIR}
15170a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov    )
15270a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonovendmacro(add_clang_compiler_rt_libraries)
15370a7095e37289746fd20595457d517faf66dc6a4Alexey Samsonov
154d865fecddccebf898ceed24d096fc58fb29a6e57Chandler Carruth# Add the public header's directory to the includes for all of compiler-rt.
155d865fecddccebf898ceed24d096fc58fb29a6e57Chandler Carruthinclude_directories(include)
156d865fecddccebf898ceed24d096fc58fb29a6e57Chandler Carruth
157d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthadd_subdirectory(lib)
158d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruth
159d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthif(LLVM_INCLUDE_TESTS)
1602e82a8bf4cbe8b891f64a0081f64447af9e4ac06Chandler Carruth  # Currently the tests have not been ported to CMake, so disable this
1612e82a8bf4cbe8b891f64a0081f64447af9e4ac06Chandler Carruth  # directory.
1622e82a8bf4cbe8b891f64a0081f64447af9e4ac06Chandler Carruth  #
1632e82a8bf4cbe8b891f64a0081f64447af9e4ac06Chandler Carruth  #add_subdirectory(test)
164d51e0a05b4aefc85c356c9bddd8a24efc77721d9Chandler Carruthendif()
165