CompilerFlags.cmake 5.9 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_GENERATOR STREQUAL "Xcode")
  27. # On the Xcode generator, CMake generates a separate make target definition for
  28. # every config, so it ends up spamming warnings once we try to build.
  29. set(intdir $<CONFIG>)
  30. else()
  31. set(intdir ${PANDA_CFG_INTDIR})
  32. endif()
  33. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${intdir}/bin")
  34. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${intdir}/lib")
  35. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${intdir}/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 "${PROJECT_BINARY_DIR}/${PANDA_CFG_INTDIR}/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. # Since we're using CMAKE_CFG_INTDIR to put everything in a
  56. # configuration-specific subdirectory when building on a multi-config
  57. # generator, we need to suppress the usual configuration name appending
  58. # behavior of CMake. In CMake 3.4+, it will suppress this behavior
  59. # automatically if the *_OUTPUT_DIRECTORY property contains a generator
  60. # expresssion, but:
  61. # a) As of this writing we support as early as CMake 3.0.2
  62. # b) ${CMAKE_CFG_INTDIR} doesn't actually expand to a generator expression
  63. #
  64. # So, to solve both of these, let's just do this:
  65. foreach(_type RUNTIME ARCHIVE LIBRARY)
  66. foreach(_config ${CMAKE_CONFIGURATION_TYPES})
  67. string(TOUPPER "${_config}" _config)
  68. set(CMAKE_${_type}_OUTPUT_DIRECTORY_${_config} "${CMAKE_${_type}_OUTPUT_DIRECTORY}")
  69. endforeach(_config)
  70. endforeach(_type)
  71. # Set warning levels
  72. if(MSVC)
  73. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
  74. set(CMAKE_CCXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
  75. else()
  76. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
  77. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
  78. endif()
  79. if(NOT "x${CMAKE_CXX_COMPILER_ID}" STREQUAL "xMSVC")
  80. set(disable_flags "-Wno-unused-function -Wno-unused-parameter")
  81. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${disable_flags}")
  82. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${disable_flags} -Wno-reorder")
  83. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wno-unused-variable")
  84. set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -Wno-unused-variable")
  85. set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -Wno-unused-variable")
  86. if(MSVC)
  87. # Clang behaving as MSVC`
  88. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-command-line-argument")
  89. set(CMAKE_CXX_FLAGS
  90. "${CMAKE_CXX_FLAGS} -Wno-microsoft-template -Wno-unused-command-line-argument")
  91. endif()
  92. endif()
  93. if(WIN32)
  94. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  95. endif()
  96. # CMake will often pass -rdynamic when linking executables as a convenience for
  97. # projects that might forget when to use ENABLE_EXPORTS. This is preposterous,
  98. # since it prevents the linker from removing symbols unneeded by the executable
  99. # and stops us from identifying cases where ENABLE_EXPORTS is needed.
  100. set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
  101. set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
  102. # As long as we're figuring out compiler flags, figure out the flags for
  103. # turning C++ exception support on and off
  104. if(MSVC)
  105. set(cxx_exceptions_on "/EHsc")
  106. set(cxx_exceptions_off "/D_HAS_EXCEPTIONS=0")
  107. else()
  108. check_cxx_compiler_flag("-fno-exceptions" COMPILER_SUPPORTS_FEXCEPTIONS)
  109. if(COMPILER_SUPPORTS_FEXCEPTIONS)
  110. set(cxx_exceptions_on "-fexceptions")
  111. set(cxx_exceptions_off "-fno-exceptions")
  112. else()
  113. set(cxx_exceptions_on)
  114. set(cxx_exceptions_off)
  115. endif()
  116. endif()
  117. set(cxx_exceptions_property "$<BOOL:$<TARGET_PROPERTY:CXX_EXCEPTIONS>>")
  118. add_compile_options(
  119. "$<${cxx_exceptions_property}:${cxx_exceptions_on}>"
  120. "$<$<NOT:${cxx_exceptions_property}>:${cxx_exceptions_off}>")
  121. if(MSVC)
  122. set(msvc_bigobj_property "$<BOOL:$<TARGET_PROPERTY:MSVC_BIGOBJ>>")
  123. add_compile_options("$<${msvc_bigobj_property}:/bigobj>")
  124. endif()
  125. # We should use -fvisibility=hidden everywhere, as it makes sure we think
  126. # about what symbols really should be exposed externally. For more info, see:
  127. # https://gcc.gnu.org/wiki/Visibility
  128. if(NOT MSVC)
  129. check_cxx_compiler_flag("-fvisibility=hidden" COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  130. if(COMPILER_SUPPORTS_FVISIBILITY_HIDDEN)
  131. add_compile_options("-fvisibility=hidden")
  132. endif()
  133. endif()
  134. # These are flags for the custom configurations we add
  135. # Distribution
  136. set(CMAKE_C_FLAGS_DISTRIBUTION "")
  137. set(CMAKE_CXX_FLAGS_DISTRIBUTION "")
  138. set(CMAKE_SHARED_LINKER_FLAGS_DISTRIBUTION "")
  139. set(CMAKE_MODULE_LINKER_FLAGS_DISTRIBUTION "")
  140. set(CMAKE_EXE_LINKER_FLAGS_DISTRIBUTION "")