CMakeLists.txt 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. cmake_minimum_required(VERSION 3.12)
  2. cmake_policy(SET CMP0077 NEW)
  3. if ("${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}" VERSION_GREATER_EQUAL "3.24")
  4. cmake_policy(SET CMP0135 NEW)
  5. endif()
  6. project(fastgltf VERSION 0.5.0 LANGUAGES C CXX)
  7. option(FASTGLTF_DOWNLOAD_SIMDJSON "Downloads a copy of simdjson itself to satisfy the dependency" ON)
  8. option(FASTGLTF_USE_CUSTOM_SMALLVECTOR "Uses a custom SmallVector type optimised for small arrays" OFF)
  9. option(FASTGLTF_ENABLE_TESTS "Enables test targets for fastlgtf" OFF)
  10. option(FASTGLTF_ENABLE_EXAMPLES "Enables example targets for fastgltf" OFF)
  11. option(FASTGLTF_ENABLE_DOCS "Enables the configuration of targets that build/generate documentation" OFF)
  12. option(FASTGLTF_ENABLE_GLTF_RS "Enables the benchmark usage of gltf-rs" OFF)
  13. option(FASTGLTF_ENABLE_DEPRECATED_EXT "Enables support for deprecated extensions" OFF)
  14. include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/add_source_directory.cmake)
  15. include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/compiler_flags.cmake)
  16. if (FASTGLTF_DOWNLOAD_SIMDJSON)
  17. # Download and configure simdjson
  18. set(SIMDJSON_TARGET_VERSION "3.3.0")
  19. set(SIMDJSON_DL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/deps/simdjson")
  20. file(MAKE_DIRECTORY ${SIMDJSON_DL_DIR})
  21. set(SIMDJSON_HEADER_FILE "${SIMDJSON_DL_DIR}/simdjson.h")
  22. set(SIMDJSON_SOURCE_FILE "${SIMDJSON_DL_DIR}/simdjson.cpp")
  23. macro(download_simdjson)
  24. file(DOWNLOAD "https://raw.githubusercontent.com/simdjson/simdjson/v${SIMDJSON_TARGET_VERSION}/singleheader/simdjson.h" ${SIMDJSON_HEADER_FILE})
  25. file(DOWNLOAD "https://raw.githubusercontent.com/simdjson/simdjson/v${SIMDJSON_TARGET_VERSION}/singleheader/simdjson.cpp" ${SIMDJSON_SOURCE_FILE})
  26. endmacro()
  27. if (EXISTS ${SIMDJSON_HEADER_FILE})
  28. # Look for the SIMDJSON_VERSION define in the header to check the version.
  29. file(STRINGS ${SIMDJSON_HEADER_FILE} SIMDJSON_HEADER_VERSION_LINE REGEX "^#define SIMDJSON_VERSION ")
  30. string(REGEX MATCHALL "[0-9.]+" SIMDJSON_HEADER_VERSION "${SIMDJSON_HEADER_VERSION_LINE}")
  31. message(STATUS "fastgltf: Found simdjson (Version ${SIMDJSON_HEADER_VERSION})")
  32. if (SIMDJSON_HEADER_VERSION VERSION_LESS SIMDJSON_TARGET_VERSION)
  33. message(STATUS "fastgltf: simdjson outdated, downloading...")
  34. download_simdjson()
  35. endif()
  36. else()
  37. message(STATUS "fastgltf: Did not find simdjson, downloading...")
  38. download_simdjson()
  39. if (NOT EXISTS "${SIMDJSON_HEADER_FILE}")
  40. message(FATAL_ERROR "fastgltf: Failed to download simdjson.")
  41. endif()
  42. endif()
  43. add_library(fastgltf_simdjson ${SIMDJSON_HEADER_FILE} ${SIMDJSON_SOURCE_FILE})
  44. target_compile_features(fastgltf_simdjson PRIVATE cxx_std_17)
  45. target_include_directories(fastgltf_simdjson PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/deps/simdjson> $<INSTALL_INTERFACE:include>)
  46. compiler_flags(fastgltf_simdjson)
  47. enable_debug_inlining(fastgltf_simdjson)
  48. install(
  49. FILES deps/simdjson/simdjson.h
  50. DESTINATION include
  51. )
  52. install(
  53. TARGETS fastgltf_simdjson
  54. EXPORT fastgltf_simdjson-targets
  55. LIBRARY DESTINATION lib
  56. ARCHIVE DESTINATION lib
  57. RUNTIME DESTINATION bin
  58. INCLUDES DESTINATION include
  59. )
  60. install(
  61. EXPORT fastgltf_simdjson-targets
  62. FILE fastgltf_simdjsonTargets.cmake
  63. NAMESPACE fastgltf::
  64. DESTINATION lib/cmake/fastgltf
  65. )
  66. endif()
  67. # Create the library target
  68. add_library(fastgltf
  69. "src/fastgltf.cpp" "src/base64.cpp"
  70. "include/fastgltf/base64.hpp" "include/fastgltf/glm_element_traits.hpp" "include/fastgltf/parser.hpp" "include/fastgltf/tools.hpp" "include/fastgltf/types.hpp" "include/fastgltf/util.hpp")
  71. add_library(fastgltf::fastgltf ALIAS fastgltf)
  72. compiler_flags(fastgltf)
  73. enable_debug_inlining(fastgltf)
  74. target_compile_features(fastgltf PUBLIC cxx_std_17)
  75. target_include_directories(fastgltf PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include> $<INSTALL_INTERFACE:include>)
  76. set_target_properties(fastgltf PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS YES)
  77. set_target_properties(fastgltf PROPERTIES VERSION ${PROJECT_VERSION})
  78. if (TARGET fastgltf_simdjson)
  79. target_link_libraries(fastgltf PRIVATE fastgltf_simdjson)
  80. elseif(TARGET simdjson::simdjson)
  81. target_link_libraries(fastgltf PRIVATE simdjson::simdjson)
  82. endif()
  83. if (SIMDJSON_TARGET_VERSION)
  84. target_compile_definitions(fastgltf PRIVATE SIMDJSON_TARGET_VERSION="${SIMDJSON_TARGET_VERSION}")
  85. endif()
  86. target_compile_definitions(fastgltf PUBLIC "FASTGLTF_USE_CUSTOM_SMALLVECTOR=$<BOOL:${FASTGLTF_USE_CUSTOM_SMALLVECTOR}>")
  87. target_compile_definitions(fastgltf PUBLIC "FASTGLTF_ENABLE_DEPRECATED_EXT=$<BOOL:${FASTGLTF_ENABLE_DEPRECATED_EXT}>")
  88. if (ANDROID)
  89. target_link_libraries(fastgltf PRIVATE android)
  90. endif()
  91. install(
  92. FILES "include/fastgltf/base64.hpp" "include/fastgltf/glm_element_traits.hpp" "include/fastgltf/parser.hpp" "include/fastgltf/tools.hpp" "include/fastgltf/types.hpp" "include/fastgltf/util.hpp"
  93. DESTINATION include/fastgltf
  94. )
  95. install(
  96. TARGETS fastgltf
  97. EXPORT fastgltf-targets
  98. LIBRARY DESTINATION lib
  99. ARCHIVE DESTINATION lib
  100. RUNTIME DESTINATION bin
  101. INCLUDES DESTINATION include
  102. )
  103. install(
  104. EXPORT fastgltf-targets
  105. FILE fastgltfConfig.cmake
  106. NAMESPACE fastgltf::
  107. DESTINATION lib/cmake/fastgltf
  108. )
  109. if (FASTGLTF_ENABLE_TESTS OR FASTGLTF_ENABLE_EXAMPLES)
  110. # This is required so that Catch2 compiles with C++17, enabling various features we use in tests.
  111. if (NOT DEFINED CMAKE_CXX_STANDARD OR CMAKE_CXX_STANDARD STREQUAL "" OR CMAKE_CXX_STANDARD LESS 17)
  112. set(CMAKE_CXX_STANDARD "17" CACHE STRING "C++ standard" FORCE)
  113. endif()
  114. add_subdirectory(deps)
  115. endif()
  116. if (FASTGLTF_ENABLE_EXAMPLES)
  117. add_subdirectory(examples)
  118. endif()
  119. if (FASTGLTF_ENABLE_TESTS)
  120. add_subdirectory(tests)
  121. endif()
  122. if (FASTGLTF_ENABLE_DOCS)
  123. add_subdirectory(docs)
  124. endif()