Skip to content

Instantly share code, notes, and snippets.

@antmd
Last active August 22, 2018 17:13
Show Gist options
  • Save antmd/0c2230d6732ba75a96ef32521d3d95c8 to your computer and use it in GitHub Desktop.
Save antmd/0c2230d6732ba75a96ef32521d3d95c8 to your computer and use it in GitHub Desktop.
# cat rh-toolset-redirect
#!/bin/bash -
#===============================================================================
#
# FILE: rh-toolset-redirect.sh
#
# DESCRIPTION: Redirect command to devtoolset command. This script is designed
# to be the target of symlinks of the form:
#
# <RedHatToolsetName>-redirect
#
# e.g. 'devtoolset-redirect', or 'llvm-toolset-redirect'
#
# And that symlink should in turn be the target of a versioned
# tool name such as 'clang7' or 'g++6'
#
# E.g. g++6. It will strip the toolset_version, and redirect
# the arguments to the toolset version of <binary_name>
#
# Additionally, an unversioned symlink to a versioned symlink
# will use the versioned symlink to determine the version.
#
# E.g., if we have symlinks:
#
# clang --> clang7 --> llvm-toolset-redirect -> <this-script>
#
# Invoking 'clang' will mean the version will be taken from
# 'clang7', and the toolset name will be 'llvm-toolset'.
# This allows you to set a default version by
# creating an unversioned symlink to the desired versioned
# symlink.
#
# The toolset to redirect to is taken from the penultimate
# symlink (in the example 'llvm-toolset')
#
# CREATED: 2017-07-18
#
# AUTHOR: Anthony Dervish
#
#===============================================================================
set -o nounset # Treat unset variables as an error
Script="${BASH_SOURCE[0]}"
ScriptDir="$( cd "$( dirname "${Script}" )" && pwd -P )"
function GetVersion() {
local bname=$(basename $Script)
local version=$(echo "${bname}" | sed -E 's/^.*([0-9]+)$/\1/')
if [[ "${version}" != "${bname}" ]]; then
echo "$version"
fi
}
ToolsetVersion=$(GetVersion "${Script}")
if [[ -z "$ToolsetVersion" ]]; then
if [[ -h "${Script}" ]]; then
Script="${ScriptDir}/$(basename $(readlink "${Script}"))"
ToolsetVersion=$(GetVersion "${Script}")
if [[ -z "${ToolsetVersion}" ]]; then
echo "Symlink name to this script should end in an integer toolset version!" >&2
exit 1
fi
fi
fi
if [[ ! -h "${Script}" ]]; then
echo "${Script} should be a symlink to <toolset>-redirect, which in turn should be a"
echo "symlink to this script."
exit 1
fi
ToolsetSymlink=$(readlink "${Script}")
UnversionedToolset="${ToolsetSymlink%-redirect}"
OrigName=$(basename "${Script}")
ToolsetName=${UnversionedToolset}-${ToolsetVersion}
ToolsetDir="/opt/rh/${ToolsetName}"
if [[ ! -d "${ToolsetDir}" ]]; then
echo "Unknown ${UnversionedToolset} version '${ToolsetVersion}'" >&2
exit 1
fi
RedirectBaseDir="${ToolsetDir}/root/bin"
ToolName=${OrigName%${ToolsetVersion}}
ToolBinary="${RedirectBaseDir}/${ToolName}"
cat <<EOF | sed 's/\"/\\"/g' |
${ToolBinary} $@
EOF
exec scl enable "${ToolsetName}" -
# vim: filetype=sh:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment