1 /* depend.c - Handle dependency tracking.
2 Copyright 1997, 1998, 2000, 2001, 2003, 2004, 2005, 2007
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 #include "filenames.h"
25 /* The file to write to, or NULL if no dependencies being kept. */
26 static char * dep_file
= NULL
;
31 struct dependency
* next
;
34 /* All the files we depend on. */
35 static struct dependency
* dep_chain
= NULL
;
37 /* Current column in output file. */
38 static int column
= 0;
40 static int quote_string_for_make (FILE *, char *);
41 static void wrap_output (FILE *, char *, int);
43 /* Number of columns allowable. */
44 #define MAX_COLUMNS 72
46 /* Start saving dependencies, to be written to FILENAME. If this is
47 never called, then dependency tracking is simply skipped. */
50 start_dependencies (char *filename
)
55 /* Noticed a new filename, so try to register it. */
58 register_dependency (char *filename
)
60 struct dependency
*dep
;
65 for (dep
= dep_chain
; dep
!= NULL
; dep
= dep
->next
)
67 if (!filename_cmp (filename
, dep
->file
))
71 dep
= (struct dependency
*) xmalloc (sizeof (struct dependency
));
72 dep
->file
= xstrdup (filename
);
73 dep
->next
= dep_chain
;
77 /* Quote a file name the way `make' wants it, and print it to FILE.
78 If FILE is NULL, do no printing, but return the length of the
81 This code is taken from gcc with only minor changes. */
84 quote_string_for_make (FILE *file
, char *src
)
99 /* GNU make uses a weird quoting scheme for white space.
100 A space or tab preceded by 2N+1 backslashes represents
101 N backslashes followed by space; a space or tab
102 preceded by 2N backslashes represents N backslashes at
103 the end of a file name; and backslashes in other
104 contexts should not be doubled. */
107 for (q
= p
- 1; src
< q
&& q
[-1] == '\\'; q
--)
125 /* Fall through. This can mishandle things like "$(" but
126 there's no easy fix. */
129 /* This can mishandle characters in the string "\0\n%*?[\\~";
130 exactly which chars are mishandled depends on the `make' version.
131 We know of no portable solution for this;
132 even GNU make 3.76.1 doesn't solve the problem entirely.
133 (Also, '\0' is mishandled due to our calling conventions.) */
142 /* Append some output to the file, keeping track of columns and doing
143 wrapping as necessary. */
146 wrap_output (FILE *f
, char *string
, int spacer
)
148 int len
= quote_string_for_make (NULL
, string
);
159 fprintf (f
, " \\\n ");
171 quote_string_for_make (f
, string
);
181 /* Print dependency file. */
184 print_dependencies (void)
187 struct dependency
*dep
;
189 if (dep_file
== NULL
)
192 f
= fopen (dep_file
, FOPEN_WT
);
195 as_warn (_("can't open `%s' for writing"), dep_file
);
200 wrap_output (f
, out_file_name
, ':');
201 for (dep
= dep_chain
; dep
!= NULL
; dep
= dep
->next
)
202 wrap_output (f
, dep
->file
, ' ');
207 as_warn (_("can't close `%s'"), dep_file
);