yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformat cmake files (#5406)657287e77

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