HandleLibunwindFlags.cmake 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # HandleLibcxxFlags - A set of macros used to setup the flags used to compile
  2. # and link libc++abi. These macros add flags to the following CMake variables.
  3. # - LIBUNWIND_COMPILE_FLAGS: flags used to compile libunwind
  4. # - LIBUNWIND_LINK_FLAGS: flags used to link libunwind
  5. # - LIBUNWIND_LIBRARIES: libraries to link libunwind to.
  6. include(CheckCCompilerFlag)
  7. include(CheckCXXCompilerFlag)
  8. unset(add_flag_if_supported)
  9. # Mangle the name of a compiler flag into a valid CMake identifier.
  10. # Ex: --std=c++11 -> STD_EQ_CXX11
  11. macro(mangle_name str output)
  12. string(STRIP "${str}" strippedStr)
  13. string(REGEX REPLACE "^/" "" strippedStr "${strippedStr}")
  14. string(REGEX REPLACE "^-+" "" strippedStr "${strippedStr}")
  15. string(REGEX REPLACE "-+$" "" strippedStr "${strippedStr}")
  16. string(REPLACE "-" "_" strippedStr "${strippedStr}")
  17. string(REPLACE "=" "_EQ_" strippedStr "${strippedStr}")
  18. string(REPLACE "+" "X" strippedStr "${strippedStr}")
  19. string(TOUPPER "${strippedStr}" ${output})
  20. endmacro()
  21. # Remove a list of flags from all CMake variables that affect compile flags.
  22. # This can be used to remove unwanted flags specified on the command line
  23. # or added in other parts of LLVM's cmake configuration.
  24. macro(remove_flags)
  25. foreach(var ${ARGN})
  26. string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
  27. string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL}")
  28. string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
  29. string(REPLACE "${var}" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
  30. string(REPLACE "${var}" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  31. string(REPLACE "${var}" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  32. string(REPLACE "${var}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
  33. string(REPLACE "${var}" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
  34. string(REPLACE "${var}" "" CMAKE_SHARED_MODULE_FLAGS "${CMAKE_SHARED_MODULE_FLAGS}")
  35. remove_definitions(${var})
  36. endforeach()
  37. endmacro(remove_flags)
  38. macro(check_flag_supported flag)
  39. mangle_name("${flag}" flagname)
  40. check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG")
  41. endmacro()
  42. macro(append_flags DEST)
  43. foreach(value ${ARGN})
  44. list(APPEND ${DEST} ${value})
  45. list(APPEND ${DEST} ${value})
  46. endforeach()
  47. endmacro()
  48. # If the specified 'condition' is true then append the specified list of flags to DEST
  49. macro(append_flags_if condition DEST)
  50. if (${condition})
  51. list(APPEND ${DEST} ${ARGN})
  52. endif()
  53. endmacro()
  54. # Add each flag in the list specified by DEST if that flag is supported by the current compiler.
  55. macro(append_flags_if_supported DEST)
  56. foreach(flag ${ARGN})
  57. mangle_name("${flag}" flagname)
  58. check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG")
  59. append_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${DEST} ${flag})
  60. endforeach()
  61. endmacro()
  62. # Add a macro definition if condition is true.
  63. macro(define_if condition def)
  64. if (${condition})
  65. add_definitions(${def})
  66. endif()
  67. endmacro()
  68. # Add a macro definition if condition is not true.
  69. macro(define_if_not condition def)
  70. if (NOT ${condition})
  71. add_definitions(${def})
  72. endif()
  73. endmacro()
  74. # Add a macro definition to the __config_site file if the specified condition
  75. # is 'true'. Note that '-D${def}' is not added. Instead it is expected that
  76. # the build include the '__config_site' header.
  77. macro(config_define_if condition def)
  78. if (${condition})
  79. set(${def} ON)
  80. set(LIBUNWIND_NEEDS_SITE_CONFIG ON)
  81. endif()
  82. endmacro()
  83. macro(config_define_if_not condition def)
  84. if (NOT ${condition})
  85. set(${def} ON)
  86. set(LIBUNWIND_NEEDS_SITE_CONFIG ON)
  87. endif()
  88. endmacro()
  89. macro(config_define value def)
  90. set(${def} ${value})
  91. set(LIBUNWIND_NEEDS_SITE_CONFIG ON)
  92. endmacro()
  93. # Add a list of flags to all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS',
  94. # 'LIBUNWIND_COMPILE_FLAGS' and 'LIBUNWIND_LINK_FLAGS'.
  95. macro(add_target_flags)
  96. foreach(value ${ARGN})
  97. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${value}")
  98. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${value}")
  99. list(APPEND LIBUNWIND_COMPILE_FLAGS ${value})
  100. list(APPEND LIBUNWIND_LINK_FLAGS ${value})
  101. endforeach()
  102. endmacro()
  103. # If the specified 'condition' is true then add a list of flags to
  104. # all of 'CMAKE_CXX_FLAGS', 'CMAKE_C_FLAGS', 'LIBUNWIND_COMPILE_FLAGS'
  105. # and 'LIBUNWIND_LINK_FLAGS'.
  106. macro(add_target_flags_if condition)
  107. if (${condition})
  108. add_target_flags(${ARGN})
  109. endif()
  110. endmacro()
  111. # Add a specified list of flags to both 'LIBUNWIND_COMPILE_FLAGS' and
  112. # 'LIBUNWIND_LINK_FLAGS'.
  113. macro(add_flags)
  114. foreach(value ${ARGN})
  115. list(APPEND LIBUNWIND_COMPILE_FLAGS ${value})
  116. list(APPEND LIBUNWIND_LINK_FLAGS ${value})
  117. endforeach()
  118. endmacro()
  119. # If the specified 'condition' is true then add a list of flags to both
  120. # 'LIBUNWIND_COMPILE_FLAGS' and 'LIBUNWIND_LINK_FLAGS'.
  121. macro(add_flags_if condition)
  122. if (${condition})
  123. add_flags(${ARGN})
  124. endif()
  125. endmacro()
  126. # Add each flag in the list to LIBUNWIND_COMPILE_FLAGS and LIBUNWIND_LINK_FLAGS
  127. # if that flag is supported by the current compiler.
  128. macro(add_flags_if_supported)
  129. foreach(flag ${ARGN})
  130. mangle_name("${flag}" flagname)
  131. check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG")
  132. add_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag})
  133. endforeach()
  134. endmacro()
  135. # Add a list of flags to 'LIBUNWIND_COMPILE_FLAGS'.
  136. macro(add_compile_flags)
  137. foreach(f ${ARGN})
  138. list(APPEND LIBUNWIND_COMPILE_FLAGS ${f})
  139. endforeach()
  140. endmacro()
  141. # If 'condition' is true then add the specified list of flags to
  142. # 'LIBUNWIND_COMPILE_FLAGS'
  143. macro(add_compile_flags_if condition)
  144. if (${condition})
  145. add_compile_flags(${ARGN})
  146. endif()
  147. endmacro()
  148. # For each specified flag, add that flag to 'LIBUNWIND_COMPILE_FLAGS' if the
  149. # flag is supported by the C++ compiler.
  150. macro(add_compile_flags_if_supported)
  151. foreach(flag ${ARGN})
  152. mangle_name("${flag}" flagname)
  153. check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG")
  154. add_compile_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag})
  155. endforeach()
  156. endmacro()
  157. # Add a list of flags to 'LIBUNWIND_C_FLAGS'.
  158. macro(add_c_flags)
  159. foreach(f ${ARGN})
  160. list(APPEND LIBUNWIND_C_FLAGS ${f})
  161. endforeach()
  162. endmacro()
  163. # If 'condition' is true then add the specified list of flags to
  164. # 'LIBUNWIND_C_FLAGS'
  165. macro(add_c_flags_if condition)
  166. if (${condition})
  167. add_c_flags(${ARGN})
  168. endif()
  169. endmacro()
  170. # For each specified flag, add that flag to 'LIBUNWIND_C_FLAGS' if the
  171. # flag is supported by the C compiler.
  172. macro(add_c_compile_flags_if_supported)
  173. foreach(flag ${ARGN})
  174. mangle_name("${flag}" flagname)
  175. check_c_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG")
  176. add_c_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag})
  177. endforeach()
  178. endmacro()
  179. # Add a list of flags to 'LIBUNWIND_CXX_FLAGS'.
  180. macro(add_cxx_flags)
  181. foreach(f ${ARGN})
  182. list(APPEND LIBUNWIND_CXX_FLAGS ${f})
  183. endforeach()
  184. endmacro()
  185. # If 'condition' is true then add the specified list of flags to
  186. # 'LIBUNWIND_CXX_FLAGS'
  187. macro(add_cxx_flags_if condition)
  188. if (${condition})
  189. add_cxx_flags(${ARGN})
  190. endif()
  191. endmacro()
  192. # For each specified flag, add that flag to 'LIBUNWIND_CXX_FLAGS' if the
  193. # flag is supported by the C compiler.
  194. macro(add_cxx_compile_flags_if_supported)
  195. foreach(flag ${ARGN})
  196. mangle_name("${flag}" flagname)
  197. check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG")
  198. add_cxx_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag})
  199. endforeach()
  200. endmacro()
  201. # Add a list of flags to 'LIBUNWIND_LINK_FLAGS'.
  202. macro(add_link_flags)
  203. foreach(f ${ARGN})
  204. list(APPEND LIBUNWIND_LINK_FLAGS ${f})
  205. endforeach()
  206. endmacro()
  207. # If 'condition' is true then add the specified list of flags to
  208. # 'LIBUNWIND_LINK_FLAGS'
  209. macro(add_link_flags_if condition)
  210. if (${condition})
  211. add_link_flags(${ARGN})
  212. endif()
  213. endmacro()
  214. # For each specified flag, add that flag to 'LIBUNWIND_LINK_FLAGS' if the
  215. # flag is supported by the C++ compiler.
  216. macro(add_link_flags_if_supported)
  217. foreach(flag ${ARGN})
  218. mangle_name("${flag}" flagname)
  219. check_cxx_compiler_flag("${flag}" "LIBUNWIND_SUPPORTS_${flagname}_FLAG")
  220. add_link_flags_if(LIBUNWIND_SUPPORTS_${flagname}_FLAG ${flag})
  221. endforeach()
  222. endmacro()
  223. # Add a list of libraries or link flags to 'LIBUNWIND_LIBRARIES'.
  224. macro(add_library_flags)
  225. foreach(lib ${ARGN})
  226. list(APPEND LIBUNWIND_LIBRARIES ${lib})
  227. endforeach()
  228. endmacro()
  229. # if 'condition' is true then add the specified list of libraries and flags
  230. # to 'LIBUNWIND_LIBRARIES'.
  231. macro(add_library_flags_if condition)
  232. if(${condition})
  233. add_library_flags(${ARGN})
  234. endif()
  235. endmacro()
  236. # Turn a comma separated CMake list into a space separated string.
  237. macro(split_list listname)
  238. string(REPLACE ";" " " ${listname} "${${listname}}")
  239. endmacro()