Home | History | Annotate | Download | only in gas
      1 /* depend.c - Handle dependency tracking.
      2    Copyright (C) 1997-2016 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 3, 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, 51 Franklin Street - Fifth Floor, Boston, MA
     19    02110-1301, USA.  */
     20 
     21 #include "as.h"
     22 #include "filenames.h"
     23 
     24 /* The file to write to, or NULL if no dependencies being kept.  */
     25 static char * dep_file = NULL;
     26 
     27 struct dependency
     28 {
     29   char * file;
     30   struct dependency * next;
     31 };
     32 
     33 /* All the files we depend on.  */
     34 static struct dependency * dep_chain = NULL;
     35 
     36 /* Current column in output file.  */
     37 static int column = 0;
     38 
     39 static int quote_string_for_make (FILE *, const char *);
     40 static void wrap_output (FILE *, const char *, int);
     41 
     42 /* Number of columns allowable.  */
     43 #define MAX_COLUMNS 72
     44 
     45 /* Start saving dependencies, to be written to FILENAME.  If this is
     47    never called, then dependency tracking is simply skipped.  */
     48 
     49 void
     50 start_dependencies (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 (const 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 (!filename_cmp (filename, dep->file))
     68 	return;
     69     }
     70 
     71   dep = XNEW (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 *file, const char *src)
     85 {
     86   const char *p = src;
     87   int i = 0;
     88 
     89   for (;;)
     90     {
     91       char c = *p++;
     92 
     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 	    const char *q;
    106 
    107 	    for (q = p - 1; src < q && q[-1] == '\\'; q--)
    108 	      {
    109 		if (file)
    110 		  putc ('\\', file);
    111 		i++;
    112 	      }
    113 	  }
    114 	  if (!c)
    115 	    return i;
    116 	  if (file)
    117 	    putc ('\\', file);
    118 	  i++;
    119 	  goto ordinary_char;
    120 
    121 	case '$':
    122 	  if (file)
    123 	    putc (c, file);
    124 	  i++;
    125 	  /* Fall through.  This can mishandle things like "$(" but
    126 	     there's no easy fix.  */
    127 	default:
    128 	ordinary_char:
    129 	  /* This can mishandle characters in the string "\0\n%*?[\\~";
    130 	     exactly which chars are mishandled depends on the `make' version.
    131 	     We know of no portable solution for this;
    132 	     even GNU make 3.76.1 doesn't solve the problem entirely.
    133 	     (Also, '\0' is mishandled due to our calling conventions.)  */
    134 	  if (file)
    135 	    putc (c, file);
    136 	  i++;
    137 	  break;
    138 	}
    139     }
    140 }
    141 
    142 /* Append some output to the file, keeping track of columns and doing
    143    wrapping as necessary.  */
    144 
    145 static void
    146 wrap_output (FILE *f, const char *string, int spacer)
    147 {
    148   int len = quote_string_for_make (NULL, string);
    149 
    150   if (len == 0)
    151     return;
    152 
    153   if (column
    154       && (MAX_COLUMNS
    155 	  - 1 /* spacer */
    156 	  - 2 /* ` \'   */
    157 	  < column + len))
    158     {
    159       fprintf (f, " \\\n ");
    160       column = 0;
    161       if (spacer == ' ')
    162 	spacer = '\0';
    163     }
    164 
    165   if (spacer == ' ')
    166     {
    167       putc (spacer, f);
    168       ++column;
    169     }
    170 
    171   quote_string_for_make (f, string);
    172   column += len;
    173 
    174   if (spacer == ':')
    175     {
    176       putc (spacer, f);
    177       ++column;
    178     }
    179 }
    180 
    181 /* Print dependency file.  */
    182 
    183 void
    184 print_dependencies (void)
    185 {
    186   FILE *f;
    187   struct dependency *dep;
    188 
    189   if (dep_file == NULL)
    190     return;
    191 
    192   f = fopen (dep_file, FOPEN_WT);
    193   if (f == NULL)
    194     {
    195       as_warn (_("can't open `%s' for writing"), dep_file);
    196       return;
    197     }
    198 
    199   column = 0;
    200   wrap_output (f, out_file_name, ':');
    201   for (dep = dep_chain; dep != NULL; dep = dep->next)
    202     wrap_output (f, dep->file, ' ');
    203 
    204   putc ('\n', f);
    205 
    206   if (fclose (f))
    207     as_warn (_("can't close `%s'"), dep_file);
    208 }
    209