| 1 | /* depend.c - Handle dependency tracking.
|
|---|
| 2 | Copyright 1997, 1998, 2000 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 | char *file;
|
|---|
| 28 | struct dependency *next;
|
|---|
| 29 | };
|
|---|
| 30 |
|
|---|
| 31 | /* All the files we depend on. */
|
|---|
| 32 | static struct dependency *dep_chain = NULL;
|
|---|
| 33 |
|
|---|
| 34 | /* Current column in output file. */
|
|---|
| 35 | static int column = 0;
|
|---|
| 36 |
|
|---|
| 37 | static int quote_string_for_make PARAMS ((FILE *, char *));
|
|---|
| 38 | static void wrap_output PARAMS ((FILE *, char *, int));
|
|---|
| 39 |
|
|---|
| 40 | /* Number of columns allowable. */
|
|---|
| 41 | #define MAX_COLUMNS 72
|
|---|
| 42 | |
|---|
| 43 |
|
|---|
| 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 (filename)
|
|---|
| 49 | char *filename;
|
|---|
| 50 | {
|
|---|
| 51 | dep_file = filename;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /* Noticed a new filename, so try to register it. */
|
|---|
| 55 |
|
|---|
| 56 | void
|
|---|
| 57 | register_dependency (filename)
|
|---|
| 58 | char *filename;
|
|---|
| 59 | {
|
|---|
| 60 | struct dependency *dep;
|
|---|
| 61 |
|
|---|
| 62 | if (dep_file == NULL)
|
|---|
| 63 | return;
|
|---|
| 64 |
|
|---|
| 65 | for (dep = dep_chain; dep != NULL; dep = dep->next)
|
|---|
| 66 | {
|
|---|
| 67 | if (!strcmp (filename, dep->file))
|
|---|
| 68 | return;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | dep = (struct dependency *) xmalloc (sizeof (struct dependency));
|
|---|
| 72 | dep->file = xstrdup (filename);
|
|---|
| 73 | dep->next = dep_chain;
|
|---|
| 74 | dep_chain = dep;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 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
|
|---|
| 79 | quoted string.
|
|---|
| 80 |
|
|---|
| 81 | This code is taken from gcc with only minor changes. */
|
|---|
| 82 |
|
|---|
| 83 | static int
|
|---|
| 84 | quote_string_for_make (file, src)
|
|---|
| 85 | FILE *file;
|
|---|
| 86 | char *src;
|
|---|
| 87 | {
|
|---|
| 88 | char *p = src;
|
|---|
| 89 | int i = 0;
|
|---|
| 90 | for (;;)
|
|---|
| 91 | {
|
|---|
| 92 | char c = *p++;
|
|---|
| 93 | switch (c)
|
|---|
| 94 | {
|
|---|
| 95 | case '\0':
|
|---|
| 96 | case ' ':
|
|---|
| 97 | case '\t':
|
|---|
| 98 | {
|
|---|
| 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. */
|
|---|
| 105 | char *q;
|
|---|
| 106 | for (q = p - 1; src < q && q[-1] == '\\'; q--)
|
|---|
| 107 | {
|
|---|
| 108 | if (file)
|
|---|
| 109 | putc ('\\', file);
|
|---|
| 110 | i++;
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | if (!c)
|
|---|
| 114 | return i;
|
|---|
| 115 | if (file)
|
|---|
| 116 | putc ('\\', file);
|
|---|
| 117 | i++;
|
|---|
| 118 | goto ordinary_char;
|
|---|
| 119 |
|
|---|
| 120 | case '$':
|
|---|
| 121 | if (file)
|
|---|
| 122 | putc (c, file);
|
|---|
| 123 | i++;
|
|---|
| 124 | /* Fall through. This can mishandle things like "$(" but
|
|---|
| 125 | there's no easy fix. */
|
|---|
| 126 | default:
|
|---|
| 127 | 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 | if (file)
|
|---|
| 134 | putc (c, file);
|
|---|
| 135 | i++;
|
|---|
| 136 | break;
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 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 (f, string, spacer)
|
|---|
| 146 | FILE *f;
|
|---|
| 147 | char *string;
|
|---|
| 148 | int spacer;
|
|---|
| 149 | {
|
|---|
| 150 | int len = quote_string_for_make (NULL, string);
|
|---|
| 151 |
|
|---|
| 152 | if (len == 0)
|
|---|
| 153 | return;
|
|---|
| 154 |
|
|---|
| 155 | if (column
|
|---|
| 156 | && (MAX_COLUMNS
|
|---|
| 157 | - 1 /* spacer */
|
|---|
| 158 | - 2 /* ` \' */
|
|---|
| 159 | < column + len))
|
|---|
| 160 | {
|
|---|
| 161 | fprintf (f, " \\\n ");
|
|---|
| 162 | column = 0;
|
|---|
| 163 | if (spacer == ' ')
|
|---|
| 164 | spacer = '\0';
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | if (spacer == ' ')
|
|---|
| 168 | {
|
|---|
| 169 | putc (spacer, f);
|
|---|
| 170 | ++column;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | quote_string_for_make (f, string);
|
|---|
| 174 | column += len;
|
|---|
| 175 |
|
|---|
| 176 | if (spacer == ':')
|
|---|
| 177 | {
|
|---|
| 178 | putc (spacer, f);
|
|---|
| 179 | ++column;
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /* Print dependency file. */
|
|---|
| 184 |
|
|---|
| 185 | void
|
|---|
| 186 | print_dependencies ()
|
|---|
| 187 | {
|
|---|
| 188 | FILE *f;
|
|---|
| 189 | struct dependency *dep;
|
|---|
| 190 |
|
|---|
| 191 | if (dep_file == NULL)
|
|---|
| 192 | return;
|
|---|
| 193 |
|
|---|
| 194 | f = fopen (dep_file, "w");
|
|---|
| 195 | if (f == NULL)
|
|---|
| 196 | {
|
|---|
| 197 | as_warn (_("Can't open `%s' for writing"), dep_file);
|
|---|
| 198 | return;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | column = 0;
|
|---|
| 202 | wrap_output (f, out_file_name, ':');
|
|---|
| 203 | for (dep = dep_chain; dep != NULL; dep = dep->next)
|
|---|
| 204 | wrap_output (f, dep->file, ' ');
|
|---|
| 205 |
|
|---|
| 206 | putc ('\n', f);
|
|---|
| 207 |
|
|---|
| 208 | if (fclose (f))
|
|---|
| 209 | as_warn (_("Can't close `%s'"), dep_file);
|
|---|
| 210 | }
|
|---|