Skip to content

Commit f94c8f9

Browse files
committed
Use a generated file instead of a macro.
Hopefully this will not trigger to re-build the whole project in case of a version change.
1 parent c957228 commit f94c8f9

8 files changed

Lines changed: 107 additions & 63 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
doc/html/
88
doc/latex/
99
.vscode/settings.json
10+
__pycache__
11+
/lib/utilities/vcs_identifier_gen.hpp

define_vcs_id_macro.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

generate_vcs_identifier.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
generate_vcs_identifier.py
3+
4+
This script generates a source file containing a version control system (VCS)
5+
identifier string. It retrieves the VCS identifier from the 'vcs_utils' module
6+
and writes it to a specified source file.
7+
8+
The script will overwrite the file if it already exists, and any write failures
9+
are handled gracefully.
10+
11+
This script should be loaded by PlatformIO during the project build and is not
12+
designed to be run independently.
13+
"""
14+
15+
from vcs_utils import get_vcs_id, write_vcs_id_to_file
16+
import os.path
17+
Import("env")
18+
19+
# Get the source directory path from PlatformIO configuration
20+
source_dir = env.subst("$PROJECT_DIR")
21+
22+
# Path to the output file within
23+
output_file = os.path.join(source_dir, 'lib', 'utilities', 'vcs_identifier_gen.hpp')
24+
25+
vcs_id = get_vcs_id()
26+
27+
if vcs_id:
28+
write_vcs_id_to_file(vcs_id, output_file)

get_vcs_identifier.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/utilities/version.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#include <optional>
4+
#if __has_include("vcs_identifier_gen.hpp")
5+
#include "vcs_identifier_gen.hpp"
6+
constexpr std::optional<const char *> vcsId = VCS_ID;
7+
#else
8+
constexpr std::optional<const char *> vcsId = std::nullopt;
9+
#endif

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ build_flags =
2222
-DBAUD_RATE=${this.monitor_speed}
2323
monitor_speed = 115200
2424
extra_scripts =
25-
pre:define_vcs_id_macro.py
25+
pre:generate_vcs_identifier.py
2626

2727
[env:native]
2828
platform = native

src/main.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,9 @@ void setup()
1919
{
2020
serial_port::initialize();
2121
serial_port::cout << "\x1b[20h"; // Tell the terminal to use CR/LF for newlines instead of just CR.
22-
23-
constexpr const auto programIdentificationString = []() {
24-
constexpr const auto versionString = getVersionString();
25-
if constexpr (versionString)
26-
{
27-
return versionString.value();
28-
}
29-
else
30-
{
31-
return " compiled at " __DATE__ " " __TIME__;
32-
}
33-
}();
34-
3522
serial_port::cout
3623
<< std::endl
37-
<< " begin program '" << __FILE__ << programIdentificationString << std::endl;
24+
<< " begin program version '" << vcsId.value_or("unknown") << "'" << std::endl;
3825
serial_port::setCallbackForLineReception([](const serial_port::String &commandLine) {
3926
ProtocolHandler::execute(commandLine.c_str());
4027
});

vcs_utils.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
vcs_utils.py
3+
4+
This module provides utility functions to retrieve the version control system
5+
(VCS) identifier using Git and to write it to a C source file.
6+
7+
The functions handle cases where Git is not installed or the current directory
8+
is not a Git repository, and handle file write failures gracefully.
9+
"""
10+
11+
import subprocess
12+
import sys
13+
14+
def get_vcs_id():
15+
"""
16+
Attempts to retrieve the VCS identifier using the 'git describe' command.
17+
18+
Returns:
19+
str: A string containing the VCS identifier if successful, or an empty
20+
string if Git is not available or the directory is not a Git repository.
21+
22+
This function handles the following exceptions:
23+
- subprocess.CalledProcessError: Raised if the 'git' command fails, e.g.,
24+
if the current directory is not a Git repository.
25+
- FileNotFoundError: Raised if 'git' is not installed or not found in the
26+
system's PATH.
27+
28+
All warnings are printed to stderr.
29+
"""
30+
try:
31+
vcs_output = subprocess.run(
32+
["git", "describe", "--always", "--dirty", "--all", "--long"],
33+
stdout=subprocess.PIPE, text=True, check=True
34+
)
35+
vcs_string = vcs_output.stdout.strip()
36+
print("vcs string: " + vcs_string)
37+
return vcs_string
38+
except (subprocess.CalledProcessError, FileNotFoundError) as e:
39+
# Print the warning to stderr
40+
print("Warning: Unable to retrieve VCS description. Error:", str(e), file=sys.stderr)
41+
return ""
42+
43+
44+
def write_vcs_id_to_file(vcs_id, file_path):
45+
"""
46+
Writes the given VCS identifier as a C-style string to a file.
47+
48+
Args:
49+
vcs_id (str): The VCS identifier string to write to the file.
50+
file_path (str): The path to the C source file to write to.
51+
52+
If the file exists, it will be overwritten. If writing to the file fails,
53+
a warning will be printed to stderr, but the script will not terminate
54+
with an error.
55+
"""
56+
try:
57+
# Prepare the C-style string definition
58+
c_content = f'#define VCS_ID "{vcs_id}"\n'
59+
60+
# Write the content to the file (overwriting if it exists)
61+
with open(file_path, 'w') as file:
62+
file.write(c_content)
63+
64+
except IOError as e:
65+
# If writing to the file fails, print the warning to stderr
66+
print(f"Warning: Unable to write VCS ID to file '{file_path}'. Error: {str(e)}", file=sys.stderr)

0 commit comments

Comments
 (0)