Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
Set lib version using git release tag
Browse files Browse the repository at this point in the history
Set the library patch (micro) version to the commit offset from the
last release git tag.
  • Loading branch information
philipnbbc committed Jan 16, 2023
1 parent df5af7d commit 203ab19
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 21 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.15.0)
endif()

project(libMXF
VERSION 1.0.3
VERSION 1.0
DESCRIPTION "Low-level C library for reading and writing the SMPTE ST 377-1 MXF file format"
HOMEPAGE_URL https://github.com/bbc/libMXF
LANGUAGES C CXX
)
set(PROJECT_VERSION_RELEASE 2)

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

Expand Down
19 changes: 12 additions & 7 deletions cmake/git_version.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
if(TARGET cmake_git_version_tracking)
if(TARGET libmxf_cmake_git_version_tracking)
return()
endif()


# Set the git release tag pattern to describe relative to
set(GIT_DESCRIBE_TAG_PATTERN "v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
set(GIT_WORKING_DIR ${PROJECT_SOURCE_DIR})

# Set to ignore git_version errors if there is no .git directory
set(GIT_FAIL_IF_NONZERO_EXIT FALSE)

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

if(EXISTS ${git_version_deps_source})
FetchContent_Declare(cmake_git_version_tracking
FetchContent_Declare(libmxf_cmake_git_version_tracking
SOURCE_DIR ${git_version_deps_source}
)
message("-- Build using git version source: ${git_version_deps_source}")
else()
FetchContent_Declare(cmake_git_version_tracking
FetchContent_Declare(libmxf_cmake_git_version_tracking
GIT_REPOSITORY "https://github.com/andrew-hardin/cmake-git-version-tracking.git"
GIT_TAG "904dbda1336ba4b9a1415a68d5f203f576b696bb"
PATCH_COMMAND git apply --ignore-whitespace "${CMAKE_CURRENT_LIST_DIR}/git_version_904dbda.patch"
)
endif()

FetchContent_GetProperties(cmake_git_version_tracking)
if(NOT cmake_git_version_tracking_POPULATED)
FetchContent_Populate(cmake_git_version_tracking)
add_subdirectory(${cmake_git_version_tracking_SOURCE_DIR} ${cmake_git_version_tracking_BINARY_DIR})
FetchContent_GetProperties(libmxf_cmake_git_version_tracking)
if(NOT libmxf_cmake_git_version_tracking_POPULATED)
FetchContent_Populate(libmxf_cmake_git_version_tracking)
add_subdirectory(${libmxf_cmake_git_version_tracking_SOURCE_DIR} ${libmxf_cmake_git_version_tracking_BINARY_DIR})
endif()
125 changes: 125 additions & 0 deletions cmake/git_version_904dbda.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
From ba0feac4583090d45e57480a851aae95f0aeb803 Mon Sep 17 00:00:00 2001
From: Philip de Nier <philipn@rd.bbc.co.uk>
Date: Mon, 16 Jan 2023 15:48:26 +0000
Subject: [PATCH] Add git describe relative to matching tag

---
CMakeLists.txt | 4 ++--
git.c.in | 3 +++
git.h | 7 +++++++
git_watcher.cmake | 15 ++++++++++++++-
4 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 55e6d07..5a553ef 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.2)
-project(cmake_git_version_tracking
+project(libmxf_cmake_git_version_tracking
LANGUAGES C)

# Define the two required variables before including
@@ -15,7 +15,7 @@ include(git_watcher.cmake)
# 3rdparty library.
add_library(${PROJECT_NAME} STATIC ${POST_CONFIGURE_FILE})
target_include_directories(${PROJECT_NAME} SYSTEM PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
-add_dependencies(${PROJECT_NAME} check_git)
+add_dependencies(${PROJECT_NAME} libmxf_check_git)

# The C99 standard is only required because we're using <stdbool.h>.
# This could be removed if it's a problem for users, but would require the
diff --git a/git.c.in b/git.c.in
index a26d27c..8670076 100644
--- a/git.c.in
+++ b/git.c.in
@@ -30,3 +30,6 @@ const char* git_Describe() {
const char* git_Branch() {
return "@GIT_BRANCH@";
}
+const char* git_DescribeTag() {
+ return "@GIT_DESCRIBE_TAG@";
+}
diff --git a/git.h b/git.h
index fee780e..e35a79e 100644
--- a/git.h
+++ b/git.h
@@ -52,6 +52,9 @@ const char* git_Describe();
/// The symbolic reference tied to HEAD.
const char* git_Branch();

+/// The commit describe relative to matching tag.
+const char* git_DescribeTag();
+
GIT_VERSION_TRACKING_EXTERN_C_END
#undef GIT_VERSION_TRACKING_EXTERN_C_BEGIN
#undef GIT_VERSION_TRACKING_EXTERN_C_END
@@ -143,6 +146,10 @@ inline const StringOrView Branch() {
static const StringOrView kValue = internal::InitString(git_Branch());
return kValue;
}
+inline const StringOrView DescribeTag() {
+ static const StringOrView kValue = internal::InitString(git_DescribeTag());
+ return kValue;
+}

} // namespace git

diff --git a/git_watcher.cmake b/git_watcher.cmake
index 32313a3..cf2b443 100644
--- a/git_watcher.cmake
+++ b/git_watcher.cmake
@@ -36,6 +36,9 @@
# -- Ignore the presence of untracked files when detecting if the
# working tree is dirty. This is set to FALSE by default.
#
+# GIT_DESCRIBE_TAG_PATTERN (OPTIONAL)
+# -- Git tag pattern to match against to produce the GIT_DESCRIBE_TAG variable.
+#
# DESIGN
# - This script was designed similar to a Python application
# with a Main() function. I wanted to keep it compact to
@@ -105,6 +108,7 @@ set(_state_variable_names
GIT_COMMIT_BODY
GIT_DESCRIBE
GIT_BRANCH
+ GIT_DESCRIBE_TAG
# >>>
# 1. Add the name of the additional git variable you're interested in monitoring
# to this list.
@@ -242,6 +246,14 @@ function(GetGitState _working_dir)
set(ENV{GIT_BRANCH} "${output}")
endif()

+ # Get output of git describe relative to the matching tag
+ RunGitCommand(describe --match ${GIT_DESCRIBE_TAG_PATTERN} ${object})
+ if(NOT exit_code EQUAL 0)
+ set(ENV{GIT_DESCRIBE_TAG} "unknown")
+ else()
+ set(ENV{GIT_DESCRIBE_TAG} "${output}")
+ endif()
+
# >>>
# 2. Additional git properties can be added here via the
# "execute_process()" command. Be sure to set them in
@@ -324,7 +336,7 @@ endfunction()
# check the state of git before every build. If the state has
# changed, then a file is configured.
function(SetupGitMonitoring)
- add_custom_target(check_git
+ add_custom_target(libmxf_check_git
ALL
DEPENDS ${PRE_CONFIGURE_FILE}
BYPRODUCTS
@@ -341,6 +353,7 @@ function(SetupGitMonitoring)
-DPOST_CONFIGURE_FILE=${POST_CONFIGURE_FILE}
-DGIT_FAIL_IF_NONZERO_EXIT=${GIT_FAIL_IF_NONZERO_EXIT}
-DGIT_IGNORE_UNTRACKED=${GIT_IGNORE_UNTRACKED}
+ -DGIT_DESCRIBE_TAG_PATTERN=${GIT_DESCRIBE_TAG_PATTERN}
-P "${CMAKE_CURRENT_LIST_FILE}")
endfunction()

--
2.25.1

4 changes: 0 additions & 4 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
/* Library versions */
#define LIBMXF_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
#define LIBMXF_VERSION_MINOR @PROJECT_VERSION_MINOR@
#define LIBMXF_VERSION_MICRO @PROJECT_VERSION_PATCH@
#define LIBMXF_VERSION_RELEASE @PROJECT_VERSION_RELEASE@

#define LIBMXF_VERSION (LIBMXF_VERSION_MAJOR << 16 | LIBMXF_VERSION_MINOR << 8 | LIBMXF_VERSION_MICRO)

/* Define printf size_t format specifier */
#if defined(_WIN64)
Expand Down
2 changes: 1 addition & 1 deletion mxf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ target_link_libraries(MXF PRIVATE
# Add the git version tracking library code
include("${PROJECT_SOURCE_DIR}/cmake/git_version.cmake")
target_link_libraries(MXF PRIVATE
cmake_git_version_tracking
libmxf_cmake_git_version_tracking
)

set_target_properties(MXF PROPERTIES
Expand Down
58 changes: 51 additions & 7 deletions mxf/mxf_version.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#endif

#include <string.h>
#include <stdio.h>
#include <limits.h>

#include <mxf/mxf.h>
#include <mxf/mxf_utils.h>
Expand All @@ -50,12 +52,6 @@



static mxfProductVersion g_libmxfVersion = {LIBMXF_VERSION_MAJOR, /* major */
LIBMXF_VERSION_MINOR, /* minor */
LIBMXF_VERSION_MICRO, /* patch */
0, /* build */
LIBMXF_VERSION_RELEASE}; /* release */

static mxfProductVersion g_regtestVersion = {1, /* major */
0, /* minor */
0, /* patch */
Expand Down Expand Up @@ -107,6 +103,51 @@ mxf_get_scm_version_wstring_func mxf_get_scm_version_wstring = mxf_default_ge

const mxfProductVersion* mxf_default_get_version(void)
{
static mxfProductVersion g_libmxfVersion = {0};

if (g_libmxfVersion.major == 0 && g_libmxfVersion.minor == 0) {
g_libmxfVersion.major = LIBMXF_VERSION_MAJOR;
g_libmxfVersion.minor = LIBMXF_VERSION_MINOR;

// Set the patch version value to the commit offset from the release tag.
// The commit offset is part of the git describe tag string which has the
// format "<tag>-<offset>-g<commit id>"
const char *describe = git_DescribeTag();
#ifdef PACKAGE_GIT_VERSION_STRING
if (!describe[0] || strcmp(describe, "unknown") == 0)
describe = PACKAGE_GIT_VERSION_STRING;
#endif
if (describe[0] && strcmp(describe, "unknown") != 0) {
int offset;
int dash_count = 0;
const char *offset_str = &describe[strlen(describe) - 1];

// position offset_str after the second '-' in reverse order
while (offset_str != describe) {
if (*offset_str == '-') {
if (dash_count >= 1) {
offset_str++;
break;
}
dash_count++;
}
offset_str--;
}
if (offset_str == describe)
offset_str = 0;

if (offset_str && sscanf(offset_str, "%d", &offset) == 1 && offset >= 0 && offset <= UINT16_MAX) {
g_libmxfVersion.patch = (uint16_t)offset;
if (git_AnyUncommittedChanges())
g_libmxfVersion.release = 0; /* Unknown version */
else if (offset == 0)
g_libmxfVersion.release = 1; /* Released version */
else
g_libmxfVersion.release = 2; /* Post release, development version */
}
}
}

return &g_libmxfVersion;
}

Expand All @@ -124,7 +165,10 @@ const char* mxf_default_get_scm_version_string(void)
{
static char version_string[64] = {0};
if (version_string[0] == 0) {
const char *describe = git_Describe();
const char *describe = git_DescribeTag();
if (!describe[0] || strcmp(describe, "unknown") == 0)
describe = git_Describe();

#ifdef PACKAGE_GIT_VERSION_STRING
if (strcmp(describe, "unknown") == 0) {
mxf_snprintf(version_string, ARRAY_SIZE(version_string), "%s", PACKAGE_GIT_VERSION_STRING);
Expand Down

0 comments on commit 203ab19

Please sign in to comment.