LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.9 KiB

2 months ago
  1. # - Adds a compiler flag if it is supported by the compiler
  2. #
  3. # This function checks that the supplied compiler flag is supported and then
  4. # adds it to the corresponding compiler flags
  5. #
  6. # add_cxx_compiler_flag(<FLAG> [<VARIANT>])
  7. #
  8. # - Example
  9. #
  10. # include(AddCXXCompilerFlag)
  11. # add_cxx_compiler_flag(-Wall)
  12. # add_cxx_compiler_flag(-no-strict-aliasing RELEASE)
  13. # Requires CMake 2.6+
  14. if(__add_cxx_compiler_flag)
  15. return()
  16. endif()
  17. set(__add_cxx_compiler_flag INCLUDED)
  18. include(CheckCXXCompilerFlag)
  19. function(mangle_compiler_flag FLAG OUTPUT)
  20. string(TOUPPER "HAVE_CXX_FLAG_${FLAG}" SANITIZED_FLAG)
  21. string(REPLACE "+" "X" SANITIZED_FLAG ${SANITIZED_FLAG})
  22. string(REGEX REPLACE "[^A-Za-z_0-9]" "_" SANITIZED_FLAG ${SANITIZED_FLAG})
  23. string(REGEX REPLACE "_+" "_" SANITIZED_FLAG ${SANITIZED_FLAG})
  24. set(${OUTPUT} "${SANITIZED_FLAG}" PARENT_SCOPE)
  25. endfunction(mangle_compiler_flag)
  26. function(add_cxx_compiler_flag FLAG)
  27. mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
  28. set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  29. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}")
  30. check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
  31. set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
  32. if(${MANGLED_FLAG})
  33. if(ARGC GREATER 1)
  34. set(VARIANT ${ARGV1})
  35. string(TOUPPER "_${VARIANT}" VARIANT)
  36. else()
  37. set(VARIANT "")
  38. endif()
  39. set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${BENCHMARK_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
  40. endif()
  41. endfunction()
  42. function(add_required_cxx_compiler_flag FLAG)
  43. mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
  44. set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  45. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}")
  46. check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
  47. set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
  48. if(${MANGLED_FLAG})
  49. if(ARGC GREATER 1)
  50. set(VARIANT ${ARGV1})
  51. string(TOUPPER "_${VARIANT}" VARIANT)
  52. else()
  53. set(VARIANT "")
  54. endif()
  55. set(CMAKE_CXX_FLAGS${VARIANT} "${CMAKE_CXX_FLAGS${VARIANT}} ${FLAG}" PARENT_SCOPE)
  56. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE)
  57. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE)
  58. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${FLAG}" PARENT_SCOPE)
  59. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${FLAG}" PARENT_SCOPE)
  60. else()
  61. message(FATAL_ERROR "Required flag '${FLAG}' is not supported by the compiler")
  62. endif()
  63. endfunction()
  64. function(check_cxx_warning_flag FLAG)
  65. mangle_compiler_flag("${FLAG}" MANGLED_FLAG)
  66. set(OLD_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
  67. # Add -Werror to ensure the compiler generates an error if the warning flag
  68. # doesn't exist.
  69. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror ${FLAG}")
  70. check_cxx_compiler_flag("${FLAG}" ${MANGLED_FLAG})
  71. set(CMAKE_REQUIRED_FLAGS "${OLD_CMAKE_REQUIRED_FLAGS}")
  72. endfunction()