tree.h (PHI_CHAIN): New.
[gcc.git] / gcc / opts.sh
1 #!/bin/sh
2 #
3 # Copyright (C) 2003 Free Software Foundation, Inc.
4 # Contributed by Neil Booth, May 2003.
5 #
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
9 # later version.
10 #
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.
15 #
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.
19 #
20 # Usage: opts.sh moveifchange srcdir outfile.c outfile.h file1.opt [ ...]
21
22 # Always operate in the C locale.
23 LANG=C
24 LANGUAGE=C
25 LC_ALL=C
26 export LANG LANGUAGE LC_ALL
27
28 # Set AWK if environment has not already set it.
29 AWK=${AWK-awk}
30
31 SORT=sort # Could be /bin/sort or /usr/bin/sort
32
33 MOVEIFCHANGE=$1; shift
34 C_FILE=$1; shift
35 H_FILE=$1; shift
36 TMP_C_FILE=tmp-${C_FILE}
37 TMP_H_FILE=tmp-${H_FILE}
38
39 ${AWK} '
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
44 do { getline tmp;
45 if (!(tmp ~ "^[ \t]*(;|$)"))
46 record = record "\034" tmp
47 } while (tmp != "")
48 print record
49 }
50 ' "$@" | ${SORT} | ${AWK} '
51 function switch_flags (flags, result)
52 {
53 flags = " " flags " "
54 result = "0"
55 for (j = 0; j < n_langs; j++) {
56 regex = " " langs[j] " "
57 gsub ( "\\+", "\\+", regex )
58 if (flags ~ regex)
59 result = result " | " macros[j]
60 }
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 )
71 return result
72 }
73 function var_args(flags)
74 {
75 if (flags !~ "Var\\(")
76 return "";
77 sub(".*Var\\(", "", flags);
78 sub("\\).*", "", flags);
79
80 return flags;
81 }
82 function var_name(flags)
83 {
84 s = var_args(flags);
85 if (s == "")
86 return "";
87 sub( ",.*", "", s);
88 return s;
89 }
90 function var_set(flags)
91 {
92 s = var_args(flags);
93 if (s !~ ",")
94 return "0, 0";
95 sub( "[^,]*,", "", s);
96 return "1, " s;
97 }
98 function var_ref(flags)
99 {
100 name = var_name(flags);
101 if (name == "")
102 return "0";
103 else
104 return "&" name;
105 }
106
107 BEGIN {
108 FS = "\034"
109 n_opts = 0
110 n_langs = 0
111 }
112
113 # Collect the text and flags of each option into an array
114 {
115 if ($1 == "Language") {
116 langs[n_langs] = $2
117 n_langs++;
118 } else {
119 opts[n_opts] = $1
120 flags[n_opts] = $2
121 help[n_opts] = $3
122 n_opts++;
123 }
124 }
125
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.
128 END {
129 c_file = "'${TMP_C_FILE}'"
130 h_file = "'${TMP_H_FILE}'"
131 realh_file = "'${H_FILE}'"
132 comma = ","
133
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
138
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
142
143 for (i = 0; i < n_opts; i++) {
144 name = var_name(flags[i]);
145 if (name == "")
146 continue;
147
148 printf ("/* Set by -%s.\n %s */\nextern int %s;\n\n",
149 opts[i], help[i], name) >> h_file
150
151 if (flags[i] ~ "VarExists")
152 continue;
153
154 if (flags[i] ~ "Init\\(")
155 {
156 init = flags[i];
157 sub(".*Init\\(","",init);
158 sub("\\).*","",init);
159 init = " = " init;
160 }
161 else
162 init = "";
163
164 printf ("/* Set by -%s.\n %s */\nint %s%s;\n\n",
165 opts[i], help[i], name,init) >> c_file
166 }
167
168
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
176 }
177
178 print " 0\n};\n" >> c_file
179 print "const unsigned int cl_options_count = N_OPTS;\n" >> c_file
180
181 print "const struct cl_option cl_options[] =\n{" >> c_file
182
183 print "\nenum opt_code\n{" >> h_file
184
185 for (i = 0; i < n_opts; i++)
186 back_chain[i] = "N_OPTS";
187
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
191 # ends, for example.
192 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
193 flags[i + 1] = flags[i] " " flags[i + 1];
194 i++;
195 }
196
197 len = length (opts[i]);
198 enum = "OPT_" opts[i]
199 if (opts[i] == "finline-limit=")
200 enum = enum "eq"
201 gsub ("[^A-Za-z0-9]", "_", enum)
202
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])
211 break;
212 back_chain[j] = enum;
213 }
214 }
215
216 s = substr(" ", length (opts[i]))
217 if (i + 1 == n_opts)
218 comma = ""
219
220 if (help[i] == "")
221 hlp = "0"
222 else
223 hlp = "N_(\"" help[i] "\")";
224
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
230 }
231
232 print " N_OPTS\n};\n" >> h_file
233 print "#endif /* OPTIONS_H */" >> h_file
234 print "};" >> c_file
235 }
236 '
237
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}