Fixtypos in ChangeLogs, fix copyright dates in files
[binutils.git] / gas / depend.c
blobc6538ddfd0fbe40d1e5c176f20ede5c66fd8a5db
1 /* depend.c - Handle dependency tracking.
2 Copyright 1997, 1998, 2000 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GAS 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.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #include "as.h"
23 /* The file to write to, or NULL if no dependencies being kept. */
24 static char *dep_file = NULL;
26 struct dependency {
27 char *file;
28 struct dependency *next;
31 /* All the files we depend on. */
32 static struct dependency *dep_chain = NULL;
34 /* Current column in output file. */
35 static int column = 0;
37 static int quote_string_for_make PARAMS ((FILE *, char *));
38 static void wrap_output PARAMS ((FILE *, char *, int));
40 /* Number of columns allowable. */
41 #define MAX_COLUMNS 72
43 /* Start saving dependencies, to be written to FILENAME. If this is
44 never called, then dependency tracking is simply skipped. */
46 void
47 start_dependencies (filename)
48 char *filename;
50 dep_file = filename;
53 /* Noticed a new filename, so try to register it. */
55 void
56 register_dependency (filename)
57 char *filename;
59 struct dependency *dep;
61 if (dep_file == NULL)
62 return;
64 for (dep = dep_chain; dep != NULL; dep = dep->next)
66 if (!strcmp (filename, dep->file))
67 return;
70 dep = (struct dependency *) xmalloc (sizeof (struct dependency));
71 dep->file = xstrdup (filename);
72 dep->next = dep_chain;
73 dep_chain = dep;
76 /* Quote a file name the way `make' wants it, and print it to FILE.
77 If FILE is NULL, do no printing, but return the length of the
78 quoted string.
80 This code is taken from gcc with only minor changes. */
82 static int
83 quote_string_for_make (file, src)
84 FILE *file;
85 char *src;
87 char *p = src;
88 int i = 0;
89 for (;;)
91 char c = *p++;
92 switch (c)
94 case '\0':
95 case ' ':
96 case '\t':
98 /* GNU make uses a weird quoting scheme for white space.
99 A space or tab preceded by 2N+1 backslashes represents
100 N backslashes followed by space; a space or tab
101 preceded by 2N backslashes represents N backslashes at
102 the end of a file name; and backslashes in other
103 contexts should not be doubled. */
104 char *q;
105 for (q = p - 1; src < q && q[-1] == '\\'; q--)
107 if (file)
108 putc ('\\', file);
109 i++;
112 if (!c)
113 return i;
114 if (file)
115 putc ('\\', file);
116 i++;
117 goto ordinary_char;
119 case '$':
120 if (file)
121 putc (c, file);
122 i++;
123 /* Fall through. This can mishandle things like "$(" but
124 there's no easy fix. */
125 default:
126 ordinary_char:
127 /* This can mishandle characters in the string "\0\n%*?[\\~";
128 exactly which chars are mishandled depends on the `make' version.
129 We know of no portable solution for this;
130 even GNU make 3.76.1 doesn't solve the problem entirely.
131 (Also, '\0' is mishandled due to our calling conventions.) */
132 if (file)
133 putc (c, file);
134 i++;
135 break;
140 /* Append some output to the file, keeping track of columns and doing
141 wrapping as necessary. */
143 static void
144 wrap_output (f, string, spacer)
145 FILE *f;
146 char *string;
147 int spacer;
149 int len = quote_string_for_make (NULL, string);
151 if (len == 0)
152 return;
154 if (column
155 && (MAX_COLUMNS
156 - 1 /* spacer */
157 - 2 /* ` \' */
158 < column + len))
160 fprintf (f, " \\\n ");
161 column = 0;
162 if (spacer == ' ')
163 spacer = '\0';
166 if (spacer == ' ')
168 putc (spacer, f);
169 ++column;
172 quote_string_for_make (f, string);
173 column += len;
175 if (spacer == ':')
177 putc (spacer, f);
178 ++column;
182 /* Print dependency file. */
184 void
185 print_dependencies ()
187 FILE *f;
188 struct dependency *dep;
190 if (dep_file == NULL)
191 return;
193 f = fopen (dep_file, "w");
194 if (f == NULL)
196 as_warn (_("Can't open `%s' for writing"), dep_file);
197 return;
200 column = 0;
201 wrap_output (f, out_file_name, ':');
202 for (dep = dep_chain; dep != NULL; dep = dep->next)
203 wrap_output (f, dep->file, ' ');
205 putc ('\n', f);
207 if (fclose (f))
208 as_warn (_("Can't close `%s'"), dep_file);