CompilerFlags.cmake 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. Newer versions of CMake support this out of
  44. # the box; for older versions we take a shot in the dark:
  45. if(CMAKE_VERSION VERSION_LESS "3.1")
  46. check_cxx_compiler_flag("-std=gnu++11" COMPILER_SUPPORTS_CXX11)
  47. if(COMPILER_SUPPORTS_CXX11)
  48. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
  49. else()
  50. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
  51. endif()
  52. else()
  53. set(CMAKE_CXX_STANDARD 11)
  54. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  55. endif()
  56. # Set certain CMake flags we expect
  57. set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
  58. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  59. set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
  60. # Set up the output directory structure, mimicking that of makepanda
  61. set(CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}/cmake")
  62. if(CMAKE_GENERATOR STREQUAL "Xcode")
  63. set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE NO) # for now
  64. set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
  65. set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED "NO")
  66. # Xcode complains about codesigning if this option isn't set.
  67. set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
  68. endif()
  69. if(CMAKE_CFG_INTDIR STREQUAL ".")
  70. # Single-configuration generator; output goes straight in the binary dir
  71. set(PANDA_OUTPUT_DIR "${PROJECT_BINARY_DIR}")
  72. else()
  73. # Multi-configuration generator; add a per-configuration path prefix
  74. set(PANDA_OUTPUT_DIR "${PROJECT_BINARY_DIR}/$<CONFIG>")
  75. endif()
  76. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/bin")
  77. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/lib")
  78. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/lib")
  79. set(MODULE_DESTINATION "${CMAKE_INSTALL_LIBDIR}")
  80. # Runtime code assumes that dynamic modules have a "lib" prefix; Windows
  81. # assumes that debug libraries have a _d suffix.
  82. set(CMAKE_SHARED_MODULE_PREFIX "lib")
  83. if(WIN32)
  84. set(CMAKE_DEBUG_POSTFIX "_d")
  85. # Windows uses libfoo.lib for static libraries and foo.lib/dll for dynamic.
  86. set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
  87. # On Windows, modules (DLLs) are located in bin; lib is just for .lib files
  88. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/bin")
  89. if(BUILD_SHARED_LIBS)
  90. set(MODULE_DESTINATION "bin")
  91. endif()
  92. endif()
  93. # Though not technically correct, we'll still want MODULE type libraries
  94. # (used for display and audio plugins) to use a .dylib extension.
  95. if(APPLE)
  96. set(CMAKE_SHARED_MODULE_SUFFIX ".dylib")
  97. endif()
  98. # We want the output structured like build/CONFIG/bin, not build/bin/CONFIG per
  99. # the default for multi-configuration generators. In CMake 3.4+, it switches
  100. # automatically if the *_OUTPUT_DIRECTORY property contains a generator
  101. # expresssion, but as of this writing we support as early as CMake 3.0.2.
  102. #
  103. # So, let's just do this:
  104. if(CMAKE_VERSION VERSION_LESS "3.4")
  105. foreach(_type RUNTIME ARCHIVE LIBRARY)
  106. foreach(_config ${CMAKE_CONFIGURATION_TYPES})
  107. string(TOUPPER "${_config}" _config)
  108. set(CMAKE_${_type}_OUTPUT_DIRECTORY_${_config} "${CMAKE_${_type}_OUTPUT_DIRECTORY}")
  109. endforeach(_config)
  110. endforeach(_type)
  111. endif()
  112. # Set warning levels
  113. if(MSVC)
  114. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
  115. set(CMAKE_CCXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
  116. else()
  117. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
  118. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
  119. endif()
  120. if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
  121. set(global_flags
  122. "-Wno-unused-function -Wno-unused-parameter -fno-strict-aliasing")
  123. set(release_flags "-Wno-unused-variable")
  124. if(NOT MSVC)
  125. # These flags upset Clang when it's in MSVC mode
  126. set(release_flags "${release_flags} -fno-stack-protector -ffast-math -fno-unsafe-math-optimizations")
  127. # Allow NaN to occur in the public SDK
  128. set(standard_flags "${release_flags} -fno-finite-math-only")
  129. else()
  130. set(standard_flags "${release_flags}")
  131. endif()
  132. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${global_flags}")
  133. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${global_flags} -Wno-reorder")
  134. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${release_flags}")
  135. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${release_flags}")
  136. set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} ${release_flags}")
  137. set(CMAKE_CXX_FLAGS_STANDARD "${CMAKE_CXX_FLAGS_STANDARD} ${standard_flags}")
  138. if(MSVC)
  139. # Clang behaving as MSVC
  140. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-command-line-argument")
  141. set(CMAKE_CXX_FLAGS
  142. "${CMAKE_CXX_FLAGS} -Wno-microsoft-template -Wno-unused-command-line-argument")
  143. endif()
  144. endif()
  145. if(WIN32)
  146. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  147. endif()
  148. # CMake will often pass -rdynamic when linking executables as a convenience for
  149. # projects that might forget when to use ENABLE_EXPORTS. This is preposterous,
  150. # since it prevents the linker from removing symbols unneeded by the executable
  151. # and stops us from identifying cases where ENABLE_EXPORTS is needed.
  152. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  153. set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
  154. # As long as we're figuring out compiler flags, figure out the flags for
  155. # turning C++ exception support on and off
  156. if(MSVC)
  157. set(cxx_exceptions_on "/EHsc")
  158. set(cxx_exceptions_off "/D_HAS_EXCEPTIONS=0")
  159. set(cxx_rtti_on "/GR")
  160. set(cxx_rtti_off "/GR-")
  161. else()
  162. check_cxx_compiler_flag("-fno-exceptions" COMPILER_SUPPORTS_FEXCEPTIONS)
  163. if(COMPILER_SUPPORTS_FEXCEPTIONS)
  164. set(cxx_exceptions_on "-fexceptions")
  165. set(cxx_exceptions_off "-fno-exceptions")
  166. else()
  167. set(cxx_exceptions_on)
  168. set(cxx_exceptions_off)
  169. endif()
  170. check_cxx_compiler_flag("-fno-rtti" COMPILER_SUPPORTS_FRTTI)
  171. if(COMPILER_SUPPORTS_FRTTI)
  172. set(cxx_rtti_on "-frtti")
  173. set(cxx_rtti_off "-fno-rtti")
  174. else()
  175. set(cxx_rtti_on)
  176. set(cxx_rtti_off)
  177. endif()
  178. endif()
  179. set(cxx_exceptions_property "$<BOOL:$<TARGET_PROPERTY:CXX_EXCEPTIONS>>")
  180. add_compile_options(
  181. "$<${cxx_exceptions_property}:${cxx_exceptions_on}>"
  182. "$<$<NOT:${cxx_exceptions_property}>:${cxx_exceptions_off}>")
  183. set(cxx_rtti_property "$<BOOL:$<TARGET_PROPERTY:CXX_RTTI>>")
  184. if(NOT ANDROID)
  185. # Normally, our Debug build defaults RTTI on. This is not the case on
  186. # Android, where we don't use it even on Debug, to save space.
  187. set(cxx_rtti_property "$<OR:$<CONFIG:Debug>,${cxx_rtti_property}>")
  188. endif()
  189. add_compile_options(
  190. "$<${cxx_rtti_property}:${cxx_rtti_on}>"
  191. "$<$<NOT:${cxx_rtti_property}>:${cxx_rtti_off}>")
  192. set_property(DIRECTORY "${PROJECT_SOURCE_DIR}" APPEND PROPERTY
  193. COMPILE_DEFINITIONS "$<${cxx_rtti_property}:HAVE_RTTI>")
  194. if(MSVC)
  195. set(msvc_bigobj_property "$<BOOL:$<TARGET_PROPERTY:MSVC_BIGOBJ>>")
  196. add_compile_options("$<${msvc_bigobj_property}:/bigobj>")
  197. endif()
  198. # We should use -fvisibility=hidden everywhere, as it makes sure we think
  199. # about what symbols really should be exposed externally. For more info, see:
  200. # https://gcc.gnu.org/wiki/Visibility
  201. if(NOT MSVC)
  202. check_cxx_compiler_flag("-fvisibility=hidden" COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  203. if(COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  204. add_compile_options("-fvisibility=hidden")
  205. endif()
  206. endif()