OSDN Git Service

* gcc.c (init_gcc_specs): New function. Make -shared-libgcc
[pf3gnuchains/gcc-fork.git] / gcc / mkdeps.c
1 /* Dependency generator for Makefile fragments.
2    Copyright (C) 2000, 2001 Free Software Foundation, Inc.
3    Contributed by Zack Weinberg, Mar 2000
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19  In other words, you are welcome to use, share and improve this program.
20  You are forbidden to forbid anyone else to use, share and improve
21  what you give them.   Help stamp out software-hoarding!  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "mkdeps.h"
26
27 /* Keep this structure local to this file, so clients don't find it
28    easy to start making assumptions.  */
29 struct deps
30 {
31   const char **targetv;
32   unsigned int ntargets;        /* number of slots actually occupied */
33   unsigned int targets_size;    /* amt of allocated space - in words */
34
35   const char **depv;
36   unsigned int ndeps;
37   unsigned int deps_size;
38 };
39
40 static const char *munge        PARAMS ((const char *));
41
42 /* Given a filename, quote characters in that filename which are
43    significant to Make.  Note that it's not possible to quote all such
44    characters - e.g. \n, %, *, ?, [, \ (in some contexts), and ~ are
45    not properly handled.  It isn't possible to get this right in any
46    current version of Make.  (??? Still true?  Old comment referred to
47    3.76.1.)  */
48    
49 static const char *
50 munge (filename)
51      const char *filename;
52 {
53   int len;
54   const char *p, *q;
55   char *dst, *buffer;
56
57   for (p = filename, len = 0; *p; p++, len++)
58     {
59       switch (*p)
60         {
61         case ' ':
62         case '\t':
63           /* GNU make uses a weird quoting scheme for white space.
64              A space or tab preceded by 2N+1 backslashes represents
65              N backslashes followed by space; a space or tab
66              preceded by 2N backslashes represents N backslashes at
67              the end of a file name; and backslashes in other
68              contexts should not be doubled.  */
69           for (q = p - 1; filename <= q && *q == '\\';  q--)
70             len++;
71           len++;
72           break;
73
74         case '$':
75           /* '$' is quoted by doubling it.  */
76           len++;
77           break;
78         }
79     }
80
81   /* Now we know how big to make the buffer.  */
82   buffer = xmalloc (len + 1);
83
84   for (p = filename, dst = buffer; *p; p++, dst++)
85     {
86       switch (*p)
87         {
88         case ' ':
89         case '\t':
90           for (q = p - 1; filename <= q && *q == '\\';  q--)
91             *dst++ = '\\';
92           *dst++ = '\\';
93           break;
94
95         case '$':
96           *dst++ = '$';
97           break;
98
99         default:
100           /* nothing */;
101         }
102       *dst = *p;
103     }
104
105   *dst = '\0';
106   return buffer;
107 }
108
109 /* Public routines.  */
110
111 struct deps *
112 deps_init ()
113 {
114   struct deps *d = (struct deps *) xmalloc (sizeof (struct deps));
115
116   /* Allocate space for the vectors only if we need it.  */
117
118   d->targetv = 0;
119   d->depv = 0;
120
121   d->ntargets = 0;
122   d->targets_size = 0;
123   d->ndeps = 0;
124   d->deps_size = 0;
125
126   return d;
127 }
128
129 void
130 deps_free (d)
131      struct deps *d;
132 {
133   unsigned int i;
134
135   if (d->targetv)
136     {
137       for (i = 0; i < d->ntargets; i++)
138         free ((PTR) d->targetv[i]);
139       free (d->targetv);
140     }
141
142   if (d->depv)
143     {
144       for (i = 0; i < d->ndeps; i++)
145         free ((PTR) d->depv[i]);
146       free (d->depv);
147     }
148
149   free (d);
150 }
151
152 /* Adds a target T.  We make a copy, so it need not be a permanent
153    string.  QUOTE is true if the string should be quoted.  */
154 void
155 deps_add_target (d, t, quote)
156      struct deps *d;
157      const char *t;
158      int quote;
159 {
160   if (d->ntargets == d->targets_size)
161     {
162       d->targets_size = d->targets_size * 2 + 4;
163       d->targetv = (const char **) xrealloc (d->targetv,
164                              d->targets_size * sizeof (const char *));
165     }
166
167   if (quote)
168     t = munge (t);  /* Also makes permanent copy.  */
169   else
170     t = xstrdup (t);
171
172   d->targetv[d->ntargets++] = t;
173 }
174
175 /* Sets the default target if none has been given already.  An empty
176    string as the default target in interpreted as stdin.  The string
177    is quoted for MAKE.  */
178 void
179 deps_add_default_target (d, tgt)
180      struct deps *d;
181      const char *tgt;
182 {
183   char *o, *suffix;
184
185   /* Only if we have no targets.  */
186   if (d->ntargets)
187     return;
188
189   if (tgt[0] == '\0')
190     deps_add_target (d, "-", 1);
191   else
192     {
193       o = (char *) alloca (strlen (tgt) + 8);
194
195       strcpy (o, tgt);
196       suffix = strrchr (o, '.');
197
198 #ifndef OBJECT_SUFFIX
199 # define OBJECT_SUFFIX ".o"
200 #endif
201
202       if (suffix)
203         strcpy (suffix, OBJECT_SUFFIX);
204       else
205         strcat (o, OBJECT_SUFFIX);
206       deps_add_target (d, o, 1);
207     }
208 }
209
210 void
211 deps_add_dep (d, t)
212      struct deps *d;
213      const char *t;
214 {
215   t = munge (t);  /* Also makes permanent copy.  */
216
217   if (d->ndeps == d->deps_size)
218     {
219       d->deps_size = d->deps_size * 2 + 8;
220       d->depv = (const char **)
221         xrealloc (d->depv, d->deps_size * sizeof (const char *));
222     }
223   d->depv[d->ndeps++] = t;
224 }
225
226 void
227 deps_write (d, fp, colmax)
228      const struct deps *d;
229      FILE *fp;
230      unsigned int colmax;
231 {
232   unsigned int size, i, column;
233
234   column = 0;
235   if (colmax && colmax < 34)
236     colmax = 34;
237
238   for (i = 0; i < d->ntargets; i++)
239     {
240       size = strlen (d->targetv[i]);
241       column += size;
242       if (colmax && column > colmax)
243         {
244           fputs (" \\\n ", fp);
245           column = 1 + size;
246         }
247       if (i)
248         {
249           putc (' ', fp);
250           column++;
251         }
252       fputs (d->targetv[i], fp);
253     }
254
255   putc (':', fp);
256   putc (' ', fp);
257   column += 2;
258
259   for (i = 0; i < d->ndeps; i++)
260     {
261       size = strlen (d->depv[i]);
262       column += size;
263       if (colmax && column > colmax)
264         {
265           fputs (" \\\n ", fp);
266           column = 1 + size;
267         }
268       if (i)
269         {
270           putc (' ', fp);
271           column++;
272         }
273       fputs (d->depv[i], fp);
274     }
275   putc ('\n', fp);
276 }
277   
278 void
279 deps_phony_targets (d, fp)
280      const struct deps *d;
281      FILE *fp;
282 {
283   unsigned int i;
284
285   for (i = 1; i < d->ndeps; i++)
286     {
287       putc ('\n', fp);
288       fputs (d->depv[i], fp);
289       putc (':', fp);
290       putc ('\n', fp);
291     }
292 }