blob: 6c134aa20cd43ab9d54d177ba78f68d96718f15f [file] [log] [blame]
Chris Bieneman18929dd2016-05-09 21:45:52 +00001# The CompilerRT build system requires CMake version 2.8.8 or higher in order
2# to use its support for building convenience "libraries" as a collection of
3# .o files. This is particularly useful in producing larger, more complex
4# runtime libraries.
5
6include(CheckIncludeFile)
7check_include_file(unwind.h HAVE_UNWIND_H)
8
9# Top level target used to build all compiler-rt libraries.
10add_custom_target(compiler-rt ALL)
11
Chris Bienemanb6fb3832016-06-03 23:15:04 +000012# Setting these variables from an LLVM build is sufficient that compiler-rt can
13# construct the output paths, so it can behave as if it were in-tree here.
14if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION)
15 set(LLVM_TREE_AVAILABLE On)
16endif()
17
18if (LLVM_TREE_AVAILABLE)
Chris Bieneman18929dd2016-05-09 21:45:52 +000019 # Compute the Clang version from the LLVM version.
20 # FIXME: We should be able to reuse CLANG_VERSION variable calculated
21 # in Clang cmake files, instead of copying the rules here.
22 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
23 ${PACKAGE_VERSION})
24 # Setup the paths where compiler-rt runtimes and headers should be stored.
25 set(COMPILER_RT_OUTPUT_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION})
26 set(COMPILER_RT_EXEC_OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
27 set(COMPILER_RT_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
28 option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests."
29 ${LLVM_INCLUDE_TESTS})
30 option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered"
31 ${LLVM_ENABLE_WERROR})
32 # Use just-built Clang to compile/link tests on all platforms, except for
33 # Windows where we need to use clang-cl instead.
34 if(NOT MSVC)
35 set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
36 set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++)
37 else()
38 set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang.exe)
39 set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++.exe)
40 endif()
41else()
42 # Take output dir and install path from the user.
43 set(COMPILER_RT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH
44 "Path where built compiler-rt libraries should be stored.")
45 set(COMPILER_RT_EXEC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin CACHE PATH
46 "Path where built compiler-rt executables should be stored.")
47 set(COMPILER_RT_INSTALL_PATH ${CMAKE_INSTALL_PREFIX} CACHE PATH
48 "Path where built compiler-rt libraries should be installed.")
49 option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests." OFF)
50 option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered" OFF)
51 # Use a host compiler to compile/link tests.
52 set(COMPILER_RT_TEST_COMPILER ${CMAKE_C_COMPILER} CACHE PATH "Compiler to use for testing")
53 set(COMPILER_RT_TEST_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE PATH "C++ Compiler to use for testing")
54endif()
55
56if("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang[+]*$")
57 set(COMPILER_RT_TEST_COMPILER_ID Clang)
58elseif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang.*.exe$")
59 set(COMPILER_RT_TEST_COMPILER_ID Clang)
60else()
61 set(COMPILER_RT_TEST_COMPILER_ID GNU)
62endif()
63
Chris Bieneman18929dd2016-05-09 21:45:52 +000064string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR)
65set(COMPILER_RT_LIBRARY_OUTPUT_DIR
66 ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR})
67set(COMPILER_RT_LIBRARY_INSTALL_DIR
68 ${COMPILER_RT_INSTALL_PATH}/lib/${COMPILER_RT_OS_DIR})
69
70if(APPLE)
71 # On Darwin if /usr/include doesn't exist, the user probably has Xcode but not
72 # the command line tools. If this is the case, we need to find the OS X
73 # sysroot to pass to clang.
74 if(NOT EXISTS /usr/include)
75 execute_process(COMMAND xcodebuild -version -sdk macosx Path
76 OUTPUT_VARIABLE OSX_SYSROOT
77 ERROR_QUIET
78 OUTPUT_STRIP_TRAILING_WHITESPACE)
79 set(OSX_SYSROOT_FLAG "-isysroot${OSX_SYSROOT}")
80 endif()
81
82 option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" Off)
83 option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off)
84 option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off)
85endif()
86
87macro(test_targets)
88 # Find and run MSVC (not clang-cl) and get its version. This will tell clang-cl
89 # what version of MSVC to pretend to be so that the STL works.
90 set(MSVC_VERSION_FLAG "")
91 if (MSVC)
92 # Find and run MSVC (not clang-cl) and get its version. This will tell
93 # clang-cl what version of MSVC to pretend to be so that the STL works.
94 execute_process(COMMAND "$ENV{VSINSTALLDIR}/VC/bin/cl.exe"
95 OUTPUT_QUIET
96 ERROR_VARIABLE MSVC_COMPAT_VERSION
97 )
98 string(REGEX REPLACE "^.*Compiler Version ([0-9.]+) for .*$" "\\1"
99 MSVC_COMPAT_VERSION "${MSVC_COMPAT_VERSION}")
100 if (MSVC_COMPAT_VERSION MATCHES "^[0-9].+$")
101 set(MSVC_VERSION_FLAG "-fms-compatibility-version=${MSVC_COMPAT_VERSION}")
102 # Add this flag into the host build if this is clang-cl.
103 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
104 append("${MSVC_VERSION_FLAG}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Reid Klecknerd80ed422016-06-17 17:48:52 +0000105 elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
Etienne Bergeron73429462016-06-21 14:32:52 +0000106 # Add this flag to test compiles to suppress clang's auto-detection
107 # logic.
108 append("${MSVC_VERSION_FLAG}" COMPILER_RT_TEST_COMPILER_CFLAGS)
Chris Bieneman18929dd2016-05-09 21:45:52 +0000109 endif()
110 endif()
111 endif()
112
113 # Generate the COMPILER_RT_SUPPORTED_ARCH list.
114 if(ANDROID)
115 # Examine compiler output to determine target architecture.
116 detect_target_arch()
Chris Bienemance10b552016-06-28 16:30:23 +0000117 set(COMPILER_RT_OS_SUFFIX "-android")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000118 elseif(NOT APPLE) # Supported archs for Apple platforms are generated later
119 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "i[2-6]86|x86|amd64")
120 if(NOT MSVC)
121 test_target_arch(x86_64 "" "-m64")
122 # FIXME: We build runtimes for both i686 and i386, as "clang -m32" may
123 # target different variant than "$CMAKE_C_COMPILER -m32". This part should
124 # be gone after we resolve PR14109.
125 test_target_arch(i686 __i686__ "-m32")
126 test_target_arch(i386 __i386__ "-m32")
127 else()
128 if (CMAKE_SIZEOF_VOID_P EQUAL 4)
Etienne Bergeron73429462016-06-21 14:32:52 +0000129 test_target_arch(i386 "" "")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000130 else()
Etienne Bergeron73429462016-06-21 14:32:52 +0000131 test_target_arch(x86_64 "" "")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000132 endif()
133 endif()
134 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc")
135 TEST_BIG_ENDIAN(HOST_IS_BIG_ENDIAN)
136 if(HOST_IS_BIG_ENDIAN)
137 test_target_arch(powerpc64 "" "-m64")
138 else()
139 test_target_arch(powerpc64le "" "-m64")
140 endif()
141 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
142 test_target_arch(s390x "" "")
143 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mipsel|mips64el")
144 # Gcc doesn't accept -m32/-m64 so we do the next best thing and use
145 # -mips32r2/-mips64r2. We don't use -mips1/-mips3 because we want to match
146 # clang's default CPU's. In the 64-bit case, we must also specify the ABI
147 # since the default ABI differs between gcc and clang.
148 # FIXME: Ideally, we would build the N32 library too.
149 test_target_arch(mipsel "" "-mips32r2" "--target=mipsel-linux-gnu")
150 test_target_arch(mips64el "" "-mips64r2" "--target=mips64el-linux-gnu" "-mabi=n64")
151 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips")
152 test_target_arch(mips "" "-mips32r2" "--target=mips-linux-gnu")
153 test_target_arch(mips64 "" "-mips64r2" "--target=mips64-linux-gnu" "-mabi=n64")
154 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm")
155 test_target_arch(arm "" "-march=armv7-a" "-mfloat-abi=soft")
156 test_target_arch(armhf "" "-march=armv7-a" "-mfloat-abi=hard")
157 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch32")
158 test_target_arch(aarch32 "" "-march=armv8-a")
159 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch64")
160 test_target_arch(aarch64 "" "-march=armv8-a")
161 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm32")
162 test_target_arch(wasm32 "" "--target=wasm32-unknown-unknown")
163 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm64")
164 test_target_arch(wasm64 "" "--target=wasm64-unknown-unknown")
165 endif()
166 set(COMPILER_RT_OS_SUFFIX "")
167 endif()
168endmacro()