blob: 4d16ca7be6d1169ecf3e4072f558e21aef48d716 [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)
52 if(polarity)
53 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
79# Takes ${ARGN} and puts only supported architectures in @out_var list.
80function(filter_available_targets out_var)
81 set(archs ${${out_var}})
82 foreach(arch ${ARGN})
83 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
84 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
85 list(APPEND archs ${arch})
86 endif()
87 endforeach()
88 set(${out_var} ${archs} PARENT_SCOPE)
89endfunction()
90
91function(check_compile_definition def argstring out_var)
92 if("${def}" STREQUAL "")
93 set(${out_var} TRUE PARENT_SCOPE)
94 return()
95 endif()
96 cmake_push_check_state()
97 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${argstring}")
98 check_symbol_exists(${def} "" ${out_var})
99 cmake_pop_check_state()
100endfunction()
101
102# test_target_arch(<arch> <def> <target flags...>)
103# Checks if architecture is supported: runs host compiler with provided
104# flags to verify that:
105# 1) <def> is defined (if non-empty)
106# 2) simple file can be successfully built.
107# If successful, saves target flags for this architecture.
108macro(test_target_arch arch def)
109 set(TARGET_${arch}_CFLAGS ${ARGN})
110 set(argstring "")
111 foreach(arg ${ARGN})
112 set(argstring "${argstring} ${arg}")
113 endforeach()
114 check_compile_definition("${def}" "${argstring}" HAS_${arch}_DEF)
115 if(NOT HAS_${arch}_DEF)
116 set(CAN_TARGET_${arch} FALSE)
117 else()
118 set(argstring "${CMAKE_EXE_LINKER_FLAGS} ${argstring}")
119 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
120 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
121 OUTPUT_VARIABLE TARGET_${arch}_OUTPUT
122 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${argstring}")
123 endif()
124 if(${CAN_TARGET_${arch}})
125 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
126 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "${arch}" AND
127 COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE)
128 # Bail out if we cannot target the architecture we plan to test.
129 message(FATAL_ERROR "Cannot compile for ${arch}:\n${TARGET_${arch}_OUTPUT}")
130 endif()
131endmacro()