1 /* Functions for handling dependency tracking when reading .class files.
3 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
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 GNU CC; see the file COPYING. If not, write to
17 the Free Software Foundation, 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Written by Tom Tromey <tromey@cygnus.com>, October 1998. */
35 /* We keep a linked list of all the files we've already read. */
42 static void free_entry PARAMS ((struct entry **));
43 static void add_entry PARAMS ((struct entry **, const char *));
44 static const char *munge PARAMS ((const char *));
45 static int print_ents PARAMS ((struct entry *, int));
48 static struct entry *dependencies = NULL;
50 /* Name of targets. We support multiple targets when writing .class
52 static struct entry *targets = NULL;
54 /* Number of columns in output. */
55 #define MAX_OUTPUT_COLUMNS 72
57 /* The output file, or NULL if we aren't doing dependency tracking. */
58 static FILE *dep_out = NULL;
60 /* Nonzero if system files should be added. */
61 static int system_files;
65 /* Helper to free an entry list. */
70 struct entry *ent, *next;
72 for (ent = *entp; ent != NULL; ent = next)
81 /* Helper to add to the end of the entry list. */
83 add_entry (entp, name)
87 struct entry *ent, *last;
89 for (last = ent = *entp; ent != NULL; last = ent, ent = ent->next)
90 if (! strcmp (ent->file, name))
93 ent = (struct entry *) xmalloc (sizeof (struct entry));
94 ent->file = xstrdup (name);
99 // This is only true the first time through, when the entry list
107 /* Call this to reset the dependency module. This is required if
108 multiple dependency files are being generated from a single tool
111 jcf_dependency_reset ()
113 free_entry (&dependencies);
114 free_entry (&targets);
118 if (dep_out != stdout)
125 jcf_dependency_set_target (name)
128 free_entry (&targets);
130 add_entry (&targets, name);
134 jcf_dependency_add_target (name)
137 add_entry (&targets, name);
141 jcf_dependency_set_dep_file (name)
144 assert (dep_out != stdout);
147 if (! strcmp (name, "-"))
150 dep_out = fopen (name, "w");
154 jcf_dependency_add_file (filename, system_p)
155 const char *filename;
158 /* Just omit system files. */
159 if (system_p && ! system_files)
162 add_entry (&dependencies, filename);
166 jcf_dependency_init (system_p)
169 system_files = system_p;
172 /* FIXME: this is taken almost directly from cccp.c. Such duplication
176 const char *filename;
178 static char *buffer = NULL;
179 static int buflen = 0;
181 int len = 2 * strlen (filename) + 1;
188 buffer = xrealloc (buffer, buflen);
192 for (p = filename; *p; ++p)
199 /* GNU make uses a weird quoting scheme for white space.
200 A space or tab preceded by 2N+1 backslashes represents
201 N backslashes followed by space; a space or tab
202 preceded by 2N backslashes represents N backslashes at
203 the end of a file name; and backslashes in other
204 contexts should not be doubled. */
206 for (q = p - 1; filename < q && q[-1] == '\\'; q--)
214 /* Fall through. This can mishandle things like "$(" but
215 there's no easy fix. */
218 /* This can mishandle characters in the string "\0\n%*?[\\~";
219 exactly which chars are mishandled depends on the `make' version.
220 We know of no portable solution for this;
221 even GNU make 3.76.1 doesn't solve the problem entirely.
222 (Also, '\0' is mishandled due to our calling conventions.) */
232 /* Helper to print list of files. */
234 print_ents (ent, column)
240 for (; ent != NULL; ent = ent->next)
242 const char *depname = munge (ent->file);
243 int len = strlen (depname);
245 if (column + len + 2 > MAX_OUTPUT_COLUMNS)
247 fprintf (dep_out, " \\\n ");
252 fputs (" ", dep_out);
253 fputs (depname, dep_out);
262 jcf_dependency_write ()
270 column = print_ents (targets, 0);
271 fputs (" : ", dep_out);
273 print_ents (dependencies, column);
274 fputs ("\n", dep_out);