CMakeLists.txt 7.5 KB

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