|
| 1 | +# Findtinygltf.cmake |
| 2 | +# |
| 3 | +# Finds the tinygltf library |
| 4 | +# |
| 5 | +# This will define the following variables |
| 6 | +# |
| 7 | +# tinygltf_FOUND |
| 8 | +# tinygltf_INCLUDE_DIRS |
| 9 | +# |
| 10 | +# and the following imported targets |
| 11 | +# |
| 12 | +# tinygltf::tinygltf |
| 13 | +# |
| 14 | + |
| 15 | +# First, try to find nlohmann_json |
| 16 | +find_package(nlohmann_json QUIET) |
| 17 | +if(NOT nlohmann_json_FOUND) |
| 18 | + include(FetchContent) |
| 19 | + message(STATUS "nlohmann_json not found, fetching v3.12.0 from GitHub...") |
| 20 | + FetchContent_Declare( |
| 21 | + nlohmann_json |
| 22 | + GIT_REPOSITORY https://github.com/nlohmann/json.git |
| 23 | + GIT_TAG v3.12.0 # Use a specific tag for stability |
| 24 | + ) |
| 25 | + FetchContent_MakeAvailable(nlohmann_json) |
| 26 | +endif() |
| 27 | + |
| 28 | +# Try to find tinygltf using standard find_package |
| 29 | +find_path(tinygltf_INCLUDE_DIR |
| 30 | + NAMES tiny_gltf.h |
| 31 | + PATHS |
| 32 | + ${PC_tinygltf_INCLUDE_DIRS} |
| 33 | + /usr/include |
| 34 | + /usr/local/include |
| 35 | + $ENV{VULKAN_SDK}/include |
| 36 | + ${ANDROID_NDK}/sources/third_party |
| 37 | + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../external |
| 38 | + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../third_party |
| 39 | + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../attachments/external |
| 40 | + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../attachments/third_party |
| 41 | + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../attachments/include |
| 42 | + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../../external |
| 43 | + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../../third_party |
| 44 | + ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../../include |
| 45 | + PATH_SUFFIXES tinygltf include |
| 46 | +) |
| 47 | + |
| 48 | +# If not found, use FetchContent to download and build |
| 49 | +if(NOT tinygltf_INCLUDE_DIR) |
| 50 | + # If not found, use FetchContent to download and build |
| 51 | + include(FetchContent) |
| 52 | + |
| 53 | + message(STATUS "tinygltf not found, fetching from GitHub...") |
| 54 | + FetchContent_Declare( |
| 55 | + tinygltf |
| 56 | + GIT_REPOSITORY https://github.com/syoyo/tinygltf.git |
| 57 | + GIT_TAG v2.8.18 # Use a specific tag for stability |
| 58 | + ) |
| 59 | + |
| 60 | + # Set policy to suppress the deprecation warning |
| 61 | + if(POLICY CMP0169) |
| 62 | + cmake_policy(SET CMP0169 OLD) |
| 63 | + endif() |
| 64 | + |
| 65 | + # Populate the content but don't configure it yet |
| 66 | + FetchContent_GetProperties(tinygltf) |
| 67 | + if(NOT tinygltf_POPULATED) |
| 68 | + FetchContent_Populate(tinygltf) |
| 69 | + |
| 70 | + # Update the minimum required CMake version to avoid deprecation warning |
| 71 | + file(READ "${tinygltf_SOURCE_DIR}/CMakeLists.txt" TINYGLTF_CMAKE_CONTENT) |
| 72 | + string(REPLACE "cmake_minimum_required(VERSION 3.6)" |
| 73 | + "cmake_minimum_required(VERSION 3.10)" |
| 74 | + TINYGLTF_CMAKE_CONTENT "${TINYGLTF_CMAKE_CONTENT}") |
| 75 | + file(WRITE "${tinygltf_SOURCE_DIR}/CMakeLists.txt" "${TINYGLTF_CMAKE_CONTENT}") |
| 76 | + |
| 77 | + # Create a symbolic link to make nlohmann/json.hpp available |
| 78 | + if(EXISTS "${tinygltf_SOURCE_DIR}/json.hpp") |
| 79 | + file(MAKE_DIRECTORY "${tinygltf_SOURCE_DIR}/nlohmann") |
| 80 | + file(CREATE_LINK "${tinygltf_SOURCE_DIR}/json.hpp" "${tinygltf_SOURCE_DIR}/nlohmann/json.hpp" SYMBOLIC) |
| 81 | + endif() |
| 82 | + |
| 83 | + # Set tinygltf to header-only mode |
| 84 | + set(TINYGLTF_HEADER_ONLY ON CACHE BOOL "Use header only version" FORCE) |
| 85 | + set(TINYGLTF_INSTALL OFF CACHE BOOL "Do not install tinygltf" FORCE) |
| 86 | + |
| 87 | + # Add the subdirectory after modifying the CMakeLists.txt |
| 88 | + add_subdirectory(${tinygltf_SOURCE_DIR} ${tinygltf_BINARY_DIR}) |
| 89 | + else() |
| 90 | + # If already populated, just make it available |
| 91 | + FetchContent_MakeAvailable(tinygltf) |
| 92 | + endif() |
| 93 | + |
| 94 | + # Get the include directory from the target |
| 95 | + get_target_property(tinygltf_INCLUDE_DIR tinygltf INTERFACE_INCLUDE_DIRECTORIES) |
| 96 | + if(NOT tinygltf_INCLUDE_DIR) |
| 97 | + # If we can't get the include directory from the target, use the source directory |
| 98 | + FetchContent_GetProperties(tinygltf SOURCE_DIR tinygltf_SOURCE_DIR) |
| 99 | + set(tinygltf_INCLUDE_DIR ${tinygltf_SOURCE_DIR}) |
| 100 | + endif() |
| 101 | +endif() |
| 102 | + |
| 103 | +include(FindPackageHandleStandardArgs) |
| 104 | +find_package_handle_standard_args(tinygltf |
| 105 | + REQUIRED_VARS tinygltf_INCLUDE_DIR |
| 106 | +) |
| 107 | + |
| 108 | +if(tinygltf_FOUND) |
| 109 | + set(tinygltf_INCLUDE_DIRS ${tinygltf_INCLUDE_DIR}) |
| 110 | + |
| 111 | + if(NOT TARGET tinygltf::tinygltf) |
| 112 | + add_library(tinygltf::tinygltf INTERFACE IMPORTED) |
| 113 | + set_target_properties(tinygltf::tinygltf PROPERTIES |
| 114 | + INTERFACE_INCLUDE_DIRECTORIES "${tinygltf_INCLUDE_DIRS}" |
| 115 | + INTERFACE_COMPILE_DEFINITIONS "TINYGLTF_IMPLEMENTATION;TINYGLTF_NO_EXTERNAL_IMAGE;TINYGLTF_NO_STB_IMAGE;TINYGLTF_NO_STB_IMAGE_WRITE" |
| 116 | + ) |
| 117 | + if(TARGET nlohmann_json::nlohmann_json) |
| 118 | + target_link_libraries(tinygltf::tinygltf INTERFACE nlohmann_json::nlohmann_json) |
| 119 | + endif() |
| 120 | + endif() |
| 121 | +elseif(TARGET tinygltf) |
| 122 | + # If find_package_handle_standard_args failed but we have a tinygltf target from FetchContent |
| 123 | + # Create an alias for the tinygltf target |
| 124 | + if(NOT TARGET tinygltf::tinygltf) |
| 125 | + add_library(tinygltf_wrapper INTERFACE) |
| 126 | + target_link_libraries(tinygltf_wrapper INTERFACE tinygltf) |
| 127 | + target_compile_definitions(tinygltf_wrapper INTERFACE |
| 128 | + TINYGLTF_IMPLEMENTATION |
| 129 | + TINYGLTF_NO_EXTERNAL_IMAGE |
| 130 | + TINYGLTF_NO_STB_IMAGE |
| 131 | + TINYGLTF_NO_STB_IMAGE_WRITE |
| 132 | + ) |
| 133 | + if(TARGET nlohmann_json::nlohmann_json) |
| 134 | + target_link_libraries(tinygltf_wrapper INTERFACE nlohmann_json::nlohmann_json) |
| 135 | + endif() |
| 136 | + add_library(tinygltf::tinygltf ALIAS tinygltf_wrapper) |
| 137 | + endif() |
| 138 | + |
| 139 | + # Set variables to indicate that tinygltf was found |
| 140 | + set(tinygltf_FOUND TRUE) |
| 141 | + set(TINYGLTF_FOUND TRUE) |
| 142 | + |
| 143 | + # Set include directories |
| 144 | + get_target_property(tinygltf_INCLUDE_DIR tinygltf INTERFACE_INCLUDE_DIRECTORIES) |
| 145 | + if(tinygltf_INCLUDE_DIR) |
| 146 | + set(tinygltf_INCLUDE_DIRS ${tinygltf_INCLUDE_DIR}) |
| 147 | + else() |
| 148 | + # If we can't get the include directory from the target, use the source directory |
| 149 | + FetchContent_GetProperties(tinygltf SOURCE_DIR tinygltf_SOURCE_DIR) |
| 150 | + set(tinygltf_INCLUDE_DIR ${tinygltf_SOURCE_DIR}) |
| 151 | + set(tinygltf_INCLUDE_DIRS ${tinygltf_INCLUDE_DIR}) |
| 152 | + |
| 153 | + # Explicitly set the include directory on the target |
| 154 | + if(TARGET tinygltf) |
| 155 | + set_target_properties(tinygltf PROPERTIES |
| 156 | + INTERFACE_INCLUDE_DIRECTORIES "${tinygltf_INCLUDE_DIR}" |
| 157 | + ) |
| 158 | + endif() |
| 159 | +endif() |
| 160 | +endif() |
| 161 | + |
| 162 | +mark_as_advanced(tinygltf_INCLUDE_DIR) |
0 commit comments