Skip to content

Commit

Permalink
newer version string code, this should be final
Browse files Browse the repository at this point in the history
  • Loading branch information
mhekkel committed Aug 16, 2023
1 parent 8565e1b commit 9dc5d11
Showing 1 changed file with 51 additions and 45 deletions.
96 changes: 51 additions & 45 deletions cmake/VersionString.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: BSD-2-Clause

# Copyright (c) 2021-2023 NKI/AVL, Netherlands Cancer Institute
# Copyright (c) 2021-2023 Maarten L. Hekkelman

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -30,6 +30,7 @@ cmake_minimum_required(VERSION 3.15)

# We want the revision.hpp file to be updated whenever the status of the
# git repository changes. Use the same technique as in GetGitRevisionDescription.cmake
# from https://github.com/rpavlik/cmake-modules


#[=======================================================================[.rst:
Expand Down Expand Up @@ -65,10 +66,8 @@ function(_get_git_dir _start_dir _variable)
get_filename_component(cur_dir "${cur_dir}" DIRECTORY)
if(cur_dir STREQUAL prev_dir OR cur_dir STREQUAL ${_start_dir})
# we are not in git since we either hit root or
# the CMAKE_SOURCE_DIR which should be the top
set(${_variable}
""
PARENT_SCOPE)
# the ${_start_dir} which should be the top
set(${_variable} "" PARENT_SCOPE)
return()
endif()
set(git_dir "${cur_dir}/.git")
Expand Down Expand Up @@ -248,8 +247,6 @@ function(write_version_header dir)

string(TIMESTAMP REVISION_DATE_TIME "%Y-%m-%dT%H:%M:%SZ" UTC)
else()
message(STATUS "no git info available, cannot update version string")

set(REVISION_GIT_TAGREF "")
set(BUILD_NUMBER 0)
set(REVISION_DATE_TIME "")
Expand All @@ -263,6 +260,7 @@ function(write_version_header dir)
set(IDENT_PREFIX "")
endif()

# And finally, write out the header file
file(WRITE "${VERSION_STRING_DATA}/${file_name}.in" [[// This file was generated by VersionString.cmake

#pragma once
Expand All @@ -278,63 +276,73 @@ constexpr const char k@VAR_PREFIX@RevisionDate[] = "@REVISION_DATE_TIME@";
#ifndef VERSION_INFO_DEFINED
#define VERSION_INFO_DEFINED 1

namespace version_info_v1
{

class version_info_base
{
public:
virtual ~version_info_base() = default;

static void write(std::ostream &os, bool verbose)
{
auto s_head = head();
auto &s_head = head();
if (s_head != nullptr)
write(s_head, os, verbose);
}

protected:

struct instance
version_info_base(const char *name, const char *version, int build_number, const char *git_tag, const char *revision_date)
: m_name(name)
, m_version(version)
, m_build_number(build_number)
, m_git_tag(git_tag)
, m_revision_date(revision_date)
{
const char *m_name;
const char *m_version;
int m_build;
const char *m_git_tag;
const char *m_revision_date;

instance *m_next = nullptr;
};
auto &s_head = head();
m_next = s_head;
s_head = this;
}

static void write(const instance *inst, std::ostream &os, bool verbose)
static void write(const version_info_base *inst, std::ostream &os, bool verbose)
{
if (inst->m_next != nullptr)
if (inst->m_next)
{
write(inst->m_next, os, verbose);

if (not verbose)
return;

os << '-' << std::endl;
}

os << inst->m_name << " version " << inst->m_version << std::endl;

if (verbose)
{
if (inst->m_build != 0)
if (inst->m_build_number != 0)
{
os << "build: " << inst->m_build << ' ' << inst->m_revision_date << std::endl;
os << "build: " << inst->m_build_number << ' ' << inst->m_revision_date << std::endl;
if (inst->m_git_tag[0] != 0)
os << "git tag: " << inst->m_git_tag << std::endl;
}
else
os << "No revision information available" << std::endl;
}
}

using instance_ptr = instance *;
using version_info_ptr = version_info_base *;

static instance_ptr &head()
static version_info_ptr &head()
{
static instance_ptr s_head = nullptr;
static version_info_ptr s_head = nullptr;
return s_head;
}

const char *m_name;
const char *m_version;
int m_build_number;
const char *m_git_tag;
const char *m_revision_date;
version_info_base *m_next = nullptr;
};

template<typename T>
Expand All @@ -343,45 +351,43 @@ class version_info : public version_info_base
public:
using implementation_type = T;

version_info(const char *name, const char *version, int build_number, const char *git_tag, const char *revision_date)
: version_info_base(name, version, build_number, git_tag, revision_date)
{
}

struct register_object
{
register_object()
{
auto &s_head = version_info_base::head();
static instance s_next{
implementation_type::name(),
implementation_type::version(),
implementation_type::build_number(),
implementation_type::git_tag(),
implementation_type::revision_date(),
s_head };
s_head = &s_next;
static implementation_type s_instance;
}
};

template<register_object&> struct referrence_object;
template<register_object&> struct reference_object;

static register_object s_registered_object;
static referrence_object<s_registered_object> s_referrenced_object;
static reference_object<s_registered_object> s_referenced_object;
};

template<typename T> typename version_info<T>::register_object version_info<T>::s_registered_object;

}

inline void write_version_string(std::ostream &os, bool verbose)
{
version_info_base::write(os, verbose);
version_info_v1::version_info_base::write(os, verbose);
}

#endif

class version_info_@IDENT_PREFIX@impl : public version_info<version_info_@IDENT_PREFIX@impl>
class version_info_@IDENT_PREFIX@impl : public version_info_v1::version_info<version_info_@IDENT_PREFIX@impl>
{
public:
static constexpr const char *name() { return k@VAR_PREFIX@ProjectName; }
static constexpr const char *version() { return k@VAR_PREFIX@VersionNumber; }
static constexpr int build_number() { return k@VAR_PREFIX@BuildNumber; }
static constexpr const char *git_tag() { return k@VAR_PREFIX@RevisionGitTag; }
static constexpr const char *revision_date() { return k@VAR_PREFIX@RevisionDate; }
version_info_@IDENT_PREFIX@impl()
: version_info(k@VAR_PREFIX@ProjectName, k@VAR_PREFIX@VersionNumber, k@VAR_PREFIX@BuildNumber, k@VAR_PREFIX@RevisionGitTag, k@VAR_PREFIX@RevisionDate)
{
}
};
]])
configure_file("${VERSION_STRING_DATA}/${file_name}.in" "${dir}/${file_name}" @ONLY)
Expand Down

0 comments on commit 9dc5d11

Please sign in to comment.