CMake: Fix version generation: regenare on every build

This commit is contained in:
Maximilian Keßler 2024-02-05 19:17:26 +01:00
parent 742266fe82
commit 7699b20498
Signed by: max
GPG key ID: BCC5A619923C0BA5
2 changed files with 33 additions and 34 deletions

View file

@ -1,25 +1,6 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(endgame-analyzer CXX) project(endgame-analyzer CXX)
execute_process(
COMMAND git describe
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_DESCRIPTION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND git -c log.showsignature=false log -1 --format=%h
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
configure_file(
${CMAKE_SOURCE_DIR}/include/version.h.in
${CMAKE_BINARY_DIR}/generated/version.h
)
include_directories(${CMAKE_BINARY_DIR}/generated)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wpedantic -Werror") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wpedantic -Werror")
@ -41,8 +22,7 @@ set(Boost_USE_STATIC_LIBS ON)
include_directories(.) include_directories(.)
include_directories(${Boost_INCLUDE_DIR}) include_directories(${Boost_INCLUDE_DIR})
add_executable(endgame-analyzer set(SOURCES
src/main.cpp
src/state_explorer.cpp src/state_explorer.cpp
src/download.cpp src/download.cpp
src/command_line_interface.cpp src/command_line_interface.cpp
@ -52,15 +32,29 @@ add_executable(endgame-analyzer
src/make_state.cpp src/make_state.cpp
) )
# Include git module from https://github.com/rpavlik/cmake-modules to
# dynamically include git revisions into the build.
# This re-generates the file version.cpp whenever the git HEAD changes.
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake-modules/")
include(GetGitRevisionDescription)
get_git_head_revision(GIT_REFSPEC GIT_SHA1)
git_describe(GIT_DESCRIPTION)
git_local_changes(GIT_LOCAL_CHANGES)
configure_file(
${CMAKE_SOURCE_DIR}/src/version.cpp.in
${CMAKE_CURRENT_BINARY_DIR}/generated/version.cpp
)
list(APPEND SOURCES ${CMAKE_CURRENT_BINARY_DIR}/generated/version.cpp)
add_executable(endgame-analyzer
src/main.cpp
${SOURCES}
)
add_executable(unit-tests add_executable(unit-tests
test/unit_test.cpp test/unit_test.cpp
src/state_explorer.cpp ${SOURCES}
src/download.cpp
src/command_line_interface.cpp
src/parse_game.cpp
src/hanabi_types.cpp
src/game_interface.cpp
src/make_state.cpp
) )
target_link_libraries(endgame-analyzer cpr) target_link_libraries(endgame-analyzer cpr)

View file

@ -39,7 +39,12 @@ namespace Hanabi
int run_cli(CLIParms const & parms) int run_cli(CLIParms const & parms)
{ {
if (parms.version_info) { if (parms.version_info) {
std::cout << "endgame-analyzer " << VERSION_GIT_DESCRIPTION << " (commit " << VERSION_GIT_COMMIT_HASH << ")"; std::cout << "endgame-analyzer " << Version::git_description << " (commit ";
std::cout.write(Version::git_sha1.data(), 8) << ")" << std::endl;
if (Version::is_dirty())
{
std::cout << "Warning: The repository contains local changes that are not part of the build." << std::endl;
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
// We want to do this sanity check here again, // We want to do this sanity check here again,