CMakeLists.txt 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. cmake_minimum_required(VERSION 3.0.2)
  2. set(CMAKE_DISABLE_SOURCE_CHANGES ON) # Must go before project() below
  3. set(CMAKE_DISABLE_IN_SOURCE_BUILD ON) # Must go before project() below
  4. if(CMAKE_VERSION VERSION_GREATER "3.11" OR POLICY CMP0072)
  5. # Prefer GLVND over libGL when available; this will be enabled by default
  6. # once the minimum CMake version is at least 3.11.
  7. cmake_policy(SET CMP0072 NEW)
  8. endif()
  9. if(CMAKE_VERSION VERSION_GREATER "3.12" OR POLICY CMP0074)
  10. # Needed for THIRDPARTY_DIRECTORY support; this will be enabled by default
  11. # once the minimum CMake version is at least 3.12.
  12. cmake_policy(SET CMP0074 NEW)
  13. endif()
  14. if(POLICY CMP0091)
  15. # Needed for CMake to pass /MD flag properly with non-VC generators.
  16. cmake_policy(SET CMP0091 NEW)
  17. endif()
  18. # Determine whether we are using a multi-config generator.
  19. if(CMAKE_VERSION VERSION_GREATER "3.8")
  20. get_property(IS_MULTICONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  21. else()
  22. message(WARNING "Multi-configuration builds may not work properly when using
  23. a CMake < 3.9. Making a guess if this is a multi-config generator.")
  24. if(DEFINED CMAKE_CONFIGURATION_TYPES)
  25. set(IS_MULTICONFIG ON)
  26. else()
  27. set(IS_MULTICONFIG OFF)
  28. endif()
  29. endif()
  30. # Set the default CMAKE_BUILD_TYPE before calling project().
  31. if(IS_MULTICONFIG)
  32. message(STATUS "Using multi-configuration generator")
  33. else()
  34. if(NOT CMAKE_BUILD_TYPE)
  35. set(CMAKE_BUILD_TYPE Standard CACHE STRING "Choose the type of build." FORCE)
  36. message(STATUS "Using default build type ${CMAKE_BUILD_TYPE}")
  37. else()
  38. message(STATUS "Using build type ${CMAKE_BUILD_TYPE}")
  39. endif()
  40. endif()
  41. # Set defaults for macOS, must be before project().
  42. if(APPLE)
  43. set(CMAKE_OSX_DEPLOYMENT_TARGET "10.9" CACHE STRING "Minimum macOS version to target")
  44. set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
  45. if(CMAKE_VERSION VERSION_LESS "3.19" AND NOT CMAKE_OSX_SYSROOT)
  46. # Older CMake chose SDK based on deployment target, against Apple's recommendations.
  47. # However, we need to use the latest to be able to target arm64.
  48. if(IS_DIRECTORY "/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk")
  49. set(CMAKE_OSX_SYSROOT "/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk" CACHE STRING "")
  50. elseif(IS_DIRECTORY "/Library/Developer/CommandLineTools/SDKs/MacOSX11.0.sdk")
  51. set(CMAKE_OSX_SYSROOT "/Library/Developer/CommandLineTools/SDKs/MacOSX11.0.sdk" CACHE STRING "")
  52. endif()
  53. endif()
  54. endif()
  55. # Figure out the version
  56. set(_s "[\\t ]*") # CMake doesn't support \s*
  57. file(STRINGS "setup.cfg" _version REGEX "^version${_s}=${_s}")
  58. string(REGEX REPLACE "^.*=${_s}" "" _version "${_version}")
  59. project(Panda3D VERSION ${_version})
  60. unset(_version)
  61. unset(_s)
  62. # Determine the possible build types. Must be *after* calling project().
  63. set(_configs Standard Release RelWithDebInfo Debug MinSizeRel)
  64. if(CMAKE_CXX_COMPILER_ID MATCHES "(AppleClang|Clang|GCC)")
  65. list(APPEND _configs Coverage)
  66. endif()
  67. if(IS_MULTICONFIG)
  68. set(CMAKE_CONFIGURATION_TYPES "${_configs}" CACHE STRING "" FORCE)
  69. else()
  70. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS ${_configs})
  71. endif()
  72. enable_testing()
  73. string(REPLACE "$(EFFECTIVE_PLATFORM_NAME)" "" PANDA_CFG_INTDIR "${CMAKE_CFG_INTDIR}")
  74. # Add generic modules to cmake module path,
  75. # and add Panda3D specific modules to cmake module path
  76. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/")
  77. set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/macros/")
  78. if(CMAKE_VERSION VERSION_GREATER "3.8")
  79. # When using the Xcode generator, don't append the platform name to the
  80. # intermediate configuration directory.
  81. set_property(GLOBAL PROPERTY XCODE_EMIT_EFFECTIVE_PLATFORM_NAME OFF)
  82. endif()
  83. # Include modules builtin to CMake
  84. include(GNUInstallDirs) # Defines CMAKE_INSTALL_<dir> variables
  85. # Include global modules needed for configure scripts
  86. include(PackageConfig) # Defines package_option
  87. include(PerConfigOption) # Defines per_config_option
  88. # Configure Panda3D
  89. include(dtool/CompilerFlags.cmake)
  90. include(dtool/PandaVersion.cmake)
  91. include(dtool/Package.cmake)
  92. include(dtool/Config.cmake)
  93. # Include global modules
  94. include(AddBisonTarget) # Defines add_bison_target function
  95. include(AddFlexTarget) # Defines add_flex_target function
  96. include(BuildMetalib) # Defines add_component_library AND add_metalib
  97. include(CompositeSources) # Defines composite_sources function
  98. include(Python) # Defines add_python_target AND install_python_package
  99. include(Interrogate) # Defines target_interrogate AND add_python_module
  100. include(RunPzip) # Defines run_pzip function
  101. include(Versioning) # Hooks 'add_library' to apply VERSION/SOVERSION
  102. # Determine which trees to build.
  103. option(BUILD_DTOOL "Build the dtool source tree." ON)
  104. option(BUILD_PANDA "Build the panda source tree." ON)
  105. option(BUILD_DIRECT "Build the direct source tree." ON)
  106. option(BUILD_PANDATOOL "Build the pandatool source tree." ON)
  107. option(BUILD_CONTRIB "Build the contrib source tree." ON)
  108. option(BUILD_MODELS "Build/install the built-in models." ON)
  109. # Include Panda3D packages
  110. if(BUILD_DTOOL)
  111. add_subdirectory(dtool "${CMAKE_BINARY_DIR}/dtool")
  112. endif()
  113. if(BUILD_PANDA)
  114. add_subdirectory(panda "${CMAKE_BINARY_DIR}/panda")
  115. endif()
  116. if(BUILD_DIRECT)
  117. add_subdirectory(direct "${CMAKE_BINARY_DIR}/direct")
  118. endif()
  119. if(BUILD_PANDATOOL)
  120. add_subdirectory(pandatool "${CMAKE_BINARY_DIR}/pandatool")
  121. endif()
  122. if(BUILD_CONTRIB)
  123. add_subdirectory(contrib "${CMAKE_BINARY_DIR}/contrib")
  124. endif()
  125. if(BUILD_MODELS)
  126. run_pzip(models
  127. "${CMAKE_CURRENT_SOURCE_DIR}/models/"
  128. "${PROJECT_BINARY_DIR}/${PANDA_CFG_INTDIR}/models"
  129. *.egg)
  130. add_custom_command(TARGET models
  131. POST_BUILD
  132. COMMAND ${CMAKE_COMMAND}
  133. -DSOURCE="${CMAKE_CURRENT_SOURCE_DIR}/models/audio/"
  134. -DDESTINATION="${PANDA_OUTPUT_DIR}/models/audio"
  135. -P ${PROJECT_SOURCE_DIR}/cmake/scripts/CopyPattern.cmake
  136. COMMENT "Copying models/audio")
  137. add_custom_command(TARGET models
  138. POST_BUILD
  139. COMMAND ${CMAKE_COMMAND}
  140. -DSOURCE="${CMAKE_CURRENT_SOURCE_DIR}/models/icons/"
  141. -DDESTINATION="${PANDA_OUTPUT_DIR}/models/icons"
  142. -P ${PROJECT_SOURCE_DIR}/cmake/scripts/CopyPattern.cmake
  143. COMMENT "Copying models/icons")
  144. add_custom_command(TARGET models
  145. POST_BUILD
  146. COMMAND ${CMAKE_COMMAND}
  147. -DSOURCE="${CMAKE_CURRENT_SOURCE_DIR}/models/maps/"
  148. -DDESTINATION="${PANDA_OUTPUT_DIR}/models/maps"
  149. -P ${PROJECT_SOURCE_DIR}/cmake/scripts/CopyPattern.cmake
  150. COMMENT "Copying models/maps")
  151. install(DIRECTORY "${PANDA_OUTPUT_DIR}/models"
  152. COMPONENT Models DESTINATION ${CMAKE_INSTALL_DATADIR}/panda3d)
  153. endif()
  154. if(INTERROGATE_PYTHON_INTERFACE)
  155. # If we built the Python interface, run the test suite. Note, we do NOT test
  156. # for pytest before adding this test. If the user doesn't have pytest, we'd
  157. # like for the tests to fail.
  158. # In the Coverage configuration, we also require pytest-cov
  159. add_test(NAME pytest
  160. COMMAND "${PYTHON_EXECUTABLE}" -m pytest "${PROJECT_SOURCE_DIR}/tests"
  161. $<$<CONFIG:Coverage>:--cov=.>
  162. WORKING_DIRECTORY "${PANDA_OUTPUT_DIR}")
  163. endif()
  164. # Generate the Panda3DConfig.cmake file so find_package(Panda3D) works, and
  165. # also register the build directory with CMake's package registry.
  166. file(COPY "${PROJECT_SOURCE_DIR}/cmake/install/Panda3DConfig.cmake"
  167. DESTINATION "${PROJECT_BINARY_DIR}")
  168. install(FILES "${PROJECT_SOURCE_DIR}/cmake/install/Panda3DConfig.cmake"
  169. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/Panda3D")
  170. include(CMakePackageConfigHelpers)
  171. write_basic_package_version_file(
  172. "${PROJECT_BINARY_DIR}/Panda3DConfigVersion.cmake"
  173. VERSION "${PROJECT_VERSION}"
  174. COMPATIBILITY AnyNewerVersion)
  175. install(FILES "${PROJECT_BINARY_DIR}/Panda3DConfigVersion.cmake"
  176. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/Panda3D")
  177. if(NOT CMAKE_CROSSCOMPILING)
  178. export(PACKAGE Panda3D)
  179. endif()