depend.c 4.6 KB

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