Skip to content

Instantly share code, notes, and snippets.

@jdmichaud
Last active December 26, 2022 00:27
Show Gist options
  • Save jdmichaud/436db5bdc363186f65028dff5e22f9d8 to your computer and use it in GitHub Desktop.
Save jdmichaud/436db5bdc363186f65028dff5e22f9d8 to your computer and use it in GitHub Desktop.
Install from source
#!/usr/bin/env bash
set -eEx
# Set to the program's basename.
_ME=$(basename "${0}")
usage() {
cat <<HEREDOC
Installs from a remote tarball.
Currently managed tarball: autoconfigure, cmake
Usage:
${_ME} [<arguments>]
${_ME} -h | --help
Options:
-h --help Show this screen.
-c --cmake Tarball shall be built by CMake.
Example:
${_ME} https://github.com/g-truc/glm/archive/0.9.9.6.tar.gz
HEREDOC
}
DOWNLOAD_FOLDER=`pwd`/".download"
INSTALL_FOLDER=`pwd`/".env"
CMAKE_OPTION=0
_get_INSTALL_folder() {
echo "./download"
}
function downloadFile() {
which curl > /dev/null
if [ $? -ne 0 ]; then
echo "You need the curl binary installed and accessible to this shell."
exit 1
fi
url=$1
destination=${DOWNLOAD_FOLDER}/`basename $url`
mkdir -p ${DOWNLOAD_FOLDER}
if test -f $destination; then
echo "$destination already downloaded"
return
fi
echo "downloading $destination"
curl --progress-bar --show-error --location --user-agent '${USER_AGENT}' $url --output $destination
if [ $? -ne 0 ]; then
echo "fail downloading from $url"
exit 1
fi
}
function untarFile() {
file=$1
mkdir -p ${INSTALL_FOLDER}
echo "untaring $file"
tar -x -f $file -C ${INSTALL_FOLDER}
if [ $? -ne 0 ]; then
echo "fail untaring file $file"
exit 2
fi
}
function unzipFile() {
file=$1
mkdir -p ${INSTALL_FOLDER}
echo "unzipping $file"
unzip $file -d ${INSTALL_FOLDER}
if [ $? -ne 0 ]; then
echo "fail unzipping file $file"
exit 2
fi
}
function compilePackage() {
package=$1
nbjobs=$2 || `nproc`
prefix=`pwd`/${INSTALL_FOLDER}
pushd .
cd ${INSTALL_FOLDER}/$package
./configure --prefix=$prefix --with-python=$prefix --enable-autotools --enable-llvm
make -j $nbjobs
make install
popd
}
function cmakePackage() {
package=$1
nbjobs=$2 || `nproc`
prefix=`pwd`/${INSTALL_FOLDER}
pushd .
mkdir -p ${INSTALL_FOLDER}/$package/build
cd ${INSTALL_FOLDER}/$package/build
${INSTALL_FOLDER}/bin/cmake -DCMAKE_INSTALL_PREFIX=$PREFIX ../
make -j `nproc`
make install
popd
}
function tag() {
TAG=$1
touch ${INSTALL_FOLDER}/".${TAG}"
}
function isPackageInstalled() {
TAG=$1
if [[ -f ${INSTALL_FOLDER}/".${TAG}" ]]; then
echo "installed"
fi
echo "notinstalled"
}
function installCMake() {
installed=`isPackageInstalled cmake-3.15.3`
if [[ $installed == "notinstalled" ]]
then
downloadFile https://github.com/Kitware/CMake/releases/download/v3.15.3/cmake-3.15.3.tar.gz
untarFile ${DOWNLOAD_FOLDER}/cmake-3.15.3.tar.gz
(
cd ${INSTALL_FOLDER}/cmake-3.15.3
./bootstrap --prefix=${INSTALL_FOLDER} --parallel=`nproc`
make -j `nproc`
make install
)
tag "cmake-3.15.3"
fi
}
function installCPackage() {
uri=$1
nbjobs=$2
if [[ CMAKE_OPTION -ne 0 ]]; then
echo "Installing CMAKE..."
installCMake
fi
tarball=`basename ${uri}`
downloadFile $uri
untarFile ${DOWNLOAD_FOLDER}/${tarball}
# We can't rely on the fact that the name of what is unzipped is the same
# as the tarball. So we just take the latest created file.
# TODO: Some unices do not manage -c.
package=`ls -c ${INSTALL_FOLDER} | head -n 1`
if [[ CMAKE_OPTION -ne 0 ]]; then
cmakePackage ${package} ${nbjobs}
else
compilePackage ${package} ${nbjobs}
fi
}
_main() {
if [[ $# -eq 0 ]]; then
echo "error: Not enough parameter. Source uri expected."
echo ""
usage
exit 1
fi
uri=$1
while getopts ":hc:" opt; do
case ${opt} in
h)
usage
exit 0
;;
c)
CMAKE_OPTION=1
uri=$OPTARG
;;
\?)
echo "error: Invalid Option: -$OPTARG" 1>&2
usage
exit 1
;;
:)
echo "Invalid option: $OPTARG requires an argument" 1>&2
;;
esac
done
shift $((OPTIND -1))
installCPackage $uri
}
# Call `_main` after everything has been defined.
_main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment