CompilerFlags.cmake 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. set(CMAKE_C_FLAGS_STANDARD "-O3")
  12. set(CMAKE_CXX_FLAGS_STANDARD "-O3")
  13. set(CMAKE_SHARED_LINKER_FLAGS_STANDARD "")
  14. set(CMAKE_MODULE_LINKER_FLAGS_STANDARD "")
  15. set(CMAKE_EXE_LINKER_FLAGS_STANDARD "")
  16. # Panda3D is now a C++11 project. Newer versions of CMake support this out of
  17. # the box; for older versions we take a shot in the dark:
  18. if(CMAKE_VERSION VERSION_LESS "3.1")
  19. check_cxx_compiler_flag("-std=gnu++11" COMPILER_SUPPORTS_CXX11)
  20. if(COMPILER_SUPPORTS_CXX11)
  21. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
  22. else()
  23. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++0x")
  24. endif()
  25. else()
  26. set(CMAKE_CXX_STANDARD 11)
  27. endif()
  28. # Set certain CMake flags we expect
  29. set(CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)
  30. set(CMAKE_INCLUDE_CURRENT_DIR ON)
  31. # Set up the output directory structure, mimicking that of makepanda
  32. set(CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}/cmake")
  33. if(CMAKE_CFG_INTDIR STREQUAL ".")
  34. # Single-configuration generator; output goes straight in the binary dir
  35. set(PANDA_OUTPUT_DIR "${PROJECT_BINARY_DIR}")
  36. else()
  37. # Multi-configuration generator; add a per-configuration path prefix
  38. set(PANDA_OUTPUT_DIR "${PROJECT_BINARY_DIR}/$<CONFIG>")
  39. endif()
  40. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/bin")
  41. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/lib")
  42. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/lib")
  43. set(MODULE_DESTINATION "${CMAKE_INSTALL_LIBDIR}")
  44. # Runtime code assumes that dynamic modules have a "lib" prefix; Windows
  45. # assumes that debug libraries have a _d suffix.
  46. set(CMAKE_SHARED_MODULE_PREFIX "lib")
  47. if(WIN32)
  48. set(CMAKE_DEBUG_POSTFIX "_d")
  49. # Windows uses libfoo.lib for static libraries and foo.lib/dll for dynamic.
  50. set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
  51. # On Windows, modules (DLLs) are located in bin; lib is just for .lib files
  52. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PANDA_OUTPUT_DIR}/bin")
  53. if(BUILD_SHARED_LIBS)
  54. set(MODULE_DESTINATION "bin")
  55. endif()
  56. endif()
  57. # Though not technically correct, we'll still want MODULE type libraries
  58. # (used for display and audio plugins) to use a .dylib extension.
  59. if(APPLE)
  60. set(CMAKE_SHARED_MODULE_SUFFIX ".dylib")
  61. endif()
  62. # We want the output structured like build/CONFIG/bin, not build/bin/CONFIG per
  63. # the default for multi-configuration generators. In CMake 3.4+, it switches
  64. # automatically if the *_OUTPUT_DIRECTORY property contains a generator
  65. # expresssion, but as of this writing we support as early as CMake 3.0.2.
  66. #
  67. # So, let's just do this:
  68. if(CMAKE_VERSION VERSION_LESS "3.4")
  69. foreach(_type RUNTIME ARCHIVE LIBRARY)
  70. foreach(_config ${CMAKE_CONFIGURATION_TYPES})
  71. string(TOUPPER "${_config}" _config)
  72. set(CMAKE_${_type}_OUTPUT_DIRECTORY_${_config} "${CMAKE_${_type}_OUTPUT_DIRECTORY}")
  73. endforeach(_config)
  74. endforeach(_type)
  75. endif()
  76. # Set warning levels
  77. if(MSVC)
  78. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
  79. set(CMAKE_CCXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
  80. else()
  81. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
  82. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
  83. endif()
  84. if(NOT "x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC")
  85. set(disable_flags "-Wno-unused-function -Wno-unused-parameter")
  86. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${disable_flags}")
  87. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${disable_flags} -Wno-reorder")
  88. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wno-unused-variable")
  89. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -Wno-unused-variable")
  90. set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -Wno-unused-variable")
  91. set(CMAKE_CXX_FLAGS_STANDARD "${CMAKE_CXX_FLAGS_STANDARD} -Wno-unused-variable")
  92. if(MSVC)
  93. # Clang behaving as MSVC
  94. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-command-line-argument")
  95. set(CMAKE_CXX_FLAGS
  96. "${CMAKE_CXX_FLAGS} -Wno-microsoft-template -Wno-unused-command-line-argument")
  97. endif()
  98. endif()
  99. if(WIN32)
  100. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  101. endif()
  102. # CMake will often pass -rdynamic when linking executables as a convenience for
  103. # projects that might forget when to use ENABLE_EXPORTS. This is preposterous,
  104. # since it prevents the linker from removing symbols unneeded by the executable
  105. # and stops us from identifying cases where ENABLE_EXPORTS is needed.
  106. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  107. set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
  108. # As long as we're figuring out compiler flags, figure out the flags for
  109. # turning C++ exception support on and off
  110. if(MSVC)
  111. set(cxx_exceptions_on "/EHsc")
  112. set(cxx_exceptions_off "/D_HAS_EXCEPTIONS=0")
  113. set(cxx_rtti_on "/GR")
  114. set(cxx_rtti_off "/GR-")
  115. else()
  116. check_cxx_compiler_flag("-fno-exceptions" COMPILER_SUPPORTS_FEXCEPTIONS)
  117. if(COMPILER_SUPPORTS_FEXCEPTIONS)
  118. set(cxx_exceptions_on "-fexceptions")
  119. set(cxx_exceptions_off "-fno-exceptions")
  120. else()
  121. set(cxx_exceptions_on)
  122. set(cxx_exceptions_off)
  123. endif()
  124. check_cxx_compiler_flag("-fno-rtti" COMPILER_SUPPORTS_FRTTI)
  125. if(COMPILER_SUPPORTS_FRTTI)
  126. set(cxx_rtti_on "-frtti")
  127. set(cxx_rtti_off "-fno-rtti")
  128. else()
  129. set(cxx_rtti_on)
  130. set(cxx_rtti_off)
  131. endif()
  132. endif()
  133. set(cxx_exceptions_property "$<BOOL:$<TARGET_PROPERTY:CXX_EXCEPTIONS>>")
  134. add_compile_options(
  135. "$<${cxx_exceptions_property}:${cxx_exceptions_on}>"
  136. "$<$<NOT:${cxx_exceptions_property}>:${cxx_exceptions_off}>")
  137. set(cxx_rtti_property "$<BOOL:$<TARGET_PROPERTY:CXX_RTTI>>")
  138. if(NOT ANDROID)
  139. # Normally, our Debug build defaults RTTI on. This is not the case on
  140. # Android, where we don't use it even on Debug, to save space.
  141. set(cxx_rtti_property "$<OR:$<CONFIG:Debug>,${cxx_rtti_property}>")
  142. endif()
  143. add_compile_options(
  144. "$<${cxx_rtti_property}:${cxx_rtti_on}>"
  145. "$<$<NOT:${cxx_rtti_property}>:${cxx_rtti_off}>")
  146. set_property(DIRECTORY "${PROJECT_SOURCE_DIR}" APPEND PROPERTY
  147. COMPILE_DEFINITIONS "$<${cxx_rtti_property}:HAVE_RTTI>")
  148. if(MSVC)
  149. set(msvc_bigobj_property "$<BOOL:$<TARGET_PROPERTY:MSVC_BIGOBJ>>")
  150. add_compile_options("$<${msvc_bigobj_property}:/bigobj>")
  151. endif()
  152. # We should use -fvisibility=hidden everywhere, as it makes sure we think
  153. # about what symbols really should be exposed externally. For more info, see:
  154. # https://gcc.gnu.org/wiki/Visibility
  155. if(NOT MSVC)
  156. check_cxx_compiler_flag("-fvisibility=hidden" COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  157. if(COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  158. add_compile_options("-fvisibility=hidden")
  159. endif()
  160. endif()