Makefile: display firmware size
[RRG-proxmark3.git] / tools / mkversion.sh
blobf4dd350d6d69555f3a6140ef9490164b33b412d6
1 #!/usr/bin/env sh
3 if [ "$1" = "--help" ] || [ "$1" = "-h" ] || [ "$1" = "" ]; then
4 echo "To report a short string about the current version:"
5 echo " $0 --short"
6 echo "To regenerate version_pm3.c if needed:"
7 echo " $0 [--force] [--undecided] path/to/version_pm3.c"
8 exit 0
9 fi
11 # Output a version_pm3.c file that includes information about the current build
12 # From mkversion.pl
13 # pure sh POSIX as now even on Windows we use WSL or ProxSpace with sh available
15 # Clear environment locale so that git will not use localized strings
16 export LC_ALL="C"
17 export LANG="C"
19 SHORT=false
20 if [ "$1" = "--short" ]; then
21 SHORT=true
22 shift
24 FORCE=false
25 if [ "$1" = "--force" ]; then
26 FORCE=true
27 shift
29 UNDECIDED=false
30 if [ "$1" = "--undecided" ]; then
31 UNDECIDED=true
32 shift
34 VERSIONSRC="$1"
36 if ! $SHORT && [ "$VERSIONSRC" = "" ]; then
37 echo "Error: $0 is missing its destination filename"
38 exit 1
41 if $SHORT && [ "$VERSIONSRC" != "" ]; then
42 echo "Error: can't output a short string and generate file at the same time"
43 exit 1
46 # if you are making your own fork, change this line to reflect your fork-name
47 fullgitinfo="Iceman"
48 # GIT status 0 = dirty, 1 = clean , 2 = undecided
49 clean=2
51 # Do we have access to git command?
52 commandGIT=$(env git)
54 if [ "$commandGIT" != "" ]; then
56 # now avoiding the "fatal: No names found, cannot describe anything." error by fallbacking to abbrev hash in such case
57 gitversion=$(git describe --dirty --always)
58 gitbranch=$(git rev-parse --abbrev-ref HEAD)
59 if $UNDECIDED; then
60 if [ "$gitversion" != "${gitversion%-dirty}" ]; then
61 clean=0
62 else
63 clean=1
66 if [ "$gitbranch" != "" ] && [ "$gitversion" != "" ]; then
67 fullgitinfo="${fullgitinfo}/${gitbranch}/${gitversion}"
68 # if FORCED_DATE present and properly formatted:
69 case "$FORCED_DATE" in
70 [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]" "[0-9][0-9]:[0-9][0-9]:[0-9][0-9])
71 ctime="$FORCED_DATE"
74 ctime="$(date '+%Y-%m-%d %H:%M:%S')"
76 esac
77 else
78 fullgitinfo="${fullgitinfo}/master/release (git)"
80 else
81 fullgitinfo="${fullgitinfo}/master/release (no_git)"
82 dl_time=$(stat --printf="%y" README.md)
83 # POSIX way...
84 ctime=${dl_time%.*}
86 if $SHORT; then
87 echo "$fullgitinfo"
88 exit 0
91 # Crop so it fits within 50 characters C string, so max 49 chars
92 # POSIX way
93 fullgitinfoextra="${fullgitinfo#??????????????????????????????????????????????}"
94 if [ "$fullgitinfoextra" != "$fullgitinfo" ]; then
95 fullgitinfo46="${fullgitinfo%"${fullgitinfoextra}"}"
96 fullgitinfo="${fullgitinfo46}..."
98 sha=$(
99 pm3path=$(dirname -- "$0")/..
100 cd "$pm3path" || return
101 # did we find the src?
102 [ -f armsrc/appmain.c ] || return
103 if [ "${OSTYPE#darwin}" != "$OSTYPE" ]; then
104 # macOS
105 ls armsrc/*.[ch] common_arm/*.[ch]|grep -E -v "(disabled|version_pm3|fpga_version_info)"|sort|xargs shasum -a 256 -t|shasum -a 256|cut -c -9
106 else
107 ls armsrc/*.[ch] common_arm/*.[ch]|grep -E -v "(disabled|version_pm3|fpga_version_info)"|sort|xargs sha256sum -t|sha256sum|cut -c -9
110 if [ "$sha" = "" ]; then
111 sha="no sha256"
114 REDO=true
115 if ! $FORCE && [ -f "$VERSIONSRC" ]; then
116 # version src file exists, check if it needs to be updated
117 # file parser quite fragile, be careful if you edit VERSIONSRC template below...
118 oldclean=$(sed '13s/.*\([0-9]\+\).*/\1/;13!d' "$VERSIONSRC")
119 oldfullgitinfo=$(sed '14s/.*"\([^"]*\)".*/\1/;14!d' "$VERSIONSRC")
120 oldsha=$(sed '16s/.*"\([^"]*\)".*/\1/;16!d' "$VERSIONSRC")
121 if [ "$oldclean" = "$clean" ] && [ "$oldfullgitinfo" = "$fullgitinfo" ] && [ "$oldsha" = "$sha" ]; then
122 REDO=false
125 if $REDO; then
126 # use a tmp file to avoid concurrent call to mkversion to parse a half-written file.
127 cat > "${VERSIONSRC}.tmp" <<EOF
128 #include "common.h"
129 /* Generated file, do not edit */
130 #ifndef ON_DEVICE
131 #define SECTVERSINFO
132 #else
133 #define SECTVERSINFO __attribute__((section(".version_information")))
134 #endif
136 const struct version_information_t SECTVERSINFO g_version_information = {
137 VERSION_INFORMATION_MAGIC,
140 $clean,
141 "$fullgitinfo",
142 "$ctime",
143 "$sha"
146 mv "${VERSIONSRC}.tmp" "${VERSIONSRC}"