ctkFunctionCheckCompilerFlags.cmake 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. INCLUDE(TestCXXAcceptsFlag)
  25. FUNCTION(ctkFunctionCheckCompilerFlags CXX_FLAG_TO_TEST RESULT_VAR)
  26. IF(CXX_FLAG_TO_TEST STREQUAL "")
  27. MESSAGE(FATAL_ERROR "CXX_FLAG_TO_TEST shouldn't be empty")
  28. ENDIF()
  29. # Internally, the macro CMAKE_CXX_ACCEPTS_FLAG calls TRY_COMPILE. To avoid
  30. # the cost of compiling the test each time the project is configured, the variable set by
  31. # the macro is added to the cache so that following invocation of the macro with
  32. # the same variable name skip the compilation step.
  33. # For that same reason, ctkFunctionCheckCompilerFlags function appends a unique suffix to
  34. # the HAS_FLAG variable. This suffix is created using a 'clean version' of the flag to test.
  35. STRING(REGEX REPLACE "-\\s\\$\\+\\*\\{\\}\\(\\)\\#" "" suffix ${CXX_FLAG_TO_TEST})
  36. CHECK_CXX_ACCEPTS_FLAG(${CXX_FLAG_TO_TEST} HAS_FLAG_${suffix})
  37. IF(HAS_FLAG_${suffix})
  38. SET(${RESULT_VAR} "${${RESULT_VAR}} ${CXX_FLAG_TO_TEST}" PARENT_SCOPE)
  39. ENDIF()
  40. ENDFUNCTION()