Skip to content

Commit

Permalink
Generate version.h during build
Browse files Browse the repository at this point in the history
  • Loading branch information
ataffanel committed Jan 30, 2024
1 parent b029c38 commit 90ccb45
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"timeslot.h": "c",
"button.h": "c",
"ble_hci.h": "c",
"nrf_mbr.h": "c"
"nrf_mbr.h": "c",
"crtp.h": "c",
"systick.h": "c"
},
"cortex-debug.variableUseNaturalFormat": false
}
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ INC_FOLDERS += \
$(SDK_ROOT)/components/ble/ble_services/ble_hrs \
$(SDK_ROOT)/components/libraries/log/src \
$(SDK_ROOT)/components/ble/ble_services/ble_dis \
$(OUTPUT_DIRECTORY) \

# Libraries common to all targets
LIB_FILES += \
Expand Down Expand Up @@ -214,7 +215,7 @@ LDFLAGS += -Wl,--gc-sections
LDFLAGS += --specs=nano.specs -lc -lnosys


.PHONY: $(TARGETS) default all clean help flash flash_softdevice
.PHONY: $(TARGETS) default all clean help flash flash_softdevice version.h

# Default target - first one defined
default: nrf51422_xxaa
Expand Down Expand Up @@ -251,3 +252,9 @@ flash_mbs:

erase:
nrfjprog --eraseall -f nrf52


$(OUTPUT_DIRECTORY)/$(TARGETS)_bootloader.c.o: version.h

version.h:
./tools/build/generateVersionHeader.py --output _build/version.h
3 changes: 1 addition & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ This update binary can be flashed over radio like a normal firmware.
Compiling
---------

To compile arm-none-eabi- tools from https://launchpad.net/gcc-arm-embedded
should be in the path.
To compile you must hase arm-none-eabi- tools in the path, Python 3 and git.

Flashing requires a jlink debug probe and nrfjprog.
``` bash
Expand Down
8 changes: 5 additions & 3 deletions src/bootloader.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include <ble.h>
#include <nrf_soc.h>

#include "version.h"


static char buffer[PAGE_SIZE*BUFFER_PAGES] = {0x42};

Expand Down Expand Up @@ -119,9 +121,9 @@ bool bootloaderProcess(CrtpPacket *packet) {
//memcpy(info->cpuId, cpuidGetId(), CPUID_LEN);
bzero(info->cpuId, CPUID_LEN);
info->version = PROTOCOL_VERSION;
info->version_major = 2024 | 0x0000U;
info->version_minor = 1;
info->version_patch = 0;
info->version_major = VERSION_MAJOR | (VERSION_DIRTY)?0x8000U:0x000;
info->version_minor = VERSION_MINOR;
info->version_patch = VERSION_PATCH;

packet->datalen = 2+sizeof(GetInfoReturns_t);

Expand Down
107 changes: 107 additions & 0 deletions tools/build/generateVersionHeader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env python3

#!/usr/bin/env python

import argparse
import os
import subprocess

version = {}


def extract_information_from_git(base):
revision = (
subprocess.check_output(["git", "-C", base, "rev-parse", "HEAD"])
.decode("utf-8")
.strip()
)

version["revision"] = revision[0:12]
version["irevision0"] = "0x" + revision[0:8]
version["irevision1"] = "0x" + revision[8:12]
version["productionRelease"] = "false"

try:
identify = subprocess.check_output(
["git", "-C", base, "describe", "--abbrev=12", "--tags", "HEAD"]
).decode("utf-8")
identify = identify.split("-")

if len(identify) > 2:
version["local_revision"] = identify[len(identify) - 2]
else:
version["local_revision"] = "0"

version["tag"] = identify[0]
for x in range(1, len(identify) - 2):
version["tag"] += "-"
version["tag"] += identify[x]
except subprocess.CalledProcessError:
# We are maybe running from a shallow tree
version["local_revision"] = "0"
version["tag"] = "NA"

version["tag"] = version["tag"].strip()

if version["local_revision"] != "0":
version["tag"] = version["tag"] + " +" + version["local_revision"]

branch = (
subprocess.check_output(
["git", "-C", base, "rev-parse", "--abbrev-ref", "HEAD"]
)
.decode("utf-8")
.strip()
)
version["branch"] = branch

subprocess.call(["git", "-C", base, "update-index", "-q", "--refresh"])
changes = (
subprocess.check_output(
["git", "-C", base, "diff-index", "--name-only", "HEAD", "--"]
)
.decode("utf-8")
.strip()
)
if len(changes):
version["modified"] = "true"
else:
version["modified"] = "false"


def generate_numeral_version():
vnum = version["tag"].split('+')[0].split('.')

version["major"] = int(vnum[0]) if len(vnum) > 0 and vnum[0] != "NA" else 0
version["minor"] = int(vnum[1]) if len(vnum) > 1 else 0
version["patch"] = int(vnum[2]) if len(vnum) > 2 else 0

version["dirty"] = "true" if version["modified"] == "true" or version["tag"].find('+') >= 0 else "false"


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--crazyflie-base",
help="base folder of the crazyflie firmware",
action="store",
default="./",
)
parser.add_argument(
"--output", help="name of output header", action="store", default="Include/version.h"
)
args = parser.parse_args()

extract_information_from_git(args.crazyflie_base)

generate_numeral_version()

with open(os.path.join(args.crazyflie_base, args.output), 'w') as fd:
fd.writelines(
['#include <stdbool.h>\n',
'\n',
f'#define VERSION_MAJOR {version["major"]}\n',
f'#define VERSION_MINOR {version["minor"]}\n',
f'#define VERSION_PATCH {version["patch"]}\n',
f'#define VERSION_DIRTY {version["dirty"]}\n',]
)

0 comments on commit 90ccb45

Please sign in to comment.