Skip to content

Commit 774af4f

Browse files
committed
versions string, again
1 parent 963112d commit 774af4f

File tree

2 files changed

+53
-54
lines changed

2 files changed

+53
-54
lines changed

cmake/VersionString.cmake

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ cmake_minimum_required(VERSION 3.15)
3939
4040
write_version_header(<destdir>
4141
[FILE_NAME <file-name>]
42-
[VAR_PREFIX <prefix>]
42+
[LIB_NAME <library-name>]
4343
)
4444
4545
This command will generate the code to write a file name
@@ -48,9 +48,9 @@ cmake_minimum_required(VERSION 3.15)
4848
``FILE_NAME``
4949
Specify the name of the file to create, default is ``revision.hpp``.
5050
51-
``VAR_PREFIX``
52-
Specify a prefix part for the variables contained in the revision
53-
file.
51+
``LIB_NAME``
52+
Specify the library name which will be used as a prefix part for the
53+
variables contained in the revision file.
5454
#]=======================================================================]
5555

5656
# First locate a .git file or directory.
@@ -199,7 +199,7 @@ endfunction()
199199
function(write_version_header dir)
200200

201201
set(flags )
202-
set(options VAR_PREFIX FILE_NAME)
202+
set(options LIB_NAME FILE_NAME)
203203
set(sources )
204204
cmake_parse_arguments(VERSION_STRING_OPTION "${flags}" "${options}" "${sources}" ${ARGN})
205205

@@ -255,73 +255,79 @@ function(write_version_header dir)
255255
set(REVISION_DATE_TIME "")
256256
endif()
257257

258-
if(VERSION_STRING_OPTION_VAR_PREFIX)
259-
set(VAR_PREFIX "${VERSION_STRING_OPTION_VAR_PREFIX}")
258+
if(VERSION_STRING_OPTION_LIB_NAME)
259+
set(VAR_PREFIX "${VERSION_STRING_OPTION_LIB_NAME}")
260+
set(IDENT_PREFIX "${VERSION_STRING_OPTION_LIB_NAME}_")
261+
else()
262+
set(VAR_PREFIX "")
263+
set(IDENT_PREFIX "")
260264
endif()
261265

262-
file(WRITE "${VERSION_STRING_DATA}/${file_name}.in" [[
263-
264-
265-
// Generated revision file
266+
file(WRITE "${VERSION_STRING_DATA}/${file_name}.in" [[// This file was generated by VersionString.cmake
266267
267268
#pragma once
268269
269270
#include <ostream>
270271
271272
constexpr const char k@VAR_PREFIX@ProjectName[] = "@PROJECT_NAME@";
272273
constexpr const char k@VAR_PREFIX@VersionNumber[] = "@PROJECT_VERSION@";
273-
constexpr const char k@VAR_PREFIX@RevisionGitTag[] = "@REVISION_GIT_TAGREF@";
274274
constexpr int k@VAR_PREFIX@BuildNumber = @BUILD_NUMBER@;
275+
constexpr const char k@VAR_PREFIX@RevisionGitTag[] = "@REVISION_GIT_TAGREF@";
275276
constexpr const char k@VAR_PREFIX@RevisionDate[] = "@REVISION_DATE_TIME@";
276277
277278
#ifndef VERSION_INFO_DEFINED
278279
#define VERSION_INFO_DEFINED 1
279280
280-
#define CONCAT(a, b, c) a_ ## b ## c
281-
#define CREATE_INSTANCE_NAME(prefix) CONCAT(k, prefix, instance)
282-
#define CREATE_CLASS_NAME(prefix) CONCAT(version_info_, prefix, impl)
283-
284281
class version_info_base
285282
{
286-
public:
283+
public:
287284
virtual ~version_info_base() = default;
288285
289286
static void write(std::ostream &os, bool verbose)
290287
{
291-
for (auto inst = head(); inst != nullptr; inst = inst->m_next)
292-
{
293-
os << inst->m_name << " version " << inst->m_version << std::endl;
294-
295-
if (verbose)
296-
{
297-
if (inst->m_build != 0)
298-
{
299-
os << "build: " << inst->m_build << ' ' << inst->m_revision_date << std::endl;
300-
if (inst->m_git_tag[0] != 0)
301-
os << "git tag: " << inst->m_git_tag << std::endl;
302-
}
303-
else
304-
os << "No revision information available" << std::endl;
305-
}
306-
307-
if (inst->m_next != nullptr)
308-
os << std::endl;
309-
}
288+
auto s_head = head();
289+
if (s_head != nullptr)
290+
write(s_head, os, verbose);
310291
}
311292
312-
protected:
293+
protected:
313294
314295
struct instance
315296
{
316297
const char *m_name;
317298
const char *m_version;
318-
const char *m_git_tag;
319299
int m_build;
300+
const char *m_git_tag;
320301
const char *m_revision_date;
321302
322303
instance *m_next = nullptr;
323304
};
324305
306+
static void write(const instance *inst, std::ostream &os, bool verbose)
307+
{
308+
if (inst->m_next != nullptr)
309+
{
310+
write(inst->m_next, os, verbose);
311+
if (not verbose)
312+
return;
313+
os << '-' << std::endl;
314+
}
315+
316+
os << inst->m_name << " version " << inst->m_version << std::endl;
317+
318+
if (verbose)
319+
{
320+
if (inst->m_build != 0)
321+
{
322+
os << "build: " << inst->m_build << ' ' << inst->m_revision_date << std::endl;
323+
if (inst->m_git_tag[0] != 0)
324+
os << "git tag: " << inst->m_git_tag << std::endl;
325+
}
326+
else
327+
os << "No revision information available" << std::endl;
328+
}
329+
}
330+
325331
using instance_ptr = instance *;
326332
327333
static instance_ptr &head()
@@ -334,19 +340,18 @@ class version_info_base
334340
template<typename T>
335341
class version_info : public version_info_base
336342
{
337-
public:
338-
343+
public:
339344
using implementation_type = T;
340345
341-
protected:
346+
protected:
342347
version_info()
343348
{
344349
auto &s_head = head();
345350
static instance s_next{
346351
implementation_type::name(),
347352
implementation_type::version(),
348-
implementation_type::git_tag(),
349353
implementation_type::build_number(),
354+
implementation_type::git_tag(),
350355
implementation_type::revision_date(),
351356
s_head };
352357
s_head = &s_next;
@@ -360,22 +365,16 @@ inline void write_version_string(std::ostream &os, bool verbose)
360365
361366
#endif
362367
363-
#define INSTANCE_NAME CREATE_INSTANCE_NAME(VAR_PREFIX)
364-
#define CLASS_NAME CREATE_CLASS_NAME(VAR_PREFIX)
365-
366-
class CLASS_NAME : public version_info<CLASS_NAME>
368+
class version_info_@IDENT_PREFIX@impl : public version_info<version_info_@IDENT_PREFIX@impl>
367369
{
368-
public:
369-
370+
public:
370371
static constexpr const char *name() { return k@VAR_PREFIX@ProjectName; }
371372
static constexpr const char *version() { return k@VAR_PREFIX@VersionNumber; }
372-
static constexpr const char *git_tag() { return k@VAR_PREFIX@RevisionGitTag; }
373373
static constexpr int build_number() { return k@VAR_PREFIX@BuildNumber; }
374+
static constexpr const char *git_tag() { return k@VAR_PREFIX@RevisionGitTag; }
374375
static constexpr const char *revision_date() { return k@VAR_PREFIX@RevisionDate; }
375-
} INSTANCE_NAME;
376-
377-
378-
]])
376+
} s_@IDENT_PREFIX@instance;
377+
]])
379378
configure_file("${VERSION_STRING_DATA}/${file_name}.in" "${dir}/${file_name}" @ONLY)
380379
endfunction()
381380

0 commit comments

Comments
 (0)