Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.

Commit 589a906

Browse files
author
Philip de Nier
committed
Use git describe relative to release tag
1 parent 8f3d17a commit 589a906

File tree

6 files changed

+186
-26
lines changed

6 files changed

+186
-26
lines changed

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.15.0)
66
endif()
77

88
project(bmx
9-
VERSION 0.1.3
9+
VERSION 0.1
1010
DESCRIPTION "A C++ library and set of utilities to read and write the SMPTE ST 377-1 MXF file format"
1111
HOMEPAGE_URL https://github.com/bbc/bmx
1212
LANGUAGES C CXX
1313
)
14-
set(PROJECT_VERSION_RELEASE 2)
1514

1615
include("${CMAKE_CURRENT_LIST_DIR}/cmake/options.cmake")
1716

cmake/git_version.cmake

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
if(TARGET cmake_git_version_tracking)
1+
if(TARGET bmx_cmake_git_version_tracking)
22
return()
33
endif()
44

55

6+
# Set the git release tag pattern to describe relative to
7+
set(GIT_DESCRIBE_TAG_PATTERN "v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
8+
set(GIT_WORKING_DIR ${PROJECT_SOURCE_DIR})
9+
610
# Set to ignore git_version errors if there is no .git directory
711
set(GIT_FAIL_IF_NONZERO_EXIT FALSE)
812

@@ -11,19 +15,20 @@ include(FetchContent)
1115
set(git_version_deps_source "${PROJECT_SOURCE_DIR}/deps/cmake-git-version-tracking")
1216

1317
if(EXISTS ${git_version_deps_source})
14-
FetchContent_Declare(cmake_git_version_tracking
18+
FetchContent_Declare(bmx_cmake_git_version_tracking
1519
SOURCE_DIR ${git_version_deps_source}
1620
)
1721
message("-- Build using git version source: ${git_version_deps_source}")
1822
else()
19-
FetchContent_Declare(cmake_git_version_tracking
23+
FetchContent_Declare(bmx_cmake_git_version_tracking
2024
GIT_REPOSITORY "https://github.com/andrew-hardin/cmake-git-version-tracking.git"
2125
GIT_TAG "904dbda1336ba4b9a1415a68d5f203f576b696bb"
26+
PATCH_COMMAND git apply --ignore-whitespace "${CMAKE_CURRENT_LIST_DIR}/git_version_904dbda.patch"
2227
)
2328
endif()
2429

25-
FetchContent_GetProperties(cmake_git_version_tracking)
26-
if(NOT cmake_git_version_tracking_POPULATED)
27-
FetchContent_Populate(cmake_git_version_tracking)
28-
add_subdirectory(${cmake_git_version_tracking_SOURCE_DIR} ${cmake_git_version_tracking_BINARY_DIR})
30+
FetchContent_GetProperties(bmx_cmake_git_version_tracking)
31+
if(NOT bmx_cmake_git_version_tracking_POPULATED)
32+
FetchContent_Populate(bmx_cmake_git_version_tracking)
33+
add_subdirectory(${bmx_cmake_git_version_tracking_SOURCE_DIR} ${bmx_cmake_git_version_tracking_BINARY_DIR})
2934
endif()

cmake/git_version_904dbda.patch

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
From 57f54da0274d3460144198b48e70efd8a846bf20 Mon Sep 17 00:00:00 2001
2+
From: Philip de Nier <[email protected]>
3+
Date: Mon, 16 Jan 2023 15:39:23 +0000
4+
Subject: [PATCH] Add git describe relative to matching tag
5+
6+
---
7+
CMakeLists.txt | 4 ++--
8+
git.c.in | 3 +++
9+
git.h | 7 +++++++
10+
git_watcher.cmake | 15 ++++++++++++++-
11+
4 files changed, 26 insertions(+), 3 deletions(-)
12+
13+
diff --git a/CMakeLists.txt b/CMakeLists.txt
14+
index 55e6d07..7b277c3 100644
15+
--- a/CMakeLists.txt
16+
+++ b/CMakeLists.txt
17+
@@ -1,5 +1,5 @@
18+
cmake_minimum_required(VERSION 3.2)
19+
-project(cmake_git_version_tracking
20+
+project(bmx_cmake_git_version_tracking
21+
LANGUAGES C)
22+
23+
# Define the two required variables before including
24+
@@ -15,7 +15,7 @@ include(git_watcher.cmake)
25+
# 3rdparty library.
26+
add_library(${PROJECT_NAME} STATIC ${POST_CONFIGURE_FILE})
27+
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
28+
-add_dependencies(${PROJECT_NAME} check_git)
29+
+add_dependencies(${PROJECT_NAME} bmx_check_git)
30+
31+
# The C99 standard is only required because we're using <stdbool.h>.
32+
# This could be removed if it's a problem for users, but would require the
33+
diff --git a/git.c.in b/git.c.in
34+
index a26d27c..8670076 100644
35+
--- a/git.c.in
36+
+++ b/git.c.in
37+
@@ -30,3 +30,6 @@ const char* git_Describe() {
38+
const char* git_Branch() {
39+
return "@GIT_BRANCH@";
40+
}
41+
+const char* git_DescribeTag() {
42+
+ return "@GIT_DESCRIBE_TAG@";
43+
+}
44+
diff --git a/git.h b/git.h
45+
index fee780e..e35a79e 100644
46+
--- a/git.h
47+
+++ b/git.h
48+
@@ -52,6 +52,9 @@ const char* git_Describe();
49+
/// The symbolic reference tied to HEAD.
50+
const char* git_Branch();
51+
52+
+/// The commit describe relative to matching tag.
53+
+const char* git_DescribeTag();
54+
+
55+
GIT_VERSION_TRACKING_EXTERN_C_END
56+
#undef GIT_VERSION_TRACKING_EXTERN_C_BEGIN
57+
#undef GIT_VERSION_TRACKING_EXTERN_C_END
58+
@@ -143,6 +146,10 @@ inline const StringOrView Branch() {
59+
static const StringOrView kValue = internal::InitString(git_Branch());
60+
return kValue;
61+
}
62+
+inline const StringOrView DescribeTag() {
63+
+ static const StringOrView kValue = internal::InitString(git_DescribeTag());
64+
+ return kValue;
65+
+}
66+
67+
} // namespace git
68+
69+
diff --git a/git_watcher.cmake b/git_watcher.cmake
70+
index 32313a3..f91f23b 100644
71+
--- a/git_watcher.cmake
72+
+++ b/git_watcher.cmake
73+
@@ -36,6 +36,9 @@
74+
# -- Ignore the presence of untracked files when detecting if the
75+
# working tree is dirty. This is set to FALSE by default.
76+
#
77+
+# GIT_DESCRIBE_TAG_PATTERN (OPTIONAL)
78+
+# -- Git tag pattern to match against to produce the GIT_DESCRIBE_TAG variable.
79+
+#
80+
# DESIGN
81+
# - This script was designed similar to a Python application
82+
# with a Main() function. I wanted to keep it compact to
83+
@@ -105,6 +108,7 @@ set(_state_variable_names
84+
GIT_COMMIT_BODY
85+
GIT_DESCRIBE
86+
GIT_BRANCH
87+
+ GIT_DESCRIBE_TAG
88+
# >>>
89+
# 1. Add the name of the additional git variable you're interested in monitoring
90+
# to this list.
91+
@@ -242,6 +246,14 @@ function(GetGitState _working_dir)
92+
set(ENV{GIT_BRANCH} "${output}")
93+
endif()
94+
95+
+ # Get output of git describe relative to the matching tag
96+
+ RunGitCommand(describe --match ${GIT_DESCRIBE_TAG_PATTERN} ${object})
97+
+ if(NOT exit_code EQUAL 0)
98+
+ set(ENV{GIT_DESCRIBE_TAG} "unknown")
99+
+ else()
100+
+ set(ENV{GIT_DESCRIBE_TAG} "${output}")
101+
+ endif()
102+
+
103+
# >>>
104+
# 2. Additional git properties can be added here via the
105+
# "execute_process()" command. Be sure to set them in
106+
@@ -324,7 +336,7 @@ endfunction()
107+
# check the state of git before every build. If the state has
108+
# changed, then a file is configured.
109+
function(SetupGitMonitoring)
110+
- add_custom_target(check_git
111+
+ add_custom_target(bmx_check_git
112+
ALL
113+
DEPENDS ${PRE_CONFIGURE_FILE}
114+
BYPRODUCTS
115+
@@ -341,6 +353,7 @@ function(SetupGitMonitoring)
116+
-DPOST_CONFIGURE_FILE=${POST_CONFIGURE_FILE}
117+
-DGIT_FAIL_IF_NONZERO_EXIT=${GIT_FAIL_IF_NONZERO_EXIT}
118+
-DGIT_IGNORE_UNTRACKED=${GIT_IGNORE_UNTRACKED}
119+
+ -DGIT_DESCRIBE_TAG_PATTERN=${GIT_DESCRIBE_TAG_PATTERN}
120+
-P "${CMAKE_CURRENT_LIST_FILE}")
121+
endfunction()
122+
123+
--
124+
2.25.1
125+

config.h.in

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
/* Library versions */
55
#define BMX_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
66
#define BMX_VERSION_MINOR @PROJECT_VERSION_MINOR@
7-
#define BMX_VERSION_MICRO @PROJECT_VERSION_PATCH@
8-
#define BMX_MXF_VERSION_RELEASE @PROJECT_VERSION_RELEASE@
9-
10-
#define BMX_VERSION (BMX_VERSION_MAJOR << 16 | BMX_VERSION_MINOR << 8 | BMX_VERSION_MICRO)
117

128
/* Define printf size_t format specifier */
139
#if defined(_WIN64)

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ endif()
7878
# Add the git version tracking library code
7979
include("${PROJECT_SOURCE_DIR}/cmake/git_version.cmake")
8080
target_link_libraries(bmx PRIVATE
81-
cmake_git_version_tracking
81+
bmx_cmake_git_version_tracking
8282
)
8383

8484
set_target_properties(bmx PROPERTIES

src/common/Version.cpp

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <cstdio>
3737
#include <cstring>
3838
#include <ctime>
39+
#include <limits.h>
3940

4041
#include "git.h"
4142
#include "fallback_git_version.h"
@@ -56,12 +57,6 @@ bool BMX_REGRESSION_TEST = false;
5657
static const char* BMX_COMPANY_NAME = "BBC";
5758
static const bmx::UUID BMX_PRODUCT_UID =
5859
{0xb8, 0x60, 0x4d, 0x31, 0x2e, 0x15, 0x47, 0x99, 0xa3, 0xc6, 0x04, 0x7e, 0xd0, 0xe6, 0xf9, 0xa1};
59-
static const mxfProductVersion BMX_MXF_PRODUCT_VERSION = {BMX_VERSION_MAJOR,
60-
BMX_VERSION_MINOR,
61-
BMX_VERSION_MICRO,
62-
0,
63-
BMX_MXF_VERSION_RELEASE};
64-
6560
static const bmx::UUID REGTEST_PRODUCT_UID =
6661
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
6762
static const mxfProductVersion REGTEST_MXF_PRODUCT_VERSION = {1, 0, 0, 0, 0};
@@ -81,8 +76,10 @@ string bmx::get_bmx_version_string()
8176
if (BMX_REGRESSION_TEST) {
8277
return "0.0.0";
8378
} else {
79+
mxfProductVersion product_version = get_bmx_mxf_product_version();
8480
char buffer[32];
85-
bmx_snprintf(buffer, sizeof(buffer), "%d.%d.%d", BMX_VERSION_MAJOR, BMX_VERSION_MINOR, BMX_VERSION_MICRO);
81+
bmx_snprintf(buffer, sizeof(buffer), "%d.%d.%d",
82+
product_version.major, product_version.minor, product_version.patch);
8683
return buffer;
8784
}
8885
}
@@ -94,9 +91,12 @@ string bmx::get_bmx_scm_version_string()
9491
} else {
9592
static string version_string;
9693
if (version_string.empty()) {
97-
version_string = git::Describe();
94+
version_string = git::DescribeTag();
95+
if (version_string.empty() || version_string == "unknown")
96+
version_string = git::Describe();
97+
9898
#ifdef PACKAGE_GIT_VERSION_STRING
99-
if (version_string == "unknown") {
99+
if (version_string.empty() || version_string == "unknown") {
100100
version_string = PACKAGE_GIT_VERSION_STRING;
101101
}
102102
else
@@ -210,8 +210,42 @@ mxfProductVersion bmx::get_bmx_mxf_product_version()
210210
{
211211
if (BMX_REGRESSION_TEST)
212212
return REGTEST_MXF_PRODUCT_VERSION;
213-
else
214-
return BMX_MXF_PRODUCT_VERSION;
213+
214+
static mxfProductVersion product_version = {0, 0, 0, 0, 0};
215+
if (product_version.major == 0 && product_version.minor == 0) {
216+
product_version.major = BMX_VERSION_MAJOR;
217+
product_version.minor = BMX_VERSION_MINOR;
218+
219+
// Set the patch version value to the commit offset from the release tag.
220+
// The commit offset is part of the git describe tag string which has the
221+
// format "<tag>-<offset>-g<commit id>"
222+
string describe = git::DescribeTag();
223+
#ifdef PACKAGE_GIT_VERSION_STRING
224+
if (describe.empty() || describe == "unknown")
225+
describe = PACKAGE_GIT_VERSION_STRING;
226+
#endif
227+
if (!describe.empty() && describe != "unknown") {
228+
size_t dash_pos = describe.rfind("-", describe.size() - 1);
229+
if (dash_pos != string::npos) {
230+
dash_pos = describe.rfind("-", dash_pos - 1);
231+
if (dash_pos != string::npos)
232+
dash_pos++;
233+
}
234+
235+
int offset;
236+
if (dash_pos != string::npos && sscanf(&describe[dash_pos], "%d", &offset) == 1 && offset >= 0 && offset <= UINT16_MAX) {
237+
product_version.patch = (uint16_t)offset;
238+
if (git::AnyUncommittedChanges())
239+
product_version.release = 0; /* Unknown version */
240+
else if (offset == 0)
241+
product_version.release = 1; /* Released version */
242+
else
243+
product_version.release = 2; /* Post release, development version */
244+
}
245+
}
246+
}
247+
248+
return product_version;
215249
}
216250

217251
string bmx::get_bmx_mxf_version_string()
@@ -221,9 +255,10 @@ string bmx::get_bmx_mxf_version_string()
221255
} else {
222256
static string version_string;
223257
if (version_string.empty()) {
258+
mxfProductVersion product_version = get_bmx_mxf_product_version();
224259
char buffer[64];
225260
bmx_snprintf(buffer, sizeof(buffer), "%d.%d.%d",
226-
BMX_VERSION_MAJOR, BMX_VERSION_MINOR, BMX_VERSION_MICRO);
261+
product_version.major, product_version.minor, product_version.patch);
227262
version_string = buffer;
228263
version_string += " (scm " + get_bmx_scm_version_string() + ")";
229264
}

0 commit comments

Comments
 (0)