mkdeps.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* Dependency generator for Makefile fragments.
  2. Copyright (C) 2000-2022 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. #include "internal.h"
  22. /* Not set up to just include std::vector et al, here's a simple
  23. implementation. */
  24. /* Keep this structure local to this file, so clients don't find it
  25. easy to start making assumptions. */
  26. class mkdeps
  27. {
  28. public:
  29. /* T has trivial cctor & dtor. */
  30. template <typename T>
  31. class vec
  32. {
  33. private:
  34. T *ary;
  35. unsigned num;
  36. unsigned alloc;
  37. public:
  38. vec ()
  39. : ary (NULL), num (0), alloc (0)
  40. {}
  41. ~vec ()
  42. {
  43. XDELETEVEC (ary);
  44. }
  45. public:
  46. unsigned size () const
  47. {
  48. return num;
  49. }
  50. const T &operator[] (unsigned ix) const
  51. {
  52. return ary[ix];
  53. }
  54. T &operator[] (unsigned ix)
  55. {
  56. return ary[ix];
  57. }
  58. void push (const T &elt)
  59. {
  60. if (num == alloc)
  61. {
  62. alloc = alloc ? alloc * 2 : 16;
  63. ary = XRESIZEVEC (T, ary, alloc);
  64. }
  65. ary[num++] = elt;
  66. }
  67. };
  68. struct velt
  69. {
  70. const char *str;
  71. size_t len;
  72. };
  73. mkdeps ()
  74. : module_name (NULL), cmi_name (NULL), is_header_unit (false), quote_lwm (0)
  75. {
  76. }
  77. ~mkdeps ()
  78. {
  79. unsigned int i;
  80. for (i = targets.size (); i--;)
  81. free (const_cast <char *> (targets[i]));
  82. for (i = deps.size (); i--;)
  83. free (const_cast <char *> (deps[i]));
  84. for (i = vpath.size (); i--;)
  85. XDELETEVEC (vpath[i].str);
  86. for (i = modules.size (); i--;)
  87. XDELETEVEC (modules[i]);
  88. XDELETEVEC (module_name);
  89. free (const_cast <char *> (cmi_name));
  90. }
  91. public:
  92. vec<const char *> targets;
  93. vec<const char *> deps;
  94. vec<velt> vpath;
  95. vec<const char *> modules;
  96. public:
  97. const char *module_name;
  98. const char *cmi_name;
  99. bool is_header_unit;
  100. unsigned short quote_lwm;
  101. };
  102. /* Apply Make quoting to STR, TRAIL. Note that it's not possible to
  103. quote all such characters - e.g. \n, %, *, ?, [, \ (in some
  104. contexts), and ~ are not properly handled. It isn't possible to
  105. get this right in any current version of Make. (??? Still true?
  106. Old comment referred to 3.76.1.) */
  107. static const char *
  108. munge (const char *str, const char *trail = nullptr)
  109. {
  110. static unsigned alloc;
  111. static char *buf;
  112. unsigned dst = 0;
  113. for (; str; str = trail, trail = nullptr)
  114. {
  115. unsigned slashes = 0;
  116. char c;
  117. for (const char *probe = str; (c = *probe++);)
  118. {
  119. if (alloc < dst + 4 + slashes)
  120. {
  121. alloc = alloc * 2 + 32;
  122. buf = XRESIZEVEC (char, buf, alloc);
  123. }
  124. switch (c)
  125. {
  126. case '\\':
  127. slashes++;
  128. break;
  129. case '$':
  130. buf[dst++] = '$';
  131. goto def;
  132. case ' ':
  133. case '\t':
  134. /* GNU make uses a weird quoting scheme for white space.
  135. A space or tab preceded by 2N+1 backslashes
  136. represents N backslashes followed by space; a space
  137. or tab preceded by 2N backslashes represents N
  138. backslashes at the end of a file name; and
  139. backslashes in other contexts should not be
  140. doubled. */
  141. while (slashes--)
  142. buf[dst++] = '\\';
  143. /* FALLTHROUGH */
  144. case '#':
  145. buf[dst++] = '\\';
  146. /* FALLTHROUGH */
  147. default:
  148. def:
  149. slashes = 0;
  150. break;
  151. }
  152. buf[dst++] = c;
  153. }
  154. }
  155. buf[dst] = 0;
  156. return buf;
  157. }
  158. /* If T begins with any of the partial pathnames listed in d->vpathv,
  159. then advance T to point beyond that pathname. */
  160. static const char *
  161. apply_vpath (class mkdeps *d, const char *t)
  162. {
  163. if (unsigned len = d->vpath.size ())
  164. for (unsigned i = len; i--;)
  165. {
  166. if (!filename_ncmp (d->vpath[i].str, t, d->vpath[i].len))
  167. {
  168. const char *p = t + d->vpath[i].len;
  169. if (!IS_DIR_SEPARATOR (*p))
  170. goto not_this_one;
  171. /* Do not simplify $(vpath)/../whatever. ??? Might not
  172. be necessary. */
  173. if (p[1] == '.' && p[2] == '.' && IS_DIR_SEPARATOR (p[3]))
  174. goto not_this_one;
  175. /* found a match */
  176. t = t + d->vpath[i].len + 1;
  177. break;
  178. }
  179. not_this_one:;
  180. }
  181. /* Remove leading ./ in any case. */
  182. while (t[0] == '.' && IS_DIR_SEPARATOR (t[1]))
  183. {
  184. t += 2;
  185. /* If we removed a leading ./, then also remove any /s after the
  186. first. */
  187. while (IS_DIR_SEPARATOR (t[0]))
  188. ++t;
  189. }
  190. return t;
  191. }
  192. /* Public routines. */
  193. class mkdeps *
  194. deps_init (void)
  195. {
  196. return new mkdeps ();
  197. }
  198. void
  199. deps_free (class mkdeps *d)
  200. {
  201. delete d;
  202. }
  203. /* Adds a target T. We make a copy, so it need not be a permanent
  204. string. QUOTE is true if the string should be quoted. */
  205. void
  206. deps_add_target (class mkdeps *d, const char *t, int quote)
  207. {
  208. t = xstrdup (apply_vpath (d, t));
  209. if (!quote)
  210. {
  211. /* Sometimes unquoted items are added after quoted ones.
  212. Swap out the lowest quoted. */
  213. if (d->quote_lwm != d->targets.size ())
  214. {
  215. const char *lowest = d->targets[d->quote_lwm];
  216. d->targets[d->quote_lwm] = t;
  217. t = lowest;
  218. }
  219. d->quote_lwm++;
  220. }
  221. d->targets.push (t);
  222. }
  223. /* Sets the default target if none has been given already. An empty
  224. string as the default target in interpreted as stdin. The string
  225. is quoted for MAKE. */
  226. void
  227. deps_add_default_target (class mkdeps *d, const char *tgt)
  228. {
  229. /* Only if we have no targets. */
  230. if (d->targets.size ())
  231. return;
  232. if (tgt[0] == '\0')
  233. d->targets.push (xstrdup ("-"));
  234. else
  235. {
  236. #ifndef TARGET_OBJECT_SUFFIX
  237. # define TARGET_OBJECT_SUFFIX ".o"
  238. #endif
  239. const char *start = lbasename (tgt);
  240. char *o = (char *) alloca (strlen (start)
  241. + strlen (TARGET_OBJECT_SUFFIX) + 1);
  242. char *suffix;
  243. strcpy (o, start);
  244. suffix = strrchr (o, '.');
  245. if (!suffix)
  246. suffix = o + strlen (o);
  247. strcpy (suffix, TARGET_OBJECT_SUFFIX);
  248. deps_add_target (d, o, 1);
  249. }
  250. }
  251. void
  252. deps_add_dep (class mkdeps *d, const char *t)
  253. {
  254. gcc_assert (*t);
  255. t = apply_vpath (d, t);
  256. d->deps.push (xstrdup (t));
  257. }
  258. void
  259. deps_add_vpath (class mkdeps *d, const char *vpath)
  260. {
  261. const char *elem, *p;
  262. for (elem = vpath; *elem; elem = p)
  263. {
  264. for (p = elem; *p && *p != ':'; p++)
  265. continue;
  266. mkdeps::velt elt;
  267. elt.len = p - elem;
  268. char *str = XNEWVEC (char, elt.len + 1);
  269. elt.str = str;
  270. memcpy (str, elem, elt.len);
  271. str[elt.len] = '\0';
  272. if (*p == ':')
  273. p++;
  274. d->vpath.push (elt);
  275. }
  276. }
  277. /* Add a new module target (there can only be one). M is the module
  278. name. */
  279. void
  280. deps_add_module_target (struct mkdeps *d, const char *m,
  281. const char *cmi, bool is_header_unit)
  282. {
  283. gcc_assert (!d->module_name);
  284. d->module_name = xstrdup (m);
  285. d->is_header_unit = is_header_unit;
  286. d->cmi_name = xstrdup (cmi);
  287. }
  288. /* Add a new module dependency. M is the module name. */
  289. void
  290. deps_add_module_dep (struct mkdeps *d, const char *m)
  291. {
  292. d->modules.push (xstrdup (m));
  293. }
  294. /* Write NAME, with a leading space to FP, a Makefile. Advance COL as
  295. appropriate, wrap at COLMAX, returning new column number. Iff
  296. QUOTE apply quoting. Append TRAIL. */
  297. static unsigned
  298. make_write_name (const char *name, FILE *fp, unsigned col, unsigned colmax,
  299. bool quote = true, const char *trail = NULL)
  300. {
  301. if (quote)
  302. name = munge (name, trail);
  303. unsigned size = strlen (name);
  304. if (col)
  305. {
  306. if (colmax && col + size> colmax)
  307. {
  308. fputs (" \\\n", fp);
  309. col = 0;
  310. }
  311. col++;
  312. fputs (" ", fp);
  313. }
  314. col += size;
  315. fputs (name, fp);
  316. return col;
  317. }
  318. /* Write all the names in VEC via make_write_name. */
  319. static unsigned
  320. make_write_vec (const mkdeps::vec<const char *> &vec, FILE *fp,
  321. unsigned col, unsigned colmax, unsigned quote_lwm = 0,
  322. const char *trail = NULL)
  323. {
  324. for (unsigned ix = 0; ix != vec.size (); ix++)
  325. col = make_write_name (vec[ix], fp, col, colmax, ix >= quote_lwm, trail);
  326. return col;
  327. }
  328. /* Write the dependencies to a Makefile. If PHONY is true, add
  329. .PHONY targets for all the dependencies too. */
  330. static void
  331. make_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
  332. {
  333. const mkdeps *d = pfile->deps;
  334. unsigned column = 0;
  335. if (colmax && colmax < 34)
  336. colmax = 34;
  337. if (d->deps.size ())
  338. {
  339. column = make_write_vec (d->targets, fp, 0, colmax, d->quote_lwm);
  340. if (CPP_OPTION (pfile, deps.modules) && d->cmi_name)
  341. column = make_write_name (d->cmi_name, fp, column, colmax);
  342. fputs (":", fp);
  343. column++;
  344. make_write_vec (d->deps, fp, column, colmax);
  345. fputs ("\n", fp);
  346. if (CPP_OPTION (pfile, deps.phony_targets))
  347. for (unsigned i = 1; i < d->deps.size (); i++)
  348. fprintf (fp, "%s:\n", munge (d->deps[i]));
  349. }
  350. if (!CPP_OPTION (pfile, deps.modules))
  351. return;
  352. if (d->modules.size ())
  353. {
  354. column = make_write_vec (d->targets, fp, 0, colmax, d->quote_lwm);
  355. if (d->cmi_name)
  356. column = make_write_name (d->cmi_name, fp, column, colmax);
  357. fputs (":", fp);
  358. column++;
  359. column = make_write_vec (d->modules, fp, column, colmax, 0, ".c++m");
  360. fputs ("\n", fp);
  361. }
  362. if (d->module_name)
  363. {
  364. if (d->cmi_name)
  365. {
  366. /* module-name : cmi-name */
  367. column = make_write_name (d->module_name, fp, 0, colmax,
  368. true, ".c++m");
  369. fputs (":", fp);
  370. column++;
  371. column = make_write_name (d->cmi_name, fp, column, colmax);
  372. fputs ("\n", fp);
  373. column = fprintf (fp, ".PHONY:");
  374. column = make_write_name (d->module_name, fp, column, colmax,
  375. true, ".c++m");
  376. fputs ("\n", fp);
  377. }
  378. if (d->cmi_name && !d->is_header_unit)
  379. {
  380. /* An order-only dependency.
  381. cmi-name :| first-target
  382. We can probably drop this this in favour of Make-4.3's grouped
  383. targets '&:' */
  384. column = make_write_name (d->cmi_name, fp, 0, colmax);
  385. fputs (":|", fp);
  386. column++;
  387. column = make_write_name (d->targets[0], fp, column, colmax);
  388. fputs ("\n", fp);
  389. }
  390. }
  391. if (d->modules.size ())
  392. {
  393. column = fprintf (fp, "CXX_IMPORTS +=");
  394. make_write_vec (d->modules, fp, column, colmax, 0, ".c++m");
  395. fputs ("\n", fp);
  396. }
  397. }
  398. /* Write out dependencies according to the selected format (which is
  399. only Make at the moment). */
  400. /* Really we should be opening fp here. */
  401. void
  402. deps_write (const cpp_reader *pfile, FILE *fp, unsigned int colmax)
  403. {
  404. make_write (pfile, fp, colmax);
  405. }
  406. /* Write out a deps buffer to a file, in a form that can be read back
  407. with deps_restore. Returns nonzero on error, in which case the
  408. error number will be in errno. */
  409. int
  410. deps_save (class mkdeps *deps, FILE *f)
  411. {
  412. unsigned int i;
  413. size_t size;
  414. /* The cppreader structure contains makefile dependences. Write out this
  415. structure. */
  416. /* The number of dependences. */
  417. size = deps->deps.size ();
  418. if (fwrite (&size, sizeof (size), 1, f) != 1)
  419. return -1;
  420. /* The length of each dependence followed by the string. */
  421. for (i = 0; i < deps->deps.size (); i++)
  422. {
  423. size = strlen (deps->deps[i]);
  424. if (fwrite (&size, sizeof (size), 1, f) != 1)
  425. return -1;
  426. if (fwrite (deps->deps[i], size, 1, f) != 1)
  427. return -1;
  428. }
  429. return 0;
  430. }
  431. /* Read back dependency information written with deps_save into
  432. the deps sizefer. The third argument may be NULL, in which case
  433. the dependency information is just skipped, or it may be a filename,
  434. in which case that filename is skipped. */
  435. int
  436. deps_restore (class mkdeps *deps, FILE *fd, const char *self)
  437. {
  438. size_t size;
  439. char *buf = NULL;
  440. size_t buf_size = 0;
  441. /* Number of dependences. */
  442. if (fread (&size, sizeof (size), 1, fd) != 1)
  443. return -1;
  444. /* The length of each dependence string, followed by the string. */
  445. for (unsigned i = size; i--;)
  446. {
  447. /* Read in # bytes in string. */
  448. if (fread (&size, sizeof (size), 1, fd) != 1)
  449. return -1;
  450. if (size >= buf_size)
  451. {
  452. buf_size = size + 512;
  453. buf = XRESIZEVEC (char, buf, buf_size);
  454. }
  455. if (fread (buf, 1, size, fd) != size)
  456. {
  457. XDELETEVEC (buf);
  458. return -1;
  459. }
  460. buf[size] = 0;
  461. /* Generate makefile dependencies from .pch if -nopch-deps. */
  462. if (self != NULL && filename_cmp (buf, self) != 0)
  463. deps_add_dep (deps, buf);
  464. }
  465. XDELETEVEC (buf);
  466. return 0;
  467. }