Skip to content

Commit

Permalink
Define build branch and commit in generated configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrm456 authored Jun 18, 2024
1 parent c2e9609 commit 2686795
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 24 deletions.
13 changes: 11 additions & 2 deletions cmake/templates/ntccfg_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ BSLS_IDENT("$Id: $")
namespace BloombergLP {
namespace ntccfg {

// The platform name.
// The build platform name.
#define NTC_BUILD_UNAME "@NTF_BUILD_UNAME@"

// The platform build configuration flags.
// The build configuration flags.
#define NTC_BUILD_UFID "@NTF_BUILD_UFID@"

// The build branch.
#define NTC_BUILD_BRANCH "@NTC_BUILD_BRANCH@"

// The build commit hash.
#define NTC_BUILD_COMMIT_HASH "@NTC_BUILD_COMMIT_HASH@"

// The build commit hash, abbreviated.
#define NTC_BUILD_COMMIT_HASH_ABBREV "@NTC_BUILD_COMMIT_HASH_ABBREV@"

// Build with BSL.
#define NTC_BUILD_WITH_BSL @NTF_BUILD_WITH_BSL@

Expand Down
13 changes: 11 additions & 2 deletions cmake/templates/ntscfg_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ BSLS_IDENT("$Id: $")
namespace BloombergLP {
namespace ntscfg {

// The platform name.
// The build platform name.
#define NTS_BUILD_UNAME "@NTF_BUILD_UNAME@"

// The platform build configuration flags.
// The build configuration flags.
#define NTS_BUILD_UFID "@NTF_BUILD_UFID@"

// The build branch.
#define NTS_BUILD_BRANCH "@NTS_BUILD_BRANCH@"

// The build commit hash.
#define NTS_BUILD_COMMIT_HASH "@NTS_BUILD_COMMIT_HASH@"

// The build commit hash, abbreviated.
#define NTS_BUILD_COMMIT_HASH_ABBREV "@NTS_BUILD_COMMIT_HASH_ABBREV@"

// Build with BSL.
#define NTS_BUILD_WITH_BSL @NTF_BUILD_WITH_BSL@

Expand Down
28 changes: 28 additions & 0 deletions groups/nts/ntscfg/ntscfg_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,33 @@ bool Platform::hasPortDatabase()
#error Not implemented
#endif

bsl::string Platform::buildBranch()
{
bsl::string result = NTS_BUILD_BRANCH;
if (result.empty()) {
bsl::stringstream ss;
ss << NTS_VERSION_MAJOR
<< '.'
<< NTS_VERSION_MINOR
<< '.'
<< NTS_VERSION_PATCH;
result = ss.str();
}

return result;
}

bsl::string Platform::buildCommitHash()
{
bsl::string result = NTS_BUILD_COMMIT_HASH;
return result;
}

bsl::string Platform::buildCommitHashAbbrev()
{
bsl::string result = NTS_BUILD_COMMIT_HASH_ABBREV;
return result;
}

} // close package namespace
} // close enterprise namespace
12 changes: 12 additions & 0 deletions groups/nts/ntscfg/ntscfg_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ struct Platform {
/// Return true if the local host database (i.e. "/etc/hosts" or the
/// platform equivalent) exists, otherwise return false.
static bool hasPortDatabase();

/// Return the build branch, or the version string if the build branch
/// is unknown.
static bsl::string buildBranch();

/// Return the build commit hash, or the empty string if the build commit
/// hash is unknown.
static bsl::string buildCommitHash();

/// Return the build commit hash, abbreviated, or the empty string if the
/// build commit hash is unknown.
static bsl::string buildCommitHashAbbrev();
};

/// @internal @brief
Expand Down
61 changes: 61 additions & 0 deletions repository.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3013,6 +3013,65 @@ function (ntf_scan)

endfunction()

function (ntf_target_build_info)
cmake_parse_arguments(
NTF_TARGET "" "NAME;PATH" "" ${ARGN})

if ("${NTF_TARGET_NAME}" STREQUAL "")
message(FATAL_ERROR "Invalid parameter: NAME")
endif()

if ("${NTF_TARGET_PATH}" STREQUAL "")
message(FATAL_ERROR "Invalid parameter: PATH")
endif()

string(TOLOWER ${NTF_TARGET_NAME} target_lowercase)
string(TOUPPER ${NTF_TARGET_NAME} target_uppercase)

set(target ${target_lowercase})

if (IS_ABSOLUTE ${NTF_TARGET_PATH})
set(target_path ${NTF_TARGET_PATH})
else()
ntf_repository_path_get(OUTPUT repository_path)
file(REAL_PATH ${NTF_TARGET_PATH} target_path
BASE_DIRECTORY ${repository_path})
endif()

execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${target_path}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
COMMAND git log -1 --format=%H
WORKING_DIRECTORY ${target_path}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
COMMAND git log -1 --format=%h
WORKING_DIRECTORY ${target_path}
OUTPUT_VARIABLE GIT_COMMIT_HASH_ABBREV
OUTPUT_STRIP_TRAILING_WHITESPACE
)

set(${target_uppercase}_BUILD_BRANCH
"${GIT_BRANCH}"
CACHE INTERNAL "" FORCE)

set(${target_uppercase}_BUILD_COMMIT_HASH
"${GIT_COMMIT_HASH}"
CACHE INTERNAL "" FORCE)

set(${target_uppercase}_BUILD_COMMIT_HASH_ABBREV
"${GIT_COMMIT_HASH_ABBREV}"
CACHE INTERNAL "" FORCE)

endfunction()

# Begin the definition of a group.
#
Expand Down Expand Up @@ -3137,6 +3196,8 @@ function (ntf_group)
endif()
endif()

ntf_target_build_info(NAME ${target} PATH ${target_path})

endfunction()

# Add a dependency to a group, attempting to find the dependency through
Expand Down
40 changes: 20 additions & 20 deletions targets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,6 @@ ntf_repository_enable_coverage(
${NTF_BUILD_WITH_COVERAGE})

if (${NTF_BUILD_WITH_NTS})
configure_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/templates/ntscfg_config.h
${CMAKE_BINARY_DIR}/ntscfg_config.h
@ONLY)

configure_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/templates/ntscfg_config.cpp
${CMAKE_BINARY_DIR}/ntscfg_config.cpp
@ONLY)

ntf_group(
NAME
nts
Expand Down Expand Up @@ -88,6 +78,16 @@ if (${NTF_BUILD_WITH_NTS})
ntsscm
)

configure_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/templates/ntscfg_config.h
${CMAKE_BINARY_DIR}/ntscfg_config.h
@ONLY)

configure_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/templates/ntscfg_config.cpp
${CMAKE_BINARY_DIR}/ntscfg_config.cpp
@ONLY)

ntf_component(PATH ${CMAKE_BINARY_DIR}/ntscfg_config.cpp)
ntf_component(NAME ntscfg_limits)
ntf_component(NAME ntscfg_platform)
Expand Down Expand Up @@ -295,16 +295,6 @@ if (${NTF_BUILD_WITH_NTS})
endif()

if (${NTF_BUILD_WITH_NTC})
configure_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/templates/ntccfg_config.h
${CMAKE_BINARY_DIR}/ntccfg_config.h
@ONLY)

configure_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/templates/ntccfg_config.cpp
${CMAKE_BINARY_DIR}/ntccfg_config.cpp
@ONLY)

ntf_group(
NAME
ntc
Expand Down Expand Up @@ -342,6 +332,16 @@ if (${NTF_BUILD_WITH_NTC})
ntcscm
)

configure_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/templates/ntccfg_config.h
${CMAKE_BINARY_DIR}/ntccfg_config.h
@ONLY)

configure_file(
${CMAKE_CURRENT_LIST_DIR}/cmake/templates/ntccfg_config.cpp
${CMAKE_BINARY_DIR}/ntccfg_config.cpp
@ONLY)

ntf_component(PATH ${CMAKE_BINARY_DIR}/ntccfg_config.cpp)

ntf_component(NAME ntccfg_bind)
Expand Down

0 comments on commit 2686795

Please sign in to comment.