ctkFunctionCheckCompilerFlags.cmake 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #
  2. # Helper macro allowing to check if the given flags are supported
  3. # by the underlying build tool
  4. #
  5. # If the flag(s) is/are supported, they will be appended to the string identified by RESULT_VAR
  6. #
  7. # Usage:
  8. # ctkFunctionCheckCompilerFlags(FLAGS_TO_CHECK VALID_FLAGS_VAR)
  9. #
  10. # Example:
  11. #
  12. # set(myflags)
  13. # ctkFunctionCheckCompilerFlags("-fprofile-arcs" myflags)
  14. # message(1-myflags:${myflags})
  15. # ctkFunctionCheckCompilerFlags("-fauto-bugfix" myflags)
  16. # message(2-myflags:${myflags})
  17. # ctkFunctionCheckCompilerFlags("-Wall" myflags)
  18. # message(1-myflags:${myflags})
  19. #
  20. # The output will be:
  21. # 1-myflags: -fprofile-arcs
  22. # 2-myflags: -fprofile-arcs
  23. # 3-myflags: -fprofile-arcs -Wall
  24. FUNCTION(ctkFunctionCheckCompilerFlags CXX_FLAGS_TO_TEST RESULT_VAR)
  25. IF(CXX_FLAGS_TO_TEST STREQUAL "")
  26. MESSAGE(FATAL_ERROR "CXX_FLAGS_TO_TEST shouldn't be empty")
  27. ENDIF()
  28. SET(bindir ${CMAKE_BINARY_DIR})
  29. SET(srcfile ${bindir}/ctkFunctionCheckCompilerFlags.cpp)
  30. FILE(WRITE ${srcfile} "
  31. #include <iostream>
  32. int main(int, char**) { std::cout << \"Rock climbing is awesome\" << std::endl;}
  33. ")
  34. SET(is_valid 0)
  35. TRY_COMPILE(
  36. is_valid ${bindir} ${srcfile}
  37. CMAKE_FLAGS "-DCMAKE_CXX_FLAGS:STRING=${CXX_FLAGS_TO_TEST}"
  38. )
  39. IF(is_valid)
  40. SET(${RESULT_VAR} "${${RESULT_VAR}} ${CXX_FLAGS_TO_TEST}" PARENT_SCOPE)
  41. ENDIF()
  42. MESSAGE(STATUS "Compiler Flags [${CXX_FLAGS_TO_TEST}] supported")
  43. ENDFUNCTION()