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 sub( "^0 \\| ", "", result )
79 # Collect the text and flags of each option into an array
81 if ($1 == "Language") {
92 # Dump out an enumeration into a .h file, and an array of options into a
93 # C file. Combine the flags of duplicate options.
95 c_file = "'${TMP_C_FILE}'"
96 h_file = "'${TMP_H_FILE}'"
97 realh_file = "'${H_FILE}'"
100 print "/* This file is auto-generated by opts.sh. */\n" > c_file
101 print "#include <intl.h>" >> c_file
102 print "#include \"" realh_file "\"" >> c_file
103 print "#include \"opts.h\"\n" >> c_file
104 print "const char * const lang_names[] =\n{" >> c_file
106 print "/* This file is auto-generated by opts.sh. */\n" > h_file
107 for (i = 0; i < n_langs; i++) {
108 macros[i] = "CL_" langs[i]
109 gsub( "[^A-Za-z0-9_]", "X", macros[i] )
110 s = substr(" ", length (macros[i]))
111 print "#define " macros[i] s " (1 << " i ")" >> h_file
112 print " \"" langs[i] "\"," >> c_file
115 print " 0\n};\n" >> c_file
116 print "const unsigned int cl_options_count = N_OPTS;\n" >> c_file
117 print "const struct cl_option cl_options[] =\n{" >> c_file
119 print "\nenum opt_code\n{" >> h_file
121 for (i = 0; i < n_opts; i++)
122 back_chain[i] = "N_OPTS";
124 for (i = 0; i < n_opts; i++) {
125 # Combine the flags of identical switches. Switches
126 # appear many times if they are handled by many front
128 while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
129 flags[i + 1] = flags[i] " " flags[i + 1];
133 len = length (opts[i]);
134 enum = "OPT_" opts[i]
135 if (opts[i] == "finline-limit=")
137 gsub ("[^A-Za-z0-9]", "_", enum)
139 # If this switch takes joined arguments, back-chain all
140 # subsequent switches to it for which it is a prefix. If
141 # a later switch S is a longer prefix of a switch T, T
142 # will be back-chained to S in a later iteration of this
143 # for() loop, which is what we want.
144 if (flags[i] ~ "Joined") {
145 for (j = i + 1; j < n_opts; j++) {
146 if (substr (opts[j], 1, len) != opts[i])
148 back_chain[j] = enum;
152 s = substr(" ", length (opts[i]))
159 hlp = "N_(\"" help[i] "\")";
161 printf(" %s,%s/* -%s */\n", enum, s, opts[i]) >> h_file
162 printf(" { \"-%s\",\n %s,\n %s, %u, %s }%s\n",
163 opts[i], hlp, back_chain[i], len,
164 switch_flags(flags[i]), comma) >> c_file
167 print " N_OPTS\n};" >> h_file
172 # Copy the newly generated files back to the correct names only if different.
173 # This is to prevent a cascade of file rebuilds when not necessary.
174 ${MOVEIFCHANGE} ${TMP_H_FILE} ${H_FILE}
175 ${MOVEIFCHANGE} ${TMP_C_FILE} ${C_FILE}