CMakeLists.txt 7.3 KB

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