Skip to content

Instantly share code, notes, and snippets.

@maddouri
Created November 19, 2017 15:47
Show Gist options
  • Save maddouri/0cca2f3cb056f2bd58629360d320ae3e to your computer and use it in GitHub Desktop.
Save maddouri/0cca2f3cb056f2bd58629360d320ae3e to your computer and use it in GitHub Desktop.
Wrapper around CMake Config/Build
#!/usr/bin/env bash
set -eux
# Usage
# cmakeit source SOURCE_DIR build BUILD_DIR config CONFIG target TARGET1,TARGET2,... -- EXTRA_TOOL_ARGS
THIS_FILE_NAME="$(basename "$(readlink -f "$0")")"
THIS_DIR="$(dirname "$(readlink -f "$0")")"
function echo_out()
{
echo -e "${THIS_FILE_NAME}: Info: $@"
}
function echo_err()
{
2>&1 echo -e "${THIS_FILE_NAME}: Error: $@"
}
function exit_if_empty_string() # str_name str_value
{
str_name="$1"
str_value="$2"
if [[ -z "$2" ]] ; then
echo_err "[${str_name}:${str_value}] must not be empty"
exit ${LINENO}
fi
}
function cmakeit()
{
src_dir=
build_dir=
config=Release
targets=
cpp_std=11
extra_args=
################################################################################################
while [[ "$#" -gt 0 ]] ; do
key="$1"
case "${key}" in
source)
src_dir="$(readlink -f "$2")"
shift
;;
build)
build_dir="$(readlink -f "$2")"
shift
;;
config)
config="$2"
shift
;;
target)
targets="${2//,/ }"
shift
;;
std)
cpp_std="$2"
shift
;;
--)
extra_args="$2"
shift
;;
*)
echo_err "Unknown option [${key}]"
exit ${LINENO}
;;
esac
shift
done
if [[ ! -d "${src_dir}" ]] ; then
echo_err "[source:${src_dir}] is not a directory"
exit ${LINENO}
fi
exit_if_empty_string "build" "${build_dir}"
exit_if_empty_string "config" "${config}"
exit_if_empty_string "std" "${cpp_std}"
################################################################################################
cmake -H"${src_dir}" -B"${build_dir}" \
-D"CMAKE_BUILD_TYPE=${config}" \
-D"CMAKE_CXX_STANDARD=${cpp_std}" \
-D"CMAKE_CXX_STANDARD_REQUIRED=ON" \
-D"CMAKE_CXX_EXTENSIONS=OFF"
if [[ -z "${targets}" ]] ; then
cmake --build "${build_dir}" --config "${config}" -- ${extra_args}
else
for tgt in ${targets} ; do
cmake --build "${build_dir}" --config "${config}" --target "${tgt}" -- ${extra_args}
done
fi
}
cmakeit "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment