Skip to content

Commit 97c973e

Browse files
authored
Merge pull request #12 from StephanKa/feature/add-configured-file-versioning
added example for versioning
2 parents bddf3c6 + 435407e commit 97c973e

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
cmake_minimum_required(VERSION 3.15)
22

33
# Set the project name to your project name, my project isn't very descriptive
4-
project(myproject CXX)
4+
project(myproject
5+
LANGUAGES CXX
6+
VERSION 0.0.1)
7+
58
include(cmake/StandardProjectSettings.cmake)
69
include(cmake/PreventInSourceBuilds.cmake)
7-
810
include(cmake/CodeFormat.cmake)
911

12+
execute_process(
13+
COMMAND git log -1 --format=%h
14+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
15+
OUTPUT_VARIABLE GIT_HASH
16+
OUTPUT_STRIP_TRAILING_WHITESPACE
17+
)
18+
19+
configure_file("templates/version.hpp.in" "${CMAKE_BINARY_DIR}/generated/include/version.hpp" ESCAPE_QUOTES)
20+
1021
# Link this 'library' to set the c++ standard / compile-time options requested
1122
add_library(project_options INTERFACE)
1223
target_compile_features(project_options INTERFACE cxx_std_17)

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,5 @@ TARGET_LINK_LIBRARIES(
3030
CONAN_PKG::docopt.cpp
3131
CONAN_PKG::fmt
3232
CONAN_PKG::spdlog)
33+
34+
TARGET_INCLUDE_DIRECTORIES(intro PRIVATE ${CMAKE_BINARY_DIR}/generated)

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <spdlog/spdlog.h>
55
#include <docopt/docopt.h>
6+
#include "include/version.hpp"
67

78
static constexpr auto USAGE =
89
R"(Naval Fate.
@@ -24,6 +25,8 @@ static constexpr auto USAGE =
2425

2526
int main(int argc, const char **argv)
2627
{
28+
fmt::print("Version\nMajor {}\nMinor {}\nPatch {}\nGit Hash {}\n", Version::Major, Version::Minor, Version::Patch, Version::GitHash);
29+
2730
std::map<std::string, docopt::value> args = docopt::docopt(USAGE,
2831
{ std::next(argv), std::next(argv, argc) },
2932
true,// show help if requested

templates/version.hpp.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
struct Version
4+
{
5+
static constexpr uint32_t Major {@PROJECT_VERSION_MAJOR@};
6+
static constexpr uint32_t Minor {@PROJECT_VERSION_MINOR@};
7+
static constexpr uint32_t Patch {@PROJECT_VERSION_PATCH@};
8+
static constexpr std::string_view GitHash {"@GIT_HASH@"};
9+
};

0 commit comments

Comments
 (0)