diff options
| author | Ellie Hermaszewska <ellieh@nvidia.com> | 2024-10-19 07:05:53 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-18 16:05:53 -0700 |
| commit | b2386f36ee3ac253457e9e532b0373a5ce1ee687 (patch) | |
| tree | d18b9fc3a7749ee7a4f2298cecc37839a362cb6a /cmake/FetchedSharedLibrary.cmake | |
| parent | f2b2db57cb7b1e8da8abc74bcf1068b52d756d00 (diff) | |
Make slang-llvm fetching and failure more robust (#5346)
* Make slang-llvm fetching and failure more robust
Improve error reporting when things go wrong.
Fall back by default to a non-llvm build
Closes https://github.com/shader-slang/slang/issues/5247
Tested sensible behavior with:
- `FETCH_BINARY_IF_POSSIBLE`, valid tag
- No errors or warnings, successful build with llvm
- `FETCH_BINARY_IF_POSSIBLE`, no valid tag
- Warning message, successful llvm build with slang-llvm from latest release
- `FETCH_BINARY_IF_POSSIBLE`, no valid tag, bad `SLANG_SLANG_LLVM_BINARY_URL` specified
- Warning message, successful no-llvm build
- `FETCH_BINARY_IF_POSSIBLE`, no valid tag, unable to fetch release information
- Warning message, successful no-llvm build
- `FETCH_BINARY`, valid tag
- No errors or warnings, successful build with llvm
- `FETCH_BINARY`, no valid tag
- Warning message, successful llvm build with slang-llvm from latest release
- `FETCH_BINARY`, no valid tag, bad `SLANG_SLANG_LLVM_BINARY_URL` specified
- Error, explaining that we couldn't fetch it
- `FETCH_BINARY`, no valid tag, unable to fetch release info
- Error, explaining that we couldn't fetch it
* Allow downloading from a local file
---------
Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'cmake/FetchedSharedLibrary.cmake')
| -rw-r--r-- | cmake/FetchedSharedLibrary.cmake | 68 |
1 files changed, 61 insertions, 7 deletions
diff --git a/cmake/FetchedSharedLibrary.cmake b/cmake/FetchedSharedLibrary.cmake index d63f03d29..3c7f24be5 100644 --- a/cmake/FetchedSharedLibrary.cmake +++ b/cmake/FetchedSharedLibrary.cmake @@ -1,3 +1,36 @@ +# Helper function to download and extract an archive +function(download_and_extract archive_name url) + get_filename_component(extension ${url} EXT) + set(archive_path "${CMAKE_CURRENT_BINARY_DIR}/${archive_name}${extension}") + set(extract_dir "${CMAKE_CURRENT_BINARY_DIR}/${archive_name}") + + if(EXISTS ${url}) + message(STATUS "Using local file for ${archive_name}: ${url}") + set(archive_path ${url}) + else() + message(STATUS "Fetching ${archive_name} from ${url}") + file(DOWNLOAD ${url} ${archive_path} + # SHOW_PROGRESS + STATUS status + ) + + list(GET status 0 status_code) + list(GET status 1 status_string) + if(NOT status_code EQUAL 0) + message(WARNING "Failed to download ${archive_name} from ${url}: ${status_string}") + return() + endif() + endif() + + file(ARCHIVE_EXTRACT + INPUT ${archive_path} + DESTINATION ${extract_dir} + ) + + set(${archive_name}_SOURCE_DIR ${extract_dir} PARENT_SCOPE) + message(STATUS "${archive_name} downloaded and extracted to ${extract_dir}") +endfunction() + # Add rules to copy & install shared library of name 'library_name' in the 'module_subdir' directory. # If 'url' is a directory, the shared library (with platform-specific shared library prefixes and suffixes) will be # taken from the directory, and whatever is found there will be used to produce the install rule. @@ -6,6 +39,13 @@ # Otherwise, the 'url' is interpreted as an URL, and the content of the URL will be fetched, extracted and searched # for the shared library to produce the install rule. function(copy_fetched_shared_library library_name url) + cmake_parse_arguments(ARG "IGNORE_FAILURE" "" "" ${ARGN}) + if(ARG_IGNORE_FAILURE) + set(error_type STATUS) + else() + set(error_type SEND_ERROR) + endif() + set(shared_library_filename "${CMAKE_SHARED_LIBRARY_PREFIX}${library_name}${CMAKE_SHARED_LIBRARY_SUFFIX}" ) @@ -18,12 +58,12 @@ function(copy_fetched_shared_library library_name url) list(LENGTH source_object nmatches) if(nmatches EQUAL 0) message( - SEND_ERROR + ${error_type} "Unable to find ${shared_library_filename} in ${url}" ) elseif(nmatches GREATER 1) message( - SEND_ERROR + ${error_type} "Found multiple files named ${shared_library_filename} in ${url}" ) endif() @@ -42,9 +82,21 @@ function(copy_fetched_shared_library library_name url) set(source_object "${url}") else() # Otherwise, download and extract from whatever URL we have - fetchcontent_declare(${library_name} URL "${url}") - fetchcontent_populate(${library_name}) - from_glob(${${library_name}_SOURCE_DIR}) + download_and_extract("${library_name}" "${url}") + if(DEFINED ${library_name}_SOURCE_DIR) + from_glob(${${library_name}_SOURCE_DIR}) + elseif(ARG_IGNORE_FAILURE) + return() + else() + message(SEND_ERROR "Unable to download and extract ${library_name} from ${url}") + return() + endif() + endif() + + # We didn't find it, just return and don't create a target and operation + # which will fail + if(NOT EXISTS ${source_object} AND ARG_IGNORE_FAILURE) + return() endif() set(dest_object @@ -72,12 +124,14 @@ function(copy_fetched_shared_library library_name url) endfunction() function(install_fetched_shared_library library_name url) - copy_fetched_shared_library(${library_name} ${url}) + copy_fetched_shared_library(${library_name} ${url} ${ARGN}) set(shared_library_filename "${CMAKE_SHARED_LIBRARY_PREFIX}${library_name}${CMAKE_SHARED_LIBRARY_SUFFIX}" ) set(dest_object ${CMAKE_BINARY_DIR}/$<CONFIG>/${module_subdir}/${shared_library_filename} ) - install(PROGRAMS ${dest_object} DESTINATION ${module_subdir}) + if(TARGET ${library_name}) + install(PROGRAMS ${dest_object} DESTINATION ${module_subdir}) + endif() endfunction() |
