File: macros.ls

package info (click to toggle)
lua-cqueues 20161214-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,672 kB
  • ctags: 3,228
  • sloc: ansic: 20,232; sh: 2,959; makefile: 24
file content (129 lines) | stat: -rwxr-xr-x 2,143 bytes parent folder | download | duplicates (4)
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
#!/bin/sh
#
# List specified macros
#

set -e

: ${CC:=cc}
: ${CPPFLAGS:=}
: ${MACRO:=.}
: ${VENDOR:=}
: ${INCLUDE:=}
: ${TMPFILE:=.$(basename $0).c}
: ${PREPEND:=}
: ${EXPAND:=no}

NL="
"

usage() {
	cat <<-EOF
	usage: $(basename $0) -m:v:i:t:xh
	  -i INCLUDE  include file (e.g. <errno.h>)
	  -m REGEX    regular expression macro name filter
	  -s PATH     path to supplemental list, one per line
	  -t TMPFILE  compiler intermediate tmp file
	  -v VENDOR   compiler vendor name (e.g. gcc, clang, sunpro)
	  -x          expand macros
	  -h          print this usage message

	Report bugs to <william@25thandClement.com>
	EOF
}

while getopts i:m:s:t:v:xh OPT; do
	case "${OPT}" in
	i)
		case "${OPTARG}" in
		\<*)
			INCLUDE="${INCLUDE}#include ${OPTARG}${NL}"
			;;
		\"*)
			INCLUDE="${INCLUDE}#include ${OPTARG}${NL}"
			;;
		*)
			INCLUDE="${INCLUDE}#include <${OPTARG}>${NL}"
			;;
		esac
		;;
	m)
		MACRO="${OPTARG}"
		;;
	s)
		PREPEND="${OPTARG}"
		;;
	t)
		TMPFILE="${OPTARG}"
		;;
	v)
		VENDOR="${OPTARG}"
		;;
	x)
		EXPAND=yes
		;;
	h)
		usage
		exit 0
		;;
	*)
		usage >&2
		exit 1
		;;
	esac
done


vendor() {
	SCRIPT="$(dirname $0)/vendor.cc"

	if [ -n "${SCRIPT}" -a -x "${SCRIPT}" ]; then
		env CC="${CC}" ${SCRIPT}
	else
		${CC} -E - <<-EOF | awk '/sunpro/||/clang/||/gcc/||/other/{ print $1; exit; }'
			#if defined __SUNPRO_C
			sunpro
			#elif defined __clang__
			clang
			#elif defined __GNUC__
			gcc
			#else
			other
			#endif
		EOF
	fi
}


filter() {
	awk "\$1~/^#define/ && \$2~/${MACRO}/{ print \$2 }"
}


macros() {
	if [ -n "${PREPEND}" ]; then
		cat "${PREPEND}"
	fi

	case "${VENDOR:-$(vendor)}" in
	*sunpro*)
		trap "rm -f ${TMPFILE}" EXIT
		echo "${INCLUDE}" >| ${TMPFILE}
		${CC} ${CPPFLAGS} -xM ${TMPFILE} | awk '/\.h$/{ print $2 }' | sort -u | xargs cat | filter
		rm ${TMPFILE}
		;;
	*)
		echo "${INCLUDE}" | ${CC} ${CPPFLAGS} -dM -E - | filter
		;;
	esac
}

expand() {
	if [ "${EXPAND}" = "yes" ]; then
		(echo "${INCLUDE}"; awk '{ print "\"<<<< "$1" >>>>\" "$1 }') | ${CC} ${CPPFLAGS} -E - | awk '$1~/^"<<<</{ print $2""substr($0, index($0, ">>>>") + 5) }'
	else
		cat
	fi
}

macros | sort -u | expand