| 1 | /* depend.c - Handle dependency tracking. |
| 2 | Copyright 1997, 1998, 2000, 2001 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 2, 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, 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include "as.h" |
| 22 | |
| 23 | /* The file to write to, or NULL if no dependencies being kept. */ |
| 24 | static char * dep_file = NULL; |
| 25 | |
| 26 | struct dependency |
| 27 | { |
| 28 | char * file; |
| 29 | struct dependency * next; |
| 30 | }; |
| 31 | |
| 32 | /* All the files we depend on. */ |
| 33 | static struct dependency * dep_chain = NULL; |
| 34 | |
| 35 | /* Current column in output file. */ |
| 36 | static int column = 0; |
| 37 | |
| 38 | static int quote_string_for_make (FILE *, char *); |
| 39 | static void wrap_output (FILE *, char *, int); |
| 40 | |
| 41 | /* Number of columns allowable. */ |
| 42 | #define MAX_COLUMNS 72 |
| 43 | \f |
| 44 | /* Start saving dependencies, to be written to FILENAME. If this is |
| 45 | never called, then dependency tracking is simply skipped. */ |
| 46 | |
| 47 | void |
| 48 | start_dependencies (char *filename) |
| 49 | { |
| 50 | dep_file = filename; |
| 51 | } |
| 52 | |
| 53 | /* Noticed a new filename, so try to register it. */ |
| 54 | |
| 55 | void |
| 56 | register_dependency (char *filename) |
| 57 | { |
| 58 | struct dependency *dep; |
| 59 | |
| 60 | if (dep_file == NULL) |
| 61 | return; |
| 62 | |
| 63 | for (dep = dep_chain; dep != NULL; dep = dep->next) |
| 64 | { |
| 65 | if (!strcmp (filename, dep->file)) |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | dep = (struct dependency *) xmalloc (sizeof (struct dependency)); |
| 70 | dep->file = xstrdup (filename); |
| 71 | dep->next = dep_chain; |
| 72 | dep_chain = dep; |
| 73 | } |
| 74 | |
| 75 | /* Quote a file name the way `make' wants it, and print it to FILE. |
| 76 | If FILE is NULL, do no printing, but return the length of the |
| 77 | quoted string. |
| 78 | |
| 79 | This code is taken from gcc with only minor changes. */ |
| 80 | |
| 81 | static int |
| 82 | quote_string_for_make (FILE *file, char *src) |
| 83 | { |
| 84 | char *p = src; |
| 85 | int i = 0; |
| 86 | |
| 87 | for (;;) |
| 88 | { |
| 89 | char c = *p++; |
| 90 | |
| 91 | switch (c) |
| 92 | { |
| 93 | case '\0': |
| 94 | case ' ': |
| 95 | case '\t': |
| 96 | { |
| 97 | /* GNU make uses a weird quoting scheme for white space. |
| 98 | A space or tab preceded by 2N+1 backslashes represents |
| 99 | N backslashes followed by space; a space or tab |
| 100 | preceded by 2N backslashes represents N backslashes at |
| 101 | the end of a file name; and backslashes in other |
| 102 | contexts should not be doubled. */ |
| 103 | char *q; |
| 104 | |
| 105 | for (q = p - 1; src < q && q[-1] == '\\'; q--) |
| 106 | { |
| 107 | if (file) |
| 108 | putc ('\\', file); |
| 109 | i++; |
| 110 | } |
| 111 | } |
| 112 | if (!c) |
| 113 | return i; |
| 114 | if (file) |
| 115 | putc ('\\', file); |
| 116 | i++; |
| 117 | goto ordinary_char; |
| 118 | |
| 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; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* Append some output to the file, keeping track of columns and doing |
| 141 | wrapping as necessary. */ |
| 142 | |
| 143 | static void |
| 144 | wrap_output (FILE *f, char *string, int spacer) |
| 145 | { |
| 146 | int len = quote_string_for_make (NULL, string); |
| 147 | |
| 148 | if (len == 0) |
| 149 | return; |
| 150 | |
| 151 | if (column |
| 152 | && (MAX_COLUMNS |
| 153 | - 1 /* spacer */ |
| 154 | - 2 /* ` \' */ |
| 155 | < column + len)) |
| 156 | { |
| 157 | fprintf (f, " \\\n "); |
| 158 | column = 0; |
| 159 | if (spacer == ' ') |
| 160 | spacer = '\0'; |
| 161 | } |
| 162 | |
| 163 | if (spacer == ' ') |
| 164 | { |
| 165 | putc (spacer, f); |
| 166 | ++column; |
| 167 | } |
| 168 | |
| 169 | quote_string_for_make (f, string); |
| 170 | column += len; |
| 171 | |
| 172 | if (spacer == ':') |
| 173 | { |
| 174 | putc (spacer, f); |
| 175 | ++column; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* Print dependency file. */ |
| 180 | |
| 181 | void |
| 182 | print_dependencies (void) |
| 183 | { |
| 184 | FILE *f; |
| 185 | struct dependency *dep; |
| 186 | |
| 187 | if (dep_file == NULL) |
| 188 | return; |
| 189 | |
| 190 | f = fopen (dep_file, FOPEN_WT); |
| 191 | if (f == NULL) |
| 192 | { |
| 193 | as_warn (_("can't open `%s' for writing"), dep_file); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | column = 0; |
| 198 | wrap_output (f, out_file_name, ':'); |
| 199 | for (dep = dep_chain; dep != NULL; dep = dep->next) |
| 200 | wrap_output (f, dep->file, ' '); |
| 201 | |
| 202 | putc ('\n', f); |
| 203 | |
| 204 | if (fclose (f)) |
| 205 | as_warn (_("can't close `%s'"), dep_file); |
| 206 | } |