@@ -39,7 +39,7 @@ cmake_minimum_required(VERSION 3.15)
39
39
40
40
write_version_header(<destdir>
41
41
[FILE_NAME <file-name>]
42
- [VAR_PREFIX <prefix >]
42
+ [LIB_NAME <library-name >]
43
43
)
44
44
45
45
This command will generate the code to write a file name
@@ -48,9 +48,9 @@ cmake_minimum_required(VERSION 3.15)
48
48
``FILE_NAME``
49
49
Specify the name of the file to create, default is ``revision.hpp``.
50
50
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.
54
54
#]=======================================================================]
55
55
56
56
# First locate a .git file or directory.
@@ -199,7 +199,7 @@ endfunction()
199
199
function (write_version_header dir )
200
200
201
201
set (flags )
202
- set (options VAR_PREFIX FILE_NAME )
202
+ set (options LIB_NAME FILE_NAME )
203
203
set (sources )
204
204
cmake_parse_arguments (VERSION_STRING_OPTION "${flags} " "${options} " "${sources} " ${ARGN} )
205
205
@@ -255,73 +255,79 @@ function(write_version_header dir)
255
255
set (REVISION_DATE_TIME "" )
256
256
endif ()
257
257
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 "" )
260
264
endif ()
261
265
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
266
267
267
268
#pragma once
268
269
269
270
#include <ostream>
270
271
271
272
constexpr const char k@VAR_PREFIX@ProjectName[] = "@PROJECT_NAME@";
272
273
constexpr const char k@VAR_PREFIX@VersionNumber[] = "@PROJECT_VERSION@";
273
- constexpr const char k@VAR_PREFIX@RevisionGitTag[] = "@REVISION_GIT_TAGREF@";
274
274
constexpr int k@VAR_PREFIX@BuildNumber = @BUILD_NUMBER@;
275
+ constexpr const char k@VAR_PREFIX@RevisionGitTag[] = "@REVISION_GIT_TAGREF@";
275
276
constexpr const char k@VAR_PREFIX@RevisionDate[] = "@REVISION_DATE_TIME@";
276
277
277
278
#ifndef VERSION_INFO_DEFINED
278
279
#define VERSION_INFO_DEFINED 1
279
280
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
-
284
281
class version_info_base
285
282
{
286
- public:
283
+ public:
287
284
virtual ~version_info_base() = default;
288
285
289
286
static void write(std::ostream &os, bool verbose)
290
287
{
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);
310
291
}
311
292
312
- protected:
293
+ protected:
313
294
314
295
struct instance
315
296
{
316
297
const char *m_name;
317
298
const char *m_version;
318
- const char *m_git_tag;
319
299
int m_build;
300
+ const char *m_git_tag;
320
301
const char *m_revision_date;
321
302
322
303
instance *m_next = nullptr;
323
304
};
324
305
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
+
325
331
using instance_ptr = instance *;
326
332
327
333
static instance_ptr &head()
@@ -334,19 +340,18 @@ class version_info_base
334
340
template<typename T>
335
341
class version_info : public version_info_base
336
342
{
337
- public:
338
-
343
+ public:
339
344
using implementation_type = T;
340
345
341
- protected:
346
+ protected:
342
347
version_info()
343
348
{
344
349
auto &s_head = head();
345
350
static instance s_next{
346
351
implementation_type::name(),
347
352
implementation_type::version(),
348
- implementation_type::git_tag(),
349
353
implementation_type::build_number(),
354
+ implementation_type::git_tag(),
350
355
implementation_type::revision_date(),
351
356
s_head };
352
357
s_head = &s_next;
@@ -360,22 +365,16 @@ inline void write_version_string(std::ostream &os, bool verbose)
360
365
361
366
#endif
362
367
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>
367
369
{
368
- public:
369
-
370
+ public:
370
371
static constexpr const char *name() { return k@VAR_PREFIX@ProjectName; }
371
372
static constexpr const char *version() { return k@VAR_PREFIX@VersionNumber; }
372
- static constexpr const char *git_tag() { return k@VAR_PREFIX@RevisionGitTag; }
373
373
static constexpr int build_number() { return k@VAR_PREFIX@BuildNumber; }
374
+ static constexpr const char *git_tag() { return k@VAR_PREFIX@RevisionGitTag; }
374
375
static constexpr const char *revision_date() { return k@VAR_PREFIX@RevisionDate; }
375
- } INSTANCE_NAME;
376
-
377
-
378
- ]] )
376
+ } s_@IDENT_PREFIX@instance;
377
+ ]] )
379
378
configure_file ("${VERSION_STRING_DATA} /${file_name} .in" "${dir} /${file_name} " @ONLY )
380
379
endfunction ()
381
380
0 commit comments