yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
edb728938
master
1if ( CMAKE_SYSTEM_NAMESTREQUAL "Windows" ) 2set ( dep_shared_library_prefix "" ) 3else () 4set ( dep_shared_library_prefix "lib" ) 5endif () 6 7# Helper function to download and extract an archive 8function ( download_and_extract archive_name url ) 9cmake_parse_arguments ( ARG "" "SLANG_GITHUB_TOKEN" "" ${ ARGN }) 10cmake_path ( GET url FILENAME filename_with_ext ) 11cmake_path ( GET url STEM LAST_ONLY file_stem ) 12set ( archive_path " ${ CMAKE_CURRENT_BINARY_DIR } / ${ filename_with_ext } " ) 13set ( extract_dir " ${ CMAKE_CURRENT_BINARY_DIR } / ${ file_stem } " ) 14 15# Check if already extracted 16file ( GLOB EXTRACT_DIR_CONTENTS " ${ extract_dir } /*" ) 17if ( EXTRACT_DIR_CONTENTS) 18message ( STATUS "Using existing extracted files in ${ extract_dir } " ) 19else () 20# Check if archive already exists 21if ( EXISTS ${ url }) 22message ( STATUS "Using local file for ${ archive_name } : ${ url } " ) 23set ( archive_path ${ url }) 24elseif ( EXISTS ${ archive_path }) 25message ( 26STATUS 27"Using existing archive for ${ archive_name } : ${ archive_path } " 28) 29else () 30set ( download_args ${ url } ${ archive_path } STATUS status ) 31if ( ARG_SLANG_GITHUB_TOKEN) 32list ( 33APPEND 34download_args 35HTTPHEADER 36"Authorization: token ${ ARG_SLANG_GITHUB_TOKEN } " 37) 38endif () 39 40file ( DOWNLOAD ${ download_args }) 41 42list ( GET status 0 status_code ) 43list ( GET status 1 status_string ) 44if ( NOT status_codeEQUAL 0 ) 45message ( 46WARNING 47"Failed to download ${ archive_name } from ${ url } : ${ status_string } with status code ${ status_code } " 48) 49message ( 50WARNING 51"If API rate limit is exceeded, Github allows a higher limit when you use token. Try a cmake option -DSLANG_GITHUB_TOKEN=your_token_here" 52) 53return () 54endif () 55endif () 56 57file ( ARCHIVE_EXTRACT INPUT ${ archive_path } DESTINATION ${ extract_dir }) 58message ( STATUS " ${ archive_name } extracted to ${ extract_dir } " ) 59endif () 60 61set ( $ { archive_name } _SOURCE_DIR ${ extract_dir } PARENT_SCOPE ) 62endfunction () 63 64# Add rules to copy & install shared library of name 'library_name' in the 'module_subdir' directory. 65# If 'url' is a directory, the shared library (with platform-specific shared library prefixes and suffixes) will be 66# taken from the directory, and whatever is found there will be used to produce the install rule. 67# If the 'url' is a path to a file with the platform-specific shared library prefix and suffix, then that file 68# will be used to produce the install rule. 69# Otherwise, the 'url' is interpreted as an URL, and the content of the URL will be fetched, extracted and searched 70# for the shared library to produce the install rule. 71function ( copy_fetched_shared_library library_name url ) 72cmake_parse_arguments ( ARG "IGNORE_FAILURE" "SLANG_GITHUB_TOKEN" "" ${ ARGN }) 73 74# Don't ignore failure if a Github token is provided; 75# as we assume that the token will fix the rate limit issue. 76if ( ARG_IGNORE_FAILUREAND NOT ARG_SLANG_GITHUB_TOKEN) 77set ( error_type STATUS ) 78else () 79set ( error_type SEND_ERROR ) 80endif () 81 82set ( shared_library_filename 83" ${ dep_shared_library_prefix }${ library_name }${ CMAKE_SHARED_LIBRARY_SUFFIX } " 84) 85macro ( from_glob dir ) 86# A little helper function 87file ( GLOB_RECURSE source_object " ${ dir } / ${ shared_library_filename } " ) 88list ( LENGTH source_object nmatches ) 89if ( nmatchesEQUAL 0 ) 90message ( 91${ error_type } 92"Unable to find ${ shared_library_filename } in ${ url } " 93) 94elseif ( nmatchesGREATER 1 ) 95message ( 96${ error_type } 97"Found multiple files named ${ shared_library_filename } in ${ url } " 98) 99endif () 100endmacro () 101 102if ( IS_DIRECTORY " ${ url } " ) 103# Just glob directly from a local directory 104from_glob ( " ${ url } " ) 105elseif ( 106 url 107MATCHES 108" ${ dep_shared_library_prefix } .+ ${ CMAKE_SHARED_LIBRARY_SUFFIX }$ " 109AND EXISTS " ${ url } " 110) 111# Otherwise, if it's a direct path to a shared object, use that 112set ( source_object " ${ url } " ) 113else () 114# Otherwise, download and extract from whatever URL we have 115download_and_extract ( 116" ${ library_name } " 117" ${ url } " 118SLANG_GITHUB_TOKEN ${ ARG_SLANG_GITHUB_TOKEN } 119) 120if ( DEFINED ${ library_name } _SOURCE_DIR) 121from_glob ( $ { $ { library_name } _SOURCE_DIR }) 122elseif ( ARG_IGNORE_FAILURE) 123return () 124else () 125message ( 126SEND_ERROR 127"Unable to download and extract ${ library_name } from ${ url } " 128) 129return () 130endif () 131endif () 132 133# We didn't find it, just return and don't create a target and operation 134# which will fail 135if (( NOT EXISTS " ${ source_object } " ) AND ARG_IGNORE_FAILURE) 136return () 137endif () 138 139set ( dest_object 140${ CMAKE_BINARY_DIR } / $ <CONFIG>/ ${ module_subdir } / ${ shared_library_filename } 141) 142add_custom_command ( 143OUTPUT ${ dest_object } 144COMMAND 145${ CMAKE_COMMAND } -E copy_if_different ${ source_object } 146${ dest_object } 147DEPENDS ${ source_object } 148VERBATIM 149) 150# Give this copying action a name 151add_custom_target ( copy- ${ library_name } DEPENDS ${ dest_object }) 152set_target_properties ( copy- ${ library_name } PROPERTIES FOLDER generated ) 153 154# Put this into a library target 155add_library ( $ { library_name } MODULE IMPORTED GLOBAL ) 156add_dependencies ( $ { library_name } copy- ${ library_name }) 157set_property ( 158TARGET ${ library_name } 159PROPERTY IMPORTED_LOCATION ${ dest_object } 160) 161endfunction () 162 163function ( install_fetched_shared_library library_name url ) 164copy_fetched_shared_library ( $ { library_name } ${ url } ${ ARGN }) 165set ( shared_library_filename 166" ${ dep_shared_library_prefix }${ library_name }${ CMAKE_SHARED_LIBRARY_SUFFIX } " 167) 168set ( dest_object 169${ CMAKE_BINARY_DIR } / $ <CONFIG>/ ${ module_subdir } / ${ shared_library_filename } 170) 171if ( TARGET ${ library_name }) 172install ( PROGRAMS ${ dest_object } DESTINATION ${ module_subdir }) 173endif () 174endfunction ()