-
Notifications
You must be signed in to change notification settings - Fork 5
/
gccdefines.sh
executable file
·214 lines (192 loc) · 6.46 KB
/
gccdefines.sh
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env sh
#
# SweetAda GCC defines generator.
#
# Copyright (C) 2020-2024 Gabriele Galeotti
#
# This work is licensed under the terms of the MIT License.
# Please consult the LICENSE.txt file located in the top-level directory.
#
#
# Arguments:
# $1 = package name
# $2 = output filename
# $3..$n = list of GCC macro define specifications
#
# Environment variables:
# TOOLCHAIN_CC
# CC_SWITCHES_RTS
# GCC_SWITCHES_PLATFORM
#
#
# <GCC_define>:<Ada_identifier>:<type>:<specifier>
#
# GCC_define: macro name
# Ada_identifier: Ada identifier name
# type: Ada type (empty = untyped)
# specifier:
# B - Boolean (soft): True if defined, else False
# H - Boolean (hard): True if defined and <> 0, else False
# N - constant/Integer: value, else -1
# P - constant/Integer: value if > 0, else -1
# S - String: value, else ""
#
# shellcheck disable=SC2086,SC2181,SC2268
################################################################################
# Script initialization. #
# #
################################################################################
SCRIPT_FILENAME=$(basename "$0")
LOG_FILENAME=""
if [ "x${LOG_FILENAME}" != "x" ] ; then
rm -f "${LOG_FILENAME}"
touch "${LOG_FILENAME}"
fi
################################################################################
# log_print() #
# #
################################################################################
log_print()
{
if [ "x${LOG_FILENAME}" != "x" ] ; then
printf "%s\n" "$1" | tee -a "${LOG_FILENAME}"
else
printf "%s\n" "$1"
fi
return 0
}
################################################################################
# log_print_error() #
# #
################################################################################
log_print_error()
{
if [ "x${LOG_FILENAME}" != "x" ] ; then
printf "%s\n" "$1" | tee -a "${LOG_FILENAME}" 1>&2
else
printf "%s\n" "$1" 1>&2
fi
return 0
}
################################################################################
# Main loop. #
# #
################################################################################
#
# Basic input parameters check.
#
PACKAGE_NAME="$1"
if [ "x${PACKAGE_NAME}" = "x" ] ; then
log_print_error "${SCRIPT_FILENAME}: *** Error: no package name specified."
exit 1
fi
shift
OUTPUT_FILENAME="$1"
if [ "x${OUTPUT_FILENAME}" = "x" ] ; then
log_print_error "${SCRIPT_FILENAME}: *** Error: no output filename specified."
exit 1
fi
shift
if [ "x$1" = "x" ] ; then
log_print_error "${SCRIPT_FILENAME}: *** Error: no items specified."
exit 1
fi
max_tmacro_length=0
max_type_length=7
for i in "$@" ; do
IFS=':' read -r i_macro i_tmacro i_type i_spec << EOF
${i}
EOF
if [ "x${i_macro}" = "x" ] || [ "x${i_tmacro}" = "x" ] ; then
log_print_error "${SCRIPT_FILENAME}: *** Error: no item definition."
exit 1
fi
case ${i_spec} in
B|H|N|P|S)
;;
*)
log_print_error "${SCRIPT_FILENAME}: *** Error: no item specifier."
exit 1
;;
esac
tm_length=${#i_tmacro}
if [ $((tm_length)) -gt $((max_tmacro_length)) ] ; then
max_tmacro_length=${tm_length}
fi
t_length=${#i_type}
if [ $((t_length)) -gt $((max_type_length)) ] ; then
max_type_length=${t_length}
fi
done
if [ $((max_type_length)) -gt 0 ] ; then
max_type_length=$((max_type_length+1))
fi
indent=" "
format_string="%-${max_tmacro_length}s : constant %-${max_type_length}s:= %s;"
gcc_output=$( \
printf "%s\n" "void ___(void) { }" | \
${TOOLCHAIN_CC} ${CC_SWITCHES_RTS} ${GCC_SWITCHES_PLATFORM} -E -P -dM -c -
)
exit_status=$?
if [ "${exit_status}" != "0" ] ; then
log_print_error "${SCRIPT_FILENAME}: *** Error: GCC error."
exit ${exit_status}
fi
gcc_output=$( \
printf "%s" "${gcc_output}" | \
sed -e "s|^\(#define *\)\([A-Za-z_][0-9A-Za-z_]*\) *\(.*\)|\2=\3|" | \
sed -e ":a" -e "N" -e "\$!ba" -e "s|\n|!|g" \
)
NL=$(printf "\n%s" "_") ; NL=${NL%_}
gccdefines=""
gccdefines=${gccdefines}${NL}
gccdefines=${gccdefines}$(printf "%s\n" "package ${PACKAGE_NAME}")${NL}
gccdefines=${gccdefines}$(printf "%s%s\n" "${indent}" "with Pure => True")${NL}
gccdefines=${gccdefines}$(printf "%s%s\n" "${indent}" "is")${NL}
gccdefines=${gccdefines}${NL}
for i in "$@" ; do
IFS=':' read -r i_macro i_tmacro i_type i_spec << EOF
${i}
EOF
BACKUP_IFS=${IFS}
IFS='!'
found=
for d in ${gcc_output} ; do
if [ "${d%%=*}" = "${i_macro}" ] ; then
found=Y
value="${d#*=}"
break
fi
done
IFS=${BACKUP_IFS}
if [ "x${found}" = "xY" ] ; then
case ${i_spec}${i_type} in
BBoolean) value="True" ;;
HBoolean) if [ $((value)) -ne 0 ] ; then value="True" ; else value="False" ; fi ;;
N|NInteger) if [ "x${value}" = "x" ] ; then value="-1" ; fi ;;
P|PInteger) if [ $((value)) -lt 1 ] ; then value="-1" ; fi ;;
SString) if [ "x${value}" = "x" ] ; then value="\"\"" ; fi ;;
*)
log_print_error "${SCRIPT_FILENAME}: *** Error: inconsistent item specification."
exit 1
;;
esac
gccdefines=${gccdefines}$(printf "%s${format_string}\n" "$indent" "${i_tmacro}" "${i_type}" "${value}")${NL}
else
case ${i_spec}${i_type} in
BBoolean|HBoolean) value="False" ;;
N|NInteger|P|PInteger) value="-1" ;;
SString) value="\"\"" ;;
*)
log_print_error "${SCRIPT_FILENAME}: *** Error: inconsistent item specification."
exit 1
;;
esac
gccdefines=${gccdefines}$(printf "%s${format_string}\n" "$indent" "${i_tmacro}" "${i_type}" "${value}")${NL}
fi
done
gccdefines=${gccdefines}${NL}
gccdefines=${gccdefines}$(printf "%s\n" "end GCC_Defines;")${NL}
printf "%s" "${gccdefines}" > ${OUTPUT_FILENAME}
log_print "${SCRIPT_FILENAME}: ${OUTPUT_FILENAME}: done."
exit 0