compiler_flags.cmake 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. if (NOT DRACO_CMAKE_COMPILER_FLAGS_CMAKE_)
  2. set(DRACO_CMAKE_COMPILER_FLAGS_CMAKE_ 1)
  3. include(CheckCCompilerFlag)
  4. include(CheckCXXCompilerFlag)
  5. include("${draco_root}/cmake/compiler_tests.cmake")
  6. # Strings used to cache failed C/CXX flags.
  7. set(DRACO_FAILED_C_FLAGS)
  8. set(DRACO_FAILED_CXX_FLAGS)
  9. # Checks C compiler for support of $c_flag. Adds $c_flag to $CMAKE_C_FLAGS when
  10. # the compile test passes. Caches $c_flag in $DRACO_FAILED_C_FLAGS when the test
  11. # fails.
  12. macro (add_c_flag_if_supported c_flag)
  13. unset(C_FLAG_FOUND CACHE)
  14. string(FIND "${CMAKE_C_FLAGS}" "${c_flag}" C_FLAG_FOUND)
  15. unset(C_FLAG_FAILED CACHE)
  16. string(FIND "${DRACO_FAILED_C_FLAGS}" "${c_flag}" C_FLAG_FAILED)
  17. if (${C_FLAG_FOUND} EQUAL -1 AND ${C_FLAG_FAILED} EQUAL -1)
  18. unset(C_FLAG_SUPPORTED CACHE)
  19. message("Checking C compiler flag support for: " ${c_flag})
  20. check_c_compiler_flag("${c_flag}" C_FLAG_SUPPORTED)
  21. if (${C_FLAG_SUPPORTED})
  22. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${c_flag}" CACHE STRING "")
  23. else ()
  24. set(DRACO_FAILED_C_FLAGS "${DRACO_FAILED_C_FLAGS} ${c_flag}" CACHE STRING
  25. "" FORCE)
  26. endif ()
  27. endif ()
  28. endmacro ()
  29. # Checks C++ compiler for support of $cxx_flag. Adds $cxx_flag to
  30. # $CMAKE_CXX_FLAGS when the compile test passes. Caches $c_flag in
  31. # $DRACO_FAILED_CXX_FLAGS when the test fails.
  32. macro (add_cxx_flag_if_supported cxx_flag)
  33. unset(CXX_FLAG_FOUND CACHE)
  34. string(FIND "${CMAKE_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FOUND)
  35. unset(CXX_FLAG_FAILED CACHE)
  36. string(FIND "${DRACO_FAILED_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FAILED)
  37. if (${CXX_FLAG_FOUND} EQUAL -1 AND ${CXX_FLAG_FAILED} EQUAL -1)
  38. unset(CXX_FLAG_SUPPORTED CACHE)
  39. message("Checking CXX compiler flag support for: " ${cxx_flag})
  40. check_cxx_compiler_flag("${cxx_flag}" CXX_FLAG_SUPPORTED)
  41. if (${CXX_FLAG_SUPPORTED})
  42. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${cxx_flag}" CACHE STRING "")
  43. else()
  44. set(DRACO_FAILED_CXX_FLAGS "${DRACO_FAILED_CXX_FLAGS} ${cxx_flag}" CACHE
  45. STRING "" FORCE)
  46. endif ()
  47. endif ()
  48. endmacro ()
  49. # Convenience method for adding a flag to both the C and C++ compiler command
  50. # lines.
  51. macro (add_compiler_flag_if_supported flag)
  52. add_c_flag_if_supported(${flag})
  53. add_cxx_flag_if_supported(${flag})
  54. endmacro ()
  55. # Checks C compiler for support of $c_flag and terminates generation when
  56. # support is not present.
  57. macro (require_c_flag c_flag update_c_flags)
  58. unset(C_FLAG_FOUND CACHE)
  59. string(FIND "${CMAKE_C_FLAGS}" "${c_flag}" C_FLAG_FOUND)
  60. if (${C_FLAG_FOUND} EQUAL -1)
  61. unset(HAVE_C_FLAG CACHE)
  62. message("Checking C compiler flag support for: " ${c_flag})
  63. check_c_compiler_flag("${c_flag}" HAVE_C_FLAG)
  64. if (NOT ${HAVE_C_FLAG})
  65. message(FATAL_ERROR
  66. "${PROJECT_NAME} requires support for C flag: ${c_flag}.")
  67. endif ()
  68. if (${update_c_flags})
  69. set(CMAKE_C_FLAGS "${c_flag} ${CMAKE_C_FLAGS}" CACHE STRING "" FORCE)
  70. endif ()
  71. endif ()
  72. endmacro ()
  73. # Checks CXX compiler for support of $cxx_flag and terminates generation when
  74. # support is not present.
  75. macro (require_cxx_flag cxx_flag update_cxx_flags)
  76. unset(CXX_FLAG_FOUND CACHE)
  77. string(FIND "${CMAKE_CXX_FLAGS}" "${cxx_flag}" CXX_FLAG_FOUND)
  78. if (${CXX_FLAG_FOUND} EQUAL -1)
  79. unset(HAVE_CXX_FLAG CACHE)
  80. message("Checking CXX compiler flag support for: " ${cxx_flag})
  81. check_cxx_compiler_flag("${cxx_flag}" HAVE_CXX_FLAG)
  82. if (NOT ${HAVE_CXX_FLAG})
  83. message(FATAL_ERROR
  84. "${PROJECT_NAME} requires support for CXX flag: ${cxx_flag}.")
  85. endif ()
  86. if (${update_cxx_flags})
  87. set(CMAKE_CXX_FLAGS "${cxx_flag} ${CMAKE_CXX_FLAGS}" CACHE STRING ""
  88. FORCE)
  89. endif ()
  90. endif ()
  91. endmacro ()
  92. # Checks for support of $flag by both the C and CXX compilers. Terminates
  93. # generation when support is not present in both compilers.
  94. macro (require_compiler_flag flag update_cmake_flags)
  95. require_c_flag(${flag} ${update_cmake_flags})
  96. require_cxx_flag(${flag} ${update_cmake_flags})
  97. endmacro ()
  98. # Checks only non-MSVC targets for support of $c_flag and terminates generation
  99. # when support is not present.
  100. macro (require_c_flag_nomsvc c_flag update_c_flags)
  101. if (NOT MSVC)
  102. require_c_flag(${c_flag} ${update_c_flags})
  103. endif ()
  104. endmacro ()
  105. # Checks only non-MSVC targets for support of $cxx_flag and terminates
  106. # generation when support is not present.
  107. macro (require_cxx_flag_nomsvc cxx_flag update_cxx_flags)
  108. if (NOT MSVC)
  109. require_cxx_flag(${cxx_flag} ${update_cxx_flags})
  110. endif ()
  111. endmacro ()
  112. # Checks only non-MSVC targets for support of $flag by both the C and CXX
  113. # compilers. Terminates generation when support is not present in both
  114. # compilers.
  115. macro (require_compiler_flag_nomsvc flag update_cmake_flags)
  116. require_c_flag_nomsvc(${flag} ${update_cmake_flags})
  117. require_cxx_flag_nomsvc(${flag} ${update_cmake_flags})
  118. endmacro ()
  119. # Adds $flag to assembler command line.
  120. macro (append_as_flag flag)
  121. unset(AS_FLAG_FOUND CACHE)
  122. string(FIND "${DRACO_AS_FLAGS}" "${flag}" AS_FLAG_FOUND)
  123. if (${AS_FLAG_FOUND} EQUAL -1)
  124. set(DRACO_AS_FLAGS "${DRACO_AS_FLAGS} ${flag}")
  125. endif ()
  126. endmacro ()
  127. # Adds $flag to the C compiler command line.
  128. macro (append_c_flag flag)
  129. unset(C_FLAG_FOUND CACHE)
  130. string(FIND "${CMAKE_C_FLAGS}" "${flag}" C_FLAG_FOUND)
  131. if (${C_FLAG_FOUND} EQUAL -1)
  132. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
  133. endif ()
  134. endmacro ()
  135. # Adds $flag to the CXX compiler command line.
  136. macro (append_cxx_flag flag)
  137. unset(CXX_FLAG_FOUND CACHE)
  138. string(FIND "${CMAKE_CXX_FLAGS}" "${flag}" CXX_FLAG_FOUND)
  139. if (${CXX_FLAG_FOUND} EQUAL -1)
  140. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
  141. endif ()
  142. endmacro ()
  143. # Adds $flag to the C and CXX compiler command lines.
  144. macro (append_compiler_flag flag)
  145. append_c_flag(${flag})
  146. append_cxx_flag(${flag})
  147. endmacro ()
  148. # Adds $flag to the executable linker command line.
  149. macro (append_exe_linker_flag flag)
  150. unset(LINKER_FLAG_FOUND CACHE)
  151. string(FIND "${CMAKE_EXE_LINKER_FLAGS}" "${flag}" LINKER_FLAG_FOUND)
  152. if (${LINKER_FLAG_FOUND} EQUAL -1)
  153. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}")
  154. endif ()
  155. endmacro ()
  156. # Adds $flag to the link flags for $target.
  157. function (append_link_flag_to_target target flags)
  158. unset(target_link_flags)
  159. get_target_property(target_link_flags ${target} LINK_FLAGS)
  160. if (target_link_flags)
  161. unset(link_flag_found)
  162. string(FIND "${target_link_flags}" "${flags}" link_flag_found)
  163. if (NOT ${link_flag_found} EQUAL -1)
  164. return()
  165. endif ()
  166. set(target_link_flags "${target_link_flags} ${flags}")
  167. else ()
  168. set(target_link_flags "${flags}")
  169. endif ()
  170. set_target_properties(${target} PROPERTIES LINK_FLAGS ${target_link_flags})
  171. endfunction ()
  172. # Adds $flag to executable linker flags, and makes sure C/CXX builds still work.
  173. macro (require_linker_flag flag)
  174. append_exe_linker_flag(${flag})
  175. unset(c_passed)
  176. draco_check_c_compiles("LINKER_FLAG_C_TEST(${flag})" "" c_passed)
  177. unset(cxx_passed)
  178. draco_check_cxx_compiles("LINKER_FLAG_CXX_TEST(${flag})" "" cxx_passed)
  179. if (NOT c_passed OR NOT cxx_passed)
  180. message(FATAL_ERROR "Linker flag test for ${flag} failed.")
  181. endif ()
  182. endmacro ()
  183. endif () # DRACO_CMAKE_COMPILER_FLAGS_CMAKE_