1 /* Dependency generator utility.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Contributed by Zack Weinberg, May 2004
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding! */
33 static const char *output_file
;
34 static bool had_errors
;
36 /* Option lists, to give to cpplib before each input file. */
39 struct cmd_line_macro
*next
;
44 static struct cmd_line_macro
*cmd_line_macros
;
45 static cpp_dir
*cmd_line_searchpath
;
48 add_clm (const char *macro
, bool is_undef
)
50 struct cmd_line_macro
*clm
= xmalloc (sizeof (struct cmd_line_macro
));
51 clm
->next
= cmd_line_macros
;
52 clm
->is_undef
= is_undef
;
54 cmd_line_macros
= clm
;
58 add_dir (char *name
, bool sysp
)
60 cpp_dir
*dir
= xmalloc (sizeof (cpp_dir
));
61 dir
->next
= cmd_line_searchpath
;
65 dir
->user_supplied_p
= 1;
66 cmd_line_searchpath
= dir
;
69 /* Command line processing. */
71 static void ATTRIBUTE_NORETURN
75 "usage: %s [-vh] [-V vpath] [-Dname[=def]...] [-Uname] [-Idir...] [-o file] sources...\n",
81 parse_options (int argc
, char **argv
)
83 static const struct option longopts
[] = {
84 { "--help", no_argument
, 0, 'h' },
89 switch (getopt_long (argc
, argv
, "hD:U:I:J:o:V:", longopts
, 0))
92 case 'D': add_clm (optarg
, false); break;
93 case 'U': add_clm (optarg
, true); break;
94 case 'I': add_dir (optarg
, false); break;
95 case 'J': add_dir (optarg
, true); break;
99 fprintf (stderr
, "%s: too many output files\n", progname
);
102 output_file
= optarg
;
107 fprintf (stderr
, "%s: too many vpaths\n", progname
);
113 usage (2); /* getopt has issued the error message. */
115 case -1: /* end of options */
118 fprintf (stderr
, "%s: no input files\n", progname
);
128 /* Set up cpplib from command line options. */
130 reader_init (struct line_maps
*line_table
)
133 cpp_options
*options
;
135 linemap_init (line_table
);
136 reader
= cpp_create_reader (CLK_GNUC89
, 0, line_table
);
138 /* Ignore warnings and errors (we don't have access to system
139 headers). Request dependency output. */
140 options
= cpp_get_options (reader
);
141 options
->inhibit_warnings
= 1;
142 options
->inhibit_errors
= 1;
143 options
->deps
.style
= DEPS_USER
;
145 /* Further initialization. */
146 cpp_post_options (reader
);
147 cpp_init_iconv (reader
);
148 cpp_set_include_chains (reader
, cmd_line_searchpath
, cmd_line_searchpath
,
152 struct deps
*deps
= cpp_get_deps (reader
);
153 deps_add_vpath (deps
, vpath
);
159 /* Process one input source file. */
161 process_file (const char *file
)
163 struct line_maps line_table
;
164 cpp_reader
*reader
= reader_init (&line_table
);
166 if (!cpp_read_main_file (reader
, file
))
170 struct cmd_line_macro
*clm
;
172 cpp_init_builtins (reader
, true);
173 for (clm
= cmd_line_macros
; clm
; clm
= clm
->next
)
174 (clm
->is_undef
? cpp_undef
: cpp_define
) (reader
, clm
->macro
);
176 cpp_scan_nooutput (reader
);
177 if (cpp_finish (reader
, stdout
))
180 cpp_destroy (reader
);
181 linemap_free (&line_table
);
184 /* Master control. */
187 main(int argc
, char **argv
)
192 xmalloc_set_program_name (progname
);
194 first_input
= parse_options (argc
, argv
);
196 if (!freopen (output_file
, "w", stdout
))
198 perror (output_file
);
202 for (i
= first_input
; i
< argc
; i
++)
203 process_file (argv
[i
]);