blob: da5a2a1f4f887c45bf1ea6e53977ace64f694172 [file] [log] [blame]
Alexey Samsonov163ab9d2013-01-18 16:05:21 +00001# Because compiler-rt spends a lot of time setting up custom compile flags,
2# define a handy helper function for it. The compile flags setting in CMake
3# has serious issues that make its syntax challenging at best.
4function(set_target_compile_flags target)
Aaron Ballmane927a172014-10-23 22:13:52 +00005 set(argstring "")
Alexey Samsonov163ab9d2013-01-18 16:05:21 +00006 foreach(arg ${ARGN})
7 set(argstring "${argstring} ${arg}")
8 endforeach()
9 set_property(TARGET ${target} PROPERTY COMPILE_FLAGS "${argstring}")
10endfunction()
11
12function(set_target_link_flags target)
Aaron Ballmane927a172014-10-23 22:13:52 +000013 set(argstring "")
Alexey Samsonov163ab9d2013-01-18 16:05:21 +000014 foreach(arg ${ARGN})
15 set(argstring "${argstring} ${arg}")
16 endforeach()
17 set_property(TARGET ${target} PROPERTY LINK_FLAGS "${argstring}")
18endfunction()
19
Peter Collingbournecbdea322013-10-25 23:03:34 +000020# Set the variable var_PYBOOL to True if var holds a true-ish string,
21# otherwise set it to False.
22macro(pythonize_bool var)
23 if (${var})
24 set(${var}_PYBOOL True)
25 else()
26 set(${var}_PYBOOL False)
27 endif()
28endmacro()
Alexey Samsonovb73db722014-02-18 07:52:40 +000029
Alexey Samsonov32956d62014-03-13 09:31:36 +000030# Appends value to all lists in ARGN, if the condition is true.
Kuba Brecka14c0c592014-10-15 22:47:54 +000031macro(append_list_if condition value)
Alexey Samsonov32956d62014-03-13 09:31:36 +000032 if(${condition})
33 foreach(list ${ARGN})
34 list(APPEND ${list} ${value})
35 endforeach()
Alexey Samsonovb73db722014-02-18 07:52:40 +000036 endif()
37endmacro()
38
Alexey Samsonovfe7e28c2014-03-13 11:31:10 +000039# Appends value to all strings in ARGN, if the condition is true.
40macro(append_string_if condition value)
41 if(${condition})
42 foreach(str ${ARGN})
43 set(${str} "${${str}} ${value}")
44 endforeach()
45 endif()
46endmacro()
47
Filipe Cabecinhasb08c76f2016-03-05 10:01:04 +000048macro(append_rtti_flag polarity list)
49 if(polarity)
50 append_list_if(COMPILER_RT_HAS_FRTTI_FLAG -frtti ${list})
51 append_list_if(COMPILER_RT_HAS_GR_FLAG /GR ${list})
52 else()
53 append_list_if(COMPILER_RT_HAS_FNO_RTTI_FLAG -fno-rtti ${list})
54 append_list_if(COMPILER_RT_HAS_GR_FLAG /GR- ${list})
55 endif()
Alexey Samsonovb73db722014-02-18 07:52:40 +000056endmacro()
Yury Gribov8e49b472015-04-09 08:06:49 +000057
58macro(append_have_file_definition filename varname list)
59 check_include_file("${filename}" "${varname}")
Reid Kleckneraa902962015-05-20 16:56:17 +000060 if (NOT ${varname})
Yury Gribov8e49b472015-04-09 08:06:49 +000061 set("${varname}" 0)
62 endif()
63 list(APPEND ${list} "${varname}=${${varname}}")
64endmacro()
Chris Bieneman361231e2015-08-13 20:38:16 +000065
Daniel Sandersde098c92016-01-27 09:28:01 +000066macro(list_intersect output input1 input2)
Chris Bieneman361231e2015-08-13 20:38:16 +000067 set(${output})
68 foreach(it ${${input1}})
69 list(FIND ${input2} ${it} index)
70 if( NOT (index EQUAL -1))
71 list(APPEND ${output} ${it})
72 endif()
73 endforeach()
74endmacro()
Chris Bieneman5a5b21d2016-02-17 16:57:38 +000075
76# Takes ${ARGN} and puts only supported architectures in @out_var list.
77function(filter_available_targets out_var)
78 set(archs ${${out_var}})
79 foreach(arch ${ARGN})
80 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
81 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
82 list(APPEND archs ${arch})
83 endif()
84 endforeach()
85 set(${out_var} ${archs} PARENT_SCOPE)
86endfunction()
87
88function(check_compile_definition def argstring out_var)
89 if("${def}" STREQUAL "")
90 set(${out_var} TRUE PARENT_SCOPE)
91 return()
92 endif()
93 cmake_push_check_state()
94 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${argstring}")
95 check_symbol_exists(${def} "" ${out_var})
96 cmake_pop_check_state()
97endfunction()
98
99# test_target_arch(<arch> <def> <target flags...>)
100# Checks if architecture is supported: runs host compiler with provided
101# flags to verify that:
102# 1) <def> is defined (if non-empty)
103# 2) simple file can be successfully built.
104# If successful, saves target flags for this architecture.
105macro(test_target_arch arch def)
106 set(TARGET_${arch}_CFLAGS ${ARGN})
107 set(argstring "")
108 foreach(arg ${ARGN})
109 set(argstring "${argstring} ${arg}")
110 endforeach()
111 check_compile_definition("${def}" "${argstring}" HAS_${arch}_DEF)
112 if(NOT HAS_${arch}_DEF)
113 set(CAN_TARGET_${arch} FALSE)
114 else()
115 set(argstring "${CMAKE_EXE_LINKER_FLAGS} ${argstring}")
116 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
117 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}"
118 OUTPUT_VARIABLE TARGET_${arch}_OUTPUT
119 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${argstring}")
120 endif()
121 if(${CAN_TARGET_${arch}})
122 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
123 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "${arch}" AND
124 COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE)
125 # Bail out if we cannot target the architecture we plan to test.
126 message(FATAL_ERROR "Cannot compile for ${arch}:\n${TARGET_${arch}_OUTPUT}")
127 endif()
128endmacro()