Skip to content

Commit 42122de

Browse files
authored
Merge pull request #24 from flightaware/BCK-5541
Embed git hash/branch info in cpptcl libs
2 parents af2b418 + e670768 commit 42122de

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ cmake_minimum_required(VERSION 3.8)
22

33
project(cpptcl)
44

5+
include(cmake/version.cmake)
6+
load_git_properties(cpptcl ${CMAKE_BINARY_DIR}/generated)
7+
58
set(CPPTCL_VERSION 2.2.5)
69

710
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
@@ -61,8 +64,10 @@ add_compile_options(${OPTS})
6164
set(cpptcl_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
6265

6366
list(APPEND SRCS ${cpptcl_SOURCE_DIR}/cpptcl.cc)
67+
list(APPEND SRCS ${CMAKE_BINARY_DIR}/generated/cpptcl_version.cpp)
6468
list(APPEND HDRS ${cpptcl_SOURCE_DIR}/cpptcl/cpptcl.h)
6569
list(APPEND HDRS ${cpptcl_SOURCE_DIR}/cpptcl/cpptcl_object.h)
70+
list(APPEND HDRS ${cpptcl_SOURCE_DIR}/cpptcl/version.h)
6671
list(APPEND HDRS_DETAILS ${cpptcl_SOURCE_DIR}/cpptcl/details/callbacks.h)
6772
list(APPEND HDRS_DETAILS ${cpptcl_SOURCE_DIR}/cpptcl/details/callbacks_v.h)
6873
list(APPEND HDRS_DETAILS ${cpptcl_SOURCE_DIR}/cpptcl/details/constructors.h)
@@ -85,7 +90,7 @@ target_include_directories(cpptcl
8590
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
8691
)
8792
target_include_directories(cpptcl PUBLIC ${TCL_INCLUDE_PATH})
88-
target_link_libraries(cpptcl ${TCL_STUB_LIBRARY})
93+
target_link_libraries(cpptcl PUBLIC ${TCL_STUB_LIBRARY})
8994

9095
add_library(cpptcl_static STATIC ${SRCS} ${HDRS} ${HDRS_DETAILS})
9196
add_library(cpptcl::cpptcl_static ALIAS cpptcl_static)
@@ -114,7 +119,7 @@ target_include_directories(cpptcl_runtime
114119
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
115120
)
116121
target_include_directories(cpptcl_runtime PUBLIC ${TCL_INCLUDE_PATH})
117-
target_link_libraries(cpptcl_runtime ${TCL_LIBRARY})
122+
target_link_libraries(cpptcl_runtime PUBLIC ${TCL_LIBRARY})
118123

119124
if (CPPTCL_TEST)
120125
add_subdirectory(test)

cmake/version.cmake

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function (load_git_properties prefix output_dir)
2+
3+
# Load the current working branch
4+
execute_process(
5+
COMMAND git symbolic-ref -q --short HEAD
6+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
7+
OUTPUT_VARIABLE GIT_BRANCH
8+
OUTPUT_STRIP_TRAILING_WHITESPACE
9+
)
10+
11+
if (GIT_BRANCH STREQUAL "")
12+
execute_process(
13+
COMMAND git describe --tags --exact-match
14+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
15+
OUTPUT_VARIABLE GIT_BRANCH
16+
OUTPUT_STRIP_TRAILING_WHITESPACE
17+
)
18+
endif ()
19+
20+
# Get the latest abbreviated commit hash of the working branch
21+
execute_process(
22+
COMMAND git log -1 --format=%h
23+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
24+
OUTPUT_VARIABLE GIT_HASH
25+
OUTPUT_STRIP_TRAILING_WHITESPACE
26+
)
27+
28+
if ("${GIT_BRANCH}" STREQUAL "")
29+
set(GIT_BRANCH "N/A")
30+
endif ()
31+
32+
if ("${GIT_HASH}" STREQUAL "")
33+
set(GIT_HASH "N/A")
34+
endif ()
35+
36+
string(TOUPPER "${prefix}" var_prefix)
37+
string(CONCAT VERSION_DATA "const char* ${var_prefix}_GIT_HASH = \"${GIT_HASH}\";\n"
38+
"const char* ${var_prefix}_GIT_BRANCH = \"${GIT_BRANCH}\";\n"
39+
)
40+
41+
if (EXISTS ${output_dir}/${prefix}_version.cpp)
42+
file(READ ${output_dir}/${prefix}_version.cpp VERSION_DATA_)
43+
else ()
44+
set(VERSION_DATA_ "")
45+
endif ()
46+
47+
if (NOT "${VERSION_DATA}" STREQUAL "${VERSION_DATA_}")
48+
file(WRITE ${output_dir}/${prefix}_version.cpp "${VERSION_DATA}")
49+
endif ()
50+
51+
endfunction ()

cpptcl/version.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef CPPTCL_VERSION_H
2+
#define CPPTCL_VERSION_H
3+
4+
// Variables are autogenerated and compiled
5+
// into the library by cmake
6+
extern "C" {
7+
extern const char* CPPTCL_GIT_HASH;
8+
extern const char* CPPTCL_GIT_BRANCH;
9+
}
10+
11+
const char* cpptcl_git_hash() {
12+
return CPPTCL_GIT_HASH;
13+
}
14+
15+
const char* cpptcl_git_branch() {
16+
return CPPTCL_GIT_BRANCH;
17+
}
18+
19+
#endif //CPPTCL_VERSION_H

0 commit comments

Comments
 (0)