compiler_tests.cmake 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. if (NOT DRACO_CMAKE_COMPILER_TESTS_CMAKE_)
  2. set(DRACO_CMAKE_COMPILER_TESTS_CMAKE_ 1)
  3. include(CheckCSourceCompiles)
  4. include(CheckCXXSourceCompiles)
  5. # The basic main() macro used in all compile tests.
  6. set(DRACO_C_MAIN "\nint main(void) { return 0; }")
  7. set(DRACO_CXX_MAIN "\nint main() { return 0; }")
  8. # Strings containing the names of passed and failed tests.
  9. set(DRACO_C_PASSED_TESTS)
  10. set(DRACO_C_FAILED_TESTS)
  11. set(DRACO_CXX_PASSED_TESTS)
  12. set(DRACO_CXX_FAILED_TESTS)
  13. macro(draco_push_var var new_value)
  14. set(SAVED_${var} ${var})
  15. set(${var} ${new_value})
  16. endmacro ()
  17. macro(draco_pop_var var)
  18. set(var ${SAVED_${var}})
  19. unset(SAVED_${var})
  20. endmacro ()
  21. # Confirms $test_source compiles and stores $test_name in one of
  22. # $DRACO_C_PASSED_TESTS or $DRACO_C_FAILED_TESTS depending on out come. When the
  23. # test passes $result_var is set to 1. When it fails $result_var is unset.
  24. # The test is not run if the test name is found in either of the passed or
  25. # failed test variables.
  26. macro(draco_check_c_compiles test_name test_source result_var)
  27. unset(C_TEST_PASSED CACHE)
  28. unset(C_TEST_FAILED CACHE)
  29. string(FIND "${DRACO_C_PASSED_TESTS}" "${test_name}" C_TEST_PASSED)
  30. string(FIND "${DRACO_C_FAILED_TESTS}" "${test_name}" C_TEST_FAILED)
  31. if (${C_TEST_PASSED} EQUAL -1 AND ${C_TEST_FAILED} EQUAL -1)
  32. unset(C_TEST_COMPILED CACHE)
  33. message("Running C compiler test: ${test_name}")
  34. check_c_source_compiles("${test_source} ${DRACO_C_MAIN}" C_TEST_COMPILED)
  35. set(${result_var} ${C_TEST_COMPILED})
  36. if (${C_TEST_COMPILED})
  37. set(DRACO_C_PASSED_TESTS "${DRACO_C_PASSED_TESTS} ${test_name}")
  38. else ()
  39. set(DRACO_C_FAILED_TESTS "${DRACO_C_FAILED_TESTS} ${test_name}")
  40. message("C Compiler test ${test_name} failed.")
  41. endif ()
  42. elseif (NOT ${C_TEST_PASSED} EQUAL -1)
  43. set(${result_var} 1)
  44. else () # ${C_TEST_FAILED} NOT EQUAL -1
  45. unset(${result_var})
  46. endif ()
  47. endmacro ()
  48. # Confirms $test_source compiles and stores $test_name in one of
  49. # $DRACO_CXX_PASSED_TESTS or $DRACO_CXX_FAILED_TESTS depending on out come. When
  50. # the test passes $result_var is set to 1. When it fails $result_var is unset.
  51. # The test is not run if the test name is found in either of the passed or
  52. # failed test variables.
  53. macro(draco_check_cxx_compiles test_name test_source result_var)
  54. unset(CXX_TEST_PASSED CACHE)
  55. unset(CXX_TEST_FAILED CACHE)
  56. string(FIND "${DRACO_CXX_PASSED_TESTS}" "${test_name}" CXX_TEST_PASSED)
  57. string(FIND "${DRACO_CXX_FAILED_TESTS}" "${test_name}" CXX_TEST_FAILED)
  58. if (${CXX_TEST_PASSED} EQUAL -1 AND ${CXX_TEST_FAILED} EQUAL -1)
  59. unset(CXX_TEST_COMPILED CACHE)
  60. message("Running CXX compiler test: ${test_name}")
  61. check_cxx_source_compiles("${test_source} ${DRACO_CXX_MAIN}"
  62. CXX_TEST_COMPILED)
  63. set(${result_var} ${CXX_TEST_COMPILED})
  64. if (${CXX_TEST_COMPILED})
  65. set(DRACO_CXX_PASSED_TESTS "${DRACO_CXX_PASSED_TESTS} ${test_name}")
  66. else ()
  67. set(DRACO_CXX_FAILED_TESTS "${DRACO_CXX_FAILED_TESTS} ${test_name}")
  68. message("CXX Compiler test ${test_name} failed.")
  69. endif ()
  70. elseif (NOT ${CXX_TEST_PASSED} EQUAL -1)
  71. set(${result_var} 1)
  72. else () # ${CXX_TEST_FAILED} NOT EQUAL -1
  73. unset(${result_var})
  74. endif ()
  75. endmacro ()
  76. # Convenience macro that confirms $test_source compiles as C and C++.
  77. # $result_var is set to 1 when both tests are successful, and 0 when one or both
  78. # tests fail.
  79. # Note: This macro is intended to be used to write to result variables that
  80. # are expanded via configure_file(). $result_var is set to 1 or 0 to allow
  81. # direct usage of the value in generated source files.
  82. macro(draco_check_source_compiles test_name test_source result_var)
  83. unset(C_PASSED)
  84. unset(CXX_PASSED)
  85. draco_check_c_compiles(${test_name} ${test_source} C_PASSED)
  86. draco_check_cxx_compiles(${test_name} ${test_source} CXX_PASSED)
  87. if (${C_PASSED} AND ${CXX_PASSED})
  88. set(${result_var} 1)
  89. else ()
  90. set(${result_var} 0)
  91. endif ()
  92. endmacro ()
  93. # When inline support is detected for the current compiler the supported
  94. # inlining keyword is written to $result in caller scope.
  95. macro (draco_get_inline result)
  96. draco_check_source_compiles("inline_check_1"
  97. "static inline void macro(void) {}"
  98. HAVE_INLINE_1)
  99. if (HAVE_INLINE_1 EQUAL 1)
  100. set(${result} "inline")
  101. return()
  102. endif ()
  103. # Check __inline.
  104. draco_check_source_compiles("inline_check_2"
  105. "static __inline void macro(void) {}"
  106. HAVE_INLINE_2)
  107. if (HAVE_INLINE_2 EQUAL 1)
  108. set(${result} "__inline")
  109. endif ()
  110. endmacro ()
  111. endif () # DRACO_CMAKE_COMPILER_TESTS_CMAKE_