Skip to content
Snippets Groups Projects
Commit 51e43eb8 authored by Sebastian Eibl's avatar Sebastian Eibl
Browse files

example 07_git_build added

parent 09b18c67
Branches
No related tags found
No related merge requests found
Pipeline #138472 passed
...@@ -38,4 +38,8 @@ check-recipes: ...@@ -38,4 +38,8 @@ check-recipes:
- cmake --build 05_build - cmake --build 05_build
- 05_build/version-info - 05_build/version-info
- cmake -S 06_git_configure -B 06_build - cmake -S 06_git_configure -B 06_build
\ No newline at end of file
- cmake -S 07_git_build -B 07_build
- cmake --build 07_build
- 07_build/git-hash
\ No newline at end of file
cmake_minimum_required(VERSION 3.20)
project(07_git_build
LANGUAGES CXX)
add_executable(git-hash)
target_sources(git-hash PRIVATE main.cpp githash.hpp)
target_sources(git-hash PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/githash.cpp)
target_include_directories(git-hash PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
set(GIT_HASH_INPUT ${CMAKE_CURRENT_SOURCE_DIR}/githash.cpp.in)
set(GIT_HASH_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/githash.cpp)
add_custom_command(
OUTPUT
${GIT_HASH_OUTPUT}
ALL
COMMAND
${CMAKE_COMMAND} -DINPUT_FILE=${GIT_HASH_INPUT} -DOUTPUT_FILE=${GIT_HASH_OUTPUT} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/update_git_hash.cmake
)
add_custom_target(
update-git-hash
ALL
DEPENDS
${GIT_HASH_OUTPUT}
)
add_dependencies(git-hash update-git-hash)
# How to get the current git hash at build time?
We will use the ability of CMake to run as an interpreter
[interpreter](https://cmake.org/cmake/help/latest/manual/cmake.1.html#run-a-script)
to retrieve the git hash at runtime.
The idea works as follows:
* Use [How to get the current git hash at configure time?](06_git_configure) to get the git hash.
* Use [How to pass information from CMake to your code?](05_configure_file) to write the hash
into a source file.
* Move the logic into a separate CMake [file](cmake/update_git_hash.cmake).
* Use [add_custom_command](https://cmake.org/cmake/help/latest/command/add_custom_command.html)
to invoke the [CMake interpreter](https://cmake.org/cmake/help/latest/manual/cmake.1.html#run-a-script).
* Move everything into its own target to allow parallel execution:
[add_custom_target](https://cmake.org/cmake/help/latest/command/add_custom_target.html).
* Add a [dependency](https://cmake.org/cmake/help/latest/command/add_dependencies.html)
so it gets executed whenever the project is rebuild.
Note, we configure a cpp file here to avoid recompilation of huge portions of the code!
\ No newline at end of file
set(GIT_HASH "unknown")
find_package(Git QUIET)
if (GIT_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%H
OUTPUT_VARIABLE GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endif ()
configure_file(
${INPUT_FILE}
${OUTPUT_FILE}
@ONLY
)
\ No newline at end of file
#include "githash.hpp"
char const * GIT_HASH = "@GIT_HASH@";
#pragma once
extern char const * GIT_HASH;
#include <iostream>
#include "githash.hpp"
int main()
{
std::cout << "git hash: " << GIT_HASH << std::endl;
exit(EXIT_SUCCESS);
}
...@@ -14,6 +14,7 @@ we run them in our CI. ...@@ -14,6 +14,7 @@ we run them in our CI.
* [Local and cache variables?](https://cliutils.gitlab.io/modern-cmake/chapters/basics/variables.html) * [Local and cache variables?](https://cliutils.gitlab.io/modern-cmake/chapters/basics/variables.html)
* [How to pass information from CMake to your code?](05_configure_file) * [How to pass information from CMake to your code?](05_configure_file)
* [How to get the current git hash at configure time?](06_git_configure) * [How to get the current git hash at configure time?](06_git_configure)
* [How to get the current git hash at build time?](07_git_build)
## Why CMake? ## Why CMake?
* Automatically search for programs, libraries, and header files * Automatically search for programs, libraries, and header files
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment