From a0867a7f4f3ac2e4b34b3c3cd149b7ca6d8b712f Mon Sep 17 00:00:00 2001 From: David Alessio Date: Mon, 26 Aug 2019 15:09:33 +0000 Subject: [PATCH] Merged in david_alessio/nuttx/feature/add-git-revision (pull request #1020) report git info on /proc/gitrev * report git info on /proc/gitrev git info reported: branch, version, git hash, hostname, usr, build date * use existing .version and procfs for git info * reduce script's coupling Approved-by: Gregory Nutt --- fs/procfs/fs_procfsversion.c | 12 ++- tools/Makefile.unix | 24 +++-- tools/gen_gitrev.sh | 203 +++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+), 11 deletions(-) create mode 100755 tools/gen_gitrev.sh diff --git a/fs/procfs/fs_procfsversion.c b/fs/procfs/fs_procfsversion.c index dd901c936b..79cf51e39c 100644 --- a/fs/procfs/fs_procfsversion.c +++ b/fs/procfs/fs_procfsversion.c @@ -220,15 +220,23 @@ static ssize_t version_read(FAR struct file *filep, FAR char *buffer, if (filep->f_pos == 0) { -#if defined(__DATE__) && defined(__TIME__) +#ifdef CONFIG_GIT_REVISION_STR + linesize = snprintf(attr->line, VERSION_LINELEN, + "NuttX version %s %s\n" + "%s\n", + CONFIG_VERSION_STRING, CONFIG_VERSION_BUILD, + CONFIG_GIT_REVISION_STR); +#else +# if defined(__DATE__) && defined(__TIME__) linesize = snprintf(attr->line, VERSION_LINELEN, "NuttX version %s %s %s %s\n", CONFIG_VERSION_STRING, CONFIG_VERSION_BUILD, __DATE__, __TIME__); -#else +# else linesize = snprintf(attr->line, VERSION_LINELEN, "NuttX version %s %s\n", CONFIG_VERSION_STRING, CONFIG_VERSION_BUILD); +# endif #endif /* Save the linesize in case we are re-entered with f_pos > 0 */ diff --git a/tools/Makefile.unix b/tools/Makefile.unix index 146f1107dd..7e3d7e1f36 100644 --- a/tools/Makefile.unix +++ b/tools/Makefile.unix @@ -182,8 +182,8 @@ endif NUTTXNAME = nuttx BIN = $(NUTTXNAME)$(EXEEXT) -all: $(BIN) -.PHONY: dirlinks context clean_context check_context config oldconfig menuconfig nconfig qconfig gconfig export subdir_clean clean subdir_distclean distclean apps_clean apps_distclean +all: version_info $(BIN) +.PHONY: dirlinks context clean_context check_context config oldconfig menuconfig nconfig qconfig gconfig export subdir_clean clean subdir_distclean distclean apps_clean apps_distclean version_info # Target used to copy include/nuttx/lib/math.h. If CONFIG_ARCH_MATH_H is # defined, then there is an architecture specific math.h header file @@ -260,15 +260,21 @@ endif tools/mkversion$(HOSTEXEEXT): $(Q) $(MAKE) -C tools -f Makefile.host TOPDIR="$(TOPDIR)" mkversion$(HOSTEXEEXT) +# create .version iff it doesn't already exist. Note: it may have been +# created manually and may contain other info $(TOPDIR)/.version: - $(Q) if [ ! -f .version ]; then \ - echo "No .version file found, creating one"; \ - tools/version.sh -v 0.0 -b 0 .version; \ - chmod 755 .version; \ - fi + $(Q) echo "No .version file found, creating one" + $(Q) tools/version.sh -v 0.0 -b 0 .version + $(Q) chmod 755 .version -include/nuttx/version.h: $(TOPDIR)/.version tools/mkversion$(HOSTEXEEXT) - $(Q) tools/mkversion $(TOPDIR) > include/nuttx/version.h +# version_info will add git-specific info to the .version file +# which must already exist. First strip out any existing CONFIG_GIT_ info +version_info: $(TOPDIR)/.version + $(Q) sed -i '/CONFIG_GIT_/d' $< + $(Q) tools/gen_gitrev.sh -t $(TOPDIR) >> $< + +include/nuttx/version.h: $(TOPDIR)/.version tools/mkversion$(HOSTEXEEXT) version_info + $(Q) tools/mkversion $(TOPDIR) > $@ # Targets used to build include/nuttx/config.h. Creation of config.h is # part of the overall NuttX configuration sequence. Notice that the diff --git a/tools/gen_gitrev.sh b/tools/gen_gitrev.sh new file mode 100755 index 0000000000..39749dca9b --- /dev/null +++ b/tools/gen_gitrev.sh @@ -0,0 +1,203 @@ +#!/usr/bin/env bash +########################################################################### +# +# Copyright 2017, 2018, 2019 David S. Alessio, All rights reserved. +# Author: David S. Alessio +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +########################################################################### +# +# This bash script queries the git repo for info/status +# and generates a header file with version info #defs +# +# Date : 25-Aug-2019 + + +########################################################################### +# +# Global Vars +# + +# full pathname to utils used +declare WHO=/usr/bin/whoami +declare GIT=/usr/bin/git +declare TR=/usr/bin/tr +declare WC=/usr/bin/wc + +case $OSTYPE in + linux-gnu|netbsd|darwin*) + declare DATE=/bin/date + declare HOSTNAME=/bin/hostname + ;; + cygwin) + declare DATE=/usr/bin/date + declare HOSTNAME=/usr/bin/hostname + ;; + *) + declare DATE=/bin/date + declare HOSTNAME=/bin/hostname + ;; +esac + + + +########################################################################### +# +# define -- helper function to define vars with HEREDOC +# +define() { IFS='\n' read -r -d '' ${1} || true; } + + + +########################################################################### +# +# usage +# +usage() { + define usage_str < version info filename + -t top directory + -h show usage + -v increase verbosity +EOT + + echo "$usage_str" >&2 + exit 1 +} + + + +########################################################################### +# +# parse args +# +declare -i verbosity=0 +declare TOP_DIR= + +while getopts ":ht:v" opt; do + case $opt in + h) usage + ;; + + t) TOP_DIR=$OPTARG + ;; + + v) verbosity=$(($verbosity+1)) + ;; + + \?) echo "Invalid option: -$OPTARG" >&2 + usage + ;; + + :) echo "Option -$OPTARG requires an argument." >&2 + usage + ;; + esac +done + +shift $((OPTIND-1)) + + + +[[ $OPTIND -gt 1 ]] || usage + +if [[ -z "$TOP_DIR" ]]; then + echo "@@@" + echo "@@@ TOP DIR not specified" + echo "@@@ Exiting with Error!" + echo "@@@" + exit 1 +fi + + + +# bail out if .git directory doesn't exist +[[ -d $TOP_DIR/.git ]] || exit 0 + + + +########################################################################### +# +# Git info vars +# +declare BUILD_DATE=`$DATE +"%a %d-%b-%Y %T %Z" | $TR -d '\n'` +declare BUILD_MACH=`$HOSTNAME | $TR -d '\n'` +declare BUILD_USER=`$WHO` +declare GIT_BRANCH=`$GIT rev-parse --abbrev-ref HEAD` +declare GIT_FW_DESCR=`$GIT describe --long --tags --dirty='+' --always` +declare GIT_SHORT_HASH=`$GIT rev-parse --short HEAD` + +# Create the git revision string + +declare GIT_REVISION_STR="$GIT_FW_DESCR, $GIT_BRANCH, $BUILD_MACH/$BUILD_USER, $BUILD_DATE" + + + +########################################################################### +# +# Dump vars to stderr +# +show_vars() { + define vars <&2 +} + + + +if [[ verbosity -ge 1 ]]; then + show_vars +fi + + + +########################################################################### +# +# update .version file with the following: +# +define git_info <