Skip to content

Commit

Permalink
Merge pull request #24 from flightaware/BCK-5541
Browse files Browse the repository at this point in the history
Embed git hash/branch info in cpptcl libs
  • Loading branch information
maryryang2 authored Jan 7, 2021
2 parents af2b418 + e670768 commit 42122de
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ cmake_minimum_required(VERSION 3.8)

project(cpptcl)

include(cmake/version.cmake)
load_git_properties(cpptcl ${CMAKE_BINARY_DIR}/generated)

set(CPPTCL_VERSION 2.2.5)

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
Expand Down Expand Up @@ -61,8 +64,10 @@ add_compile_options(${OPTS})
set(cpptcl_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

list(APPEND SRCS ${cpptcl_SOURCE_DIR}/cpptcl.cc)
list(APPEND SRCS ${CMAKE_BINARY_DIR}/generated/cpptcl_version.cpp)
list(APPEND HDRS ${cpptcl_SOURCE_DIR}/cpptcl/cpptcl.h)
list(APPEND HDRS ${cpptcl_SOURCE_DIR}/cpptcl/cpptcl_object.h)
list(APPEND HDRS ${cpptcl_SOURCE_DIR}/cpptcl/version.h)
list(APPEND HDRS_DETAILS ${cpptcl_SOURCE_DIR}/cpptcl/details/callbacks.h)
list(APPEND HDRS_DETAILS ${cpptcl_SOURCE_DIR}/cpptcl/details/callbacks_v.h)
list(APPEND HDRS_DETAILS ${cpptcl_SOURCE_DIR}/cpptcl/details/constructors.h)
Expand All @@ -85,7 +90,7 @@ target_include_directories(cpptcl
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_include_directories(cpptcl PUBLIC ${TCL_INCLUDE_PATH})
target_link_libraries(cpptcl ${TCL_STUB_LIBRARY})
target_link_libraries(cpptcl PUBLIC ${TCL_STUB_LIBRARY})

add_library(cpptcl_static STATIC ${SRCS} ${HDRS} ${HDRS_DETAILS})
add_library(cpptcl::cpptcl_static ALIAS cpptcl_static)
Expand Down Expand Up @@ -114,7 +119,7 @@ target_include_directories(cpptcl_runtime
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_include_directories(cpptcl_runtime PUBLIC ${TCL_INCLUDE_PATH})
target_link_libraries(cpptcl_runtime ${TCL_LIBRARY})
target_link_libraries(cpptcl_runtime PUBLIC ${TCL_LIBRARY})

if (CPPTCL_TEST)
add_subdirectory(test)
Expand Down
51 changes: 51 additions & 0 deletions cmake/version.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function (load_git_properties prefix output_dir)

# Load the current working branch
execute_process(
COMMAND git symbolic-ref -q --short HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)

if (GIT_BRANCH STREQUAL "")
execute_process(
COMMAND git describe --tags --exact-match
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif ()

# Get the latest abbreviated commit hash of the working branch
execute_process(
COMMAND git log -1 --format=%h
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)

if ("${GIT_BRANCH}" STREQUAL "")
set(GIT_BRANCH "N/A")
endif ()

if ("${GIT_HASH}" STREQUAL "")
set(GIT_HASH "N/A")
endif ()

string(TOUPPER "${prefix}" var_prefix)
string(CONCAT VERSION_DATA "const char* ${var_prefix}_GIT_HASH = \"${GIT_HASH}\";\n"
"const char* ${var_prefix}_GIT_BRANCH = \"${GIT_BRANCH}\";\n"
)

if (EXISTS ${output_dir}/${prefix}_version.cpp)
file(READ ${output_dir}/${prefix}_version.cpp VERSION_DATA_)
else ()
set(VERSION_DATA_ "")
endif ()

if (NOT "${VERSION_DATA}" STREQUAL "${VERSION_DATA_}")
file(WRITE ${output_dir}/${prefix}_version.cpp "${VERSION_DATA}")
endif ()

endfunction ()
19 changes: 19 additions & 0 deletions cpptcl/version.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef CPPTCL_VERSION_H
#define CPPTCL_VERSION_H

// Variables are autogenerated and compiled
// into the library by cmake
extern "C" {
extern const char* CPPTCL_GIT_HASH;
extern const char* CPPTCL_GIT_BRANCH;
}

const char* cpptcl_git_hash() {
return CPPTCL_GIT_HASH;
}

const char* cpptcl_git_branch() {
return CPPTCL_GIT_BRANCH;
}

#endif //CPPTCL_VERSION_H

0 comments on commit 42122de

Please sign in to comment.