CompilerFlags.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
  15. else()
  16. set(CMAKE_CXX_FLAGS "${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. if(CMAKE_CFG_INTDIR STREQUAL ".")
  27. # Single-configuration generator; output goes straight in the binary dir
  28. set(PANDA_OUTPUT_DIR "${PROJECT_BINARY_DIR}")
  29. else()
  30. # Multi-configuration generator; add a per-configuration path prefix
  31. set(PANDA_OUTPUT_DIR "${PROJECT_BINARY_DIR}/$<CONFIG>")
  32. endif()
  33. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/bin")
  34. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/lib")
  35. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/lib")
  36. set(MODULE_DESTINATION "lib")
  37. # Runtime code assumes that dynamic modules have a "lib" prefix; Windows
  38. # assumes that debug libraries have a _d suffix.
  39. set(CMAKE_SHARED_MODULE_PREFIX "lib")
  40. if(WIN32)
  41. set(CMAKE_DEBUG_POSTFIX "_d")
  42. # Windows uses libfoo.lib for static libraries and foo.lib/dll for dynamic.
  43. set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
  44. # On Windows, modules (DLLs) are located in bin; lib is just for .lib files
  45. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/bin")
  46. if(BUILD_SHARED_LIBS)
  47. set(MODULE_DESTINATION "bin")
  48. endif()
  49. endif()
  50. # Though not technically correct, we'll still want MODULE type libraries
  51. # (used for display and audio plugins) to use a .dylib extension.
  52. if(APPLE)
  53. set(CMAKE_SHARED_MODULE_SUFFIX ".dylib")
  54. endif()
  55. # We want the output structured like build/CONFIG/bin, not build/bin/CONFIG per
  56. # the default for multi-configuration generators. In CMake 3.4+, it switches
  57. # automatically if the *_OUTPUT_DIRECTORY property contains a generator
  58. # expresssion, but as of this writing we support as early as CMake 3.0.2.
  59. #
  60. # So, let's just do this:
  61. if(CMAKE_VERSION VERSION_LESS "3.4")
  62. foreach(_type RUNTIME ARCHIVE LIBRARY)
  63. foreach(_config ${CMAKE_CONFIGURATION_TYPES})
  64. string(TOUPPER "${_config}" _config)
  65. set(CMAKE_${_type}_OUTPUT_DIRECTORY_${_config} "${CMAKE_${_type}_OUTPUT_DIRECTORY}")
  66. endforeach(_config)
  67. endforeach(_type)
  68. endif()
  69. # Set warning levels
  70. if(MSVC)
  71. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
  72. set(CMAKE_CCXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
  73. else()
  74. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
  75. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
  76. endif()
  77. if(NOT "x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC")
  78. set(disable_flags "-Wno-unused-function -Wno-unused-parameter")
  79. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${disable_flags}")
  80. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${disable_flags} -Wno-reorder")
  81. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wno-unused-variable")
  82. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -Wno-unused-variable")
  83. set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -Wno-unused-variable")
  84. if(MSVC)
  85. # Clang behaving as MSVC`
  86. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-command-line-argument")
  87. set(CMAKE_CXX_FLAGS
  88. "${CMAKE_CXX_FLAGS} -Wno-microsoft-template -Wno-unused-command-line-argument")
  89. endif()
  90. endif()
  91. if(WIN32)
  92. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  93. endif()
  94. # CMake will often pass -rdynamic when linking executables as a convenience for
  95. # projects that might forget when to use ENABLE_EXPORTS. This is preposterous,
  96. # since it prevents the linker from removing symbols unneeded by the executable
  97. # and stops us from identifying cases where ENABLE_EXPORTS is needed.
  98. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  99. set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
  100. # As long as we're figuring out compiler flags, figure out the flags for
  101. # turning C++ exception support on and off
  102. if(MSVC)
  103. set(cxx_exceptions_on "/EHsc")
  104. set(cxx_exceptions_off "/D_HAS_EXCEPTIONS=0")
  105. else()
  106. check_cxx_compiler_flag("-fno-exceptions" COMPILER_SUPPORTS_FEXCEPTIONS)
  107. if(COMPILER_SUPPORTS_FEXCEPTIONS)
  108. set(cxx_exceptions_on "-fexceptions")
  109. set(cxx_exceptions_off "-fno-exceptions")
  110. else()
  111. set(cxx_exceptions_on)
  112. set(cxx_exceptions_off)
  113. endif()
  114. endif()
  115. set(cxx_exceptions_property "$<BOOL:$<TARGET_PROPERTY:CXX_EXCEPTIONS>>")
  116. add_compile_options(
  117. "$<${cxx_exceptions_property}:${cxx_exceptions_on}>"
  118. "$<$<NOT:${cxx_exceptions_property}>:${cxx_exceptions_off}>")
  119. if(MSVC)
  120. set(msvc_bigobj_property "$<BOOL:$<TARGET_PROPERTY:MSVC_BIGOBJ>>")
  121. add_compile_options("$<${msvc_bigobj_property}:/bigobj>")
  122. endif()
  123. # We should use -fvisibility=hidden everywhere, as it makes sure we think
  124. # about what symbols really should be exposed externally. For more info, see:
  125. # https://gcc.gnu.org/wiki/Visibility
  126. if(NOT MSVC)
  127. check_cxx_compiler_flag("-fvisibility=hidden" COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  128. if(COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  129. add_compile_options("-fvisibility=hidden")
  130. endif()
  131. endif()
  132. # These are flags for the custom configurations we add
  133. # Distribution
  134. set(CMAKE_C_FLAGS_DISTRIBUTION "")
  135. set(CMAKE_CXX_FLAGS_DISTRIBUTION "")
  136. set(CMAKE_SHARED_LINKER_FLAGS_DISTRIBUTION "")
  137. set(CMAKE_MODULE_LINKER_FLAGS_DISTRIBUTION "")
  138. set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "")