Mon Jun 9 12:55:45 1997 H.J. Lu <hjl@gnu.ai.mit.edu>
[external/binutils.git] / gas / depend.c
1 /* depend.c - Handle dependency tracking.
2    Copyright (C) 1997 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 void wrap_output PARAMS ((FILE *, char *, int));
39
40 /* Number of columns allowable.  */
41 #define MAX_COLUMNS 72
42
43 \f
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 (filename)
50      char *filename;
51 {
52   dep_file = filename;
53 }
54
55 /* Noticed a new filename, so try to register it.  */
56
57 void
58 register_dependency (filename)
59      char *filename;
60 {
61   struct dependency *dep;
62
63   if (dep_file == NULL)
64     return;
65
66   for (dep = dep_chain; dep != NULL; dep = dep->next)
67     {
68       if (! strcmp (filename, dep->file))
69         return;
70     }
71
72   dep = (struct dependency *) xmalloc (sizeof (struct dependency));
73   dep->file = xstrdup (filename);
74   dep->next = dep_chain;
75   dep_chain = dep;
76 }
77
78 /* Append some output to the file, keeping track of columns and doing
79    wrapping as necessary.  */
80
81 static void
82 wrap_output (f, string, spacer)
83      FILE *f;
84      char *string;
85      int spacer;
86 {
87   int len = strlen (string);
88
89   if (len == 0)
90     return;
91
92   if (column && MAX_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < column + len)
93     {
94       fprintf (f, " \\\n ");
95       column = 0;
96       if (spacer == ' ')
97         spacer = '\0';
98     }
99
100   if (spacer == ' ')
101     {
102       putc (spacer, f);
103       ++column;
104     }
105
106   fputs (string, f);
107   column += len;
108
109   if (spacer == ':')
110     {
111       putc (spacer, f);
112       ++column;
113     }
114 }
115
116 /* Print dependency file.  */
117
118 void
119 print_dependencies ()
120 {
121   FILE *f;
122   struct dependency *dep;
123
124   if (dep_file == NULL)
125     return;
126
127   f = fopen (dep_file, "w");
128   if (f == NULL)
129     {
130       as_warn ("Can't open `%s' for writing", dep_file);
131       return;
132     }
133
134   column = 0;
135   wrap_output (f, out_file_name, ':');
136   for (dep = dep_chain; dep != NULL; dep = dep->next)
137     wrap_output (f, dep->file, ' ');
138
139   putc ('\n', f);
140
141   if (fclose (f))
142     as_warn ("Can't close %s", dep_file);
143 }