Skip to content

Commit

Permalink
perf: add an option VERSION_NAMESPACE_PREFIX (#114)
Browse files Browse the repository at this point in the history
- move the defaut location to ${CMAKE_CURRENT_BINARY_DIR}.
- add an option VERSION_NAMESPACE_PREFIX to configure the
  prefix namespace for cxx projects. Default to ${CMAKE_PROJECT_NAME}.

Signed-off-by: msclock <msclock@qq.com>
  • Loading branch information
msclock committed Jun 14, 2024
1 parent 4593e46 commit 383d0d2
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions cmake/configure/GitTools.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[[
This module provides some git stuffs.

]]

include_guard(GLOBAL)
Expand All @@ -10,16 +11,18 @@ project git meta.

Arguments:
CONFIGURE_HEADER_FILE - git header configuration content.
Default to ${CMAKE_BINARY_DIR}/git/git.h.in (optional)
Default to ${CMAKE_CURRENT_BINARY_DIR}/git_version_template/_version.hpp.in (optional)
DESTINATION - git header destination to generate.
Default to ${CMAKE_BINARY_DIR}/git/include/git.h (optional)
Default to ${CMAKE_CURRENT_BINARY_DIR}/git_version/_version.hpp (optional)
VERSION_NAMESPACE_PREFIX - namespace prefix for the generated cxx header.
Default to ${CMAKE_PROJECT_NAME} (optional)

Example:

include(GitTools)
gennerate_git_header()
add_library(header INTERFACE)
target_include_interface_directories(header ${CMAKE_BINARY_DIR}/git/include)
generate_git_header(VERSION_NAMESPACE_PREFIX header)
target_include_interface_directories(header ${CMAKE_CURRENT_BINARY_DIR}/git_version)
install_target(
NAME
header
Expand All @@ -28,18 +31,24 @@ Example:
TARGETS
header
INCLUDES
${CMAKE_BINARY_DIR}/git/include/)
${CMAKE_CURRENT_BINARY_DIR}/git_version/)

See also:
- https://raw.githubusercontent.com/andrew-hardin/cmake-git-version-tracking/master/git.h
]]
function(generate_git_header)
set(_opts)
set(_single_opts CONFIGURE_HEADER_FILE DESTINATION)
set(_single_opts CONFIGURE_HEADER_FILE DESTINATION VERSION_NAMESPACE_PREFIX)
set(_multi_opts)
cmake_parse_arguments(arg "${_opts}" "${_single_opts}" "${_multi_opts}"
${ARGN})

if(NOT arg_DESTINATION)
set(arg_DESTINATION ${CMAKE_BINARY_DIR}/git/include/git.h)
set(arg_DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/git_version/_version.hpp)
endif()

if(NOT arg_VERSION_NAMESPACE_PREFIX)
set(arg_VERSION_NAMESPACE_PREFIX ${CMAKE_PROJECT_NAME})
endif()

if(NOT arg_CONFIGURE_HEADER_FILE)
Expand Down Expand Up @@ -166,7 +175,7 @@ GIT_VERSION_TRACKING_EXTERN_C_END
#ifdef __cplusplus
/// This is a utility extension for C++ projects.
/// It provides a \"git\" namespace that wraps the
/// It provides a \"${arg_VERSION_NAMESPACE_PREFIX}\" namespace that wraps the
/// C methods in more(?) ergonomic types.
///
/// This is header-only in an effort to keep the
Expand All @@ -189,7 +198,7 @@ GIT_VERSION_TRACKING_EXTERN_C_END
#include <string>
#endif
namespace git {
namespace ${arg_VERSION_NAMESPACE_PREFIX} {
#if GIT_VERSION_USE_STRING_VIEW
using StringOrView = std::string_view\;
Expand Down Expand Up @@ -287,7 +296,7 @@ inline const StringOrView Describe() {
return kValue\;
}
} // namespace git
} // namespace ${arg_VERSION_NAMESPACE_PREFIX}
// Cleanup our defines to avoid polluting.
#undef GIT_VERSION_USE_STRING_VIEW
Expand All @@ -296,7 +305,8 @@ inline const StringOrView Describe() {
#endif // __cplusplus
")

set(arg_CONFIGURE_HEADER_FILE ${CMAKE_BINARY_DIR}/git/git.h.in)
set(arg_CONFIGURE_HEADER_FILE
${CMAKE_CURRENT_BINARY_DIR}/git_version_template/_version.hpp.in)
file(WRITE ${arg_CONFIGURE_HEADER_FILE} ${_configure_git_header_content})
endif()

Expand All @@ -306,10 +316,8 @@ inline const StringOrView Describe() {

message(
STATUS
"Generated git header including project metadata in ${arg_DESTINATION} from ${arg_CONFIGURE_HEADER_FILE}
"Generated a git version header including project metadata in ${arg_DESTINATION} from ${arg_CONFIGURE_HEADER_FILE}
Usage:
include_directories(${arg_DESTINATION_PATH})
# Or
target_include_directories(${arg_DESTINATION_PATH})
# Or refer to https://github.com/msclock/cmake-modules/blob/master/cmake/configure/Common.cmake
target_include_interface_directories(${arg_DESTINATION_PATH})")
Expand Down

0 comments on commit 383d0d2

Please sign in to comment.