Skip to content

Commit 541d9ee

Browse files
committed
Added support for git information in GET_INFO
This is disabled by default and must be enabled by passing ENABLE_GIT_VERSION_INFO=1 to make. When enabled, it requires git and python to be installed. By default, make expects both a "git" and a "python" binary to be present in the PATH. If this is not the case, the locations and names of these binaries can instead be supplied by setting the GIT and PYTHON variables accordingly.
1 parent de86888 commit 541d9ee

File tree

5 files changed

+167
-6
lines changed

5 files changed

+167
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/_build*/
22
deploy_*.sh
33
*~
4+
GitInfo.h
45

56
# Created by https://www.toptal.com/developers/gitignore/api/visualstudio,c,linux,windows,macos,eclipse
67
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudio,c,linux,windows,macos,eclipse

Makefile

+43-6
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,46 @@ else ifeq ($(DEVICE),LandungsbrueckeV3)
285285
endif
286286
_LDFLAGS += -specs=nosys.specs
287287
else
288-
ifeq (,$(filter clean,$(MAKECMDGOALS)))
289-
$(error You need to set the DEVICE parameter to "Landungsbruecke", "LandungsbrueckeSmall" or "LandungsbrueckeV3". When calling make directly, do this by adding DEVICE=Landungsbruecke, DEVICE=LandungsbrueckeV3 or DEVICE=LandungsbrueckeSmall to the commandline)
290-
endif
288+
ifneq (,$(filter clean,$(MAKECMDGOALS)))
289+
else ifneq (,$(filter codegen,$(MAKECMDGOALS)))
290+
else
291+
$(error You need to set the DEVICE parameter to "Landungsbruecke", "LandungsbrueckeSmall" or "LandungsbrueckeV3". When calling make directly, do this by adding DEVICE=Landungsbruecke, DEVICE=LandungsbrueckeV3 or DEVICE=LandungsbrueckeSmall to the commandline)
292+
endif
291293
endif
292294

295+
### Git info for build image ###################################################
296+
# Output file location
297+
GIT_INFO_FILE := ./GitInfo.h
298+
299+
# Command & script definitions
300+
GIT ?= git
301+
PYTHON ?= python
302+
GIT_INFO_TOOL ?= tools/generators/generate_git_info.py
303+
GIT_INFO_GENERATOR_CMD := $(PYTHON) $(GIT_INFO_TOOL)
304+
305+
# Git info retrieval - off by default
306+
ENABLE_GIT_VERSION_INFO ?= 0
307+
ifneq ($(ENABLE_GIT_VERSION_INFO),0)
308+
GIT_HASH := $(shell $(GIT) rev-parse --revs-only --short=7 HEAD)
309+
GIT_DIRTY_FLAG := $(shell $(GIT) diff --quiet && echo "0" || echo "1")
310+
$(info Building git commit $(GIT_HASH)$(if $(filter $(GIT_DIRTY_FLAG),1),-dirty))
311+
312+
# Git info file generation
313+
$(GIT_INFO_FILE): force
314+
$(GIT_INFO_GENERATOR_CMD) $(GIT_HASH) $(GIT_DIRTY_FLAG) --output $(GIT_INFO_FILE)
315+
316+
else
317+
# Git info file generation - Copy the placeholder file
318+
GIT_PLACEHOLDER_FILE := tools/generators/blank_git_info.h
319+
$(GIT_INFO_FILE): force
320+
$(info Copying placeholder git info file: $(GIT_PLACEHOLDER_FILE) -> $(GIT_INFO_FILE))
321+
cp $(GIT_PLACEHOLDER_FILE) $(GIT_INFO_FILE)
322+
endif
323+
324+
325+
# Add the file to the list of autogenerated files
326+
AUTOGENERATED_FILES += $(GIT_INFO_FILE)
327+
293328
# System and hardware abstraction layer
294329
SRC += $(TMC_HAL_SRC)/tmc/SysTick.c
295330
SRC += $(TMC_HAL_SRC)/tmc/IOs.c
@@ -553,13 +588,15 @@ DEPFILES = $(addprefix $(DEP_DIR)/, $(addsuffix .o.d, $(ALLSRCBASE)))
553588

554589
### Make targets ###
555590
# Default target. Build everything
556-
all: begin gccversion build size end
591+
all: codegen begin gccversion build size end
557592

558593
# Target: clean project.
559594
clean: begin clean_list end
560595

596+
# Target: Generate autogenerated files
597+
codegen: $(AUTOGENERATED_FILES)
561598
# Helper targets: This makes selecting all output types easier (see build target below)
562-
elf: $(DEVICE_DIR)/$(TARGET).elf
599+
elf: $(DEVICE_DIR)/$(TARGET).elf codegen
563600
lss: $(DEVICE_DIR)/$(TARGET).lss
564601
sym: $(DEVICE_DIR)/$(TARGET).sym
565602
hex: $(DEVICE_DIR)/$(TARGET).hex
@@ -712,4 +749,4 @@ force:
712749

713750
# Listing of phony targets.
714751
.PHONY : all begin end size gccversion \
715-
build elf hex bin lss sym clean clean_list program tmclfwupload
752+
build codegen elf hex bin lss sym clean clean_list program tmclfwupload

tmc/TMCL.c

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99

1010
#include "TMCL.h"
11+
12+
#include "GitInfo.h"
13+
1114
#include "BoardAssignment.h"
1215
#include "hal/derivative.h"
1316
#include "IdDetection.h"
@@ -972,6 +975,17 @@ static void handleGetInfo(void)
972975
ActualReply.Value.UInt32 = 12;
973976
break;
974977

978+
case 30: // Git info
979+
if (GIT_VERSION_INFO == 0xFFFFFFFF)
980+
{
981+
// Illegal GIT_VERSION_INFO value -> Git info is disabled
982+
ActualReply.Status = REPLY_INVALID_TYPE;
983+
break;
984+
}
985+
986+
ActualReply.Value.Int32 = GIT_VERSION_INFO;
987+
break;
988+
975989
case 200: // DeviceSpecificArea: Patch version
976990
ActualReply.Value.UInt32 = VERSION_PATCH;
977991
break;

tools/generators/blank_git_info.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*******************************************************************************
2+
* Copyright © 2025 Analog Devices Inc. All Rights Reserved. This software is
3+
* proprietary & confidential to Analog Devices, Inc. and its licensors.
4+
*******************************************************************************/
5+
/*
6+
* This is an empty placeholder file. It is copied by make if git version
7+
* information is disabled.
8+
*
9+
* To enable git version information in the build, you must:
10+
* - Have git installed and available - Either:
11+
* - Have "git" in your PATH for the Makefile to call
12+
* - Set the $GIT environment variable for the Makefile to use instead of "git"
13+
* - Have python 3 installed and available - either:
14+
* - Have "python" in your PATH for the Makefile to call
15+
* - Set the $PYTHON environment variable for the Makefile to use instead of "python"
16+
* - Pass ENABLE_GIT_VERSION_INFO=1 to make
17+
*/
18+
19+
#ifndef GIT_INFO_H_
20+
#define GIT_INFO_H_
21+
22+
// This value is invalid - it is used by C code to identify that
23+
// no git information is available.
24+
#define GIT_VERSION_INFO 0xFFFFFFFF
25+
26+
#endif /* GIT_INFO_H_ */

tools/generators/generate_git_info.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
################################################################################
2+
# Copyright © 2025 Analog Devices Inc. All Rights Reserved. This software is
3+
# proprietary & confidential to Analog Devices, Inc. and its licensors.
4+
################################################################################
5+
"""
6+
This script generates a C header containing git information.
7+
8+
This header then is used by the C firmware build to make the git version control
9+
information available in the resulting binary.
10+
11+
To enable git version information in the build, you must:
12+
- Have git installed and available - Either:
13+
- Have "git" in your PATH for the Makefile to call
14+
- Set the $GIT environment variable for the Makefile to use instead of "git"
15+
- Have python 3 installed and available - either:
16+
- Have "python" in your PATH for the Makefile to call
17+
- Set the $PYTHON environment variable for the Makefile to use instead of "python"
18+
- Pass ENABLE_GIT_VERSION_INFO=1 to make
19+
"""
20+
import argparse
21+
from datetime import datetime
22+
import sys
23+
24+
header_file_template = """
25+
/*******************************************************************************
26+
* Copyright © $CURRENT_YEAR Analog Devices Inc. All Rights Reserved. This software is
27+
* proprietary & confidential to Analog Devices, Inc. and its licensors.
28+
*******************************************************************************/
29+
/*
30+
* This code was generated, do not edit this file manually!
31+
*/
32+
33+
#ifndef GIT_INFO_H_
34+
#define GIT_INFO_H_
35+
36+
#define GIT_HASH $GIT_HASH
37+
#define GIT_DIRTY_FLAG $GIT_DIRTY_FLAG
38+
#define GIT_VERSION_INFO ((GIT_HASH & 0x0FFFFFFF) | (GIT_DIRTY_FLAG << 28))
39+
40+
#endif /* GIT_INFO_H_ */
41+
"""
42+
43+
44+
45+
def main():
46+
parser = argparse.ArgumentParser(
47+
description="Generate a header file with git hash and dirty flag defined")
48+
49+
parser.add_argument("git_hash", type=lambda x: int(x, 16))
50+
parser.add_argument("git_dirty", type=int)
51+
parser.add_argument("--output", default="src/GitInfo.h")
52+
53+
args = parser.parse_args()
54+
55+
# Sanity check inputs
56+
if not (type(args.git_hash) == int and 0 <= args.git_hash <= 0x0FFF_FFFF):
57+
print("Error: Git hash is not a seven digit hex number")
58+
sys.exit(1)
59+
if not (type(args.git_dirty) == int and args.git_dirty in [0, 1]):
60+
print("Error: Git dirty flag is neither 0 nor 1")
61+
sys.exit(1)
62+
63+
# Output generation
64+
output_text = header_file_template.lstrip()
65+
output_text = output_text.replace("$CURRENT_YEAR", datetime.now().strftime("%Y"))
66+
output_text = output_text.replace("$GIT_HASH", f"0x{args.git_hash:08X}")
67+
output_text = output_text.replace("$GIT_DIRTY_FLAG", f"{args.git_dirty}")
68+
69+
# Check whether the generated result matches the existing result
70+
try:
71+
with open(args.output, "rt", encoding="utf-8") as f:
72+
if f.read() == output_text:
73+
print(f"Skipping generation of {args.output} file - no changes")
74+
sys.exit(0)
75+
except OSError:
76+
pass
77+
78+
with open(args.output, "wt", encoding="utf-8") as f:
79+
print(f"Generating {args.output}")
80+
f.write(output_text)
81+
82+
if __name__ == "__main__":
83+
main()

0 commit comments

Comments
 (0)