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. 4function(set_target_compile_flags target) 5 set(argstring "") 6 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) 13 set(argstring "") 14 foreach(arg ${ARGN}) 15 set(argstring "${argstring} ${arg}") 16 endforeach() 17 set_property(TARGET ${target} PROPERTY LINK_FLAGS "${argstring}") 18endfunction() 19 20# 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() 29 30# Appends value to all lists in ARGN, if the condition is true. 31macro(append_list_if condition value) 32 if(${condition}) 33 foreach(list ${ARGN}) 34 list(APPEND ${list} ${value}) 35 endforeach() 36 endif() 37endmacro() 38 39# 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 48macro(append_no_rtti_flag list) 49 append_list_if(COMPILER_RT_HAS_FNO_RTTI_FLAG -fno-rtti ${list}) 50 append_list_if(COMPILER_RT_HAS_GR_FLAG /GR- ${list}) 51endmacro() 52 53macro(append_have_file_definition filename varname list) 54 check_include_file("${filename}" "${varname}") 55 if (NOT ${varname}) 56 set("${varname}" 0) 57 endif() 58 list(APPEND ${list} "${varname}=${${varname}}") 59endmacro() 60 61macro(list_intersect output input1 input2) 62 set(${output}) 63 foreach(it ${${input1}}) 64 list(FIND ${input2} ${it} index) 65 if( NOT (index EQUAL -1)) 66 list(APPEND ${output} ${it}) 67 endif() 68 endforeach() 69endmacro() 70 71# Takes ${ARGN} and puts only supported architectures in @out_var list. 72function(filter_available_targets out_var) 73 set(archs ${${out_var}}) 74 foreach(arch ${ARGN}) 75 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX) 76 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch}) 77 list(APPEND archs ${arch}) 78 endif() 79 endforeach() 80 set(${out_var} ${archs} PARENT_SCOPE) 81endfunction() 82 83function(check_compile_definition def argstring out_var) 84 if("${def}" STREQUAL "") 85 set(${out_var} TRUE PARENT_SCOPE) 86 return() 87 endif() 88 cmake_push_check_state() 89 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${argstring}") 90 check_symbol_exists(${def} "" ${out_var}) 91 cmake_pop_check_state() 92endfunction() 93 94# test_target_arch(<arch> <def> <target flags...>) 95# Checks if architecture is supported: runs host compiler with provided 96# flags to verify that: 97# 1) <def> is defined (if non-empty) 98# 2) simple file can be successfully built. 99# If successful, saves target flags for this architecture. 100macro(test_target_arch arch def) 101 set(TARGET_${arch}_CFLAGS ${ARGN}) 102 set(argstring "") 103 foreach(arg ${ARGN}) 104 set(argstring "${argstring} ${arg}") 105 endforeach() 106 check_compile_definition("${def}" "${argstring}" HAS_${arch}_DEF) 107 if(NOT HAS_${arch}_DEF) 108 set(CAN_TARGET_${arch} FALSE) 109 else() 110 set(argstring "${CMAKE_EXE_LINKER_FLAGS} ${argstring}") 111 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE} 112 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS}" 113 OUTPUT_VARIABLE TARGET_${arch}_OUTPUT 114 CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS:STRING=${argstring}") 115 endif() 116 if(${CAN_TARGET_${arch}}) 117 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch}) 118 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "${arch}" AND 119 COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE) 120 # Bail out if we cannot target the architecture we plan to test. 121 message(FATAL_ERROR "Cannot compile for ${arch}:\n${TARGET_${arch}_OUTPUT}") 122 endif() 123endmacro() 124