blob: c337523cd944e11edf743a74efd61415b8e935ac [file] [log] [blame]
Chris Bieneman1dd58a72016-04-28 18:22:01 +00001include(CMakePushCheckState)
2include(CheckSymbolExists)
3
Alexey Samsonov163ab9d2013-01-18 16:05:21 +00004# Because compiler-rt spends a lot of time setting up custom compile flags,
5# define a handy helper function for it. The compile flags setting in CMake
6# has serious issues that make its syntax challenging at best.
7function(set_target_compile_flags target)
Aaron Ballmane927a172014-10-23 22:13:52 +00008 set(argstring "")
Alexey Samsonov163ab9d2013-01-18 16:05:21 +00009 foreach(arg ${ARGN})
10 set(argstring "${argstring} ${arg}")
11 endforeach()
12 set_property(TARGET ${target} PROPERTY COMPILE_FLAGS "${argstring}")
13endfunction()
14
15function(set_target_link_flags target)
Aaron Ballmane927a172014-10-23 22:13:52 +000016 set(argstring "")
Alexey Samsonov163ab9d2013-01-18 16:05:21 +000017 foreach(arg ${ARGN})
18 set(argstring "${argstring} ${arg}")
19 endforeach()
20 set_property(TARGET ${target} PROPERTY LINK_FLAGS "${argstring}")
21endfunction()
22
Peter Collingbournecbdea322013-10-25 23:03:34 +000023# Set the variable var_PYBOOL to True if var holds a true-ish string,
24# otherwise set it to False.
25macro(pythonize_bool var)
26 if (${var})
27 set(${var}_PYBOOL True)
28 else()
29 set(${var}_PYBOOL False)
30 endif()
31endmacro()
Alexey Samsonovb73db722014-02-18 07:52:40 +000032
Alexey Samsonov32956d62014-03-13 09:31:36 +000033# Appends value to all lists in ARGN, if the condition is true.
Kuba Brecka14c0c592014-10-15 22:47:54 +000034macro(append_list_if condition value)
Alexey Samsonov32956d62014-03-13 09:31:36 +000035 if(${condition})
36 foreach(list ${ARGN})
37 list(APPEND ${list} ${value})
38 endforeach()
Alexey Samsonovb73db722014-02-18 07:52:40 +000039 endif()
40endmacro()
41
Alexey Samsonovfe7e28c2014-03-13 11:31:10 +000042# Appends value to all strings in ARGN, if the condition is true.
43macro(append_string_if condition value)
44 if(${condition})
45 foreach(str ${ARGN})
46 set(${str} "${${str}} ${value}")
47 endforeach()
48 endif()
49endmacro()
50
Filipe Cabecinhasb08c76f2016-03-05 10:01:04 +000051macro(append_rtti_flag polarity list)
Filipe Cabecinhase5914d822016-08-22 18:30:37 +000052 if(${polarity})
Filipe Cabecinhasb08c76f2016-03-05 10:01:04 +000053 append_list_if(COMPILER_RT_HAS_FRTTI_FLAG -frtti ${list})
54 append_list_if(COMPILER_RT_HAS_GR_FLAG /GR ${list})
55 else()
56 append_list_if(COMPILER_RT_HAS_FNO_RTTI_FLAG -fno-rtti ${list})
57 append_list_if(COMPILER_RT_HAS_GR_FLAG /GR- ${list})
58 endif()
Alexey Samsonovb73db722014-02-18 07:52:40 +000059endmacro()
Yury Gribov8e49b472015-04-09 08:06:49 +000060
61macro(append_have_file_definition filename varname list)
62 check_include_file("${filename}" "${varname}")
Reid Kleckneraa902962015-05-20 16:56:17 +000063 if (NOT ${varname})
Yury Gribov8e49b472015-04-09 08:06:49 +000064 set("${varname}" 0)
65 endif()
66 list(APPEND ${list} "${varname}=${${varname}}")
67endmacro()
Chris Bieneman361231e2015-08-13 20:38:16 +000068
Daniel Sandersde098c92016-01-27 09:28:01 +000069macro(list_intersect output input1 input2)
Chris Bieneman361231e2015-08-13 20:38:16 +000070 set(${output})
71 foreach(it ${${input1}})
72 list(FIND ${input2} ${it} index)
73 if( NOT (index EQUAL -1))
74 list(APPEND ${output} ${it})
75 endif()
76 endforeach()
77endmacro()
Chris Bieneman5a5b21d2016-02-17 16:57:38 +000078
Saleem Abdulrasoolb6ced622016-08-19 15:13:21 +000079function(list_replace input_list old new)
80 set(replaced_list)
81 foreach(item ${${input_list}})
82 if(${item} STREQUAL ${old})
83 list(APPEND replaced_list ${new})
84 else()
85 list(APPEND replaced_list ${item})
86 endif()
87 endforeach()
88 set(${input_list} "${replaced_list}" PARENT_SCOPE)
89endfunction()
90
Chris Bieneman5a5b21d2016-02-17 16:57:38 +000091# Takes ${ARGN} and puts only supported architectures in @out_var list.
92function(filter_available_targets out_var)
93 set(archs ${${out_var}})
94 foreach(arch ${ARGN})
95 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
96 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
97 list(APPEND archs ${arch})
98 endif()
99 endforeach()
100 set(${out_var} ${archs} PARENT_SCOPE)
101endfunction()
102
Petr Hosekf332d192016-12-12 23:14:02 +0000103# Add $arch as supported with no additional flags.
104macro(add_default_target_arch arch)
105 set(TARGET_${arch}_CFLAGS "")
106 set(CAN_TARGET_${arch} 1)
107 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
108endmacro()
109
Chris Bieneman5a5b21d2016-02-17 16:57:38 +0000110function(check_compile_definition def argstring out_var)
111 if("${def}" STREQUAL "")
112 set(${out_var} TRUE PARENT_SCOPE)
113 return()
114 endif()
115 cmake_push_check_state()
116 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${argstring}")
117 check_symbol_exists(${def} "" ${out_var})
118 cmake_pop_check_state()
119endfunction()
120
121# test_target_arch(<arch> <def> <target flags...>)
122# Checks if architecture is supported: runs host compiler with provided
123# flags to verify that:
124# 1) <def> is defined (if non-empty)
125# 2) simple file can be successfully built.
126# If successful, saves target flags for this architecture.
127macro(test_target_arch arch def)
128 set(TARGET_${arch}_CFLAGS ${ARGN})
Francis Ricci17781c72017-01-10 04:33:04 +0000129 set(TARGET_${arch}_LINK_FLAGS ${ARGN})
Chris Bieneman5a5b21d2016-02-17 16:57:38 +0000130 set(argstring "")
131 foreach(arg ${ARGN})
132 set(argstring "${argstring} ${arg}")
133 endforeach()
134 check_compile_definition("${def}" "${argstring}" HAS_${arch}_DEF)
Daniel Sandersaea8b552016-07-22 10:15:09 +0000135 if(NOT DEFINED CAN_TARGET_${arch})
136 if(NOT HAS_${arch}_DEF)
137 set(CAN_TARGET_${arch} FALSE)
138 elseif(TEST_COMPILE_ONLY)
139 try_compile_only(CAN_TARGET_${arch} ${TARGET_${arch}_CFLAGS})
140 else()
141 set(argstring "${CMAKE_EXE_LINKER_FLAGS} ${argstring}")
Jonas Hahnfeldb2978412016-09-21 05:44:06 +0000142 set(FLAG_NO_EXCEPTIONS "")
143 if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG)
144 set(FLAG_NO_EXCEPTIONS " -fno-exceptions ")
145 endif()
Daniel Sandersaea8b552016-07-22 10:15:09 +0000146 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
Jonas Hahnfeldb2978412016-09-21 05:44:06 +0000147 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS} ${FLAG_NO_EXCEPTIONS}"
Daniel Sandersaea8b552016-07-22 10:15:09 +0000148 OUTPUT_VARIABLE TARGET_${arch}_OUTPUT
149 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${argstring}")
150 endif()
Chris Bieneman5a5b21d2016-02-17 16:57:38 +0000151 endif()
152 if(${CAN_TARGET_${arch}})
153 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
Francis Riccib04a7212016-07-20 18:06:31 +0000154 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" STREQUAL "${arch}" AND
Chris Bieneman5a5b21d2016-02-17 16:57:38 +0000155 COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE)
156 # Bail out if we cannot target the architecture we plan to test.
157 message(FATAL_ERROR "Cannot compile for ${arch}:\n${TARGET_${arch}_OUTPUT}")
158 endif()
159endmacro()
Chris Bieneman648d3bc2016-06-03 17:34:02 +0000160
161macro(detect_target_arch)
162 check_symbol_exists(__arm__ "" __ARM)
163 check_symbol_exists(__aarch64__ "" __AARCH64)
164 check_symbol_exists(__x86_64__ "" __X86_64)
165 check_symbol_exists(__i686__ "" __I686)
166 check_symbol_exists(__i386__ "" __I386)
167 check_symbol_exists(__mips__ "" __MIPS)
168 check_symbol_exists(__mips64__ "" __MIPS64)
169 check_symbol_exists(__s390x__ "" __S390X)
170 check_symbol_exists(__wasm32__ "" __WEBASSEMBLY32)
171 check_symbol_exists(__wasm64__ "" __WEBASSEMBLY64)
172 if(__ARM)
173 add_default_target_arch(arm)
174 elseif(__AARCH64)
175 add_default_target_arch(aarch64)
176 elseif(__X86_64)
177 add_default_target_arch(x86_64)
178 elseif(__I686)
179 add_default_target_arch(i686)
180 elseif(__I386)
181 add_default_target_arch(i386)
182 elseif(__MIPS64) # must be checked before __MIPS
183 add_default_target_arch(mips64)
184 elseif(__MIPS)
185 add_default_target_arch(mips)
186 elseif(__S390X)
187 add_default_target_arch(s390x)
188 elseif(__WEBASSEMBLY32)
189 add_default_target_arch(wasm32)
190 elseif(__WEBASSEMBLY64)
191 add_default_target_arch(wasm64)
192 endif()
193endmacro()
Jonas Hahnfeld9b2c3ab2016-08-02 05:51:05 +0000194
195macro(load_llvm_config)
196 if (NOT LLVM_CONFIG_PATH)
197 find_program(LLVM_CONFIG_PATH "llvm-config"
198 DOC "Path to llvm-config binary")
199 if (NOT LLVM_CONFIG_PATH)
200 message(FATAL_ERROR "llvm-config not found: specify LLVM_CONFIG_PATH")
201 endif()
202 endif()
203 execute_process(
204 COMMAND ${LLVM_CONFIG_PATH} "--obj-root" "--bindir" "--libdir" "--src-root"
205 RESULT_VARIABLE HAD_ERROR
206 OUTPUT_VARIABLE CONFIG_OUTPUT)
207 if (HAD_ERROR)
208 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
209 endif()
210 string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";" CONFIG_OUTPUT ${CONFIG_OUTPUT})
Michal Gorny697026f2016-09-14 13:42:31 +0000211 list(GET CONFIG_OUTPUT 0 BINARY_DIR)
212 list(GET CONFIG_OUTPUT 1 TOOLS_BINARY_DIR)
213 list(GET CONFIG_OUTPUT 2 LIBRARY_DIR)
214 list(GET CONFIG_OUTPUT 3 MAIN_SRC_DIR)
215
216 set(LLVM_BINARY_DIR ${BINARY_DIR} CACHE PATH "Path to LLVM build tree")
217 set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
218 set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
219 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
Jonas Hahnfeld9b2c3ab2016-08-02 05:51:05 +0000220
221 # Make use of LLVM CMake modules.
Michal Gorny72f8c5f2017-01-09 23:23:56 +0000222 # --cmakedir is supported since llvm r291218 (4.0 release)
223 execute_process(
224 COMMAND ${LLVM_CONFIG_PATH} --cmakedir
225 RESULT_VARIABLE HAD_ERROR
226 OUTPUT_VARIABLE CONFIG_OUTPUT)
227 if(NOT HAD_ERROR)
228 string(STRIP "${CONFIG_OUTPUT}" LLVM_CMAKE_PATH)
229 else()
230 file(TO_CMAKE_PATH ${LLVM_BINARY_DIR} LLVM_BINARY_DIR_CMAKE_STYLE)
231 set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR_CMAKE_STYLE}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
232 endif()
233
Jonas Hahnfeld9b2c3ab2016-08-02 05:51:05 +0000234 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
235 # Get some LLVM variables from LLVMConfig.
236 include("${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
237
238 set(LLVM_LIBRARY_OUTPUT_INTDIR
Peter Collingbourne95d5b9c2016-09-22 20:51:48 +0000239 ${LLVM_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
Jonas Hahnfeld9b2c3ab2016-08-02 05:51:05 +0000240endmacro()
241
242macro(construct_compiler_rt_default_triple)
Petr Hosekf332d192016-12-12 23:14:02 +0000243 if(COMPILER_RT_DEFAULT_TARGET_ONLY)
244 if(DEFINED COMPILER_RT_DEFAULT_TARGET_TRIPLE)
245 message(FATAL_ERROR "COMPILER_RT_DEFAULT_TARGET_TRIPLE isn't supported when building for default target only")
246 endif()
247 set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${CMAKE_C_COMPILER_TARGET})
248 else()
249 set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${TARGET_TRIPLE} CACHE STRING
250 "Default triple for which compiler-rt runtimes will be built.")
251 endif()
252
Jonas Hahnfeld9b2c3ab2016-08-02 05:51:05 +0000253 if(DEFINED COMPILER_RT_TEST_TARGET_TRIPLE)
254 # Backwards compatibility: this variable used to be called
255 # COMPILER_RT_TEST_TARGET_TRIPLE.
256 set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${COMPILER_RT_TEST_TARGET_TRIPLE})
257 endif()
258
259 string(REPLACE "-" ";" TARGET_TRIPLE_LIST ${COMPILER_RT_DEFAULT_TARGET_TRIPLE})
260 list(GET TARGET_TRIPLE_LIST 0 COMPILER_RT_DEFAULT_TARGET_ARCH)
261 list(GET TARGET_TRIPLE_LIST 1 COMPILER_RT_DEFAULT_TARGET_OS)
Petr Hosekf332d192016-12-12 23:14:02 +0000262 list(LENGTH TARGET_TRIPLE_LIST TARGET_TRIPLE_LIST_LENGTH)
263 if(TARGET_TRIPLE_LIST_LENGTH GREATER 2)
264 list(GET TARGET_TRIPLE_LIST 2 COMPILER_RT_DEFAULT_TARGET_ABI)
265 endif()
Jonas Hahnfeld9b2c3ab2016-08-02 05:51:05 +0000266 # Determine if test target triple is specified explicitly, and doesn't match the
267 # default.
268 if(NOT COMPILER_RT_DEFAULT_TARGET_TRIPLE STREQUAL TARGET_TRIPLE)
269 set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE TRUE)
270 else()
271 set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE FALSE)
272 endif()
273endmacro()