mkdeps.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /* Dependency generator for Makefile fragments.
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. Contributed by Zack Weinberg, Mar 2000
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. This program 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 this program; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>.
  15. In other words, you are welcome to use, share and improve this program.
  16. You are forbidden to forbid anyone else to use, share and improve
  17. what you give them. Help stamp out software-hoarding! */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "mkdeps.h"
  21. /* Keep this structure local to this file, so clients don't find it
  22. easy to start making assumptions. */
  23. struct deps
  24. {
  25. const char **targetv;
  26. unsigned int ntargets; /* number of slots actually occupied */
  27. unsigned int targets_size; /* amt of allocated space - in words */
  28. const char **depv;
  29. unsigned int ndeps;
  30. unsigned int deps_size;
  31. const char **vpathv;
  32. size_t *vpathlv;
  33. unsigned int nvpaths;
  34. unsigned int vpaths_size;
  35. };
  36. static const char *munge (const char *);
  37. /* Given a filename, quote characters in that filename which are
  38. significant to Make. Note that it's not possible to quote all such
  39. characters - e.g. \n, %, *, ?, [, \ (in some contexts), and ~ are
  40. not properly handled. It isn't possible to get this right in any
  41. current version of Make. (??? Still true? Old comment referred to
  42. 3.76.1.) */
  43. static const char *
  44. munge (const char *filename)
  45. {
  46. int len;
  47. const char *p, *q;
  48. char *dst, *buffer;
  49. for (p = filename, len = 0; *p; p++, len++)
  50. {
  51. switch (*p)
  52. {
  53. case ' ':
  54. case '\t':
  55. /* GNU make uses a weird quoting scheme for white space.
  56. A space or tab preceded by 2N+1 backslashes represents
  57. N backslashes followed by space; a space or tab
  58. preceded by 2N backslashes represents N backslashes at
  59. the end of a file name; and backslashes in other
  60. contexts should not be doubled. */
  61. for (q = p - 1; filename <= q && *q == '\\'; q--)
  62. len++;
  63. len++;
  64. break;
  65. case '$':
  66. /* '$' is quoted by doubling it. */
  67. len++;
  68. break;
  69. case '#':
  70. /* '#' is quoted with a backslash. */
  71. len++;
  72. break;
  73. }
  74. }
  75. /* Now we know how big to make the buffer. */
  76. buffer = XNEWVEC (char, len + 1);
  77. for (p = filename, dst = buffer; *p; p++, dst++)
  78. {
  79. switch (*p)
  80. {
  81. case ' ':
  82. case '\t':
  83. for (q = p - 1; filename <= q && *q == '\\'; q--)
  84. *dst++ = '\\';
  85. *dst++ = '\\';
  86. break;
  87. case '$':
  88. *dst++ = '$';
  89. break;
  90. case '#':
  91. *dst++ = '\\';
  92. break;
  93. default:
  94. /* nothing */;
  95. }
  96. *dst = *p;
  97. }
  98. *dst = '\0';
  99. return buffer;
  100. }
  101. /* If T begins with any of the partial pathnames listed in d->vpathv,
  102. then advance T to point beyond that pathname. */
  103. static const char *
  104. apply_vpath (struct deps *d, const char *t)
  105. {
  106. if (d->vpathv)
  107. {
  108. unsigned int i;
  109. for (i = 0; i < d->nvpaths; i++)
  110. {
  111. if (!filename_ncmp (d->vpathv[i], t, d->vpathlv[i]))
  112. {
  113. const char *p = t + d->vpathlv[i];
  114. if (!IS_DIR_SEPARATOR (*p))
  115. goto not_this_one;
  116. /* Do not simplify $(vpath)/../whatever. ??? Might not
  117. be necessary. */
  118. if (p[1] == '.' && p[2] == '.' && IS_DIR_SEPARATOR (p[3]))
  119. goto not_this_one;
  120. /* found a match */
  121. t = t + d->vpathlv[i] + 1;
  122. break;
  123. }
  124. not_this_one:;
  125. }
  126. }
  127. /* Remove leading ./ in any case. */
  128. while (t[0] == '.' && IS_DIR_SEPARATOR (t[1]))
  129. {
  130. t += 2;
  131. /* If we removed a leading ./, then also remove any /s after the
  132. first. */
  133. while (IS_DIR_SEPARATOR (t[0]))
  134. ++t;
  135. }
  136. return t;
  137. }
  138. /* Public routines. */
  139. struct deps *
  140. deps_init (void)
  141. {
  142. return XCNEW (struct deps);
  143. }
  144. void
  145. deps_free (struct deps *d)
  146. {
  147. unsigned int i;
  148. if (d->targetv)
  149. {
  150. for (i = 0; i < d->ntargets; i++)
  151. free ((void *) d->targetv[i]);
  152. free (d->targetv);
  153. }
  154. if (d->depv)
  155. {
  156. for (i = 0; i < d->ndeps; i++)
  157. free ((void *) d->depv[i]);
  158. free (d->depv);
  159. }
  160. if (d->vpathv)
  161. {
  162. for (i = 0; i < d->nvpaths; i++)
  163. free ((void *) d->vpathv[i]);
  164. free (d->vpathv);
  165. free (d->vpathlv);
  166. }
  167. free (d);
  168. }
  169. /* Adds a target T. We make a copy, so it need not be a permanent
  170. string. QUOTE is true if the string should be quoted. */
  171. void
  172. deps_add_target (struct deps *d, const char *t, int quote)
  173. {
  174. if (d->ntargets == d->targets_size)
  175. {
  176. d->targets_size = d->targets_size * 2 + 4;
  177. d->targetv = XRESIZEVEC (const char *, d->targetv, d->targets_size);
  178. }
  179. t = apply_vpath (d, t);
  180. if (quote)
  181. t = munge (t); /* Also makes permanent copy. */
  182. else
  183. t = xstrdup (t);
  184. d->targetv[d->ntargets++] = t;
  185. }
  186. /* Sets the default target if none has been given already. An empty
  187. string as the default target in interpreted as stdin. The string
  188. is quoted for MAKE. */
  189. void
  190. deps_add_default_target (struct deps *d, const char *tgt)
  191. {
  192. /* Only if we have no targets. */
  193. if (d->ntargets)
  194. return;
  195. if (tgt[0] == '\0')
  196. deps_add_target (d, "-", 1);
  197. else
  198. {
  199. #ifndef TARGET_OBJECT_SUFFIX
  200. # define TARGET_OBJECT_SUFFIX ".o"
  201. #endif
  202. const char *start = lbasename (tgt);
  203. char *o = (char *) alloca (strlen (start)
  204. + strlen (TARGET_OBJECT_SUFFIX) + 1);
  205. char *suffix;
  206. strcpy (o, start);
  207. suffix = strrchr (o, '.');
  208. if (!suffix)
  209. suffix = o + strlen (o);
  210. strcpy (suffix, TARGET_OBJECT_SUFFIX);
  211. deps_add_target (d, o, 1);
  212. }
  213. }
  214. void
  215. deps_add_dep (struct deps *d, const char *t)
  216. {
  217. t = munge (apply_vpath (d, t)); /* Also makes permanent copy. */
  218. if (d->ndeps == d->deps_size)
  219. {
  220. d->deps_size = d->deps_size * 2 + 8;
  221. d->depv = XRESIZEVEC (const char *, d->depv, d->deps_size);
  222. }
  223. d->depv[d->ndeps++] = t;
  224. }
  225. void
  226. deps_add_vpath (struct deps *d, const char *vpath)
  227. {
  228. const char *elem, *p;
  229. char *copy;
  230. size_t len;
  231. for (elem = vpath; *elem; elem = p)
  232. {
  233. for (p = elem; *p && *p != ':'; p++);
  234. len = p - elem;
  235. copy = XNEWVEC (char, len + 1);
  236. memcpy (copy, elem, len);
  237. copy[len] = '\0';
  238. if (*p == ':')
  239. p++;
  240. if (d->nvpaths == d->vpaths_size)
  241. {
  242. d->vpaths_size = d->vpaths_size * 2 + 8;
  243. d->vpathv = XRESIZEVEC (const char *, d->vpathv, d->vpaths_size);
  244. d->vpathlv = XRESIZEVEC (size_t, d->vpathlv, d->vpaths_size);
  245. }
  246. d->vpathv[d->nvpaths] = copy;
  247. d->vpathlv[d->nvpaths] = len;
  248. d->nvpaths++;
  249. }
  250. }
  251. void
  252. deps_write (const struct deps *d, FILE *fp, unsigned int colmax)
  253. {
  254. unsigned int size, i, column;
  255. column = 0;
  256. if (colmax && colmax < 34)
  257. colmax = 34;
  258. for (i = 0; i < d->ntargets; i++)
  259. {
  260. size = strlen (d->targetv[i]);
  261. column += size;
  262. if (i)
  263. {
  264. if (colmax && column > colmax)
  265. {
  266. fputs (" \\\n ", fp);
  267. column = 1 + size;
  268. }
  269. else
  270. {
  271. putc (' ', fp);
  272. column++;
  273. }
  274. }
  275. fputs (d->targetv[i], fp);
  276. }
  277. putc (':', fp);
  278. column++;
  279. for (i = 0; i < d->ndeps; i++)
  280. {
  281. size = strlen (d->depv[i]);
  282. column += size;
  283. if (colmax && column > colmax)
  284. {
  285. fputs (" \\\n ", fp);
  286. column = 1 + size;
  287. }
  288. else
  289. {
  290. putc (' ', fp);
  291. column++;
  292. }
  293. fputs (d->depv[i], fp);
  294. }
  295. putc ('\n', fp);
  296. }
  297. void
  298. deps_phony_targets (const struct deps *d, FILE *fp)
  299. {
  300. unsigned int i;
  301. for (i = 1; i < d->ndeps; i++)
  302. {
  303. putc ('\n', fp);
  304. fputs (d->depv[i], fp);
  305. putc (':', fp);
  306. putc ('\n', fp);
  307. }
  308. }
  309. /* Write out a deps buffer to a file, in a form that can be read back
  310. with deps_restore. Returns nonzero on error, in which case the
  311. error number will be in errno. */
  312. int
  313. deps_save (struct deps *deps, FILE *f)
  314. {
  315. unsigned int i;
  316. /* The cppreader structure contains makefile dependences. Write out this
  317. structure. */
  318. /* The number of dependences. */
  319. if (fwrite (&deps->ndeps, sizeof (deps->ndeps), 1, f) != 1)
  320. return -1;
  321. /* The length of each dependence followed by the string. */
  322. for (i = 0; i < deps->ndeps; i++)
  323. {
  324. size_t num_to_write = strlen (deps->depv[i]);
  325. if (fwrite (&num_to_write, sizeof (size_t), 1, f) != 1)
  326. return -1;
  327. if (fwrite (deps->depv[i], num_to_write, 1, f) != 1)
  328. return -1;
  329. }
  330. return 0;
  331. }
  332. /* Read back dependency information written with deps_save into
  333. the deps buffer. The third argument may be NULL, in which case
  334. the dependency information is just skipped, or it may be a filename,
  335. in which case that filename is skipped. */
  336. int
  337. deps_restore (struct deps *deps, FILE *fd, const char *self)
  338. {
  339. unsigned int i, count;
  340. size_t num_to_read;
  341. size_t buf_size = 512;
  342. char *buf;
  343. /* Number of dependences. */
  344. if (fread (&count, 1, sizeof (count), fd) != sizeof (count))
  345. return -1;
  346. buf = XNEWVEC (char, buf_size);
  347. /* The length of each dependence string, followed by the string. */
  348. for (i = 0; i < count; i++)
  349. {
  350. /* Read in # bytes in string. */
  351. if (fread (&num_to_read, 1, sizeof (size_t), fd) != sizeof (size_t))
  352. {
  353. free (buf);
  354. return -1;
  355. }
  356. if (buf_size < num_to_read + 1)
  357. {
  358. buf_size = num_to_read + 1 + 127;
  359. buf = XRESIZEVEC (char, buf, buf_size);
  360. }
  361. if (fread (buf, 1, num_to_read, fd) != num_to_read)
  362. {
  363. free (buf);
  364. return -1;
  365. }
  366. buf[num_to_read] = '\0';
  367. /* Generate makefile dependencies from .pch if -nopch-deps. */
  368. if (self != NULL && filename_cmp (buf, self) != 0)
  369. deps_add_dep (deps, buf);
  370. }
  371. free (buf);
  372. return 0;
  373. }