summaryrefslogtreecommitdiff
path: root/scripts/mkc_check_custom.in
blob: 4b4055b684769eaab51049ce309e9b245c66800f (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
#!@SH@

############################################################
# Copyright (c) 2009-2020 by Aleksey Cheusov
#
# See LICENSE file in the distribution.
############################################################

set -e

LC_ALL=C
export LC_ALL

: ${CHECK_COMMON_SH_DIR:=@mkc_libexecdir@}

##################################################
# 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
   -t          set the testname (mandatory option)
   -l          build application, not only compile it
   -r          build application and run it
   -m text     A part of verbose message, defaults to -n args
   -s          exit status of executable will be check
   -d          delete cache files
   -e          print 0 if compiler/cmd print something to stderr
   -b          print yes/no instead of 1/0
Examples:
   mkc_check_custom my_custom_test.c
   mkc_check_custom -r mmap_works_perfectly.c
EOF
}

while getopts bdehlm:rst: f; do
    case "$f" in
	h)
	    usage
	    exit 0;;
	t)
	    testname="$OPTARG";;
	l)
	    buildit=1;;
	r)
	    buildit=1
	    runit=1;;
	m)
	    msg="$OPTARG";;
	s)
	    check_status=1;;
	d)
	    delcache=1;;
	e)
	    empty_stderr=1;;
	b)
	    boolean=1;;
	*)
	    echo "Bad option $f" 1>&2
	    exit 1;;
    esac
done
shift `expr $OPTIND - 1`

if test $# -lt 1; then
    usage
    exit 1
fi

if test -z "$testname"; then
    echo 'Option -t should be specified' 1>&2
    usage
    exit 1
fi

##################################################
# initializing
pathpart="${testname}"

. ${CHECK_COMMON_SH_DIR}/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
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="-o $tmpo $CFLAGS $CPPFLAGS $src_or_exe";;
	    *.cc|*.C|*.cxx|*.cpp)
		compiler="$CXX"
		flags="-o $tmpo $CXXFLAGS $CPPFLAGS $src_or_exe";;
	    *)
		echo 'Bad filename for custom check. What to do?' 1>&2
		return 1
	esac
    fi

    if test -n "$buildit"; then
	flags="$flags $LDFLAGS $LDADD"
    else
	flags="-c $flags"
    fi

    if test -z "$compiler"; then
	echo "Bad compiler for $src_or_exe. What to do?" 1>&2
	return 1
    fi

    if ! $compiler $flags 2>"$tmperr"; then
	echo 0
    elif test -n "$empty_stderr" -a -s "$tmperr"; then
	echo 0
    else
	echo 1
    fi
}

##################################################
# test
msg=${msg-"custom test $testname"}

check_and_cache "checking ${msg}" "$cache"

##################################################
# clean-ups

KEEP_SOURCE=1 # do not delete user's source file!
cleanup

##################################################
# finishing
case "${boolean}_$ret" in
    1_0) printme 'no\n' 1>&2;;
    1_1) printme 'yes\n' 1>&2;;
    *)   printme '%s\n' "$ret" 1>&2;;
esac

echo $ret