1 /* depend.c - Handle dependency tracking.
2 Copyright (C) 1997-2020 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 3, or (at your option)
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, 51 Franklin Street - Fifth Floor, Boston, MA
22 #include "filenames.h"
24 /* The file to write to, or NULL if no dependencies being kept. */
25 static char * dep_file
= NULL
;
30 struct dependency
* next
;
33 /* All the files we depend on. */
34 static struct dependency
* dep_chain
= NULL
;
36 /* Current column in output file. */
37 static int column
= 0;
39 static int quote_string_for_make (FILE *, const char *);
40 static void wrap_output (FILE *, const char *, int);
42 /* Number of columns allowable. */
43 #define MAX_COLUMNS 72
45 /* Start saving dependencies, to be written to FILENAME. If this is
46 never called, then dependency tracking is simply skipped. */
49 start_dependencies (char *filename
)
54 /* Noticed a new filename, so try to register it. */
57 register_dependency (const char *filename
)
59 struct dependency
*dep
;
64 for (dep
= dep_chain
; dep
!= NULL
; dep
= dep
->next
)
66 if (!filename_cmp (filename
, dep
->file
))
70 dep
= XNEW (struct dependency
);
71 dep
->file
= xstrdup (filename
);
72 dep
->next
= dep_chain
;
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
80 This code is taken from gcc with only minor changes. */
83 quote_string_for_make (FILE *file
, const char *src
)
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. */
106 for (q
= p
- 1; src
< q
&& q
[-1] == '\\'; q
--)
125 /* This can mishandle things like "$(" but there's no easy fix. */
128 /* This can mishandle characters in the string "\0\n%*?[\\~";
129 exactly which chars are mishandled depends on the `make' version.
130 We know of no portable solution for this;
131 even GNU make 3.76.1 doesn't solve the problem entirely.
132 (Also, '\0' is mishandled due to our calling conventions.) */
141 /* Append some output to the file, keeping track of columns and doing
142 wrapping as necessary. */
145 wrap_output (FILE *f
, const char *string
, int spacer
)
147 int len
= quote_string_for_make (NULL
, string
);
158 fprintf (f
, " \\\n ");
170 quote_string_for_make (f
, string
);
180 /* Print dependency file. */
183 print_dependencies (void)
186 struct dependency
*dep
;
188 if (dep_file
== NULL
)
191 f
= fopen (dep_file
, FOPEN_WT
);
194 as_warn (_("can't open `%s' for writing"), dep_file
);
199 wrap_output (f
, out_file_name
, ':');
200 for (dep
= dep_chain
; dep
!= NULL
; dep
= dep
->next
)
201 wrap_output (f
, dep
->file
, ' ');
206 as_warn (_("can't close `%s'"), dep_file
);