Skip to content

Commit 8e51d6b

Browse files
committed
Revert back to a working version of Findtinygltf.cmake
Add `force_android_build` input and improve `tinygltf` handling in CMake - Introduce `force_android_build` in the workflow to allow manual triggering of Android builds via `workflow_dispatch`. - Refactor `TinyGLTF` to `tinygltf` in CMake for consistent naming. - Enhance fallback behavior to fetch and configure `tinygltf` when not found, ensuring stability.
1 parent c71392b commit 8e51d6b

File tree

2 files changed

+113
-72
lines changed

2 files changed

+113
-72
lines changed

.github/workflows/workflow.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ on:
55
branches: [ main ]
66
pull_request:
77
branches: [ main ]
8+
workflow_dispatch:
9+
inputs:
10+
force_android_build:
11+
description: 'Force Android build to run regardless of file changes'
12+
required: false
13+
type: boolean
14+
default: false
815

916
jobs:
1017

@@ -436,7 +443,7 @@ jobs:
436443

437444
# We need to run a preliminary job to check for changes
438445
needs: check-android-changes
439-
if: needs.check-android-changes.outputs.should_build == 'true'
446+
if: needs.check-android-changes.outputs.should_build == 'true' || github.event.inputs.force_android_build == 'true'
440447

441448
steps:
442449
- uses: actions/checkout@v3
Lines changed: 105 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# FindTinyGLTF.cmake
1+
# Findtinygltf.cmake
22
#
3-
# Finds the TinyGLTF library
3+
# Finds the tinygltf library
44
#
55
# This will define the following variables
66
#
7-
# TinyGLTF_FOUND
8-
# TinyGLTF_INCLUDE_DIRS
7+
# tinygltf_FOUND
8+
# tinygltf_INCLUDE_DIRS
99
#
1010
# and the following imported targets
1111
#
@@ -25,89 +25,101 @@ if(NOT nlohmann_json_FOUND)
2525
FetchContent_MakeAvailable(nlohmann_json)
2626
endif()
2727

28-
# Try to find TinyGLTF using standard find_package
29-
find_path(TinyGLTF_INCLUDE_DIR
28+
# Try to find tinygltf using standard find_package
29+
find_path(tinygltf_INCLUDE_DIR
3030
NAMES tiny_gltf.h
31-
PATH_SUFFIXES include tinygltf
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
3246
)
3347

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+
34103
include(FindPackageHandleStandardArgs)
35104
find_package_handle_standard_args(tinygltf
36-
REQUIRED_VARS TinyGLTF_INCLUDE_DIR
37-
FAIL_MESSAGE "" # Suppress the error message to allow our fallback
105+
REQUIRED_VARS tinygltf_INCLUDE_DIR
38106
)
39107

40-
if(TinyGLTF_FOUND)
41-
set(TinyGLTF_INCLUDE_DIRS ${TinyGLTF_INCLUDE_DIR})
108+
if(tinygltf_FOUND)
109+
set(tinygltf_INCLUDE_DIRS ${tinygltf_INCLUDE_DIR})
42110

43111
if(NOT TARGET tinygltf::tinygltf)
44112
add_library(tinygltf::tinygltf INTERFACE IMPORTED)
45113
set_target_properties(tinygltf::tinygltf PROPERTIES
46-
INTERFACE_INCLUDE_DIRECTORIES "${TinyGLTF_INCLUDE_DIRS}"
114+
INTERFACE_INCLUDE_DIRECTORIES "${tinygltf_INCLUDE_DIRS}"
47115
INTERFACE_COMPILE_DEFINITIONS "TINYGLTF_IMPLEMENTATION;TINYGLTF_NO_EXTERNAL_IMAGE;TINYGLTF_NO_STB_IMAGE;TINYGLTF_NO_STB_IMAGE_WRITE"
48116
)
49117
if(TARGET nlohmann_json::nlohmann_json)
50118
target_link_libraries(tinygltf::tinygltf INTERFACE nlohmann_json::nlohmann_json)
51119
endif()
52120
endif()
53-
else()
54-
# If not found, create a custom tinygltf implementation
55-
message(STATUS "TinyGLTF not found, creating a custom implementation...")
56-
57-
# Create a directory for our custom tinygltf implementation
58-
set(TINYGLTF_DIR "${CMAKE_CURRENT_BINARY_DIR}/tinygltf")
59-
file(REMOVE_RECURSE "${TINYGLTF_DIR}")
60-
file(MAKE_DIRECTORY "${TINYGLTF_DIR}")
61-
62-
# Download the necessary files directly
63-
file(DOWNLOAD
64-
"https://raw.githubusercontent.com/syoyo/tinygltf/v2.8.18/tiny_gltf.h"
65-
"${TINYGLTF_DIR}/tiny_gltf.h"
66-
SHOW_PROGRESS
67-
)
68-
69-
file(DOWNLOAD
70-
"https://raw.githubusercontent.com/syoyo/tinygltf/v2.8.18/json.hpp"
71-
"${TINYGLTF_DIR}/json.hpp"
72-
SHOW_PROGRESS
73-
)
74-
75-
file(DOWNLOAD
76-
"https://raw.githubusercontent.com/syoyo/tinygltf/v2.8.18/stb_image.h"
77-
"${TINYGLTF_DIR}/stb_image.h"
78-
SHOW_PROGRESS
79-
)
80-
81-
file(DOWNLOAD
82-
"https://raw.githubusercontent.com/syoyo/tinygltf/v2.8.18/stb_image_write.h"
83-
"${TINYGLTF_DIR}/stb_image_write.h"
84-
SHOW_PROGRESS
85-
)
86-
87-
# Create a symbolic link to make nlohmann/json.hpp available
88-
file(MAKE_DIRECTORY "${TINYGLTF_DIR}/nlohmann")
89-
file(CREATE_LINK "${TINYGLTF_DIR}/json.hpp" "${TINYGLTF_DIR}/nlohmann/json.hpp" SYMBOLIC)
90-
91-
# Create a simple CMakeLists.txt file
92-
file(WRITE "${TINYGLTF_DIR}/CMakeLists.txt" "
93-
cmake_minimum_required(VERSION 3.10...3.29)
94-
project(tinygltf)
95-
96-
if(NOT TARGET tinygltf)
97-
add_library(tinygltf INTERFACE)
98-
target_include_directories(tinygltf INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
99-
target_compile_definitions(tinygltf INTERFACE
100-
TINYGLTF_IMPLEMENTATION
101-
TINYGLTF_NO_EXTERNAL_IMAGE
102-
TINYGLTF_NO_STB_IMAGE
103-
TINYGLTF_NO_STB_IMAGE_WRITE
104-
)
105-
endif()
106-
")
107-
108-
# Add the subdirectory
109-
add_subdirectory(${TINYGLTF_DIR} ${CMAKE_CURRENT_BINARY_DIR}/tinygltf-build)
110-
121+
elseif(TARGET tinygltf)
122+
# If find_package_handle_standard_args failed but we have a tinygltf target from FetchContent
111123
# Create an alias for the tinygltf target
112124
if(NOT TARGET tinygltf::tinygltf)
113125
add_library(tinygltf_wrapper INTERFACE)
@@ -124,5 +136,27 @@ endif()
124136
add_library(tinygltf::tinygltf ALIAS tinygltf_wrapper)
125137
endif()
126138

127-
set(TinyGLTF_FOUND TRUE)
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()
128160
endif()
161+
162+
mark_as_advanced(tinygltf_INCLUDE_DIR)

0 commit comments

Comments
 (0)