summaryrefslogtreecommitdiff
path: root/scripts/mkc_check_custom
blob: 46f751c6e93c3f4c8067a0e00cf4b1544f6de9ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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 text              prefix for cache filename, defaults to "custom"
   -n text              a part of cache filename, defaults to
                        `basename <source_file>` without extension
   -m text              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