Skip to content
Snippets Groups Projects
Commit 2bf0684e authored by Corné Lukken's avatar Corné Lukken
Browse files

CWG-45: Implement git tag based version extraction

parent 48961ba6
No related branches found
No related tags found
1 merge request!9CWG-45: Implement git tag based version extraction
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project({{cookiecutter.project_slug}} VERSION 0.1) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
include(GitVersion)
get_version_info({{cookiecutter.project_slug}} "${CMAKE_CURRENT_SOURCE_DIR}")
set(project_slug {{cookiecutter.project_slug}})
project({{cookiecutter.project_slug}} VERSION ${${project_slug}_VERSION})
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES) set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
option(BUILD_TESTING "Build the test suite" OFF) option(BUILD_TESTING "Build the test suite" OFF)
option(BUILD_DOCUMENTATION "Build the documentation" OFF) option(BUILD_DOCUMENTATION "Build the documentation" OFF)
......
...@@ -36,6 +36,15 @@ For instance: ...@@ -36,6 +36,15 @@ For instance:
- Install the project globally: ``ninja -C ./build/ install`` - Install the project globally: ``ninja -C ./build/ install``
- Clean caches: `ninja -C ./build/ clean` - Clean caches: `ninja -C ./build/ clean`
## Versioning
This project uses git tags or branch names to determine the current version of the
software. The version information is automatically made available in CMake and can be
made available in C/C++ by dynamically adding a generated header.
See for details:
- https://github.com/jahnf/CMake-GitVersion/blob/develop/cmake/GitVersion.cmake
## Contributing ## Contributing
To contribute, please create a feature branch and a "Draft" merge request. Upon To contribute, please create a feature branch and a "Draft" merge request. Upon
......
# Fallback for version generation from pure git archive exports
set(GIT_EXPORT_VERSION_SHORTHASH "$Format:%h$")
set(GIT_EXPORT_VERSION_FULLHASH "$Format:%H$")
set(GIT_EXPORT_VERSION_BRANCH "$Format:%D$") # needs parsing in cmake...
set(HAS_GIT_EXPORT_INFO 1)
# Auto generated archive version information
# Included in created source archives
set(@PREFIX@_VERSION "@VERSION@")
set(@PREFIX@_VERSION_MAJOR "@VERSION_MAJOR@")
set(@PREFIX@_VERSION_MINOR "@VERSION_MINOR@")
set(@PREFIX@_VERSION_PATCH "@VERSION_PATCH@")
set(@PREFIX@_VERSION_FLAG "@VERSION_FLAG@")
set(@PREFIX@_VERSION_DISTANCE "@VERSION_DISTANCE@")
set(@PREFIX@_VERSION_SHORTHASH "@VERSION_SHORTHASH@")
set(@PREFIX@_VERSION_FULLHASH "@VERSION_FULLHASH@")
set(@PREFIX@_VERSION_STRING "@VERSION_STRING@")
set(@PREFIX@_VERSION_ISDIRTY "@VERSION_ISDIRTY@")
set(@PREFIX@_VERSION_BRANCH "@VERSION_BRANCH@")
set(@PREFIX@_VERSION_SUCCESS 1)
#include "@PREFIX@-GitVersion.h"
namespace @PREFIX@ {
const char* version() { return "@VERSION@"; }
const char* version_string() { return "@VERSION_STRING@"; }
unsigned int version_major() { return @VERSION_MAJOR@; }
unsigned int version_minor() { return @VERSION_MINOR@; }
unsigned int version_patch() { return @VERSION_PATCH@; }
const char* version_flag() { return "@VERSION_FLAG@"; }
unsigned int version_distance() { return @VERSION_DISTANCE@; }
const char* version_shorthash() { return "@VERSION_SHORTHASH@"; }
const char* version_fullhash() { return "@VERSION_FULLHASH@"; }
bool version_isdirty() { return @VERSION_ISDIRTY@; }
const char* version_branch() { return "@VERSION_BRANCH@"; }
}
This diff is collapsed.
#ifndef @PREFIX@_GITVERSION_H
#define @PREFIX@_GITVERSION_H
namespace @PREFIX@ {
const char* version();
const char* version_string();
unsigned int version_major();
unsigned int version_minor();
unsigned int version_patch();
const char* version_flag();
unsigned int version_distance();
const char* version_shorthash();
const char* version_fullhash();
bool version_isdirty();
const char* version_branch();
}
#endif
...@@ -4,5 +4,6 @@ ...@@ -4,5 +4,6 @@
add_subdirectory(lib) add_subdirectory(lib)
add_executable(cpp main.cpp) add_executable(cpp main.cpp)
add_version_info(TARGET cpp)
target_link_libraries(cpp PRIVATE hello) target_link_libraries(cpp PRIVATE hello)
// Copyright (C) ASTRON (Netherlands Institute for Radio Astronomy) // Copyright (C) ASTRON (Netherlands Institute for Radio Astronomy)
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
#include <iostream>
#include "hello.hpp" #include "hello.hpp"
#include "cpp-GitVersion.h"
int main(int, const char **) {
lib::Hello();
int main(int, const char **) { lib::Hello(); } std::cout << "CMake-GitVersion example"<< std::endl;
std::cout << "- version: " << cpp::version() << std::endl;
std::cout << "- version_string: " << cpp::version_string() << std::endl;
std::cout << "- version_branch: " << cpp::version_branch() << std::endl;
std::cout << "- version_fullhash: " << cpp::version_fullhash() << std::endl;
std::cout << "- version_shorthash: " << cpp::version_shorthash() << std::endl;
std::cout << "- version_isdirty: " << cpp::version_isdirty() << std::endl;
std::cout << "- version_distance: " << cpp::version_distance() << std::endl;
std::cout << "- version_flag: " << cpp::version_flag() << std::endl;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment