Skip to content

Instantly share code, notes, and snippets.

@ebassi
Last active January 27, 2017 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebassi/dd026ffce924a83951b937cc90b2f1d0 to your computer and use it in GitHub Desktop.
Save ebassi/dd026ffce924a83951b937cc90b2f1d0 to your computer and use it in GitHub Desktop.
Wrapper for non-srcdir builds with autotools
#!/bin/bash
# Copyright 2017 Emmanuele Bassi. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Little helper function for reading args from the commandline.
# it automatically handles -a b and -a=b variants, and returns 1 if
# we need to shift $3. Original code from Colin Walters
# Copyright 2010, 2011, 2013 Colin Walters <walters@verbum.org>
# Licensed under the new-BSD license (http://www.opensource.org/licenses/bsd-license.php)
read_arg() {
# $1 = arg name
# $2 = arg value
# $3 = arg parameter
local rematch='^[^=]*=(.*)$'
if [[ $2 =~ $rematch ]]; then
read "$1" <<< "${BASH_REMATCH[1]}"
else
read "$1" <<< "$3"
# There is no way to shift our callers args, so
# return 1 to indicate they should do it instead.
return 1
fi
}
sanitycheck() {
# $1 = arg name
# $1 = arg command
# $2 = arg alternates
local cmd=$( which $2 2>/dev/null )
if [ -x "$cmd" ]; then
read "$1" <<< "$cmd"
return 0
fi
test -z $3 || {
for alt in $3; do
cmd=$( which $alt 2>/dev/null )
if [ -x "$cmd" ]; then
read "$1" <<< "$cmd"
return 0
fi
done
}
echo -e "\e[1;31mERROR\e[0m: Command '$2' not found"
exit 1
}
clean_builddir() {
# $1 = arg dir
if [ -d "$1" ]; then
echo -e -n "\e[1;32mINFO\e[0m: Cleaning '${builddir}'..."
rm -rf ${builddir}
echo "Done."
fi
}
autogen_srcdir() {
# $1 = arg dir
# $2 = arg file
local autogen_sh="$1/$2"
test -x ${autogen_sh} && {
echo -e "\e[1;32mINFO\e[0m: Running 'autogen.sh'..."
export NOCONFIGURE=1
${autogen_sh} || exit $?
}
}
configure_builddir() {
# $1 = arg configure
# $2 = arg options
if [ -x "$1" ]; then
echo -e "\e[1;32mINFO\e[0m: Running '$1'..."
$1 $2 || exit $?
else
echo -e "\e[1;31mERROR\e[0m: No '$1' script found."
exit 1
fi
}
build() {
# $1 = arg build
# $2 = arg options
echo -e "\e[1;32mINFO\e[0m: Running '$1'..."
$1 $2 || exit $?
}
sanitycheck AUTORECONF 'autoreconf'
sanitycheck MAKE 'make'
test -n "$srcdir" || srcdir=.
build=0
clean=0
config=0
force=0
show_help=0
while (($# > 0)); do
case "${1%%=*}" in
clean) clean=1;;
config) config=1;;
help) show_help=1;;
build) build=1;;
--force) force=1;;
--help) show_help=1;;
--builddir|-b) read_arg builddir "$@" || shift;;
--prefix) read_arg prefix "$@" || shift;;
--libdir) read_arg libddir "$@" || shift;;
--datadir) read_arg datadir "$@" || shift;;
--includedir) read_arg includedir "$@" || shift;;
*) echo -e "\e[1;31mERROR\e[0m: Unknown option '$1'"; exit 1;;
esac
shift
done
test -z "${builddir}" && builddir="_build"
test -z "${prefix}" && prefix="/usr/local"
test -z "${libdir}" && libdir="${prefix}/lib"
test -z "${datadir}" && datadir="${prefix}/share"
test -z "${includedir}" && includedir="${prefix}/include"
if [ $clean == 0 ] && [ $show_help == 0 ] && [ $config == 0 ]; then
config=1
build=1
fi
test $show_help == 1 && {
echo "build-autofoo - Wrapper for autogen && configure"
echo ""
echo "Usage: build-autofoo [OPTIONS] [COMMAND]"
echo ""
echo "COMMANDS"
echo ""
echo " build Builds the project (default)"
echo " clean Cleans an existing build and exits"
echo " configure Configures the build"
echo " help Prints this help screen and exits"
echo ""
echo "OPTIONS"
echo ""
echo " --builddir=DIR Specifies the build directory (default='_build')"
echo " --force Forces a rebuild"
echo " --prefix=DIR The installation prefix (default='/usr/local')"
echo " --libdir=DIR The installation path for libraries (default='\$prefix/lib')"
echo " --datadir=DIR The installation path for data (default='\$prefix/share')"
echo " --includedir=DIR The installation path for headers (default=\$prefix/include')"
echo ""
exit 1
}
test $clean == 1 && {
clean_builddir $builddir
exit 0
}
test $config == 1 && {
if [ $force == 1 ] || [ ! -d "${builddir}" ]; then
clean_builddir $builddir
autogen_srcdir $srcdir 'autogen.sh'
mkdir "${builddir}"
fi
pushd "${builddir}"
configure_builddir '../configure' "--prefix=$prefix --libdir=$libdir --datadir=$datadir --includedir=$includedir"
popd
}
test $build == 1 && {
nproc=$(($(nproc) + 2))
pushd "${builddir}"
build "${MAKE}" "-j${nproc}"
popd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment