Skip to content

Instantly share code, notes, and snippets.

@ony
Last active August 29, 2015 14:09
Show Gist options
  • Save ony/42b96e9053c3456c4ee4 to your computer and use it in GitHub Desktop.
Save ony/42b96e9053c3456c4ee4 to your computer and use it in GitHub Desktop.
Console IDE - script to ease work with CMake projects and test builds
#!/bin/bash
# Copyright (C) 2014 Nikolay Orliuk <virkony@gmail.com>
# Distributed under the terms of the GNU General Public License v2
#
# Console IDE - just a script to ease work with CMake projects and test builds
# under different GCC compiler versions.
#
# .ciderc - file in the current or closest parent folder may be used to customize work
#
# example from my leveldb-tl:
#
# # generate palette
# # > for n in {0..255}; do tput setf $n; echo -n " $n"; done
# color_msg=$(tput setf 135)
#
# # branches naming is gcc-4.8 gcc-4.9 etc
# if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null); then
# gcc_version=${branch##*gcc-}
# fi
#
# configure_extra_args+=(-G Ninja) # I like ninjutsu
#
# help_test_all="Usage: $0 test_all - additionally test DISABLEd google tests"
# do_test_all() {
# GTEST_ALSO_RUN_DISABLED_TESTS=yes do_test "$@"
# }
#
set -e
# defaults
gcc_version=4.7
build_type=relwithdebinfo
build_dir_prefix=/tmp/ws
src_dir=$PWD
configure_extra_args=(
-DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE
)
case "$0" in
*bin/bash|bash) cide= ;;
*) cide="$(basename $0)" ;;
esac
color_marker=$(tput setf 11) || true
color_msg=$(tput setf 33) || true
color_err=$(tput setf 9) || true
color_quote=$(tput setf 85) || true
color_reset=$(tput sgr0) || true
msg() { echo "${color_marker}» ${color_msg}$*${color_reset}" >&2; }
err() { msg "${color_err}$*"; }
die() { err "$*"; exit 2; }
extra_args() { die "Wrong argument ${color_quote}$1"; }
edo() {
msg "$*"
"$@"
local _status=$?
if (($_status)); then
msg "Failed with status ${color_err}$?"
fi
return $_status
}
help_configure="Usage: $cide configure [CMake arguments...]"
do_configure() {
local conf_args=(
"${configure_extra_args[@]}"
-DCMAKE_CXX_COMPILER:FILEPATH=${cxx_compiler:-/usr/bin/g++-${gcc_version}}
-DCMAKE_C_COMPILER:FILEPATH=${c_compiler:-/usr/bin/gcc-${gcc_version}}
-DCMAKE_BUILD_TYPE=${build_type}
)
mkdir -p "$build_dir"
cd "$build_dir"
edo cmake "$src_dir" "${conf_args[@]}" "$@"
}
help_build="Usage: $cide build [CMake --build options...]"
do_build() {
edo cmake --build "$build_dir" "$@"
}
help_test="Usage: $cide test [CTest options...]"
do_test() {
cd "$build_dir"
local cpus=$(grep processor /proc/cpuinfo | wc -l)
msg "Testing with ${cpus} job(s)"
edo ctest --output-on-failure -j${cpus} "$@"
}
cide_actions() {
declare -F | awk '$3 ~ /^do_/ {print $3}'
}
help_usage="Usage: $cide usage"
do_usage() {
local action
for action in $(cide_actions); do
action=${action#do_}
eval "local _help=\${help_$action}"
if [[ -z "$_help" ]]; then
echo "Usage: $cide ${action#do_} [args...]"
else
echo "$_help"
fi
done
}
find_project() {
if [[ -e "$1/.ciderc" ]]; then
src_dir=$1
project=$(basename "$1")
. "$1/.ciderc"
return
elif [[ -d "$1/.git" ]] || [[ -d "$1/.svn" ]]; then
src_dir=$1
project=$(basename "$1")
return
else
find_project "$(dirname "$1")"
fi
}
find_project "$PWD"
msg "Found project ${color_quote}${project}${color_marker}@${color_quote}${src_dir}"
args_unknown=()
while [[ $# -gt 0 ]]; do
case "$1" in
--gcc=*) gcc_version=${1#--gcc=}; shift ;;
--debug|--release|--relwithdebinfo)
build_type=${1#--}
shift
;;
--) break; shift ;;
--*) args_unknown+=("$1"); shift ;;
*) break ;;
esac
done
build_dir=${build_dir_prefix}/build-${project}-gcc${gcc_version}-${build_type}
[ ${#args_unknown[@]} -gt 0 ] && extra_args "$args_unknown"
if [[ $# -gt 0 ]]; then
cmd=$1
shift
else
cmd=usage
fi
# shellification handled specially
[[ "$cmd" = shell ]] && exec bash --rcfile "$0"
if [[ -z "$cide" ]]; then
for action in $(cide_actions); do
alias "${action#do_}=${action}"
done
# allow user failures ;)
set +e
else
declare -F "do_$cmd" > /dev/null || die "Unknown command ${color_quote}$cmd"
"do_$cmd" "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment