1 /* Functions for handling dependency tracking when reading .class files.
3 Copyright (C) 1998 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
PROTO ((struct entry
**));
43 static void add_entry
PROTO ((struct entry
**, const char *));
44 static const char *munge
PROTO ((const char *));
45 static int print_ents
PROTO ((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 entry list. */
83 add_entry (entp
, name
)
89 for (ent
= *entp
; ent
!= NULL
; ent
= ent
->next
)
90 if (! strcmp (ent
->file
, name
))
93 ent
= (struct entry
*) malloc (sizeof (struct entry
));
94 ent
->file
= strdup (name
);
99 /* Call this to reset the dependency module. This is required if
100 multiple dependency files are being generated from a single tool
103 jcf_dependency_reset ()
105 free_entry (&dependencies
);
106 free_entry (&targets
);
110 if (dep_out
!= stdout
)
117 jcf_dependency_set_target (name
)
120 free_entry (&targets
);
122 add_entry (&targets
, name
);
126 jcf_dependency_add_target (name
)
129 add_entry (&targets
, name
);
133 jcf_dependency_set_dep_file (name
)
136 assert (dep_out
!= stdout
);
139 if (! strcmp (name
, "-"))
142 dep_out
= fopen (name
, "w");
146 jcf_dependency_add_file (filename
, system_p
)
147 const char *filename
;
150 /* Just omit system files. */
151 if (system_p
&& ! system_files
)
154 add_entry (&dependencies
, filename
);
158 jcf_dependency_init (system_p
)
161 system_files
= system_p
;
164 /* FIXME: this is taken almost directly from cccp.c. Such duplication
168 const char *filename
;
170 static char *buffer
= NULL
;
171 static int buflen
= 0;
173 int len
= 2 * strlen (filename
) + 1;
181 buffer
= malloc (buflen
);
183 buffer
= realloc (buffer
, buflen
);
187 for (p
= filename
; *p
; ++p
)
194 /* GNU make uses a weird quoting scheme for white space.
195 A space or tab preceded by 2N+1 backslashes represents
196 N backslashes followed by space; a space or tab
197 preceded by 2N backslashes represents N backslashes at
198 the end of a file name; and backslashes in other
199 contexts should not be doubled. */
201 for (q
= p
- 1; filename
< q
&& q
[-1] == '\\'; q
--)
209 /* Fall through. This can mishandle things like "$(" but
210 there's no easy fix. */
213 /* This can mishandle characters in the string "\0\n%*?[\\~";
214 exactly which chars are mishandled depends on the `make' version.
215 We know of no portable solution for this;
216 even GNU make 3.76.1 doesn't solve the problem entirely.
217 (Also, '\0' is mishandled due to our calling conventions.) */
227 /* Helper to print list of files. */
229 print_ents (ent
, column
)
235 for (; ent
!= NULL
; ent
= ent
->next
)
237 const char *depname
= munge (ent
->file
);
238 int len
= strlen (depname
);
240 if (column
+ len
+ 2 > MAX_OUTPUT_COLUMNS
)
242 fprintf (dep_out
, " \\\n ");
247 fputs (" ", dep_out
);
248 fputs (depname
, dep_out
);
257 jcf_dependency_write ()
265 column
= print_ents (targets
, 0);
266 fputs (" : ", dep_out
);
268 print_ents (dependencies
, column
);
269 fputs ("\n", dep_out
);