blob: 4ea9eb8cebcb90ec36e53ec913b861b9ba55ddee [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)
Chris Bienemana213e3e2016-08-19 22:17:46 +000011add_custom_target(install-compiler-rt)
Etienne Bergerona088b362016-07-11 21:51:56 +000012set_target_properties(compiler-rt PROPERTIES FOLDER "Compiler-RT Misc")
Chris Bieneman18929dd2016-05-09 21:45:52 +000013
Chris Bienemanb6fb3832016-06-03 23:15:04 +000014# Setting these variables from an LLVM build is sufficient that compiler-rt can
15# construct the output paths, so it can behave as if it were in-tree here.
16if (LLVM_LIBRARY_OUTPUT_INTDIR AND LLVM_RUNTIME_OUTPUT_INTDIR AND PACKAGE_VERSION)
17 set(LLVM_TREE_AVAILABLE On)
18endif()
19
20if (LLVM_TREE_AVAILABLE)
Chris Bieneman18929dd2016-05-09 21:45:52 +000021 # Compute the Clang version from the LLVM version.
22 # FIXME: We should be able to reuse CLANG_VERSION variable calculated
23 # in Clang cmake files, instead of copying the rules here.
24 string(REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?" CLANG_VERSION
25 ${PACKAGE_VERSION})
26 # Setup the paths where compiler-rt runtimes and headers should be stored.
27 set(COMPILER_RT_OUTPUT_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/clang/${CLANG_VERSION})
28 set(COMPILER_RT_EXEC_OUTPUT_DIR ${LLVM_RUNTIME_OUTPUT_INTDIR})
29 set(COMPILER_RT_INSTALL_PATH lib${LLVM_LIBDIR_SUFFIX}/clang/${CLANG_VERSION})
30 option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests."
31 ${LLVM_INCLUDE_TESTS})
32 option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered"
33 ${LLVM_ENABLE_WERROR})
34 # Use just-built Clang to compile/link tests on all platforms, except for
35 # Windows where we need to use clang-cl instead.
36 if(NOT MSVC)
37 set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang)
38 set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++)
39 else()
40 set(COMPILER_RT_TEST_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang.exe)
41 set(COMPILER_RT_TEST_CXX_COMPILER ${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++.exe)
42 endif()
43else()
44 # Take output dir and install path from the user.
45 set(COMPILER_RT_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH
46 "Path where built compiler-rt libraries should be stored.")
47 set(COMPILER_RT_EXEC_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin CACHE PATH
48 "Path where built compiler-rt executables should be stored.")
49 set(COMPILER_RT_INSTALL_PATH ${CMAKE_INSTALL_PREFIX} CACHE PATH
50 "Path where built compiler-rt libraries should be installed.")
51 option(COMPILER_RT_INCLUDE_TESTS "Generate and build compiler-rt unit tests." OFF)
52 option(COMPILER_RT_ENABLE_WERROR "Fail and stop if warning is triggered" OFF)
53 # Use a host compiler to compile/link tests.
54 set(COMPILER_RT_TEST_COMPILER ${CMAKE_C_COMPILER} CACHE PATH "Compiler to use for testing")
55 set(COMPILER_RT_TEST_CXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE PATH "C++ Compiler to use for testing")
56endif()
57
58if("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang[+]*$")
59 set(COMPILER_RT_TEST_COMPILER_ID Clang)
60elseif("${COMPILER_RT_TEST_COMPILER}" MATCHES "clang.*.exe$")
61 set(COMPILER_RT_TEST_COMPILER_ID Clang)
62else()
63 set(COMPILER_RT_TEST_COMPILER_ID GNU)
64endif()
65
Chris Bieneman18929dd2016-05-09 21:45:52 +000066string(TOLOWER ${CMAKE_SYSTEM_NAME} COMPILER_RT_OS_DIR)
67set(COMPILER_RT_LIBRARY_OUTPUT_DIR
68 ${COMPILER_RT_OUTPUT_DIR}/lib/${COMPILER_RT_OS_DIR})
69set(COMPILER_RT_LIBRARY_INSTALL_DIR
70 ${COMPILER_RT_INSTALL_PATH}/lib/${COMPILER_RT_OS_DIR})
71
72if(APPLE)
73 # On Darwin if /usr/include doesn't exist, the user probably has Xcode but not
74 # the command line tools. If this is the case, we need to find the OS X
75 # sysroot to pass to clang.
76 if(NOT EXISTS /usr/include)
77 execute_process(COMMAND xcodebuild -version -sdk macosx Path
78 OUTPUT_VARIABLE OSX_SYSROOT
79 ERROR_QUIET
80 OUTPUT_STRIP_TRAILING_WHITESPACE)
81 set(OSX_SYSROOT_FLAG "-isysroot${OSX_SYSROOT}")
82 endif()
83
84 option(COMPILER_RT_ENABLE_IOS "Enable building for iOS" Off)
85 option(COMPILER_RT_ENABLE_WATCHOS "Enable building for watchOS - Experimental" Off)
86 option(COMPILER_RT_ENABLE_TVOS "Enable building for tvOS - Experimental" Off)
87endif()
88
Francis Ricci4a0c5cd2016-09-07 20:32:48 +000089if(WIN32 AND NOT MINGW AND NOT CYGWIN)
90 set(CMAKE_SHARED_LIBRARY_PREFIX_C "")
91 set(CMAKE_SHARED_LIBRARY_PREFIX_CXX "")
92 set(CMAKE_STATIC_LIBRARY_PREFIX_C "")
93 set(CMAKE_STATIC_LIBRARY_PREFIX_CXX "")
94 set(CMAKE_STATIC_LIBRARY_SUFFIX_C ".lib")
95 set(CMAKE_STATIC_LIBRARY_SUFFIX_CXX ".lib")
96endif()
97
Chris Bieneman18929dd2016-05-09 21:45:52 +000098macro(test_targets)
99 # Find and run MSVC (not clang-cl) and get its version. This will tell clang-cl
100 # what version of MSVC to pretend to be so that the STL works.
101 set(MSVC_VERSION_FLAG "")
102 if (MSVC)
103 # Find and run MSVC (not clang-cl) and get its version. This will tell
104 # clang-cl what version of MSVC to pretend to be so that the STL works.
105 execute_process(COMMAND "$ENV{VSINSTALLDIR}/VC/bin/cl.exe"
106 OUTPUT_QUIET
107 ERROR_VARIABLE MSVC_COMPAT_VERSION
108 )
109 string(REGEX REPLACE "^.*Compiler Version ([0-9.]+) for .*$" "\\1"
110 MSVC_COMPAT_VERSION "${MSVC_COMPAT_VERSION}")
111 if (MSVC_COMPAT_VERSION MATCHES "^[0-9].+$")
112 set(MSVC_VERSION_FLAG "-fms-compatibility-version=${MSVC_COMPAT_VERSION}")
113 # Add this flag into the host build if this is clang-cl.
114 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
115 append("${MSVC_VERSION_FLAG}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Reid Klecknerd80ed422016-06-17 17:48:52 +0000116 elseif (COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
Etienne Bergeron73429462016-06-21 14:32:52 +0000117 # Add this flag to test compiles to suppress clang's auto-detection
118 # logic.
119 append("${MSVC_VERSION_FLAG}" COMPILER_RT_TEST_COMPILER_CFLAGS)
Chris Bieneman18929dd2016-05-09 21:45:52 +0000120 endif()
121 endif()
122 endif()
123
124 # Generate the COMPILER_RT_SUPPORTED_ARCH list.
125 if(ANDROID)
126 # Examine compiler output to determine target architecture.
127 detect_target_arch()
Chris Bienemance10b552016-06-28 16:30:23 +0000128 set(COMPILER_RT_OS_SUFFIX "-android")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000129 elseif(NOT APPLE) # Supported archs for Apple platforms are generated later
130 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "i[2-6]86|x86|amd64")
131 if(NOT MSVC)
132 test_target_arch(x86_64 "" "-m64")
133 # FIXME: We build runtimes for both i686 and i386, as "clang -m32" may
134 # target different variant than "$CMAKE_C_COMPILER -m32". This part should
135 # be gone after we resolve PR14109.
136 test_target_arch(i686 __i686__ "-m32")
137 test_target_arch(i386 __i386__ "-m32")
138 else()
139 if (CMAKE_SIZEOF_VOID_P EQUAL 4)
Etienne Bergeron73429462016-06-21 14:32:52 +0000140 test_target_arch(i386 "" "")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000141 else()
Etienne Bergeron73429462016-06-21 14:32:52 +0000142 test_target_arch(x86_64 "" "")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000143 endif()
144 endif()
145 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "powerpc")
146 TEST_BIG_ENDIAN(HOST_IS_BIG_ENDIAN)
147 if(HOST_IS_BIG_ENDIAN)
148 test_target_arch(powerpc64 "" "-m64")
149 else()
150 test_target_arch(powerpc64le "" "-m64")
151 endif()
152 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "s390x")
153 test_target_arch(s390x "" "")
154 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mipsel|mips64el")
155 # Gcc doesn't accept -m32/-m64 so we do the next best thing and use
156 # -mips32r2/-mips64r2. We don't use -mips1/-mips3 because we want to match
157 # clang's default CPU's. In the 64-bit case, we must also specify the ABI
158 # since the default ABI differs between gcc and clang.
159 # FIXME: Ideally, we would build the N32 library too.
160 test_target_arch(mipsel "" "-mips32r2" "--target=mipsel-linux-gnu")
Mohit K. Bhakkadf9fc3de2016-07-18 09:23:23 +0000161 test_target_arch(mips64el "" "-mips64r2" "--target=mips64el-linux-gnu" "-mabi=64")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000162 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips")
163 test_target_arch(mips "" "-mips32r2" "--target=mips-linux-gnu")
Mohit K. Bhakkadf9fc3de2016-07-18 09:23:23 +0000164 test_target_arch(mips64 "" "-mips64r2" "--target=mips64-linux-gnu" "-mabi=64")
Chris Bieneman18929dd2016-05-09 21:45:52 +0000165 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm")
Saleem Abdulrasoolbcbb9e62016-08-05 16:53:05 +0000166 if(WIN32)
167 test_target_arch(arm "" "" "")
168 else()
169 test_target_arch(arm "" "-march=armv7-a" "-mfloat-abi=soft")
170 test_target_arch(armhf "" "-march=armv7-a" "-mfloat-abi=hard")
171 endif()
Chris Bieneman18929dd2016-05-09 21:45:52 +0000172 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch32")
173 test_target_arch(aarch32 "" "-march=armv8-a")
174 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "aarch64")
175 test_target_arch(aarch64 "" "-march=armv8-a")
176 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm32")
177 test_target_arch(wasm32 "" "--target=wasm32-unknown-unknown")
178 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm64")
179 test_target_arch(wasm64 "" "--target=wasm64-unknown-unknown")
180 endif()
181 set(COMPILER_RT_OS_SUFFIX "")
182 endif()
183endmacro()