CMakeLists.txt 7.9 KB

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