Skip to content

Commit

Permalink
[#8] [#15] [#16] Ensure build and versioning works when run from code…
Browse files Browse the repository at this point in the history
… archive
  • Loading branch information
rainerschoe committed Mar 7, 2019
1 parent fd1c1d3 commit fc204d2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 27 deletions.
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,15 @@ include_directories("${PROJECT_SOURCE_DIR}")
include_directories("${PROJECT_SOURCE_DIR}/src")

add_subdirectory(src)
add_subdirectory(tests)

if(EXISTS "${PROJECT_SOURCE_DIR}/third_party/googletest/CMakeLists.txt")
# we only build tests if googletest is available
# If the gWhisper source package is downloaded from GitHub as archive (tar or zip)
# submodules are not included. This ensures, that we build at least the functional
# code in this case.
add_subdirectory(tests)
else()
message(WARNING "googletest submodule not found, not building tests. Please be sure to get the submodules with 'git submodule update --init' to also build tests.")
endif()

add_subdirectory(third_party)
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ add_custom_target(compileCommands
)

add_custom_target(versionDefine
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/generateVersionDefine.sh > ${CMAKE_CURRENT_BINARY_DIR}/versionDefine.h
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/generateVersionDefine.sh ${PROJECT_SOURCE_DIR} > ${CMAKE_CURRENT_BINARY_DIR}/versionDefine.h
)
80 changes: 56 additions & 24 deletions src/generateVersionDefine.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,75 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# This is a code-generator which attempts to generate a header file containing
# version string. Following data sources are tried to get version information:
# 1. git tags, commit hashes and status
# 2. A version string in the environment variable GWHISPER_BUILD_VERSION if set.
# 3. Build date and time
# 4. Hard-coded version string HARD_CODED_VERSION found in this file.
#
# As an optional argument to this script a path containing the .git directory
# for the gWhisper repository can be provided. This prevents git from searching
# the FS tree for a git repository in case gWhisper is sub-packaged into another
# repository

# TODO: we could do this via git hooks. Problem is just that hooks are not
# really part of the repository. I.e. each user would need to install the
# hooks. Will need to find some solution
HARD_CODED_VERSION="v0.1.5+"

# get build date
DATE=`date +%Y-%m-%d_%H:%M:%S`

GITDIR=$1

if [ "$GITDIR" = "" ]; then
GITCMD="git"
else
cd $GITDIR
GITCMD="git --git-dir=.git"
fi

if [ "$GWHISPER_BUILD_VERSION" != "" ]; then
GWHISPER_BUILD_VERSION="Environment version:$GWHISPER_BUILD_VERSION "
fi

if [ -z "`command -v git`" ]; then
echo "$GWHISPER_BUILD_VERSION Build date: $DATE Build pwd: $PWD"
exit 0
versionString="${GWHISPER_BUILD_VERSION}Hard-coded version: $HARD_CODED_VERSION Build date: $DATE"
fi

git status &> /dev/null
$GITCMD status &> /dev/null
if [ $? -ne 0 ]; then
echo "$GWHISPER_BUILD_VERSION Build date: $DATE Build pwd: $PWD"
exit 0
versionString="${GWHISPER_BUILD_VERSION}Hard-coded version: $HARD_CODED_VERSION Build date: $DATE"
fi

# get unique readable commit identification
gitId=$(git describe --tags)
if [ "$versionString" = "" ]; then
# version string is still empty -> we can use git to generate the version string

# check for uncommited changes
uncommitedChanges=""
git diff-index --quiet HEAD --
if [ $? -eq 0 ]; then
uncommitedChanges="NoUncommitedChanges"
else
uncommitedChanges="UncommitedChanges"
fi
# get unique readable commit identification
gitId=$($GITCMD describe --tags)

# check for untracked files present
untrackedFiles=""
tmpUntracked=$(git ls-files --other --directory --exclude-standard)
if [ -z "$tmpUntracked" ]; then
untrackedFiles="NoUntrackedFiles"
else
untrackedFiles="UntrackedFiles"
fi
# check for uncommited changes
uncommitedChanges=""
$GITCMD diff-index --quiet HEAD --
if [ $? -eq 0 ]; then
uncommitedChanges="NoUncommitedChanges"
else
uncommitedChanges="UncommitedChanges"
fi

# check for untracked files present
untrackedFiles=""
#tmpUntracked=$($GITCMD ls-files --other --directory --exclude-standard)
tmpUntracked=$($GITCMD status | grep "Untracked files")
if [ "$tmpUntracked" = "" ]; then
untrackedFiles="NoUntrackedFiles"
else
untrackedFiles="UntrackedFiles"
fi

versionString="$GWHISPER_BUILD_VERSION Git identifier: ${gitId} Build date: ${DATE} ${uncommitedChanges} ${untrackedFiles}"
versionString="${GWHISPER_BUILD_VERSION}Git identifier: ${gitId} Build date: ${DATE} ${uncommitedChanges} ${untrackedFiles}"
fi

echo "#pragma once"
echo "#define GWHISPER_BUILD_VERSION \"$versionString\""
5 changes: 4 additions & 1 deletion third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@

cmake_minimum_required (VERSION 2.8)

add_subdirectory(googletest)
if(EXISTS "${PROJECT_SOURCE_DIR}/third_party/googletest/CMakeLists.txt")
add_subdirectory(googletest)
endif()

add_subdirectory(gRPC_utils)

0 comments on commit fc204d2

Please sign in to comment.