Skip to content

Commit f11f84d

Browse files
authored
fix: Add function to handle local asset file paths
1 parent f418c16 commit f11f84d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

main/CMakeLists.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,62 @@ function(build_default_assets_bin)
710710
endfunction()
711711

712712

713+
# Function to get local assets file path (handles both URL and local file)
714+
function(get_assets_local_file assets_source assets_local_file_var)
715+
# Check if it's a URL (starts with http:// or https://)
716+
if(assets_source MATCHES "^https?://")
717+
# It's a URL, download it
718+
get_filename_component(ASSETS_FILENAME "${assets_source}" NAME)
719+
set(ASSETS_LOCAL_FILE "${CMAKE_BINARY_DIR}/${ASSETS_FILENAME}")
720+
set(ASSETS_TEMP_FILE "${CMAKE_BINARY_DIR}/${ASSETS_FILENAME}.tmp")
721+
722+
# Check if local file exists
723+
if(EXISTS ${ASSETS_LOCAL_FILE})
724+
message(STATUS "Assets file ${ASSETS_FILENAME} already exists, skipping download")
725+
else()
726+
message(STATUS "Downloading ${ASSETS_FILENAME}")
727+
728+
# Clean up any existing temp file
729+
if(EXISTS ${ASSETS_TEMP_FILE})
730+
file(REMOVE ${ASSETS_TEMP_FILE})
731+
endif()
732+
733+
# Download to temporary file first
734+
file(DOWNLOAD ${assets_source} ${ASSETS_TEMP_FILE}
735+
STATUS DOWNLOAD_STATUS)
736+
list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
737+
if(NOT STATUS_CODE EQUAL 0)
738+
# Clean up temp file on failure
739+
if(EXISTS ${ASSETS_TEMP_FILE})
740+
file(REMOVE ${ASSETS_TEMP_FILE})
741+
endif()
742+
message(FATAL_ERROR "Failed to download ${ASSETS_FILENAME} from ${assets_source}")
743+
endif()
744+
745+
# Move temp file to final location (atomic operation)
746+
file(RENAME ${ASSETS_TEMP_FILE} ${ASSETS_LOCAL_FILE})
747+
message(STATUS "Successfully downloaded ${ASSETS_FILENAME}")
748+
endif()
749+
else()
750+
# It's a local file path
751+
if(IS_ABSOLUTE "${assets_source}")
752+
set(ASSETS_LOCAL_FILE "${assets_source}")
753+
else()
754+
set(ASSETS_LOCAL_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${assets_source}")
755+
endif()
756+
757+
# Check if local file exists
758+
if(NOT EXISTS ${ASSETS_LOCAL_FILE})
759+
message(FATAL_ERROR "Assets file not found: ${ASSETS_LOCAL_FILE}")
760+
endif()
761+
762+
message(STATUS "Using assets file: ${ASSETS_LOCAL_FILE}")
763+
endif()
764+
765+
set(${assets_local_file_var} ${ASSETS_LOCAL_FILE} PARENT_SCOPE)
766+
endfunction()
767+
768+
713769
partition_table_get_partition_info(size "--partition-name assets" "size")
714770
partition_table_get_partition_info(offset "--partition-name assets" "offset")
715771
if ("${size}" AND "${offset}")

0 commit comments

Comments
 (0)