aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.4.3/libcpp/makedepend.c
blob: c67f64e236ef37172f7b79383f14e9d20b36b882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* Dependency generator utility.
   Copyright (C) 2004 Free Software Foundation, Inc.
   Contributed by Zack Weinberg, May 2004

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

 In other words, you are welcome to use, share and improve this program.
 You are forbidden to forbid anyone else to use, share and improve
 what you give them.   Help stamp out software-hoarding!  */

#include "config.h"
#include "system.h"
#include "line-map.h"
#include "cpplib.h"
#include "getopt.h"
#include "mkdeps.h"

const char *progname;
const char *vpath;

static const char *output_file;
static bool had_errors;

/* Option lists, to give to cpplib before each input file.  */
struct cmd_line_macro
{
  struct cmd_line_macro *next;
  bool is_undef;
  const char *macro;
};

static struct cmd_line_macro *cmd_line_macros;
static cpp_dir *cmd_line_searchpath;

static void
add_clm (const char *macro, bool is_undef)
{
  struct cmd_line_macro *clm = XNEW (struct cmd_line_macro);
  clm->next = cmd_line_macros;
  clm->is_undef = is_undef;
  clm->macro = macro;
  cmd_line_macros = clm;
}

static void
add_dir (char *name, bool sysp)
{
  cpp_dir *dir = XNEW (cpp_dir);
  dir->next = cmd_line_searchpath;
  dir->name = name;
  dir->sysp = sysp;
  dir->construct = 0;
  dir->user_supplied_p = 1;
  cmd_line_searchpath = dir;
}

/* Command line processing.  */

static void ATTRIBUTE_NORETURN
usage (int errcode)
{
  fprintf (stderr,
"usage: %s [-vh] [-V vpath] [-Dname[=def]...] [-Uname] [-Idir...] [-o file] sources...\n",
	   progname);
  exit (errcode);
}

static int
parse_options (int argc, char **argv)
{
  static const struct option longopts[] = {
    { "--help", no_argument, 0, 'h' },
    { 0, 0, 0, 0 }
  };

  for (;;)
    switch (getopt_long (argc, argv, "hD:U:I:J:o:V:", longopts, 0))
      {
      case 'h': usage (0);
      case 'D': add_clm (optarg, false); break;
      case 'U': add_clm (optarg, true);  break;
      case 'I': add_dir (optarg, false); break;
      case 'J': add_dir (optarg, true);  break;
      case 'o':
	if (output_file)
	  {
	    fprintf (stderr, "%s: too many output files\n", progname);
	    usage (2);
	  }
	output_file = optarg;
	break;
      case 'V':
	if (vpath)
	  {
	    fprintf (stderr, "%s: too many vpaths\n", progname);
	    usage (2);
	  }
	vpath = optarg;
	break;
      case '?':
	usage (2);  /* getopt has issued the error message.  */

      case -1: /* end of options */
	if (optind == argc)
	  {
	    fprintf (stderr, "%s: no input files\n", progname);
	    usage (2);
	  }
	return optind;

      default:
	abort ();
      }
}

/* Set up cpplib from command line options.  */
static cpp_reader *
reader_init (struct line_maps *line_table)
{
  cpp_reader *reader;
  cpp_options *options;

  linemap_init (line_table);
  reader = cpp_create_reader (CLK_GNUC89, 0, line_table);

  /* Ignore warnings and errors (we don't have access to system
     headers).  Request dependency output.  */
  options = cpp_get_options (reader);
  options->inhibit_warnings = 1;
  options->inhibit_errors = 1;
  options->deps.style = DEPS_USER;

  /* Further initialization.  */
  cpp_post_options (reader);
  cpp_init_iconv (reader);
  cpp_set_include_chains (reader, cmd_line_searchpath, cmd_line_searchpath,
			  false);
  if (vpath)
    {
      struct deps *deps = cpp_get_deps (reader);
      deps_add_vpath (deps, vpath);
    }

  return reader;
}

/* Process one input source file.  */
static void
process_file (const char *file)
{
  struct line_maps line_table;
  cpp_reader *reader = reader_init (&line_table);

  if (!cpp_read_main_file (reader, file))
    had_errors = true;
  else
    {
      struct cmd_line_macro *clm;

      cpp_init_builtins (reader, true);
      for (clm = cmd_line_macros; clm; clm = clm->next)
	(clm->is_undef ? cpp_undef : cpp_define) (reader, clm->macro);

      cpp_scan_nooutput (reader);
      if (cpp_finish (reader, stdout))
	had_errors = true;
    }
  cpp_destroy (reader);
  linemap_free (&line_table);
}

/* Master control.  */

int
main(int argc, char **argv)
{
  int first_input, i;

  progname = argv[0];
  xmalloc_set_program_name (progname);

  first_input = parse_options (argc, argv);
  if (output_file)
    if (!freopen (output_file, "w", stdout))
      {
	perror (output_file);
	return 1;
      }

  for (i = first_input; i < argc; i++)
    process_file (argv[i]);

  return had_errors;
}