CompilerFlags.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #
  2. # CompilerFlags.cmake
  3. #
  4. # This file sets up various compiler flags (warning levels, turning off options
  5. # we don't need, etc.) It must be included directly from the root
  6. # CMakeLists.txt, due to its use of set(...)
  7. #
  8. include(CheckCXXCompilerFlag)
  9. # Panda3D is now a C++11 project. Newer versions of CMake support this out of
  10. # the box; for older versions we take a shot in the dark:
  11. if(CMAKE_VERSION VERSION_LESS "3.1")
  12. check_cxx_compiler_flag("-std=gnu++11" COMPILER_SUPPORTS_CXX11)
  13. if(COMPILER_SUPPORTS_CXX11)
  14. string(APPEND CMAKE_CXX_FLAGS " -std=gnu++11")
  15. else()
  16. string(APPEND CMAKE_CXX_FLAGS " -std=gnu++0x")
  17. endif()
  18. else()
  19. set(CMAKE_CXX_STANDARD 11)
  20. endif()
  21. # Set certain CMake flags we expect
  22. set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
  23. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  24. # Set up the output directory structure, mimicking that of makepanda
  25. set(CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}/cmake")
  26. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
  27. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
  28. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
  29. set(MODULE_DESTINATION "lib")
  30. # Runtime code assumes that dynamic modules have a "lib" prefix; Windows
  31. # assumes that debug libraries have a _d suffix.
  32. set(CMAKE_SHARED_MODULE_PREFIX "lib")
  33. if(WIN32)
  34. set(CMAKE_DEBUG_POSTFIX "_d")
  35. # Windows uses libfoo.lib for static libraries and foo.lib/dll for dynamic.
  36. set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
  37. # On Windows, modules (DLLs) are located in bin; lib is just for .lib files
  38. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
  39. if(BUILD_SHARED_LIBS)
  40. set(MODULE_DESTINATION "bin")
  41. endif()
  42. endif()
  43. # Set warning levels
  44. if(MSVC)
  45. string(APPEND CMAKE_C_FLAGS " /W3")
  46. string(APPEND CMAKE_CXX_FLAGS " /W3")
  47. else()
  48. string(APPEND CMAKE_C_FLAGS " -Wall")
  49. string(APPEND CMAKE_CXX_FLAGS " -Wall")
  50. endif()
  51. if(NOT "x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC")
  52. set(disable_flags "-Wno-unused-function -Wno-unused-parameter")
  53. string(APPEND CMAKE_C_FLAGS " ${disable_flags}")
  54. string(APPEND CMAKE_CXX_FLAGS " ${disable_flags} -Wno-reorder")
  55. string(APPEND CMAKE_CXX_FLAGS_RELEASE " -Wno-unused-variable")
  56. string(APPEND CMAKE_CXX_FLAGS_RELWITHDEBINFO " -Wno-unused-variable")
  57. string(APPEND CMAKE_CXX_FLAGS_MINSIZEREL " -Wno-unused-variable")
  58. if(MSVC)
  59. # Clang behaving as MSVC
  60. string(APPEND CMAKE_C_FLAGS " -Wno-unused-command-line-argument")
  61. set(CMAKE_CXX_FLAGS
  62. "${CMAKE_CXX_FLAGS} -Wno-microsoft-template -Wno-unused-command-line-argument")
  63. endif()
  64. endif()
  65. if(WIN32)
  66. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  67. endif()
  68. # CMake will often pass -rdynamic when linking executables as a convenience for
  69. # projects that might forget when to use ENABLE_EXPORTS. This is preposterous,
  70. # since it prevents the linker from removing symbols unneeded by the executable
  71. # and stops us from identifying cases where ENABLE_EXPORTS is needed.
  72. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  73. set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
  74. # As long as we're figuring out compiler flags, figure out the flags for
  75. # turning C++ exception support on and off
  76. if(MSVC)
  77. set(cxx_exceptions_on "/EHsc")
  78. set(cxx_exceptions_off "/D_HAS_EXCEPTIONS=0")
  79. else()
  80. check_cxx_compiler_flag("-fno-exceptions" COMPILER_SUPPORTS_FEXCEPTIONS)
  81. if(COMPILER_SUPPORTS_FEXCEPTIONS)
  82. set(cxx_exceptions_on "-fexceptions")
  83. set(cxx_exceptions_off "-fno-exceptions")
  84. else()
  85. set(cxx_exceptions_on)
  86. set(cxx_exceptions_off)
  87. endif()
  88. endif()
  89. set(cxx_exceptions_property "$<BOOL:$<TARGET_PROPERTY:CXX_EXCEPTIONS>>")
  90. add_compile_options(
  91. "$<${cxx_exceptions_property}:${cxx_exceptions_on}>"
  92. "$<$<NOT:${cxx_exceptions_property}>:${cxx_exceptions_off}>")
  93. # We should use -fvisibility=hidden everywhere, as it makes sure we think
  94. # about what symbols really should be exposed externally. For more info, see:
  95. # https://gcc.gnu.org/wiki/Visibility
  96. if(NOT MSVC)
  97. check_cxx_compiler_flag("-fvisibility=hidden" COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  98. if(COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  99. add_compile_options("-fvisibility=hidden")
  100. endif()
  101. endif()