summaryrefslogtreecommitdiff
path: root/mkc_check_common.sh
blob: 0f653d867794e5ecfdd5e54409ec2a183f5a1383 (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
# Copyright (c) 2009-2010 by Aleksey Cheusov
#
# See COPYRIGHT file in the distribution.
############################################################

# include file, not executable
# common variables and functions for mkc_check_xxx executables

if test "$pathpart" = ''; then
    echo "You've found a bug, please contact the author" 1>&2
    exit 1
fi

MKC_CACHEDIR=${MKC_CACHEDIR:-.}
CC=${CC:-cc}

tmpc=$MKC_CACHEDIR/_mkc_${pathpart}.c
tmpo=$MKC_CACHEDIR/_mkc_${pathpart}.o
tmperr=$MKC_CACHEDIR/_mkc_${pathpart}.err
tmpexe=$MKC_CACHEDIR/_mkc_${pathpart}.exe
cache=$MKC_CACHEDIR/_mkc_${pathpart}.res

printme (){
    if test "$MKC_VERBOSE" != 1; then
	return
    fi

    if test "$MKC_SHOW_CACHED" = 1 || test -z "$cached"; then
	printf "$@"
    fi
}

cleanup (){
    rm -f "$tmpexe" "$tmpo"
    if test "$MKC_DELETE_TMPFILES" = 1; then
	if test "$KEEP_SOURCE" != 1; then
	    rm -f "$tmpc"
	fi

	rm -f "$tmperr"
    fi
}

check_and_cache (){
    # $1 - message
    # $2 - cache file name
    # $@ - args...

    _msg="$1"
    _cache="$2"
    shift; shift

    if test "$MKC_NOCACHE" != 1 && test -f "$_cache"; then
	cached=1
	printme '%s' "$_msg... (cached) " 1>&2
	ret=`cat "$cache"`
    else
	printme '%s' "$_msg... " 1>&2

	# test itself
	ret=`check_itself "$@" 2>"$tmperr"`
	if test "$MKC_NOCACHE" = 1; then
	    rm -f $tmpc $tmpo $tmpexe $tmperr
	else
	    echo "$ret" > "$_cache"
	fi
    fi
}

find_n_match (){
    # $1 - progname
    # $2 - opts
    # $3 - regexp for matching
    __prog=`which $1 2>/dev/null`

    if test -n "$__prog" &&
	"$__prog" $2 2>/dev/null < /dev/null |
	grep -i "$3" > /dev/null
    then
	echo "$__prog"
	exit 0
    fi
}