OSDN Git Service

2003-06-18 Michael Koch <konqueror@gmx.de>
[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 # Must unset, so that RS="" works in gawk 3.0-3.1.1 (possibly earlier too)
37 # Appears to be a gawk bug, RS="" is not an extension
38 unset POSIXLY_CORRECT
39
40 ${AWK} '
41         BEGIN{ RS=""; FS="\n" }
42         # Ignore comments and blank lines
43         /^[ \t]*(;|$)/  { next }
44         /^[^ \t]/       { gsub ("\n", "\034", $0); print }
45 ' "$@" | ${SORT} | ${AWK} '
46     function switch_flags (flags,   result)
47     {
48         flags = " " flags " "
49         result = "0"
50         for (j = 0; j < n_langs; j++) {
51             regex = " " langs[j] " "
52             gsub ( "\\+", "\\+", regex )
53             if (flags ~ regex)
54                 result = result " | " macros[j]
55         }
56         if (flags ~ " Common ") result = result " | CL_COMMON"
57         if (flags ~ " Joined ") result = result " | CL_JOINED"
58         if (flags ~ " JoinedOrMissing ") \
59                 result = result " | CL_JOINED | CL_MISSING_OK"
60         if (flags ~ " Separate ") result = result " | CL_SEPARATE"
61         if (flags ~ " RejectNegative ") result = result " | CL_REJECT_NEGATIVE"
62         if (flags ~ " UInteger ") result = result " | CL_UINTEGER"
63         sub( "^0 \\| ", "", result )
64         return result
65     }
66
67     BEGIN {
68         FS = "\034"
69         n_opts = 0
70         n_langs = 0
71     }
72
73 # Collect the text and flags of each option into an array
74     {
75         if ($1 == "Language") {
76                 langs[n_langs] = $2
77                 n_langs++;
78         } else {
79                 opts[n_opts] = $1
80                 flags[n_opts] = $2
81                 n_opts++;
82         }
83     }
84
85 # Dump out an enumeration into a .h file, and an array of options into a
86 # C file.  Combine the flags of duplicate options.
87     END {
88         c_file = "'${C_FILE}'"
89         h_file = "'${H_FILE}'"
90         comma = ","
91
92         print "/* This file is auto-generated by opts.sh.  */\n" > h_file
93         for (i = 0; i < n_langs; i++) {
94             macros[i] = "CL_" langs[i]
95             gsub( "[^A-Za-z0-9_]", "X", macros[i] )
96             s = substr("         ", length (macros[i]))
97             print "#define " macros[i] s " (1 << " i ")" >> h_file
98         }
99         print "\nenum opt_code\n{"                      >> h_file
100
101         print "/* This file is auto-generated by opts.sh.  */\n" > c_file
102         print "#include \"" h_file "\""                 >> c_file
103         print "#include \"opts.h\"\n"                   >> c_file
104         print "const unsigned int cl_options_count = N_OPTS;\n" >> c_file
105         print "const struct cl_option cl_options[] =\n{" >> c_file
106
107         for (i = 0; i < n_opts; i++) {
108             while( i + 1 != n_opts && opts[i] == opts[i + 1] ) {
109                 flags[i + 1] = flags[i] " " flags[i + 1];
110                 i++;
111             }
112
113             enum = "OPT_" opts[i]
114             gsub( "[^A-Za-z0-9]", "_", enum)
115             s = substr("                                  ", length (opts[i]))
116             if (i + 1 == n_opts)
117                 comma = ""
118
119             printf("  %s,%s/* -%s */\n", enum, s, opts[i]) >> h_file
120             printf("  { \"%s\", %u, %s }%s\n", opts[i], \
121                 length(opts[i]), switch_flags(flags[i]), comma) >> c_file
122         }
123
124         print "  N_OPTS\n};"                            >> h_file
125         print "};"                                      >> c_file
126     }
127 '