Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / gcc-3.4 / 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         sub( "^0 \\| ", "", result )
70         return result
71     }
72
73     BEGIN {
74         FS = "\034"
75         n_opts = 0
76         n_langs = 0
77     }
78
79 # Collect the text and flags of each option into an array
80     {
81         if ($1 == "Language") {
82                 langs[n_langs] = $2
83                 n_langs++;
84         } else {
85                 opts[n_opts] = $1
86                 flags[n_opts] = $2
87                 help[n_opts] = $3
88                 n_opts++;
89         }
90     }
91
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.
94     END {
95         c_file = "'${TMP_C_FILE}'"
96         h_file = "'${TMP_H_FILE}'"
97         realh_file = "'${H_FILE}'"
98         comma = ","
99
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
105
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
113         }
114
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
118
119         print "\nenum opt_code\n{"                      >> h_file
120
121         for (i = 0; i < n_opts; i++)
122             back_chain[i] = "N_OPTS";
123
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
127             # ends, for example.
128             while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
129                 flags[i + 1] = flags[i] " " flags[i + 1];
130                 i++;
131             }
132
133             len = length (opts[i]);
134             enum = "OPT_" opts[i]
135             if (opts[i] == "finline-limit=")
136                 enum = enum "eq"
137             gsub ("[^A-Za-z0-9]", "_", enum)
138
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])
147                         break;
148                     back_chain[j] = enum;
149                 }
150             }
151
152             s = substr("                                  ", length (opts[i]))
153             if (i + 1 == n_opts)
154                 comma = ""
155
156             if (help[i] == "")
157                 hlp = "0"
158             else
159                 hlp = "N_(\"" help[i] "\")";
160
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
165         }
166
167         print "  N_OPTS\n};"                            >> h_file
168         print "};"                                      >> c_file
169     }
170 '
171
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}