/src/binutils-gdb/gas/depend.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* depend.c - Handle dependency tracking. |
2 | | Copyright (C) 1997-2025 Free Software Foundation, Inc. |
3 | | |
4 | | This file is part of GAS, the GNU Assembler. |
5 | | |
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) |
9 | | any later version. |
10 | | |
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. |
15 | | |
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 |
19 | | 02110-1301, USA. */ |
20 | | |
21 | | #include "as.h" |
22 | | #include "filenames.h" |
23 | | |
24 | | /* The file to write to, or NULL if no dependencies being kept. */ |
25 | | static char * dep_file = NULL; |
26 | | |
27 | | struct dependency |
28 | | { |
29 | | char * file; |
30 | | struct dependency * next; |
31 | | }; |
32 | | |
33 | | /* All the files we depend on. */ |
34 | | static struct dependency * dep_chain = NULL; |
35 | | |
36 | | /* Current column in output file. */ |
37 | | static int column = 0; |
38 | | |
39 | | static int quote_string_for_make (FILE *, const char *); |
40 | | static void wrap_output (FILE *, const char *, int); |
41 | | |
42 | | /* Number of columns allowable. */ |
43 | 0 | #define MAX_COLUMNS 72 |
44 | | |
45 | | /* Start saving dependencies, to be written to FILENAME. If this is |
46 | | never called, then dependency tracking is simply skipped. */ |
47 | | |
48 | | void |
49 | | start_dependencies (char *filename) |
50 | 0 | { |
51 | 0 | dep_file = filename; |
52 | 0 | } |
53 | | |
54 | | /* Noticed a new filename, so try to register it. */ |
55 | | |
56 | | void |
57 | | register_dependency (const char *filename) |
58 | 460 | { |
59 | 460 | struct dependency *dep; |
60 | | |
61 | 460 | if (dep_file == NULL) |
62 | 460 | return; |
63 | | |
64 | 0 | for (dep = dep_chain; dep != NULL; dep = dep->next) |
65 | 0 | { |
66 | 0 | if (!filename_cmp (filename, dep->file)) |
67 | 0 | return; |
68 | 0 | } |
69 | | |
70 | 0 | dep = XNEW (struct dependency); |
71 | 0 | dep->file = xstrdup (filename); |
72 | 0 | dep->next = dep_chain; |
73 | 0 | dep_chain = dep; |
74 | 0 | } |
75 | | |
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. |
79 | | |
80 | | This code is taken from gcc with only minor changes. */ |
81 | | |
82 | | static int |
83 | | quote_string_for_make (FILE *file, const char *src) |
84 | 0 | { |
85 | 0 | const char *p = src; |
86 | 0 | int i = 0; |
87 | |
|
88 | 0 | for (;;) |
89 | 0 | { |
90 | 0 | char c = *p++; |
91 | |
|
92 | 0 | switch (c) |
93 | 0 | { |
94 | 0 | case '\0': |
95 | 0 | case ' ': |
96 | 0 | case '\t': |
97 | 0 | { |
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 | 0 | const char *q; |
105 | |
|
106 | 0 | for (q = p - 1; src < q && q[-1] == '\\'; q--) |
107 | 0 | { |
108 | 0 | if (file) |
109 | 0 | putc ('\\', file); |
110 | 0 | i++; |
111 | 0 | } |
112 | 0 | } |
113 | 0 | if (!c) |
114 | 0 | return i; |
115 | 0 | if (file) |
116 | 0 | putc ('\\', file); |
117 | 0 | i++; |
118 | 0 | goto ordinary_char; |
119 | | |
120 | 0 | case '$': |
121 | 0 | if (file) |
122 | 0 | putc (c, file); |
123 | 0 | i++; |
124 | | /* Fall through. */ |
125 | | /* This can mishandle things like "$(" but there's no easy fix. */ |
126 | 0 | default: |
127 | 0 | ordinary_char: |
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.) */ |
133 | 0 | if (file) |
134 | 0 | putc (c, file); |
135 | 0 | i++; |
136 | 0 | break; |
137 | 0 | } |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | | /* Append some output to the file, keeping track of columns and doing |
142 | | wrapping as necessary. */ |
143 | | |
144 | | static void |
145 | | wrap_output (FILE *f, const char *string, int spacer) |
146 | 0 | { |
147 | 0 | int len = quote_string_for_make (NULL, string); |
148 | |
|
149 | 0 | if (len == 0) |
150 | 0 | return; |
151 | | |
152 | 0 | if (column |
153 | 0 | && (MAX_COLUMNS |
154 | 0 | - 1 /* spacer */ |
155 | 0 | - 2 /* ` \' */ |
156 | 0 | < column + len)) |
157 | 0 | { |
158 | 0 | fprintf (f, " \\\n "); |
159 | 0 | column = 0; |
160 | 0 | if (spacer == ' ') |
161 | 0 | spacer = '\0'; |
162 | 0 | } |
163 | |
|
164 | 0 | if (spacer == ' ') |
165 | 0 | { |
166 | 0 | putc (spacer, f); |
167 | 0 | ++column; |
168 | 0 | } |
169 | |
|
170 | 0 | quote_string_for_make (f, string); |
171 | 0 | column += len; |
172 | |
|
173 | 0 | if (spacer == ':') |
174 | 0 | { |
175 | 0 | putc (spacer, f); |
176 | 0 | ++column; |
177 | 0 | } |
178 | 0 | } |
179 | | |
180 | | /* Print dependency file. */ |
181 | | |
182 | | void |
183 | | print_dependencies (void) |
184 | 0 | { |
185 | 0 | FILE *f; |
186 | 0 | struct dependency *dep; |
187 | |
|
188 | 0 | if (dep_file == NULL) |
189 | 0 | return; |
190 | | |
191 | 0 | f = fopen (dep_file, FOPEN_WT); |
192 | 0 | if (f == NULL) |
193 | 0 | { |
194 | 0 | as_warn (_("can't open `%s' for writing"), dep_file); |
195 | 0 | return; |
196 | 0 | } |
197 | | |
198 | 0 | column = 0; |
199 | 0 | wrap_output (f, out_file_name, ':'); |
200 | 0 | for (dep = dep_chain; dep != NULL; dep = dep->next) |
201 | 0 | wrap_output (f, dep->file, ' '); |
202 | |
|
203 | 0 | putc ('\n', f); |
204 | |
|
205 | 0 | if (fclose (f)) |
206 | 0 | as_warn (_("can't close `%s'"), dep_file); |
207 | 0 | } |