Alexey Samsonov | 163ab9d | 2013-01-18 16:05:21 +0000 | [diff] [blame] | 1 | # 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. |
| 4 | function(set_target_compile_flags target) |
Aaron Ballman | e927a17 | 2014-10-23 22:13:52 +0000 | [diff] [blame] | 5 | set(argstring "") |
Alexey Samsonov | 163ab9d | 2013-01-18 16:05:21 +0000 | [diff] [blame] | 6 | foreach(arg ${ARGN}) |
| 7 | set(argstring "${argstring} ${arg}") |
| 8 | endforeach() |
| 9 | set_property(TARGET ${target} PROPERTY COMPILE_FLAGS "${argstring}") |
| 10 | endfunction() |
| 11 | |
| 12 | function(set_target_link_flags target) |
Aaron Ballman | e927a17 | 2014-10-23 22:13:52 +0000 | [diff] [blame] | 13 | set(argstring "") |
Alexey Samsonov | 163ab9d | 2013-01-18 16:05:21 +0000 | [diff] [blame] | 14 | foreach(arg ${ARGN}) |
| 15 | set(argstring "${argstring} ${arg}") |
| 16 | endforeach() |
| 17 | set_property(TARGET ${target} PROPERTY LINK_FLAGS "${argstring}") |
| 18 | endfunction() |
| 19 | |
Peter Collingbourne | cbdea32 | 2013-10-25 23:03:34 +0000 | [diff] [blame] | 20 | # Set the variable var_PYBOOL to True if var holds a true-ish string, |
| 21 | # otherwise set it to False. |
| 22 | macro(pythonize_bool var) |
| 23 | if (${var}) |
| 24 | set(${var}_PYBOOL True) |
| 25 | else() |
| 26 | set(${var}_PYBOOL False) |
| 27 | endif() |
| 28 | endmacro() |
Alexey Samsonov | b73db72 | 2014-02-18 07:52:40 +0000 | [diff] [blame] | 29 | |
Alexey Samsonov | 32956d6 | 2014-03-13 09:31:36 +0000 | [diff] [blame] | 30 | # Appends value to all lists in ARGN, if the condition is true. |
Kuba Brecka | 14c0c59 | 2014-10-15 22:47:54 +0000 | [diff] [blame] | 31 | macro(append_list_if condition value) |
Alexey Samsonov | 32956d6 | 2014-03-13 09:31:36 +0000 | [diff] [blame] | 32 | if(${condition}) |
| 33 | foreach(list ${ARGN}) |
| 34 | list(APPEND ${list} ${value}) |
| 35 | endforeach() |
Alexey Samsonov | b73db72 | 2014-02-18 07:52:40 +0000 | [diff] [blame] | 36 | endif() |
| 37 | endmacro() |
| 38 | |
Alexey Samsonov | fe7e28c | 2014-03-13 11:31:10 +0000 | [diff] [blame] | 39 | # Appends value to all strings in ARGN, if the condition is true. |
| 40 | macro(append_string_if condition value) |
| 41 | if(${condition}) |
| 42 | foreach(str ${ARGN}) |
| 43 | set(${str} "${${str}} ${value}") |
| 44 | endforeach() |
| 45 | endif() |
| 46 | endmacro() |
| 47 | |
Filipe Cabecinhas | b08c76f | 2016-03-05 10:01:04 +0000 | [diff] [blame] | 48 | macro(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 Samsonov | b73db72 | 2014-02-18 07:52:40 +0000 | [diff] [blame] | 56 | endmacro() |
Yury Gribov | 8e49b47 | 2015-04-09 08:06:49 +0000 | [diff] [blame] | 57 | |
| 58 | macro(append_have_file_definition filename varname list) |
| 59 | check_include_file("${filename}" "${varname}") |
Reid Kleckner | aa90296 | 2015-05-20 16:56:17 +0000 | [diff] [blame] | 60 | if (NOT ${varname}) |
Yury Gribov | 8e49b47 | 2015-04-09 08:06:49 +0000 | [diff] [blame] | 61 | set("${varname}" 0) |
| 62 | endif() |
| 63 | list(APPEND ${list} "${varname}=${${varname}}") |
| 64 | endmacro() |
Chris Bieneman | 361231e | 2015-08-13 20:38:16 +0000 | [diff] [blame] | 65 | |
Daniel Sanders | de098c9 | 2016-01-27 09:28:01 +0000 | [diff] [blame] | 66 | macro(list_intersect output input1 input2) |
Chris Bieneman | 361231e | 2015-08-13 20:38:16 +0000 | [diff] [blame] | 67 | 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() |
| 74 | endmacro() |
Chris Bieneman | 5a5b21d | 2016-02-17 16:57:38 +0000 | [diff] [blame] | 75 | |
| 76 | # Takes ${ARGN} and puts only supported architectures in @out_var list. |
| 77 | function(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) |
| 86 | endfunction() |
| 87 | |
| 88 | function(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() |
| 97 | endfunction() |
| 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. |
| 105 | macro(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() |
| 128 | endmacro() |