yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
657287e77
master
1function ( check_assets_for_file json_content filename found_var ) 2string ( JSON asset_count LENGTH " ${ json_content } " "assets" ) 3set ( found "FALSE" ) 4 5# Never change, CMake... 6math ( EXPR max_asset_index " ${ asset_count } - 1" ) 7foreach ( i RANGE0 ${ max_asset_index }) 8string ( JSON asset_name GET " ${ json_content } " "assets" ${ i } "name" ) 9if ( " ${ asset_name } " STREQUAL " ${ filename } " ) 10set ( found "TRUE" ) 11break () 12endif () 13endforeach () 14set ( $ { found_var } " ${ found } " PARENT_SCOPE ) 15endfunction () 16 17function ( 18get_latest 19owner 20repo 21os 22arch 23github_token 24out_var 25) 26set ( json_output_file 27" ${ CMAKE_CURRENT_BINARY_DIR } / ${ owner } _ ${ repo } _release_info.json" 28) 29set ( latest_release_url 30"https://api.github.com/repos/ ${ owner } / ${ repo } /releases/latest" 31) 32 33set ( download_args 34" ${ latest_release_url } " 35" ${ json_output_file } " 36STATUS 37download_statuses 38) 39 40if ( github_token) 41list ( 42APPEND 43download_args 44HTTPHEADER 45"Authorization: token ${ github_token } " 46) 47endif () 48 49file ( DOWNLOAD ${ download_args }) 50list ( GET download_statuses 0 status_code ) 51if ( NOT status_codeEQUAL 0 ) 52message ( 53WARNING 54"Failed to download latest release info from ${ latest_release_url } " 55) 56return () 57endif () 58 59# Get the tag from this release json file 60file ( READ " ${ json_output_file } " latest_json_content ) 61string ( JSON latest_release_tag GET " ${ latest_json_content } " "tag_name" ) 62string ( REGEX REPLACE "^v" "" latest_version " ${ latest_release_tag } " ) 63 64# Check if the expected ZIP file is in the latest release 65set ( desired_zip " ${ repo } - ${ latest_version } - ${ os } - ${ arch } .zip" ) 66message ( 67VERBOSE 68"searching for the prebuilt slang-llvm library in ${ latest_release_url } " 69) 70check_assets_for_file ( 71" ${ latest_json_content } " 72" ${ desired_zip } " 73file_found_latest 74) 75 76if ( file_found_latest) 77# If we got it, we found a good version 78set ( $ { out_var } " ${ latest_version } " PARENT_SCOPE ) 79else () 80message ( 81WARNING 82"No release binary for ${ os } - ${ arch } exists for the latest version: ${ latest_version } " 83) 84endif () 85endfunction () 86 87function ( 88check_release_and_get_latest 89owner 90repo 91version 92os 93arch 94github_token 95out_var 96) 97# Construct the URL for the specified version's release API endpoint 98set ( version_url 99"https://api.github.com/repos/ ${ owner } / ${ repo } /releases/tags/v ${ version } " 100) 101 102set ( json_output_file 103" ${ CMAKE_CURRENT_BINARY_DIR } / ${ owner } _ ${ repo } _release_info.json" 104) 105 106# Prepare download arguments 107set ( download_args 108" ${ version_url } " 109" ${ json_output_file } " 110STATUS 111download_statuses 112) 113 114if ( github_token) 115# Add authorization header if token is provided 116list ( 117APPEND 118download_args 119HTTPHEADER 120"Authorization: token ${ github_token } " 121) 122endif () 123 124# Perform the download 125file ( DOWNLOAD ${ download_args }) 126 127# Check if the downloading was successful 128list ( GET download_statuses 0 status_code ) 129if ( status_codeEQUAL 0 ) 130file ( READ " ${ json_output_file } " json_content ) 131 132# Check if the specified version contains the expected ZIP file 133set ( desired_zip " ${ repo } - ${ version } - ${ os } - ${ arch } .zip" ) 134message ( 135VERBOSE 136"searching for the prebuilt slang-llvm library in ${ version_url } " 137) 138check_assets_for_file ( " ${ json_content } " " ${ desired_zip } " file_found ) 139 140if ( file_found) 141set ( $ { out_var } " ${ version } " PARENT_SCOPE ) 142return () 143endif () 144message ( 145WARNING 146"Failed to find ${ desired_zip } in release assets for ${ version } from ${ version_url } \nFalling back to latest version if it differs" 147) 148else () 149set ( w 150"Failed to download release info for version ${ version } from ${ version_url } \nFalling back to latest version if it differs" 151) 152if ( status_codeEQUAL 22) 153set ( w 154" ${ w } \nIf you think this is failing because of GitHub API rate limiting, Github allows a higher limit if you use a token. Try the cmake option -DSLANG_GITHUB_TOKEN=your_token_here" 155) 156endif () 157 158message ( WARNING ${ w }) 159endif () 160 161# If not found, get the latest release tag 162get_latest ( $ { owner } ${ repo } ${ os } ${ arch } " ${ github_token } " latest_version ) 163if ( NOT DEFINED latest_version) 164return () 165endif () 166set ( $ { out_var } " ${ latest_version } " PARENT_SCOPE ) 167endfunction () 168 169function ( get_best_slang_binary_release_url github_token out_var ) 170if ( CMAKE_SYSTEM_PROCESSORMATCHES "x86_64|amd64|AMD64" ) 171set ( arch "x86_64" ) 172elseif ( CMAKE_SYSTEM_PROCESSORMATCHES "aarch64|ARM64|arm64" ) 173set ( arch "aarch64" ) 174else () 175message ( 176WARNING 177"Unsupported architecture for slang binary releases: ${ CMAKE_SYSTEM_PROCESSOR } " 178) 179return () 180endif () 181 182if ( CMAKE_SYSTEM_NAMESTREQUAL "Windows" ) 183set ( os "windows" ) 184elseif ( CMAKE_SYSTEM_NAMESTREQUAL "Darwin" ) 185set ( os "macos" ) 186elseif ( CMAKE_SYSTEM_NAMESTREQUAL "Linux" ) 187set ( os "linux" ) 188else () 189message ( 190WARNING 191"Unsupported operating system for slang binary releases: ${ CMAKE_SYSTEM_NAME } " 192) 193return () 194endif () 195 196set ( owner "shader-slang" ) 197set ( repo "slang" ) 198 199# This is the first version which distributed libslang-llvm.so, if it's 200# older than that then someone didn't fetch tags, emit a message and 201# fallback to the latest release 202if (${ SLANG_VERSION_NUMERIC } VERSION_LESS "2024.1.27" ) 203if (${ SLANG_VERSION_NUMERIC } VERSION_EQUAL "0.0.0" ) 204message ( 205VERBOSE 206"The detected version of slang is ${ SLANG_VERSION_NUMERIC } , fetching libslang-llvm from the latest release" 207) 208else () 209message ( 210WARNING 211"The detected version of slang ${ SLANG_VERSION_NUMERIC } is very old (probably you haven't fetched tags recently?), libslang-llvm will be fetched from the latest release rather than the one matching ${ SLANG_VERSION_NUMERIC } " 212) 213endif () 214get_latest ( 215${ owner } 216${ repo } 217${ os } 218${ arch } 219" ${ github_token } " 220release_version 221) 222else () 223check_release_and_get_latest ( 224${ owner } 225${ repo } 226${ SLANG_VERSION_NUMERIC } 227${ os } 228${ arch } 229" ${ github_token } " 230release_version 231) 232endif () 233if ( DEFINED release_version) 234message ( 235VERBOSE 236"Found a version of libslang-llvm.so in ${ release_version } " 237) 238set ( $ { out_var } 239"https://github.com/ ${ owner } / ${ repo } /releases/download/v ${ release_version } /slang- ${ release_version } - ${ os } - ${ arch } .zip" 240PARENT_SCOPE 241) 242endif () 243endfunction ()