Subversion Repositories Kolibri OS

Rev

Rev 5222 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5222 Rev 6324
1
/* depend.c - Handle dependency tracking.
1
/* depend.c - Handle dependency tracking.
2
   Copyright 1997, 1998, 2000, 2001, 2003, 2004, 2005, 2007
-
 
3
   Free Software Foundation, Inc.
2
   Copyright (C) 1997-2015 Free Software Foundation, Inc.
4
 
3
 
5
   This file is part of GAS, the GNU Assembler.
4
   This file is part of GAS, the GNU Assembler.
6
 
5
 
7
   GAS is free software; you can redistribute it and/or modify
6
   GAS is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
7
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3, or (at your option)
8
   the Free Software Foundation; either version 3, or (at your option)
10
   any later version.
9
   any later version.
11
 
10
 
12
   GAS is distributed in the hope that it will be useful,
11
   GAS is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
14
   GNU General Public License for more details.
16
 
15
 
17
   You should have received a copy of the GNU General Public License
16
   You should have received a copy of the GNU General Public License
18
   along with GAS; see the file COPYING.  If not, write to the Free
17
   along with GAS; see the file COPYING.  If not, write to the Free
19
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
18
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20
   02110-1301, USA.  */
19
   02110-1301, USA.  */
21
 
20
 
22
#include "as.h"
21
#include "as.h"
23
#include "filenames.h"
22
#include "filenames.h"
24
 
23
 
25
/* The file to write to, or NULL if no dependencies being kept.  */
24
/* The file to write to, or NULL if no dependencies being kept.  */
26
static char * dep_file = NULL;
25
static char * dep_file = NULL;
27
 
26
 
28
struct dependency
27
struct dependency
29
  {
28
  {
30
    char * file;
29
    char * file;
31
    struct dependency * next;
30
    struct dependency * next;
32
  };
31
  };
33
 
32
 
34
/* All the files we depend on.  */
33
/* All the files we depend on.  */
35
static struct dependency * dep_chain = NULL;
34
static struct dependency * dep_chain = NULL;
36
 
35
 
37
/* Current column in output file.  */
36
/* Current column in output file.  */
38
static int column = 0;
37
static int column = 0;
39
 
38
 
40
static int quote_string_for_make (FILE *, char *);
39
static int quote_string_for_make (FILE *, char *);
41
static void wrap_output (FILE *, char *, int);
40
static void wrap_output (FILE *, char *, int);
42
 
41
 
43
/* Number of columns allowable.  */
42
/* Number of columns allowable.  */
44
#define MAX_COLUMNS 72
43
#define MAX_COLUMNS 72
45

44

46
/* Start saving dependencies, to be written to FILENAME.  If this is
45
/* Start saving dependencies, to be written to FILENAME.  If this is
47
   never called, then dependency tracking is simply skipped.  */
46
   never called, then dependency tracking is simply skipped.  */
48
 
47
 
49
void
48
void
50
start_dependencies (char *filename)
49
start_dependencies (char *filename)
51
{
50
{
52
  dep_file = filename;
51
  dep_file = filename;
53
}
52
}
54
 
53
 
55
/* Noticed a new filename, so try to register it.  */
54
/* Noticed a new filename, so try to register it.  */
56
 
55
 
57
void
56
void
58
register_dependency (char *filename)
57
register_dependency (char *filename)
59
{
58
{
60
  struct dependency *dep;
59
  struct dependency *dep;
61
 
60
 
62
  if (dep_file == NULL)
61
  if (dep_file == NULL)
63
    return;
62
    return;
64
 
63
 
65
  for (dep = dep_chain; dep != NULL; dep = dep->next)
64
  for (dep = dep_chain; dep != NULL; dep = dep->next)
66
    {
65
    {
67
      if (!filename_cmp (filename, dep->file))
66
      if (!filename_cmp (filename, dep->file))
68
	return;
67
	return;
69
    }
68
    }
70
 
69
 
71
  dep = (struct dependency *) xmalloc (sizeof (struct dependency));
70
  dep = (struct dependency *) xmalloc (sizeof (struct dependency));
72
  dep->file = xstrdup (filename);
71
  dep->file = xstrdup (filename);
73
  dep->next = dep_chain;
72
  dep->next = dep_chain;
74
  dep_chain = dep;
73
  dep_chain = dep;
75
}
74
}
76
 
75
 
77
/* Quote a file name the way `make' wants it, and print it to FILE.
76
/* 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
77
   If FILE is NULL, do no printing, but return the length of the
79
   quoted string.
78
   quoted string.
80
 
79
 
81
   This code is taken from gcc with only minor changes.  */
80
   This code is taken from gcc with only minor changes.  */
82
 
81
 
83
static int
82
static int
84
quote_string_for_make (FILE *file, char *src)
83
quote_string_for_make (FILE *file, char *src)
85
{
84
{
86
  char *p = src;
85
  char *p = src;
87
  int i = 0;
86
  int i = 0;
88
 
87
 
89
  for (;;)
88
  for (;;)
90
    {
89
    {
91
      char c = *p++;
90
      char c = *p++;
92
 
91
 
93
      switch (c)
92
      switch (c)
94
	{
93
	{
95
	case '\0':
94
	case '\0':
96
	case ' ':
95
	case ' ':
97
	case '\t':
96
	case '\t':
98
	  {
97
	  {
99
	    /* GNU make uses a weird quoting scheme for white space.
98
	    /* GNU make uses a weird quoting scheme for white space.
100
	       A space or tab preceded by 2N+1 backslashes represents
99
	       A space or tab preceded by 2N+1 backslashes represents
101
	       N backslashes followed by space; a space or tab
100
	       N backslashes followed by space; a space or tab
102
	       preceded by 2N backslashes represents N backslashes at
101
	       preceded by 2N backslashes represents N backslashes at
103
	       the end of a file name; and backslashes in other
102
	       the end of a file name; and backslashes in other
104
	       contexts should not be doubled.  */
103
	       contexts should not be doubled.  */
105
	    char *q;
104
	    char *q;
106
 
105
 
107
	    for (q = p - 1; src < q && q[-1] == '\\'; q--)
106
	    for (q = p - 1; src < q && q[-1] == '\\'; q--)
108
	      {
107
	      {
109
		if (file)
108
		if (file)
110
		  putc ('\\', file);
109
		  putc ('\\', file);
111
		i++;
110
		i++;
112
	      }
111
	      }
113
	  }
112
	  }
114
	  if (!c)
113
	  if (!c)
115
	    return i;
114
	    return i;
116
	  if (file)
115
	  if (file)
117
	    putc ('\\', file);
116
	    putc ('\\', file);
118
	  i++;
117
	  i++;
119
	  goto ordinary_char;
118
	  goto ordinary_char;
120
 
119
 
121
	case '$':
120
	case '$':
122
	  if (file)
121
	  if (file)
123
	    putc (c, file);
122
	    putc (c, file);
124
	  i++;
123
	  i++;
125
	  /* Fall through.  This can mishandle things like "$(" but
124
	  /* Fall through.  This can mishandle things like "$(" but
126
	     there's no easy fix.  */
125
	     there's no easy fix.  */
127
	default:
126
	default:
128
	ordinary_char:
127
	ordinary_char:
129
	  /* This can mishandle characters in the string "\0\n%*?[\\~";
128
	  /* This can mishandle characters in the string "\0\n%*?[\\~";
130
	     exactly which chars are mishandled depends on the `make' version.
129
	     exactly which chars are mishandled depends on the `make' version.
131
	     We know of no portable solution for this;
130
	     We know of no portable solution for this;
132
	     even GNU make 3.76.1 doesn't solve the problem entirely.
131
	     even GNU make 3.76.1 doesn't solve the problem entirely.
133
	     (Also, '\0' is mishandled due to our calling conventions.)  */
132
	     (Also, '\0' is mishandled due to our calling conventions.)  */
134
	  if (file)
133
	  if (file)
135
	    putc (c, file);
134
	    putc (c, file);
136
	  i++;
135
	  i++;
137
	  break;
136
	  break;
138
	}
137
	}
139
    }
138
    }
140
}
139
}
141
 
140
 
142
/* Append some output to the file, keeping track of columns and doing
141
/* Append some output to the file, keeping track of columns and doing
143
   wrapping as necessary.  */
142
   wrapping as necessary.  */
144
 
143
 
145
static void
144
static void
146
wrap_output (FILE *f, char *string, int spacer)
145
wrap_output (FILE *f, char *string, int spacer)
147
{
146
{
148
  int len = quote_string_for_make (NULL, string);
147
  int len = quote_string_for_make (NULL, string);
149
 
148
 
150
  if (len == 0)
149
  if (len == 0)
151
    return;
150
    return;
152
 
151
 
153
  if (column
152
  if (column
154
      && (MAX_COLUMNS
153
      && (MAX_COLUMNS
155
	  - 1 /* spacer */
154
	  - 1 /* spacer */
156
	  - 2 /* ` \'   */
155
	  - 2 /* ` \'   */
157
	  < column + len))
156
	  < column + len))
158
    {
157
    {
159
      fprintf (f, " \\\n ");
158
      fprintf (f, " \\\n ");
160
      column = 0;
159
      column = 0;
161
      if (spacer == ' ')
160
      if (spacer == ' ')
162
	spacer = '\0';
161
	spacer = '\0';
163
    }
162
    }
164
 
163
 
165
  if (spacer == ' ')
164
  if (spacer == ' ')
166
    {
165
    {
167
      putc (spacer, f);
166
      putc (spacer, f);
168
      ++column;
167
      ++column;
169
    }
168
    }
170
 
169
 
171
  quote_string_for_make (f, string);
170
  quote_string_for_make (f, string);
172
  column += len;
171
  column += len;
173
 
172
 
174
  if (spacer == ':')
173
  if (spacer == ':')
175
    {
174
    {
176
      putc (spacer, f);
175
      putc (spacer, f);
177
      ++column;
176
      ++column;
178
    }
177
    }
179
}
178
}
180
 
179
 
181
/* Print dependency file.  */
180
/* Print dependency file.  */
182
 
181
 
183
void
182
void
184
print_dependencies (void)
183
print_dependencies (void)
185
{
184
{
186
  FILE *f;
185
  FILE *f;
187
  struct dependency *dep;
186
  struct dependency *dep;
188
 
187
 
189
  if (dep_file == NULL)
188
  if (dep_file == NULL)
190
    return;
189
    return;
191
 
190
 
192
  f = fopen (dep_file, FOPEN_WT);
191
  f = fopen (dep_file, FOPEN_WT);
193
  if (f == NULL)
192
  if (f == NULL)
194
    {
193
    {
195
      as_warn (_("can't open `%s' for writing"), dep_file);
194
      as_warn (_("can't open `%s' for writing"), dep_file);
196
      return;
195
      return;
197
    }
196
    }
198
 
197
 
199
  column = 0;
198
  column = 0;
200
  wrap_output (f, out_file_name, ':');
199
  wrap_output (f, out_file_name, ':');
201
  for (dep = dep_chain; dep != NULL; dep = dep->next)
200
  for (dep = dep_chain; dep != NULL; dep = dep->next)
202
    wrap_output (f, dep->file, ' ');
201
    wrap_output (f, dep->file, ' ');
203
 
202
 
204
  putc ('\n', f);
203
  putc ('\n', f);
205
 
204
 
206
  if (fclose (f))
205
  if (fclose (f))
207
    as_warn (_("can't close `%s'"), dep_file);
206
    as_warn (_("can't close `%s'"), dep_file);
208
}
207
}