OSDN Git Service

985cf9668c21833640c940dc907865c28a7bcedb
[pf3gnuchains/gcc-fork.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 outfile.c outfile.h file1.opt [file2.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 C_FILE=$1; shift
34 H_FILE=$1; shift
35
36 ${AWK} '
37         # Ignore comments and blank lines
38         /^[ \t]*(;|$)/  { next }
39         # Note that RS="" falls foul of gawk 3.1.2 bugs
40         /^[^ \t]/       { record = $0
41                           do { getline tmp;
42                                if (tmp != "" )
43                                   record = record "\034" tmp
44                           } while (tmp != "")
45                           print record
46                         }
47 ' "$@" | ${SORT} | ${AWK} '
48     function switch_flags (flags,   result)
49     {
50         flags = " " flags " "
51         result = "0"
52         for (j = 0; j < n_langs; j++) {
53             regex = " " langs[j] " "
54             gsub ( "\\+", "\\+", regex )
55             if (flags ~ regex)
56                 result = result " | " macros[j]
57         }
58         if (flags ~ " Common ") result = result " | CL_COMMON"
59         if (flags ~ " Joined ") result = result " | CL_JOINED"
60         if (flags ~ " JoinedOrMissing ") \
61                 result = result " | CL_JOINED | CL_MISSING_OK"
62         if (flags ~ " Separate ") result = result " | CL_SEPARATE"
63         if (flags ~ " RejectNegative ") result = result " | CL_REJECT_NEGATIVE"
64         if (flags ~ " UInteger ") result = result " | CL_UINTEGER"
65         sub( "^0 \\| ", "", result )
66         return result
67     }
68
69     BEGIN {
70         FS = "\034"
71         n_opts = 0
72         n_langs = 0
73     }
74
75 # Collect the text and flags of each option into an array
76     {
77         if ($1 == "Language") {
78                 langs[n_langs] = $2
79                 n_langs++;
80         } else {
81                 opts[n_opts] = $1
82                 flags[n_opts] = $2
83                 help[n_opts] = $3
84                 n_opts++;
85         }
86     }
87
88 # Dump out an enumeration into a .h file, and an array of options into a
89 # C file.  Combine the flags of duplicate options.
90     END {
91         c_file = "'${C_FILE}'"
92         h_file = "'${H_FILE}'"
93         comma = ","
94
95         print "/* This file is auto-generated by opts.sh.  */\n" > c_file
96         print "#include <intl.h>"                       >> c_file
97         print "#include \"" h_file "\""                 >> c_file
98         print "#include \"opts.h\"\n"                   >> c_file
99         print "const char * const lang_names[] =\n{"    >> c_file
100
101         print "/* This file is auto-generated by opts.sh.  */\n" > h_file
102         for (i = 0; i < n_langs; i++) {
103             macros[i] = "CL_" langs[i]
104             gsub( "[^A-Za-z0-9_]", "X", macros[i] )
105             s = substr("         ", length (macros[i]))
106             print "#define " macros[i] s " (1 << " i ")" >> h_file
107             print "  \"" langs[i] "\","                 >> c_file
108         }
109
110         print "  0\n};\n"                               >> c_file
111         print "const unsigned int cl_options_count = N_OPTS;\n" >> c_file
112         print "const struct cl_option cl_options[] =\n{" >> c_file
113
114         print "\nenum opt_code\n{"                      >> h_file
115
116         for (i = 0; i < n_opts; i++)
117             back_chain[i] = "N_OPTS";
118
119         for (i = 0; i < n_opts; i++) {
120             # Combine the flags of identical switches.  Switches
121             # appear many times if they are handled by many front
122             # ends, for example.
123             while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
124                 flags[i + 1] = flags[i] " " flags[i + 1];
125                 i++;
126             }
127
128             len = length (opts[i]);
129             enum = "OPT_" opts[i]
130             if (opts[i] == "finline-limit=")
131                 enum = enum "eq"
132             gsub ("[^A-Za-z0-9]", "_", enum)
133
134             # If this switch takes joined arguments, back-chain all
135             # subsequent switches to it for which it is a prefix.  If
136             # a later switch S is a longer prefix of a switch T, T
137             # will be back-chained to S in a later iteration of this
138             # for() loop, which is what we want.
139             if (flags[i] ~ "Joined") {
140                 for (j = i + 1; j < n_opts; j++) {
141                     if (substr (opts[j], 1, len) != opts[i])
142                         break;
143                     back_chain[j] = enum;
144                 }
145             }
146
147             s = substr("                                  ", length (opts[i]))
148             if (i + 1 == n_opts)
149                 comma = ""
150
151             if (help[i] == "")
152                 hlp = "0"
153             else
154                 hlp = "N_(\"" help[i] "\")";
155
156             printf("  %s,%s/* -%s */\n", enum, s, opts[i]) >> h_file
157             printf("  { \"-%s\",\n    %s,\n    %s, %u, %s }%s\n",
158                    opts[i], hlp, back_chain[i], len,
159                    switch_flags(flags[i]), comma)       >> c_file
160         }
161
162         print "  N_OPTS\n};"                            >> h_file
163         print "};"                                      >> c_file
164     }
165 '