Skip to content

Commit d7dd307

Browse files
author
Marcos Del Sol Vives
committed
Add versioning functions
1 parent 17d62a4 commit d7dd307

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
*.o
1111
/amiitool
1212
/amiitool.exe
13+
14+
# Ignore version files
15+
/gitversion.h

Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ MBEDTLS_DIR = $(PWD)/mbedtls
2323
MBEDTLS_CONFIG = $(PWD)/configs/mbedtls.h
2424
MBEDTLS_CFLAGS = -DMBEDTLS_CONFIG_FILE='\"$(MBEDTLS_CONFIG)\"' $(CFLAGS)
2525

26-
HEADERS := $(wildcard *.h)
26+
HEADERS := $(wildcard *.h) gitversion.h
2727
OBJECTS := $(patsubst %.c,%.o,$(wildcard *.c))
2828
LIBSOBJ := $(filter-out $(BINS:%=%.o),$(OBJECTS))
2929

@@ -45,6 +45,10 @@ all: $(BINS)
4545
%.o: %.c $(HEADERS)
4646
$(CC) $(ALL_CFLAGS) -c $< -o $@
4747

48+
gitversion.h:
49+
echo "#define GIT_COMMIT_ID 0x`git rev-parse HEAD | head -c8`" > $(PWD)/gitversion.h
50+
echo "#define GIT_COMMIT_COUNT `git rev-list --count --all`" >> $(PWD)/gitversion.h
51+
4852
# Static mbed TLS
4953
mbedtls: $(MBEDTLS_CONFIG)
5054
"$(MAKE)" lib -C $(MBEDTLS_DIR) CFLAGS="$(MBEDTLS_CFLAGS)"
@@ -54,7 +58,7 @@ clean: mostlyclean
5458
$(MAKE) -C $(MBEDTLS_DIR) clean
5559

5660
mostlyclean:
57-
$(RM) $(OBJECTS) $(BINS)
61+
$(RM) $(OBJECTS) $(BINS) gitversion.h
5862

5963
# Install
6064
install: $(BINS:%=install_%)

amiitool.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* SPDX-License-Identifier: MIT
66
*/
77

8-
#include "nfc3d/amiibo.h"
8+
#include <nfc3d/amiibo.h>
9+
#include <nfc3d/version.h>
910
#include "util.h"
1011
#include <stdio.h>
1112
#include <string.h>
@@ -18,7 +19,7 @@ static char * self;
1819

1920
void usage() {
2021
fprintf(stderr,
21-
"amiitool\n"
22+
"amiitool v%s (commit %08x)\n"
2223
"by Marcos Del Sol Vives <[email protected]>\n"
2324
"\n"
2425
"Usage: %s (-e|-d) -k keyfile [-i input] [-o output]\n"
@@ -28,7 +29,7 @@ void usage() {
2829
" -i input file. If not specified, stdin will be used.\n"
2930
" -o output file. If not specified, stdout will be used.\n"
3031
" -l decrypt files with invalid signatures.\n",
31-
self
32+
nfc3d_version(), nfc3d_commit_id(), self
3233
);
3334
}
3435

include/nfc3d/version.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* (c) 2017 Marcos Del Sol Vives
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#ifndef HAVE_NFC3D_VERSION_H
8+
#define HAVE_NFC3D_VERSION_H
9+
10+
#include <stdint.h>
11+
12+
const char * nfc3d_version();
13+
uint32_t nfc3d_version_code();
14+
uint32_t nfc3d_commit_id();
15+
uint32_t nfc3d_commit_count();
16+
17+
#endif

version.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* (c) 2015-2017 Marcos Del Sol Vives
3+
* (c) 2016 javiMaD
4+
*
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
8+
#include <nfc3d/version.h>
9+
#include <stdio.h>
10+
#include "gitversion.h"
11+
12+
static char vstr[16] = { 0 };
13+
14+
const char * nfc3d_version() {
15+
if (vstr[0]) {
16+
return vstr;
17+
}
18+
19+
uint32_t version = nfc3d_version_code();
20+
uint32_t major = version >> 24;
21+
uint32_t minor = version >> 12 & 0xFFF;
22+
uint32_t revision = version & 0xFFF;
23+
24+
snprintf(vstr, sizeof(vstr) - 1, "%x.%x.%x", major, minor, revision);
25+
return vstr;
26+
}
27+
28+
uint32_t nfc3d_version_code() {
29+
// TODO: can we get this from Git tags somehow?
30+
return 0x01000000;
31+
}
32+
33+
uint32_t nfc3d_commit_id() {
34+
return GIT_COMMIT_ID;
35+
}
36+
37+
uint32_t nfc3d_commit_count() {
38+
return GIT_COMMIT_COUNT;
39+
}

0 commit comments

Comments
 (0)