|
1 | 1 | find_package(Git) |
2 | 2 |
|
| 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 | + |
3 | 22 | # Extract a version from the latest tag matching something like v1.2.3.4 |
4 | 23 | function(get_git_version var_numeric var dir) |
5 | 24 | if(NOT DEFINED ${var}) |
6 | 25 | set(version_numeric "0.0.0") |
7 | 26 | 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]" |
23 | 38 | ) |
24 | | - if(NOT result EQUAL 0) |
| 39 | + set(version_out "${version_file_content}") |
25 | 40 | 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}" |
28 | 43 | ) |
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." |
36 | 89 | ) |
37 | 90 | 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) |
39 | 101 | message( |
40 | 102 | 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" |
42 | 104 | ) |
43 | 105 | endif() |
44 | 106 | endif() |
|
0 commit comments