CompilerFlags.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. # These are flags for the custom configurations we add
  10. # Standard
  11. if(MSVC)
  12. set(CMAKE_C_FLAGS_STANDARD "/Ox")
  13. set(CMAKE_CXX_FLAGS_STANDARD "/Ox")
  14. else()
  15. set(CMAKE_C_FLAGS_STANDARD "-O3")
  16. set(CMAKE_CXX_FLAGS_STANDARD "-O3")
  17. endif()
  18. set(CMAKE_SHARED_LINKER_FLAGS_STANDARD "")
  19. set(CMAKE_MODULE_LINKER_FLAGS_STANDARD "")
  20. set(CMAKE_EXE_LINKER_FLAGS_STANDARD "")
  21. # Coverage (when we know how to support it)
  22. if(CMAKE_CXX_COMPILER_ID MATCHES "(AppleClang|Clang)")
  23. set(CMAKE_C_FLAGS_COVERAGE
  24. "${CMAKE_C_FLAGS_DEBUG} -fprofile-instr-generate -fcoverage-mapping")
  25. set(CMAKE_CXX_FLAGS_COVERAGE
  26. "${CMAKE_CXX_FLAGS_DEBUG} -fprofile-instr-generate -fcoverage-mapping")
  27. set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
  28. "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} -fprofile-instr-generate")
  29. set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE
  30. "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} -fprofile-instr-generate")
  31. set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
  32. "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fprofile-instr-generate")
  33. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GCC")
  34. set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} --coverage")
  35. set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} --coverage")
  36. set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
  37. "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} --coverage")
  38. set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE
  39. "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} --coverage")
  40. set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
  41. "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --coverage")
  42. endif()
  43. # Panda3D is now a C++11 project.
  44. set(CMAKE_CXX_STANDARD 11)
  45. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  46. # Set certain CMake flags we expect
  47. set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
  48. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  49. set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
  50. # Set up the output directory structure, mimicking that of makepanda
  51. set(CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}/cmake")
  52. if(CMAKE_CFG_INTDIR STREQUAL ".")
  53. # Single-configuration generator; output goes straight in the binary dir
  54. set(PANDA_OUTPUT_DIR "${PROJECT_BINARY_DIR}")
  55. else()
  56. # Multi-configuration generator; add a per-configuration path prefix
  57. set(PANDA_OUTPUT_DIR "${PROJECT_BINARY_DIR}/$<CONFIG>")
  58. endif()
  59. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/bin")
  60. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/lib")
  61. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/lib")
  62. set(MODULE_DESTINATION "${CMAKE_INSTALL_LIBDIR}")
  63. # Runtime code assumes that dynamic modules have a "lib" prefix; Windows
  64. # assumes that debug libraries have a _d suffix.
  65. set(CMAKE_SHARED_MODULE_PREFIX "lib")
  66. if(WIN32)
  67. set(CMAKE_DEBUG_POSTFIX "_d")
  68. # Windows uses libfoo.lib for static libraries and foo.lib/dll for dynamic.
  69. set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
  70. # On Windows, modules (DLLs) are located in bin; lib is just for .lib files
  71. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/bin")
  72. if(BUILD_SHARED_LIBS)
  73. set(MODULE_DESTINATION "bin")
  74. endif()
  75. endif()
  76. # Though not technically correct, we'll still want MODULE type libraries
  77. # (used for display and audio plugins) to use a .dylib extension.
  78. if(APPLE)
  79. set(CMAKE_SHARED_MODULE_SUFFIX ".dylib")
  80. endif()
  81. # Set warning levels
  82. if(MSVC)
  83. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
  84. set(CMAKE_CCXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
  85. else()
  86. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
  87. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
  88. endif()
  89. if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
  90. set(global_flags
  91. "-Wno-unused-function -Wno-unused-parameter -fno-strict-aliasing -Werror=return-type")
  92. set(release_flags "-Wno-unused-variable")
  93. if(NOT MSVC)
  94. # These flags upset Clang when it's in MSVC mode
  95. set(release_flags "${release_flags} -fno-stack-protector -ffast-math -fno-unsafe-math-optimizations")
  96. # Allow NaN to occur in the public SDK
  97. set(standard_flags "${release_flags} -fno-finite-math-only")
  98. else()
  99. set(standard_flags "${release_flags}")
  100. endif()
  101. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${global_flags}")
  102. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${global_flags}")
  103. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${release_flags}")
  104. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${release_flags}")
  105. set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} ${release_flags}")
  106. set(CMAKE_CXX_FLAGS_STANDARD "${CMAKE_CXX_FLAGS_STANDARD} ${standard_flags}")
  107. if(MSVC)
  108. # Clang behaving as MSVC
  109. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-command-line-argument")
  110. set(CMAKE_CXX_FLAGS
  111. "${CMAKE_CXX_FLAGS} -Wno-microsoft-template -Wno-unused-command-line-argument")
  112. endif()
  113. endif()
  114. if(WIN32)
  115. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  116. endif()
  117. # CMake will often pass -rdynamic when linking executables as a convenience for
  118. # projects that might forget when to use ENABLE_EXPORTS. This is preposterous,
  119. # since it prevents the linker from removing symbols unneeded by the executable
  120. # and stops us from identifying cases where ENABLE_EXPORTS is needed.
  121. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  122. set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
  123. # As long as we're figuring out compiler flags, figure out the flags for
  124. # turning C++ exception support on and off
  125. if(MSVC)
  126. set(cxx_exceptions_on "/EHsc")
  127. set(cxx_exceptions_off "/D_HAS_EXCEPTIONS=0")
  128. set(cxx_rtti_on "/GR")
  129. set(cxx_rtti_off "/GR-")
  130. else()
  131. check_cxx_compiler_flag("-fno-exceptions" COMPILER_SUPPORTS_FEXCEPTIONS)
  132. if(COMPILER_SUPPORTS_FEXCEPTIONS)
  133. set(cxx_exceptions_on "-fexceptions")
  134. set(cxx_exceptions_off "-fno-exceptions")
  135. else()
  136. set(cxx_exceptions_on)
  137. set(cxx_exceptions_off)
  138. endif()
  139. check_cxx_compiler_flag("-fno-rtti" COMPILER_SUPPORTS_FRTTI)
  140. if(COMPILER_SUPPORTS_FRTTI)
  141. set(cxx_rtti_on "-frtti")
  142. set(cxx_rtti_off "-fno-rtti")
  143. else()
  144. set(cxx_rtti_on)
  145. set(cxx_rtti_off)
  146. endif()
  147. endif()
  148. set(cxx_exceptions_property "$<BOOL:$<TARGET_PROPERTY:CXX_EXCEPTIONS>>")
  149. add_compile_options(
  150. "$<${cxx_exceptions_property}:${cxx_exceptions_on}>"
  151. "$<$<NOT:${cxx_exceptions_property}>:${cxx_exceptions_off}>")
  152. set(cxx_rtti_property "$<BOOL:$<TARGET_PROPERTY:CXX_RTTI>>")
  153. if(NOT ANDROID)
  154. # Normally, our Debug build defaults RTTI on. This is not the case on
  155. # Android, where we don't use it even on Debug, to save space.
  156. set(cxx_rtti_property "$<OR:$<CONFIG:Debug>,${cxx_rtti_property}>")
  157. endif()
  158. add_compile_options(
  159. "$<${cxx_rtti_property}:${cxx_rtti_on}>"
  160. "$<$<NOT:${cxx_rtti_property}>:${cxx_rtti_off}>")
  161. set_property(DIRECTORY "${PROJECT_SOURCE_DIR}" APPEND PROPERTY
  162. COMPILE_DEFINITIONS "$<${cxx_rtti_property}:HAVE_RTTI>")
  163. if(MSVC)
  164. set(msvc_bigobj_property "$<BOOL:$<TARGET_PROPERTY:MSVC_BIGOBJ>>")
  165. add_compile_options("$<${msvc_bigobj_property}:/bigobj>")
  166. endif()
  167. # We should use -fvisibility=hidden everywhere, as it makes sure we think
  168. # about what symbols really should be exposed externally. For more info, see:
  169. # https://gcc.gnu.org/wiki/Visibility
  170. if(NOT MSVC)
  171. check_cxx_compiler_flag("-fvisibility=hidden" COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  172. if(COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  173. add_compile_options("-fvisibility=hidden")
  174. endif()
  175. endif()
  176. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  177. check_cxx_compiler_flag("-fno-semantic-interposition" COMPILER_SUPPORTS_FNO_SEMANTIC_INTERPOSITION)
  178. if(COMPILER_SUPPORTS_FNO_SEMANTIC_INTERPOSITION)
  179. add_compile_options("-fno-semantic-interposition")
  180. endif()
  181. endif()