diff options
Diffstat (limited to 'scripts/mkc_check_custom')
-rwxr-xr-x | scripts/mkc_check_custom | 180 |
1 files changed, 180 insertions, 0 deletions
diff --git a/scripts/mkc_check_custom b/scripts/mkc_check_custom new file mode 100755 index 0000000..5213d29 --- /dev/null +++ b/scripts/mkc_check_custom @@ -0,0 +1,180 @@ +#!/bin/sh + +############################################################ +# Copyright (c) 2009-2010 by Aleksey Cheusov +# +# See LICENSE file in the distribution. +############################################################ + +set -e + +LC_ALL=C +export LC_ALL + +################################################## +# options +usage (){ + cat <<'EOF' +mkc_check_custom - tries to compile source file specified by user, + optionally builds and runs an application, and + returns the result (1 - build succeded, 0 - build failed, + other value returned by built application) + +Usage: + mkc_check_custom [OPTIONS] source_file + mkc_check_custom [OPTIONS] cmd [args...] + +OPTIONS: + -h display this help + -r build application and run it + -p a part of cache filename, defaults to "custom" + -n a part of cache filename, defaults to + `basename <source_file>` without extension + -m A part of verbose message, defaults to -n args + -s exit status of executable will be check + -d delete cache files +Examples: + mkc_check_custom my_custom_test.c + mkc_check_custom -r mmap_works_perfectly.c +EOF +} + +if test $# -eq 0; then + usage + exit 1 +fi +if test "_$1" = '_-h'; then + usage + exit 0 +fi + +while test $# -ne 0; do + case "$1" in + -r) + runit=1;; + -p) + pref="$2" + shift;; + -n) + basefn="$2" + shift;; + -m) + msg="$2" + shift;; + -s) + check_status=1;; + -d) + delcache=1;; + -*) + echo "Bad option $1" 1>&2 + exit 1;; + *) + break; + esac + shift +done + +if test $# -lt 1; then + usage + exit 1 +fi + +################################################## +# initializing +if test -z "$basefn"; then + basefn=`basename $1 | sed 's|[.][^.]*$||'` +fi +pathpart="${pref-custom}_$basefn" + +. mkc_check_common.sh + +src_or_exe="$1" + +shquote (){ + __cmd=`printf '%s\n' "$1" | sed "s|'|'\\\\\''|g"` + printf "%s\n" "'$__cmd'" +} + +for i in "$@"; do + cmd="$cmd "`shquote "$1"` + shift +done + +################################################## +# functions + +compile (){ + if $CC -c -o "$tmpo" $CPPFLAGS $CFLAGS "$src_or_exe" 2>"$tmperr"; then + echo 1 + else + echo 0 + fi +} + +check_itself (){ + if test -x "$src_or_exe"; then + if test -n "$check_status"; then + set +e # workaround for buggy FreeBSD shell + if eval "$cmd"; then + echo 1 + else + echo 0 + fi + set -e # workaround for buggy FreeBSD shell + else + eval "$cmd" + fi + return 0 + else + case "$src_or_exe" in + *.c) + compiler="$CC" + flags="-c -o $tmpo $CFLAGS $CPPFLAGS $src_or_exe";; + *.cc|*.C|*.cxx|*.cpp) + compiler="$CXX" + flags="-c -o $tmpo $CXXFLAGS $CPPFLAGS $src_or_exe";; + *.f) + compiler="$FC" + flags="-c -o $tmpo $FFLAGS $src_or_exe";; + *) + echo 'Bad filename for custom check. What to do?' 1>&2 + return 1 + esac + fi + + if test -z "$compiler"; then + echo "Bad compiler for $src_or_exe. What to do?" 1>&2 + return 1 + fi + + if $compiler $flags; then + echo 1 + else + echo 0 + fi +} + +################################################## +# test +msg=${msg-"custom test $basefn"} +check_and_cache "checking for ${msg}" "$cache" + +################################################## +# clean-ups + +KEEP_SOURCE=1 # do not delete user's source file! +cleanup + +################################################## +# finishing + +case "$ret" in + 1) + printme '1 (yes)\n' 1>&2;; + 0) + printme '0 (no)\n' 1>&2;; + *) + printme '%s\n' "$ret" 1>&2;; +esac + +echo $ret |