Skip to content

Commit

Permalink
Merge pull request #5 from AlexFabre/feat/add-zephyr-compatibility
Browse files Browse the repository at this point in the history
Add zephyr compatibility
  • Loading branch information
AlexFabre authored Oct 29, 2024
2 parents 5bf50f6 + 6135c85 commit 439a8b3
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 90 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Generated file (base format)
version.h

# Generated file (Zephyr format)
VERSION
74 changes: 51 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Works by parsing the `git describe` command to retrieve all information.
## Compatibility

- [x] C/C++ project
- [ ] Zephyr project
- [x] Zephyr project

## Requirements

Expand All @@ -22,61 +22,89 @@ All available options can be listed with option `-h`

~~~sh
$ ./rever.sh -h
==> rever.sh 0.4.1
==> rever.sh 0.5.0
A little POSIX shell script to generate
version information for your C project
version information for your C project.
ref: https://github.com/AlexFabre/rever
Usage:
rever.sh [options]
-o <output file path>
-f <tag format> (default 'v')
-h <help>
-v <script version>
-l <script logs> (default none)
rever.sh <options>
-f <project format>
-f zephyr: Generate a 'VERSION' file, specific to zephyr projects
-f base: (default) Generate the basic rever format file
-o <output file>
-t <git tag format> (default 'v')
-h <help>
-v <script version>
-l <script logs> (default none)
~~~

### Example of generated header
### Example of generated header for C/C++ project

~~~c
/**
* @file version.h
* @brief version information of project build
*
* Generated with rever.sh 0.4.1
* Generated with rever.sh 0.5.0
* A little POSIX shell script to generate
* version information for your C project
* version information for your C project.
* ref: https://github.com/AlexFabre/rever
*
* @warning Do not edit this file manually. Its content
* Do not edit this file manually. Its content
* is generated with rever.sh script.
*/
#ifndef VERSION_H__
#define VERSION_H__

/* Project version */
#define VERSION_MAJOR 0
#define VERSION_MINOR 4
#define VERSION_PATCH 1
#define VERSION_MINOR 5
#define VERSION_PATCH 0

/* Git repo info */
#define VERSION_BRANCH_NAME "wip/documentation"
#define VERSION_NB_COMMITS_SINCE_LAST_TAG 0
#define VERSION_COMMIT_SHORT_SHA "4850f52"
#define VERSION_BRANCH_NAME "feat/add-zephyr-compatibility"
#define VERSION_NB_COMMITS_SINCE_LAST_TAG 6
#define VERSION_COMMIT_SHORT_SHA "08df77b"

/* Build date time (UTC) */
#define VERSION_BUILD_DAY 16
#define VERSION_BUILD_MONTH 8
#define VERSION_BUILD_DAY 29
#define VERSION_BUILD_MONTH 10
#define VERSION_BUILD_YEAR 2024
#define VERSION_BUILD_HOUR 8
#define VERSION_BUILD_HOUR 11

#endif /* VERSION_H__ */

~~~

### Example of generated header for Zephyr project

~~~txt
# This file declares the firmware revision information
# both for cmake and for mcuboot
#
# ref: https://docs.zephyrproject.org/latest/build/version/index.html
#
# Generated with rever.sh 0.5.0
# A little POSIX shell script to generate
# version information for your C project.
# ref: https://github.com/AlexFabre/rever
#
# Do not edit this file manually. Its content
# is generated with rever.sh script.
VERSION_MAJOR = 0
VERSION_MINOR = 5
PATCHLEVEL = 0
VERSION_TWEAK = 6
EXTRAVERSION = "feat/add-zephyr-compatibility08df77b"
~~~

## Code quality
- Shellcheck
- Codespell

## Improvements...

* Path handling in `file_path_checker` function: The function currently assumes that the provided path always ends with a filename. If the input path doesn't have a filename or extension, it defaults to `version.h`. This could lead to incorrect behavior in some cases. Consider validating the input path and handling errors more explicitly.
* Zephyr compliance: Zephyr project uses a VERSION file, similar to what is currently generated. Through an option, the script should be able to generate this VERSION file.
https://docs.zephyrproject.org/latest/build/version/index.html
176 changes: 109 additions & 67 deletions rever.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,35 @@
# $> rever.sh -o dir/subdir/version.h
#
# Usage with custom tag names fw-XX.YY.ZZ
# $> rever.sh -o dir/subdir/file_version.h -f fw-
# $> rever.sh -o dir/subdir/file_version.h -t fw-
#
# ==========================================

# Script self version information
REVER_MAJOR=0
REVER_MINOR=4
REVER_PATCH=1
REVER_MINOR=5
REVER_PATCH=0

# Print variables
REVER_SCRIPT_NAME="rever.sh"
REVER_VERSION="$REVER_MAJOR.$REVER_MINOR.$REVER_PATCH"
REVER_INTRO_L1="A little POSIX shell script to generate"
REVER_INTRO_L2="version information for your C project"
REVER_INTRO_L2="version information for your C project."
REVER_INTRO_L3="ref: https://github.com/AlexFabre/rever"

# ==========================================
# Default settings
# ==========================================

# If the path provided with option -o does end on a directory
# with a trailing '/' (ex. -o dir/subdir/ ), then the script
# will create the file version.h in that directory
DEFAULT_FILE_NAME="version.h"

# Extension to look for when checking that the path
# given with option -o leads to a header file
EXTENSION=".h"
# (with a trailing '/' (ex. -o dir/subdir/ )), then the script
# will create the file in that directory with the following name.
DEFAULT_FILE_NAME="VERSION"

# By default the script will look for tags in the format
# v1.0.4
# Option -f allow for custom tag prefix
# ex. "-f fv-" if your tags are like this "fw-1.0.4"
# Option -t allow for custom tag prefix
# ex. "-t fv-" if your tags are like this "fw-1.0.4"
TAG_PREFIX="v"

# By default the script runs and prints only:
Expand All @@ -58,32 +55,39 @@ LOG_VERBOSITY=0
# Default file path output
OUTPUT_FILE_PATH=$DEFAULT_FILE_NAME

OUTPUT_FILE_FORMAT="base"

# ==========================================
# Script call checks
# ==========================================

# The user has to provide the path for the
# dest file when calling the script
usage() {
echo "==> $REVER_SCRIPT_NAME $REVER_VERSION"
echo "$REVER_INTRO_L1"
echo "$REVER_INTRO_L2"
echo "$REVER_INTRO_L3"
echo "Usage:"
echo "$REVER_SCRIPT_NAME [options]"
echo "-o <output file path>"
echo "-f <tag format> (default 'v')"
echo "-h <help>"
echo "-v <script version>"
echo "-l <script logs> (default none)"
echo "$REVER_SCRIPT_NAME <options>"
echo " -f <project format>"
echo " -f zephyr: Generate a 'VERSION' file, specific to zephyr projects"
echo " -f base: (default) Generate the basic rever format file"
echo " -o <output file>"
echo " -t <git tag format> (default 'v')"
echo " -h <help>"
echo " -v <script version>"
echo " -l <script logs> (default none)"
}

# Check the call of the script
while getopts ":o:f:hvl" opt; do
while getopts ":f:o:t:hvl" opt; do
case "${opt}" in
f)
OUTPUT_FILE_FORMAT=${OPTARG}
;;
o)
OUTPUT_FILE_PATH=${OPTARG}
;;
f)
t)
TAG_PREFIX=${OPTARG}
;;
h)
Expand Down Expand Up @@ -125,7 +129,7 @@ file_path_checker() {

# Default filename when missing
if [ -z "$filename" ]; then
filename="$DEFAULT_FILE_NAME"
filename="$DEFAULT_FILE_NAME""$EXTENSION"
fi

# Appends the extension if missing
Expand Down Expand Up @@ -156,6 +160,16 @@ fi
git_version=$(git --version)
log "$git_version"

# Handle file format depending on project format
log "Format: $OUTPUT_FILE_FORMAT"
if [ "$OUTPUT_FILE_FORMAT" = "base" ]; then
EXTENSION=".h"
fi

if [ "$OUTPUT_FILE_FORMAT" = "zephyr" ]; then
EXTENSION=""
fi

# Version file path
FILE_PATH=$(file_path_checker "$OUTPUT_FILE_PATH")

Expand Down Expand Up @@ -225,54 +239,82 @@ HOUR=$(date -u +"%-H")
# Extract filename with extension...
BASENAME="$(basename "$FILE_PATH")"

# Change filename chars to UPPER and non-alphanum to UNDERSCORES...
BUILD_LOCK=$(echo "${BASENAME}" | awk 'BEGIN { getline; print toupper($0) }' | sed 's/[^[:alnum:]\r\t]/_/g')

MACRO_PREFIX="${BUILD_LOCK#_}"
MACRO_PREFIX="${MACRO_PREFIX%%_*}"

# Modify the tmp version file
{ echo "/**";
echo " * @file $BASENAME";
echo " * @brief version information of project build";
echo " *";
echo " * Generated with $REVER_SCRIPT_NAME $REVER_VERSION";
echo " * $REVER_INTRO_L1";
echo " * $REVER_INTRO_L2";
echo " *";
echo " * @warning Do not edit this file manually. Its content";
echo " * is generated with rever.sh script.";
echo " */";
echo "#ifndef ${BUILD_LOCK}__";
echo "#define ${BUILD_LOCK}__";
echo "";
echo "/* Project version */";
echo "#define ""$MACRO_PREFIX""_MAJOR $FW_MAJOR";
echo "#define ""$MACRO_PREFIX""_MINOR $FW_MINOR";
echo "#define ""$MACRO_PREFIX""_PATCH $FW_PATCH";
echo ""
echo "/* Git repo info */";
echo "#define ""$MACRO_PREFIX""_BRANCH_NAME \"$BRANCH_NAME\"";
echo "#define ""$MACRO_PREFIX""_NB_COMMITS_SINCE_LAST_TAG $NB_COMMIT_SINCE_LAST_TAG";
echo "#define ""$MACRO_PREFIX""_COMMIT_SHORT_SHA \"$COMMIT_SHA\"";
echo ""
echo "/* Build date time (UTC) */";
echo "#define ""$MACRO_PREFIX""_BUILD_DAY $DAY";
echo "#define ""$MACRO_PREFIX""_BUILD_MONTH $MONTH";
echo "#define ""$MACRO_PREFIX""_BUILD_YEAR $YEAR";
echo "#define ""$MACRO_PREFIX""_BUILD_HOUR $HOUR";
echo "";
echo "#endif /* ${BUILD_LOCK}__ */";
} > "${FILE_PATH}_tmp.h"

if cmp -s "${FILE_PATH}" "${FILE_PATH}_tmp.h"
# Generated for a Zephyr project
if [ "$OUTPUT_FILE_FORMAT" = "zephyr" ]; then
# Modify the tmp version file
{ echo "# This file declares the firmware revision information";
echo "# both for cmake and for mcuboot";
echo "#";
echo "# ref: https://docs.zephyrproject.org/latest/build/version/index.html";
echo "#";
echo "# Generated with $REVER_SCRIPT_NAME $REVER_VERSION";
echo "# $REVER_INTRO_L1";
echo "# $REVER_INTRO_L2";
echo "# $REVER_INTRO_L3";
echo "#";
echo "# Do not edit this file manually. Its content";
echo "# is generated with rever.sh script.";
echo "";
echo "VERSION_MAJOR = $FW_MAJOR";
echo "VERSION_MINOR = $FW_MINOR";
echo "PATCHLEVEL = $FW_PATCH";
echo "VERSION_TWEAK = $NB_COMMIT_SINCE_LAST_TAG";
echo "EXTRAVERSION = $BRANCH_NAME";
} > "${FILE_PATH}_tmp"
fi

# Generated for a base project
if [ "$OUTPUT_FILE_FORMAT" = "base" ]; then
# Change filename chars to UPPER and non-alphanum to UNDERSCORES...
BUILD_LOCK=$(echo "${BASENAME}" | awk 'BEGIN { getline; print toupper($0) }' | sed 's/[^[:alnum:]\r\t]/_/g')

MACRO_PREFIX="${BUILD_LOCK#_}"
MACRO_PREFIX="${MACRO_PREFIX%%_*}"

# Modify the tmp version file
{ echo "/**";
echo " * @file $BASENAME";
echo " * @brief version information of project build";
echo " *";
echo " * Generated with $REVER_SCRIPT_NAME $REVER_VERSION";
echo " * $REVER_INTRO_L1";
echo " * $REVER_INTRO_L2";
echo " * $REVER_INTRO_L3";
echo " *";
echo " * Do not edit this file manually. Its content";
echo " * is generated with rever.sh script.";
echo " */";
echo "#ifndef ${BUILD_LOCK}__";
echo "#define ${BUILD_LOCK}__";
echo "";
echo "/* Project version */";
echo "#define ""$MACRO_PREFIX""_MAJOR $FW_MAJOR";
echo "#define ""$MACRO_PREFIX""_MINOR $FW_MINOR";
echo "#define ""$MACRO_PREFIX""_PATCH $FW_PATCH";
echo ""
echo "/* Git repo info */";
echo "#define ""$MACRO_PREFIX""_BRANCH_NAME \"$BRANCH_NAME\"";
echo "#define ""$MACRO_PREFIX""_NB_COMMITS_SINCE_LAST_TAG $NB_COMMIT_SINCE_LAST_TAG";
echo "#define ""$MACRO_PREFIX""_COMMIT_SHORT_SHA \"$COMMIT_SHA\"";
echo ""
echo "/* Build date time (UTC) */";
echo "#define ""$MACRO_PREFIX""_BUILD_DAY $DAY";
echo "#define ""$MACRO_PREFIX""_BUILD_MONTH $MONTH";
echo "#define ""$MACRO_PREFIX""_BUILD_YEAR $YEAR";
echo "#define ""$MACRO_PREFIX""_BUILD_HOUR $HOUR";
echo "";
echo "#endif /* ${BUILD_LOCK}__ */";
} > "${FILE_PATH}_tmp${EXTENSION}"
fi

if cmp -s "${FILE_PATH}" "${FILE_PATH}_tmp${EXTENSION}"
then
# New file and previous one are identical. No need to rewrite it
rm "${FILE_PATH}_tmp.h"
rm "${FILE_PATH}_tmp${EXTENSION}"
echo "==> \"$FILE_PATH\" unchanged: $TAG_PREFIX$FW_MAJOR.$FW_MINOR.$FW_PATCH (sha: $COMMIT_SHA)"
exit 0 # exit with the success code
else
mv "${FILE_PATH}_tmp.h" "${FILE_PATH}"
mv "${FILE_PATH}_tmp${EXTENSION}" "${FILE_PATH}"
echo "==> \"$FILE_PATH\" updated: $TAG_PREFIX$FW_MAJOR.$FW_MINOR.$FW_PATCH (sha: $COMMIT_SHA)"
exit 0 # exit with the success code
fi

0 comments on commit 439a8b3

Please sign in to comment.