Files
stdouterr/dev/test
kevinnls 50a84ee248 add: clang to compiler list
did i even have to?
2021-04-05 22:25:08 +05:30

117 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
# set -x for debugging if DEBUG is set
function debug() { set -xv; }
${DEBUG+debug}
# declare project info variables
declare -l project_lang project_name project_version;
project_name='stdouterr'
project_version='v0.1.0-beta'
project_lang='C'
script_dir="$(realpath -e $(dirname "${0}") 2>/dev/null)"
project_root=${script_dir%/*} # remove everything after the final `/`
test_dir="${project_root}/dev/tests.d"
[[ ! -d "${test_dir}" ]] && mkdir -p "${test_dir}"
src_dir="${project_root}/src"
c_compilers=('gcc' 'clang')
cpp_compilers=('g++' 'clang')
_oname="${project_name:0:5}-${project_version}~test"
oname="$test_dir/${output:-${_oname}}"
_iname="${src_dir}/main.c"
iname="${input:-${_iname}}"
_compileargs=( )
# add args from env or set args based on env
# script version of -o retained unless overridden by env
if [[ -n "${compile_args}" ]]; then
if [[ ! "${compile_args}" =~ /*-o/ ]]; then
compile_args+=( -o "${oname}" )
fi
else
compile_args=( "${_compileargs[@]}" -o "${oname}" )
fi
compile_args+=( "${compile_aargs[@]}" )
case "${project_lang}" in
'c')
compilers=( "${c_compilers[@]}" )
;;
'cpp')
compilers=( "${cpp_compilers[@]}" )
;;
*)
err_unknown_lang
;;
esac
list_compilers(){
for compiler in "${compilers[@]}"; do
echo -e " - ${compiler}\\n"
done
}
err_no_compilers(){
1>&2 echo "ERR: no supported compiler found for ${project_lang}"
1>&2 echo "install one of:"
1>&2 list_compilers
exit 127
}
check_compilers(){
for compiler in "${compilers[@]}"; do
compiler_path=$(command -v "${compiler}" 2>/dev/null)
if [[ -n "${compiler_path}" ]]; then
echo "found compiler: ${compiler}"
compile(){
echo "=== compiling ==="
"${compiler_path}" "${compile_args[@]}" "${iname}"
if [[ $? -eq 0 ]]; then
echo "=== compiled! ==="
return 0
else
echo "=== compile failed! ==="
return 1
fi
}
break
else
continue
fi
done
{ command -v compile ; } &>/dev/null \
|| err_no_compilers
}
test(){
echo "=== start of test ==="
echo
"${oname}" "$@"
echo
echo
echo "=== end of test ==="
}
if check_compilers; then
if compile; then
if test "$@"; then
echo "completed with exit code $?"
fi
fi
else
>>2 echo "some error occurred"
exit 1
fi