Skip to content

Commit ef56879

Browse files
Add SLANG_VERSION for source archives (#9020)
Fixes #9019 Slang already depended on git tags for version information, but a recent change to add library versioning created an additional dependency on git to provide the library version number when generating the library filenames. This information is simply not available in a source package. (The source packages we generate don't configure correctly with the preset profiles due to the submodules not being included in the packaging, but that doesn't mean we should make the problem _worse_.) To address this, a `cmake/slang_git_version` file has been added to store the version number, which is filled out automatically on `git archive` using git's `export-subst`. For source releases created using `git archive`, this will replace the contents of `slang_git_version` with the most recent version number. `GitVersion.cmake` is updated to prioritize the contents of the `slang_git_version` file (if and only if `export-subst` has been applied to the file) higher than the `git describe` results. This order was chosen in case the source archive was subsequently ingested into a different git repository. --------- Co-authored-by: slangbot <[email protected]>
1 parent db30f83 commit ef56879

File tree

6 files changed

+110
-28
lines changed

6 files changed

+110
-28
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33

44
# Set the line endings to LF for shell scripts
55
*.sh text eol=lf
6+
7+
# Enable git archive substitution for tagged version file
8+
cmake/slang_git_version export-subst

.github/workflows/release-linux-glibc-2-27.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
with:
1717
submodules: "recursive"
1818
fetch-depth: "0"
19+
fetch-tags: true
1920

2021
# build the binary in docker image
2122
- name: Run the build process with Docker
@@ -42,6 +43,8 @@ jobs:
4243
else
4344
version=$triggering_ref
4445
fi
46+
# Sanitize version for use in filenames (replace path separators)
47+
version=${version//\//-}
4548
base=$(pwd)/slang-${version}-linux-x86_64-glibc-2.27
4649
sudo mv "$(pwd)/build/dist-release/slang.zip" "${base}.zip"
4750
echo "SLANG_BINARY_ARCHIVE_ZIP=${base}.zip" >> "$GITHUB_OUTPUT"

.github/workflows/release.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ jobs:
9595
with:
9696
submodules: "recursive"
9797
fetch-depth: "0"
98+
fetch-tags: true
9899

99100
- name: Setup
100101
uses: ./.github/actions/common-setup
@@ -266,6 +267,8 @@ jobs:
266267
else
267268
version=$triggering_ref
268269
fi
270+
# Sanitize version for use in filenames (replace path separators)
271+
version=${version//\//-}
269272
base=slang-${version}-${{matrix.os}}-${{matrix.platform}}
270273
271274
# WASM packaging

cmake/GitVersion.cmake

Lines changed: 89 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,106 @@
11
find_package(Git)
22

3+
# Helper function to display consistent warning message about missing Slang version from git tags
4+
function(_warn_missing_git_version var version context_msg)
5+
message(
6+
WARNING
7+
"${context_msg}\n"
8+
"Git tags are the AUTHORITATIVE source for version information in this project.\n"
9+
"If you cloned from https://github.com/shader-slang/slang.git, fetch tags:\n"
10+
" git fetch --tags\n"
11+
"If you cloned from a mirror or fork, fetch tags from the official repository:\n"
12+
" git fetch https://github.com/shader-slang/slang.git 'refs/tags/*:refs/tags/*'\n"
13+
"Key implications of using an incorrect version number:\n"
14+
" (a) Version APIs and headers will return incorrect version information\n"
15+
" (b) Versioned filenames will be incorrect\n"
16+
"If you cannot fetch tags, you can use -D${var}=<version> as a fallback,\n"
17+
"but this is NOT the preferred method.\n"
18+
"Falling back to default version: ${version}"
19+
)
20+
endfunction()
21+
322
# Extract a version from the latest tag matching something like v1.2.3.4
423
function(get_git_version var_numeric var dir)
524
if(NOT DEFINED ${var})
625
set(version_numeric "0.0.0")
726
set(version "0.0.0-unknown")
8-
if(GIT_EXECUTABLE)
9-
set(command
10-
"${GIT_EXECUTABLE}"
11-
-C
12-
"${dir}"
13-
describe
14-
--tags
15-
--match
16-
v20[2-9][0-9].[0-9]*
17-
)
18-
execute_process(
19-
COMMAND ${command}
20-
RESULT_VARIABLE result
21-
OUTPUT_STRIP_TRAILING_WHITESPACE
22-
OUTPUT_VARIABLE version_out
27+
set(version_out "")
28+
29+
# First, try reading from slang_git_version file
30+
set(version_file "${dir}/cmake/slang_git_version")
31+
if(EXISTS "${version_file}")
32+
file(READ "${version_file}" version_file_content)
33+
string(STRIP "${version_file_content}" version_file_content)
34+
# Check if it's not a git archive format string and matches expected pattern
35+
if(
36+
NOT version_file_content MATCHES "^\\$Format:"
37+
AND version_file_content MATCHES "^v20[2-9][0-9]\\.[0-9]"
2338
)
24-
if(NOT result EQUAL 0)
39+
set(version_out "${version_file_content}")
2540
message(
26-
WARNING
27-
"Getting ${var} failed: ${command} returned ${result}\nIs this a Git repo with tags?\nConsider settings -D${var} to specify a version manually"
41+
STATUS
42+
"Using version from slang_git_version file: ${version_out}"
2843
)
29-
elseif("${version_out}" MATCHES "^v(([0-9]+(\\.[0-9]+)*).*)")
30-
set(version "${CMAKE_MATCH_1}")
31-
set(version_numeric "${CMAKE_MATCH_2}")
32-
else()
33-
message(
34-
WARNING
35-
"Couldn't parse version (like v2025.21 or v2025.21-foo) from ${version_out}, using ${version} for now"
44+
endif()
45+
endif()
46+
47+
# If slang_git_version file didn't provide a valid version, try git describe
48+
if(NOT version_out)
49+
if(GIT_EXECUTABLE AND EXISTS "${dir}/.git")
50+
set(command
51+
"${GIT_EXECUTABLE}"
52+
-C
53+
"${dir}"
54+
describe
55+
--tags
56+
--match
57+
v20[2-9][0-9].[0-9]*
58+
)
59+
execute_process(
60+
COMMAND ${command}
61+
RESULT_VARIABLE result
62+
OUTPUT_STRIP_TRAILING_WHITESPACE
63+
OUTPUT_VARIABLE git_describe_out
64+
)
65+
if(result EQUAL 0)
66+
set(version_out "${git_describe_out}")
67+
message(
68+
STATUS
69+
"Using version from git describe: ${version_out}"
70+
)
71+
else()
72+
_warn_missing_git_version(
73+
"${var}"
74+
"${version}"
75+
"Failed to get version information from git tags."
76+
)
77+
endif()
78+
elseif(NOT GIT_EXECUTABLE)
79+
_warn_missing_git_version(
80+
"${var}"
81+
"${version}"
82+
"Git executable not found - unable to get version information from git tags.\nPlease install git and fetch tags from the remote repository."
83+
)
84+
elseif(NOT EXISTS "${dir}/.git")
85+
_warn_missing_git_version(
86+
"${var}"
87+
"${version}"
88+
"Git repository not found - unable to get version information from git tags.\nIf you cloned the repository, please fetch tags from the remote."
3689
)
3790
endif()
38-
else()
91+
endif()
92+
93+
# Parse the version string (from either slang_git_version file or git describe)
94+
if(
95+
version_out
96+
AND "${version_out}" MATCHES "^v(([0-9]+(\\.[0-9]+)*).*)"
97+
)
98+
set(version "${CMAKE_MATCH_1}")
99+
set(version_numeric "${CMAKE_MATCH_2}")
100+
elseif(version_out)
39101
message(
40102
WARNING
41-
"Couldn't find git executable to get ${var}, please use -D${var}, using ${version} for now"
103+
"Couldn't parse version (like v2025.21 or v2025.21-foo) from ${version_out}, using ${version} for now"
42104
)
43105
endif()
44106
endif()

cmake/slang_git_version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$Format:%(describe:tags=true,match=v20[2-9][0-9].[0-9]*)$

docs/building.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ fetch the submodules also.
3434
git clone https://github.com/shader-slang/slang --recursive
3535
```
3636

37+
You will need the git tags from this repository, otherwise versioning
38+
information (including the Slang modules directory name and the library
39+
filenames on macOS and Linux) will be incorrect. The above command should fetch
40+
them for you, but if you're fetching from a fork you may need to explicitly
41+
fetch the latest tags from the shader-slang repository with:
42+
43+
```bash
44+
git fetch https://github.com/shader-slang/slang.git 'refs/tags/*:refs/tags/*'
45+
```
46+
3747
## Configure and build
3848

3949
> This section assumes cmake 3.25 or greater, if you're on a lower version
@@ -160,7 +170,7 @@ against the fully versioned library filenames (e.g.,
160170
`libslang-compiler.so.0.2025.21` instead of `libslang-compiler.so`).
161171

162172
Slang libraries for **Windows** do not have an explicit version in the
163-
filename, but the same guidance about stability of the ABI applies.
173+
library filename, but the the same guidance about stability of the ABI applies.
164174

165175
Downstream users of Slang distributing their products as binaries should
166176
therefor **on all platforms, including Windows** redistribute the Slang

0 commit comments

Comments
 (0)