3 # Copyright (C) 2003 Free Software Foundation, Inc.
4 # Contributed by Neil Booth, May 2003.
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 2, or (at your option) any
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # Usage: opts.sh moveifchange srcdir outfile.c outfile.h file1.opt [ ...]
22 # Always operate in the C locale.
26 export LANG LANGUAGE LC_ALL
28 # Set AWK if environment has not already set it.
31 SORT
=sort # Could be /bin/sort or /usr/bin/sort
33 MOVEIFCHANGE
=$1; shift
36 TMP_C_FILE
=tmp-
${C_FILE}
37 TMP_H_FILE
=tmp-
${H_FILE}
40 # Ignore comments and blank lines
41 /^[ \t]*(;|$)/ { next }
42 # Note that RS="" falls foul of gawk 3.1.2 bugs
43 /^[^ \t]/ { record = $0
45 if (!(tmp ~ "^[ \t]*(;|$)"))
46 record = record "\034" tmp
50 ' "$@" |
${SORT} |
${AWK} '
51 function switch_flags (flags, result)
55 for (j = 0; j < n_langs; j++) {
56 regex = " " langs[j] " "
57 gsub ( "\\+", "\\+", regex )
59 result = result " | " macros[j]
61 if (flags ~ " Common ") result = result " | CL_COMMON"
62 if (flags ~ " Joined ") result = result " | CL_JOINED"
63 if (flags ~ " JoinedOrMissing ") \
64 result = result " | CL_JOINED | CL_MISSING_OK"
65 if (flags ~ " Separate ") result = result " | CL_SEPARATE"
66 if (flags ~ " RejectNegative ") result = result " | CL_REJECT_NEGATIVE"
67 if (flags ~ " UInteger ") result = result " | CL_UINTEGER"
68 if (flags ~ " Undocumented ") result = result " | CL_UNDOCUMENTED"
69 if (flags ~ " Report ") result = result " | CL_REPORT"
70 sub( "^0 \\| ", "", result )
73 function var_args(flags)
75 if (flags !~ "Var\\(")
77 sub(".*Var\\(", "", flags);
78 sub("\\).*", "", flags);
82 function var_name(flags)
90 function var_set(flags)
95 sub( "[^,]*,", "", s);
98 function var_ref(flags)
100 name = var_name(flags);
113 # Collect the text and flags of each option into an array
115 if ($1 == "Language") {
126 # Dump out an enumeration into a .h file, and an array of options into a
127 # C file. Combine the flags of duplicate options.
129 c_file = "'${TMP_C_FILE}'"
130 h_file = "'${TMP_H_FILE}'"
131 realh_file = "'${H_FILE}'"
134 print "/* This file is auto-generated by opts.sh. */\n" > c_file
135 print "#include <intl.h>" >> c_file
136 print "#include \"" realh_file "\"" >> c_file
137 print "#include \"opts.h\"\n" >> c_file
139 print "/* This file is auto-generated by opts.sh. */\n" > h_file
140 print "#ifndef OPTIONS_H" >> h_file
141 print "#define OPTIONS_H\n" >> h_file
143 for (i = 0; i < n_opts; i++) {
144 name = var_name(flags[i]);
148 printf ("/* Set by -%s.\n %s */\nextern int %s;\n\n",
149 opts[i], help[i], name) >> h_file
151 if (flags[i] ~ "VarExists")
154 if (flags[i] ~ "Init\\(")
157 sub(".*Init\\(","",init);
158 sub("\\).*","",init);
164 printf ("/* Set by -%s.\n %s */\nint %s%s;\n\n",
165 opts[i], help[i], name,init) >> c_file
169 print "const char * const lang_names[] =\n{" >> c_file
170 for (i = 0; i < n_langs; i++) {
171 macros[i] = "CL_" langs[i]
172 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
173 s = substr(" ", length (macros[i]))
174 print "#define " macros[i] s " (1 << " i ")" >> h_file
175 print " \"" langs[i] "\"," >> c_file
178 print " 0\n};\n" >> c_file
179 print "const unsigned int cl_options_count = N_OPTS;\n" >> c_file
181 print "const struct cl_option cl_options[] =\n{" >> c_file
183 print "\nenum opt_code\n{" >> h_file
185 for (i = 0; i < n_opts; i++)
186 back_chain[i] = "N_OPTS";
188 for (i = 0; i < n_opts; i++) {
189 # Combine the flags of identical switches. Switches
190 # appear many times if they are handled by many front
192 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
193 flags[i + 1] = flags[i] " " flags[i + 1];
197 len = length (opts[i]);
198 enum = "OPT_" opts[i]
199 if (opts[i] == "finline-limit=")
201 gsub ("[^A-Za-z0-9]", "_", enum)
203 # If this switch takes joined arguments, back-chain all
204 # subsequent switches to it for which it is a prefix. If
205 # a later switch S is a longer prefix of a switch T, T
206 # will be back-chained to S in a later iteration of this
207 # for() loop, which is what we want.
208 if (flags[i] ~ "Joined") {
209 for (j = i + 1; j < n_opts; j++) {
210 if (substr (opts[j], 1, len) != opts[i])
212 back_chain[j] = enum;
216 s = substr(" ", length (opts[i]))
223 hlp = "N_(\"" help[i] "\")";
225 printf(" %s,%s/* -%s */\n", enum, s, opts[i]) >> h_file
226 printf(" { \"-%s\",\n %s,\n %s, %u, %s, %s, %s }%s\n",
227 opts[i], hlp, back_chain[i], len,
228 switch_flags(flags[i]),
229 var_ref(flags[i]), var_set(flags[i]), comma) >> c_file
232 print " N_OPTS\n};\n" >> h_file
233 print "#endif /* OPTIONS_H */" >> h_file
238 # Copy the newly generated files back to the correct names only if different.
239 # This is to prevent a cascade of file rebuilds when not necessary.
240 ${MOVEIFCHANGE} ${TMP_H_FILE} ${H_FILE}
241 ${MOVEIFCHANGE} ${TMP_C_FILE} ${C_FILE}