CheckOBJCXXCompilerFlag.cmake 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CheckOBJCXXCompilerFlag
  5. -----------------------
  6. Check whether the Objective-C++ compiler supports a given flag.
  7. .. command:: check_objcxx_compiler_flag
  8. .. code-block:: cmake
  9. check_objcxx_compiler_flag(<flag> <var>)
  10. Check that the ``<flag>`` is accepted by the compiler without
  11. a diagnostic. Stores the result in an internal cache entry
  12. named ``<var>``.
  13. This command temporarily sets the ``CMAKE_REQUIRED_DEFINITIONS`` variable
  14. and calls the ``check_objcxx_source_compiles`` macro from the
  15. :module:`CheckOBJCXXSourceCompiles` module. See documentation of that
  16. module for a listing of variables that can otherwise modify the build.
  17. A positive result from this check indicates only that the compiler did not
  18. issue a diagnostic message when given the flag. Whether the flag has any
  19. effect or even a specific one is beyond the scope of this module.
  20. .. note::
  21. Since the :command:`try_compile` command forwards flags from variables
  22. like :variable:`CMAKE_OBJCXX_FLAGS <CMAKE_<LANG>_FLAGS>`, unknown flags
  23. in such variables may cause a false negative for this check.
  24. #]=======================================================================]
  25. include_guard(GLOBAL)
  26. include(CheckOBJCXXSourceCompiles)
  27. include(CMakeCheckCompilerFlagCommonPatterns)
  28. macro (CHECK_OBJCXX_COMPILER_FLAG _FLAG _RESULT)
  29. set(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}")
  30. set(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}")
  31. # Normalize locale during test compilation.
  32. set(_CheckOBJCXXCompilerFlag_LOCALE_VARS LC_ALL LC_MESSAGES LANG)
  33. foreach(v ${_CheckOBJCXXCompilerFlag_LOCALE_VARS})
  34. set(_CheckOBJCXXCompilerFlag_SAVED_${v} "$ENV{${v}}")
  35. set(ENV{${v}} OBJCXX)
  36. endforeach()
  37. CHECK_COMPILER_FLAG_COMMON_PATTERNS(_CheckOBJCXXCompilerFlag_COMMON_PATTERNS)
  38. CHECK_OBJCXX_SOURCE_COMPILES("#ifndef __OBJC__\n# error \"Not an Objective-C++ compiler\"\n#endif\nint main(void) { return 0; }" ${_RESULT}
  39. # Some compilers do not fail with a bad flag
  40. FAIL_REGEX "command line option .* is valid for .* but not for Objective-C\\\\+\\\\+" # GNU
  41. FAIL_REGEX "argument unused during compilation: .*" # Clang
  42. ${_CheckOBJCXXCompilerFlag_COMMON_PATTERNS}
  43. )
  44. foreach(v ${_CheckOBJCXXCompilerFlag_LOCALE_VARS})
  45. set(ENV{${v}} ${_CheckOBJCXXCompilerFlag_SAVED_${v}})
  46. unset(_CheckOBJCXXCompilerFlag_SAVED_${v})
  47. endforeach()
  48. unset(_CheckOBJCXXCompilerFlag_LOCALE_VARS)
  49. unset(_CheckOBJCXXCompilerFlag_COMMON_PATTERNS)
  50. set (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}")
  51. endmacro ()