OSDN Git Service

* Makefile.in (options.h): Depend on Makefile. Add move-if-change
[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 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 != "" )
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         sub( "^0 \\| ", "", result )
69         return result
70     }
71
72     BEGIN {
73         FS = "\034"
74         n_opts = 0
75         n_langs = 0
76     }
77
78 # Collect the text and flags of each option into an array
79     {
80         if ($1 == "Language") {
81                 langs[n_langs] = $2
82                 n_langs++;
83         } else {
84                 opts[n_opts] = $1
85                 flags[n_opts] = $2
86                 help[n_opts] = $3
87                 n_opts++;
88         }
89     }
90
91 # Dump out an enumeration into a .h file, and an array of options into a
92 # C file.  Combine the flags of duplicate options.
93     END {
94         c_file = "'${TMP_C_FILE}'"
95         h_file = "'${TMP_H_FILE}'"
96         realh_file = "'${H_FILE}'"
97         comma = ","
98
99         print "/* This file is auto-generated by opts.sh.  */\n" > c_file
100         print "#include <intl.h>"                       >> c_file
101         print "#include \"" realh_file "\""             >> c_file
102         print "#include \"opts.h\"\n"                   >> c_file
103         print "const char * const lang_names[] =\n{"    >> c_file
104
105         print "/* This file is auto-generated by opts.sh.  */\n" > h_file
106         for (i = 0; i < n_langs; i++) {
107             macros[i] = "CL_" langs[i]
108             gsub( "[^A-Za-z0-9_]", "X", macros[i] )
109             s = substr("         ", length (macros[i]))
110             print "#define " macros[i] s " (1 << " i ")" >> h_file
111             print "  \"" langs[i] "\","                 >> c_file
112         }
113
114         print "  0\n};\n"                               >> c_file
115         print "const unsigned int cl_options_count = N_OPTS;\n" >> c_file
116         print "const struct cl_option cl_options[] =\n{" >> c_file
117
118         print "\nenum opt_code\n{"                      >> h_file
119
120         for (i = 0; i < n_opts; i++)
121             back_chain[i] = "N_OPTS";
122
123         for (i = 0; i < n_opts; i++) {
124             # Combine the flags of identical switches.  Switches
125             # appear many times if they are handled by many front
126             # ends, for example.
127             while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
128                 flags[i + 1] = flags[i] " " flags[i + 1];
129                 i++;
130             }
131
132             len = length (opts[i]);
133             enum = "OPT_" opts[i]
134             if (opts[i] == "finline-limit=")
135                 enum = enum "eq"
136             gsub ("[^A-Za-z0-9]", "_", enum)
137
138             # If this switch takes joined arguments, back-chain all
139             # subsequent switches to it for which it is a prefix.  If
140             # a later switch S is a longer prefix of a switch T, T
141             # will be back-chained to S in a later iteration of this
142             # for() loop, which is what we want.
143             if (flags[i] ~ "Joined") {
144                 for (j = i + 1; j < n_opts; j++) {
145                     if (substr (opts[j], 1, len) != opts[i])
146                         break;
147                     back_chain[j] = enum;
148                 }
149             }
150
151             s = substr("                                  ", length (opts[i]))
152             if (i + 1 == n_opts)
153                 comma = ""
154
155             if (help[i] == "")
156                 hlp = "0"
157             else
158                 hlp = "N_(\"" help[i] "\")";
159
160             printf("  %s,%s/* -%s */\n", enum, s, opts[i]) >> h_file
161             printf("  { \"-%s\",\n    %s,\n    %s, %u, %s }%s\n",
162                    opts[i], hlp, back_chain[i], len,
163                    switch_flags(flags[i]), comma)       >> c_file
164         }
165
166         print "  N_OPTS\n};"                            >> h_file
167         print "};"                                      >> c_file
168     }
169 '
170
171 # Copy the newly generated files back to the correct names only if different.
172 # This is to prevent a cascade of file rebuilds when not necessary.
173 ${MOVEIFCHANGE} ${TMP_H_FILE} ${H_FILE}
174 ${MOVEIFCHANGE} ${TMP_C_FILE} ${C_FILE}