3
0
Fork 0
tomsfastmath/CMake.sh

465 lines
7.1 KiB
Bash
Executable File

#!/bin/bash
todo() {
<< TODO
- Continue with adding platform(s)
- Add support for other platforms such as x86.
- Add support for Release | Debug | DebugInfo etc.
- If generating CodeBlocks support...
- Parse the source and for each CMakeLists found add it to the workspace
TODO
}
load_opts() {
GETOPT=$( getopt -o bcdDfhiru \
--long build,cmakehelp,debug,dry-run,fbver:,fresh,help,info,reset,usecodeblocks,verbose \
-n "$0" -- "$@" )
}
ShowHelp(){
tabs 3,5,9,13,17,21,25,29,33,37,41,45,49,53,57,61,65,69,73,77
DOCNOTE=$(cat << MSG > ${BASENAME}-readme.md
# CMake wrapper script
This script encapsulates the necessary calls to the CMake binary to configure
and generate the makefiles required to build this application.
Optionally a full build can also be executed.
### Options
- (boolean) help
Outputs this help screen and quits.
- (boolean) build
Will execute make in the build dir if makefile generation was successful.
- (boolean) cmakehelp
Outputs the CMake help screen and quits.
- (string) --fbver
The firebird version to build against.
This must be in the format 'fb4', 'fb5' etc.
The script will exit if this parameter is not supplied.
NOTE: Currently this application does not need to know the firebird version.
This setting is just a formality. However, firebird __MUST__ be installed.
- (boolean) --fresh
Pass --fresh to cmake, removing existing cache files.
- (boolean) --reset
Delete the Build dir. Recommended if thinks start to go wrong after
successive runs of CMake but should not usually be required.
- (boolean) --usecodeblocks
Generates CodeBlocks project files in addition to standard make files
These will be placed under the Build dir.
(Not tested.)
### Examples
- Configure and Generate make files for all platforms
CMake.sh --fbver fb4
- Check the CMake Help screen
CMake.sh --cmakehelp
- Configure, Generate and build all platforms
CMake.sh --fbver fb4 -build
MSG
)
cat ${BASENAME}-readme.md
}
######################### START OF GENERIC FUNCTIONS #########################
Pause(){
local _aprompt=""
if [ "${1:-}" = "" ]; then
_aprompt="Press any key to continue..."
else
_aprompt="$*"
fi
#shellcheck disable=SC2162
read -p "$_aprompt"
}
GetTime(){
export LOGSTAMP=$(date "+%Y-%m-%d %H:%M:%S")
export DATESTAMP=$(date +%Y%m%d)
export TIMESTAMP=$(date +%Y%m%d-%H%M%S)
export TAGSTAMP=$(date +:%Y%m%d%H%M)
case "${1:-}" in
LOGSTAMP)
echo LOGSTAMP is "$LOGSTAMP"
;;
DATESTAMP)
echo DATE is "$DATESTAMP"
;;
TIMESTAMP)
echo TIMESTAMP is "$TIMESTAMP"
;;
TAGSTAMP)
echo TAGSTAMP is "$TAGSTAMP"
;;
esac
}
GetLogStamp() {
GetTime "LOGSTAMP"
}
Debug() {
# Debug is intended for printing values assigned to variables etc.
if [ "${DEBUG:-}" == "1" ]; then
GetTime
local FUNC=$1
shift
echo -e $LOGSTAMP $FUNC "-" "$@"
fi
}
Verbose() {
# Verbose is intended to give the user a more
# detailed narrative of programm execution.
# shellcheck disable=SC2166
if [ "${VERBOSE:-}" == "1" -o "${DEBUG:-}" == "1" ]; then
GetTime
if [ "${2:-}" = "" ]; then
echo -e $LOGSTAMP "$@"
else
local FUNC=${1:-}
shift
echo -e $LOGSTAMP $FUNC "-" "$@"
fi
fi
}
EchoSpacer() {
set +x
# Echo a divider line to output
if [ -z ${1:-} ]; then
_spacer="-"
else
_spacer=$1
fi
if [ -z ${2:-} ]; then
_repeat="80"
else
_repeat=$2
fi
echo -e "\n"
for a in $(seq 1 ${_repeat} )
do
echo -n "$_spacer"
done
echo -e "\n"
}
WriteLog() {
# Param 1 - Logfile
# Param 2 - FUNCNAME
# We automatically call Verbose after shifting LOGFILE
# We then shift FUNCNAME and write out to the log.
GetTime
local LOGFILE=${1:-}
shift
Verbose "$@"
# shellcheck disable=SC2166
if [ ! "${VERBOSE:-}" = 1 -o ! "${DEBUG:-}" = 1 ]; then
shift
fi
if [ ! "${LOGFILE:-}" = "" ]; then
echo -e $LOGSTAMP "-" "$@" >> $LOGFILE
fi
}
Stop() {
# echo an error code and a message and stop execution.
local _err=0
if [ "${1:-}" -ge 0 ] 2>/dev/null ; then
_err=$1
shift
fi
echo "$@"
exit $_err
}
Error(){
local _err=0
if [ "${1:-}" -ge 0 ] 2>/dev/null ; then
_err=$1
shift
fi
EchoSpacer
echo ""
echo " Error in $0"
echo " $* "
echo ""
EchoSpacer
return $_err
<< COMMENT
Use Error on its own thus:
Error 1 A remark about the error
In this case it is up to the caller to test the error code
Or use it with Stop:
Error 1 A remark about the error
Stop $?
COMMENT
}
######################### END OF GENERIC FUNCTIONS #########################
ResetVars() {
unset BUILD
unset CMAKEHELP
unset DEBUG
unset DRYRUN
unset FRESH
unset FBVER
unset RESET
unset SHOWINFO
unset USECODEBLOCKS
BASENAME=$(basename "$0" .sh)
GENERATOR="Unix Makefiles"
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
}
ParseCommandLine() {
#set -x
if [ $# -le 0 ]; then
ShowHelp
return 1
fi
eval set -- "$GETOPT"
while true; do
case "${1:-}" in
-h|--help) return 1 ;;
-b|--build)
BUILD=" --build ";
shift;
;;
-c|--cmakehelp)
CMAKEHELP=" --help ";
shift;
;;
-d|--debug)
DEBUG=1;
shift;
;;
-D|--dry-run)
DRYRUN=1;
shift;
;;
-f|--fresh)
FRESH=" --fresh ";
shift;
;;
--fbver)
if [ "$2" ] > /dev/null 2>&1; then
FBVER="$2";
fi
shift; shift;
;;
-i|--info)
SHOWINFO=1;
shift;
;;
-r|--reset)
RESET=1;
shift;
;;
-u|--usecodeblocks)
GENERATOR="CodeBlocks - Unix Makefiles";
shift;
;;
-v|--verbose)
VERBOSE=1;
shift;
;;
--)
shift;
break;
;;
*)
echo "Invalid Option ${1:-}";
#ShowHelp
shift; shift;
return 1;
;;
esac
done
}
RemoveBuildDir() {
if [ -d $SCRIPTPATH/Build ]; then
Verbose Removing $SCRIPTPATH/Build directory
rm $SCRIPTPATH/Build --recursive
else
Verbose $SCRIPTPATH/Build directory does not exist.
fi
}
CheckParams() {
if [ "${RESET:-}" == "1" ]; then
RemoveBuildDir
fi
if [ ! "${CMAKEHELP:-}" == "" ]; then
cmake $CMAKEHELP
Stop $?
fi
}
ShowInfo() {
Debug Entering FUNCNAME "$FUNCNAME"
if [ "${SHOWINFO:-}" == "1" ]; then
echo
echo g++ version is...........$(g++ --version) | cut -c1-80
echo cmake version is ........$(cmake --version) | cut -c1-80
echo make version is..........$(make --version) | cut -c1-80
echo
fi
return 0
}
Generate() {
Debug Entering FUNCNAME "$FUNCNAME"
local _err=0
# Be sure that we are actually in the root of our repo.
pushd $SCRIPTPATH > /dev/null
_cmdstr='cmake ${FRESH:-} -G "${GENERATOR}" -S . -B Build '
Verbose Executing "$_cmdstr"
eval "$_cmdstr"
_err=$?
popd > /dev/null
return $_err
}
Build() {
if [ "${BUILD:-}" != "" ]; then
pushd $SCRIPTPATH/Build > /dev/null
make
popd > /dev/null
fi
}
main () {
EchoSpacer "="
ResetVars
load_opts "$@"
#set -x
ParseCommandLine "$@" || return 1
CheckParams
ShowInfo
Generate || return $?
Build
EchoSpacer "="
}
main "$@"
if [ $? -ne 0 ]; then echo "An error occurred."; fi
set +x