Skip to content

Commit ce442b6

Browse files
authored
Now set project version in cmake automatically using git tag (#153)
* new version.h.in template, which gets set by new function in CMakeLists * do a full checkout for all workflows
1 parent 4470bb7 commit ce442b6

File tree

10 files changed

+82
-1
lines changed

10 files changed

+82
-1
lines changed

.github/workflows/CI-Python.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ jobs:
1919

2020
steps:
2121
- uses: actions/checkout@v4
22+
with:
23+
fetch-depth: 0
2224

2325
- uses: actions/setup-python@v5
2426
with:

.github/workflows/CI-cpp.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ jobs:
4646
steps:
4747
# this Action should follow steps to set up Python build environment
4848
- uses: actions/checkout@v4
49+
with:
50+
fetch-depth: 0
4951

5052
- name: Install Protoc Windows
5153
if: ${{ startsWith(matrix.OS, 'windows') }}

.github/workflows/Lint-cpp-main.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ jobs:
1010
runs-on: ubuntu-22.04
1111
steps:
1212
- uses: actions/checkout@v5
13+
with:
14+
fetch-depth: 0
1315

1416
- name: Set Flags
1517
run: export USE_CUDA=0

.github/workflows/Lint-cpp.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ jobs:
1212
runs-on: ubuntu-22.04
1313
steps:
1414
- uses: actions/checkout@v5
15+
with:
16+
fetch-depth: 0
1517

1618
- name: Set Flags
1719
run: export USE_CUDA=0

.github/workflows/benchmarking-track-main.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
runs-on: ubuntu-22.04
1717
steps:
1818
- uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
1922
- uses: bencherdev/bencher@main
2023

2124
- name: Set Flags

.github/workflows/benchmarking.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ jobs:
2626
runs-on: ubuntu-22.04
2727
steps:
2828
- uses: actions/checkout@v4
29+
with:
30+
fetch-depth: 0
31+
2932
- uses: bencherdev/bencher@main
3033

3134
- name: Set Flags

.github/workflows/deployment-docs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525

2626
# Checks-out repository
2727
- uses: actions/checkout@v4
28+
with:
29+
fetch-depth: 0
2830

2931
- name: Install dependencies
3032
run: |

.github/workflows/pypi-distribution-reusable.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616

1717
steps:
1818
- uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
1921

2022
## runner will run out of space if we don't clear some up by removing some unused tools
2123
- name: Clear space

CMakeLists.txt

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,64 @@ if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
77
set(CMAKE_CUDA_ARCHITECTURES 75)
88
endif()
99

10+
# function to get the project version from the most recent git tag
11+
function(get_version_from_git)
12+
find_package(Git QUIET)
13+
if(NOT Git_FOUND)
14+
message(WARNING "Git not found")
15+
return()
16+
endif()
17+
18+
execute_process(
19+
COMMAND ${GIT_EXECUTABLE} describe --tags --always
20+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
21+
OUTPUT_VARIABLE GIT_TAG
22+
OUTPUT_STRIP_TRAILING_WHITESPACE
23+
RESULT_VARIABLE GIT_RESULT
24+
)
25+
26+
if(NOT GIT_RESULT EQUAL 0)
27+
message(WARNING "Failed to get git tag")
28+
return()
29+
endif()
30+
31+
execute_process(
32+
COMMAND ${GIT_EXECUTABLE} rev-parse --short=7 HEAD
33+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
34+
OUTPUT_VARIABLE GIT_COMMIT_SHORT_HASH
35+
OUTPUT_STRIP_TRAILING_WHITESPACE
36+
)
37+
38+
string(REGEX REPLACE "^v" "" CLEAN_TAG "${GIT_TAG}")
39+
if(CLEAN_TAG MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)(-.*)?$")
40+
41+
set(PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1})
42+
set(PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1} PARENT_SCOPE)
43+
set(PROJECT_VERSION_MINOR ${CMAKE_MATCH_2})
44+
set(PROJECT_VERSION_MINOR ${CMAKE_MATCH_2} PARENT_SCOPE)
45+
set(PROJECT_VERSION_PATCH ${CMAKE_MATCH_3})
46+
set(PROJECT_VERSION_PATCH ${CMAKE_MATCH_3} PARENT_SCOPE)
47+
48+
set(FULL_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}+${GIT_COMMIT_SHORT_HASH}")
49+
set(FULL_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}+${GIT_COMMIT_SHORT_HASH}" PARENT_SCOPE)
50+
set(PROJECT_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}")
51+
set(PROJECT_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}" PARENT_SCOPE)
52+
else()
53+
message(WARNING "Tag '${CLEAN_TAG}' does not match semver format")
54+
endif()
55+
endfunction()
56+
1057
# set the project name and version
11-
project(nuTens VERSION 0.4.0)
58+
get_version_from_git()
59+
message(INFO "nuTens PROJECT_VERSION: ${PROJECT_VERSION}")
60+
project(nuTens VERSION ${PROJECT_VERSION})
61+
62+
# configure the version.h file with all above version info
63+
configure_file(
64+
${PROJECT_SOURCE_DIR}/cmake/templates/version.h.in
65+
${PROJECT_BINARY_DIR}/include/version.h
66+
)
67+
1268

1369
# Changes default install path to be a subdirectory of the build dir.
1470
# Can set build dir at configure time with -DCMAKE_INSTALL_PREFIX=/install/path

cmake/templates/version.h.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#define PROJECT_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
4+
#define PROJECT_VERSION_MINOR @PROJECT_VERSION_MINOR@
5+
#define PROJECT_VERSION_PATCH @PROJECT_VERSION_PATCH@
6+
#define PROJECT_VERSION "@PROJECT_VERSION@"
7+
#define FULL_VERSION "@FULL_VERSION@"

0 commit comments

Comments
 (0)