fixdep.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * "Optimize" a list of dependencies as spit out by gcc -MD
  3. * for the kernel build
  4. * ===========================================================================
  5. *
  6. * Author Kai Germaschewski
  7. * Copyright 2002 by Kai Germaschewski <kai.germaschewski@gmx.de>
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. *
  12. *
  13. * Introduction:
  14. *
  15. * gcc produces a very nice and correct list of dependencies which
  16. * tells make when to remake a file.
  17. *
  18. * To use this list as-is however has the drawback that virtually
  19. * every file in the kernel includes <linux/config.h> which then again
  20. * includes <linux/autoconf.h>
  21. *
  22. * If the user re-runs make *config, linux/autoconf.h will be
  23. * regenerated. make notices that and will rebuild every file which
  24. * includes autoconf.h, i.e. basically all files. This is extremely
  25. * annoying if the user just changed CONFIG_HIS_DRIVER from n to m.
  26. *
  27. * So we play the same trick that "mkdep" played before. We replace
  28. * the dependency on linux/autoconf.h by a dependency on every config
  29. * option which is mentioned in any of the listed prequisites.
  30. *
  31. * To be exact, split-include populates a tree in include/config/,
  32. * e.g. include/config/his/driver.h, which contains the #define/#undef
  33. * for the CONFIG_HIS_DRIVER option.
  34. *
  35. * So if the user changes his CONFIG_HIS_DRIVER option, only the objects
  36. * which depend on "include/linux/config/his/driver.h" will be rebuilt,
  37. * so most likely only his driver ;-)
  38. *
  39. * The idea above dates, by the way, back to Michael E Chastain, AFAIK.
  40. *
  41. * So to get dependencies right, there are two issues:
  42. * o if any of the files the compiler read changed, we need to rebuild
  43. * o if the command line given to the compile the file changed, we
  44. * better rebuild as well.
  45. *
  46. * The former is handled by using the -MD output, the later by saving
  47. * the command line used to compile the old object and comparing it
  48. * to the one we would now use.
  49. *
  50. * Again, also this idea is pretty old and has been discussed on
  51. * kbuild-devel a long time ago. I don't have a sensibly working
  52. * internet connection right now, so I rather don't mention names
  53. * without double checking.
  54. *
  55. * This code here has been based partially based on mkdep.c, which
  56. * says the following about its history:
  57. *
  58. * Copyright abandoned, Michael Chastain, <mailto:mec@shout.net>.
  59. * This is a C version of syncdep.pl by Werner Almesberger.
  60. *
  61. *
  62. * It is invoked as
  63. *
  64. * fixdep <depfile> <target> <cmdline>
  65. *
  66. * and will read the dependency file <depfile>
  67. *
  68. * The transformed dependency snipped is written to stdout.
  69. *
  70. * It first generates a line
  71. *
  72. * cmd_<target> = <cmdline>
  73. *
  74. * and then basically copies the .<target>.d file to stdout, in the
  75. * process filtering out the dependency on linux/autoconf.h and adding
  76. * dependencies on include/config/my/option.h for every
  77. * CONFIG_MY_OPTION encountered in any of the prequisites.
  78. *
  79. * It will also filter out all the dependencies on *.ver. We need
  80. * to make sure that the generated version checksum are globally up
  81. * to date before even starting the recursive build, so it's too late
  82. * at this point anyway.
  83. *
  84. * The algorithm to grep for "CONFIG_..." is bit unusual, but should
  85. * be fast ;-) We don't even try to really parse the header files, but
  86. * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will
  87. * be picked up as well. It's not a problem with respect to
  88. * correctness, since that can only give too many dependencies, thus
  89. * we cannot miss a rebuild. Since people tend to not mention totally
  90. * unrelated CONFIG_ options all over the place, it's not an
  91. * efficiency problem either.
  92. *
  93. * (Note: it'd be easy to port over the complete mkdep state machine,
  94. * but I don't think the added complexity is worth it)
  95. */
  96. /*
  97. * Note 2: if somebody writes HELLO_CONFIG_BOOM in a file, it will depend onto
  98. * CONFIG_BOOM. This could seem a bug (not too hard to fix), but please do not
  99. * fix it! Some UserModeLinux files (look at arch/um/) call CONFIG_BOOM as
  100. * UML_CONFIG_BOOM, to avoid conflicts with /usr/include/linux/autoconf.h,
  101. * through arch/um/include/uml-config.h; this fixdep "bug" makes sure that
  102. * those files will have correct dependencies.
  103. */
  104. #include <sys/types.h>
  105. #include <sys/stat.h>
  106. #include <sys/mman.h>
  107. #include <unistd.h>
  108. #include <fcntl.h>
  109. #include <string.h>
  110. #include <stdlib.h>
  111. #include <stdio.h>
  112. #include <limits.h>
  113. #include <ctype.h>
  114. #include <arpa/inet.h>
  115. #define INT_CONF ntohl(0x434f4e46)
  116. #define INT_ONFI ntohl(0x4f4e4649)
  117. #define INT_NFIG ntohl(0x4e464947)
  118. #define INT_FIG_ ntohl(0x4649475f)
  119. char *target;
  120. char *depfile;
  121. char *cmdline;
  122. void usage(void)
  123. {
  124. fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
  125. exit(1);
  126. }
  127. void print_cmdline(void)
  128. {
  129. printf("cmd_%s := %s\n\n", target, cmdline);
  130. }
  131. char * str_config = NULL;
  132. int size_config = 0;
  133. int len_config = 0;
  134. /*
  135. * Grow the configuration string to a desired length.
  136. * Usually the first growth is plenty.
  137. */
  138. void grow_config(int len)
  139. {
  140. while (len_config + len > size_config) {
  141. if (size_config == 0)
  142. size_config = 2048;
  143. str_config = realloc(str_config, size_config *= 2);
  144. if (str_config == NULL)
  145. { perror("fixdep:malloc"); exit(1); }
  146. }
  147. }
  148. /*
  149. * Lookup a value in the configuration string.
  150. */
  151. int is_defined_config(const char * name, int len)
  152. {
  153. const char * pconfig;
  154. const char * plast = str_config + len_config - len;
  155. for ( pconfig = str_config + 1; pconfig < plast; pconfig++ ) {
  156. if (pconfig[ -1] == '\n'
  157. && pconfig[len] == '\n'
  158. && !memcmp(pconfig, name, len))
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. /*
  164. * Add a new value to the configuration string.
  165. */
  166. void define_config(const char * name, int len)
  167. {
  168. grow_config(len + 1);
  169. memcpy(str_config+len_config, name, len);
  170. len_config += len;
  171. str_config[len_config++] = '\n';
  172. }
  173. /*
  174. * Clear the set of configuration strings.
  175. */
  176. void clear_config(void)
  177. {
  178. len_config = 0;
  179. define_config("", 0);
  180. }
  181. /*
  182. * Record the use of a CONFIG_* word.
  183. */
  184. void use_config(char *m, int slen)
  185. {
  186. char s[PATH_MAX];
  187. char *p;
  188. if (is_defined_config(m, slen))
  189. return;
  190. define_config(m, slen);
  191. memcpy(s, m, slen); s[slen] = 0;
  192. for (p = s; p < s + slen; p++) {
  193. if (*p == '_')
  194. *p = '/';
  195. else
  196. *p = tolower((int)*p);
  197. }
  198. printf(" $(wildcard include/config/%s.h) \\\n", s);
  199. }
  200. void parse_config_file(char *map, size_t len)
  201. {
  202. int *end = (int *) (map + len);
  203. /* start at +1, so that p can never be < map */
  204. int *m = (int *) map + 1;
  205. char *p, *q;
  206. for (; m < end; m++) {
  207. if (*m == INT_CONF) { p = (char *) m ; goto conf; }
  208. if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
  209. if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
  210. if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
  211. continue;
  212. conf:
  213. if (p > map + len - 7)
  214. continue;
  215. if (memcmp(p, "CONFIG_", 7))
  216. continue;
  217. for (q = p + 7; q < map + len; q++) {
  218. if (!(isalnum(*q) || *q == '_'))
  219. goto found;
  220. }
  221. continue;
  222. found:
  223. use_config(p+7, q-p-7);
  224. }
  225. }
  226. /* test is s ends in sub */
  227. int strrcmp(char *s, char *sub)
  228. {
  229. int slen = strlen(s);
  230. int sublen = strlen(sub);
  231. if (sublen > slen)
  232. return 1;
  233. return memcmp(s + slen - sublen, sub, sublen);
  234. }
  235. void do_config_file(char *filename)
  236. {
  237. struct stat st;
  238. int fd;
  239. void *map;
  240. fd = open(filename, O_RDONLY);
  241. if (fd < 0) {
  242. fprintf(stderr, "fixdep: ");
  243. perror(filename);
  244. exit(2);
  245. }
  246. fstat(fd, &st);
  247. if (st.st_size == 0) {
  248. close(fd);
  249. return;
  250. }
  251. map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  252. if ((long) map == -1) {
  253. perror("fixdep: mmap");
  254. close(fd);
  255. return;
  256. }
  257. parse_config_file(map, st.st_size);
  258. munmap(map, st.st_size);
  259. close(fd);
  260. }
  261. void parse_dep_file(void *map, size_t len)
  262. {
  263. char *m = map;
  264. char *end = m + len;
  265. char *p;
  266. char s[PATH_MAX];
  267. p = strchr(m, ':');
  268. if (!p) {
  269. fprintf(stderr, "fixdep: parse error\n");
  270. exit(1);
  271. }
  272. memcpy(s, m, p-m); s[p-m] = 0;
  273. printf("deps_%s := \\\n", target);
  274. m = p+1;
  275. clear_config();
  276. while (m < end) {
  277. while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
  278. m++;
  279. p = m;
  280. while (p < end && *p != ' ') p++;
  281. if (p == end) {
  282. do p--; while (!isalnum(*p));
  283. p++;
  284. }
  285. memcpy(s, m, p-m); s[p-m] = 0;
  286. if (strrcmp(s, "include/linux/autoconf.h") &&
  287. strrcmp(s, "arch/um/include/uml-config.h") &&
  288. strrcmp(s, ".ver")) {
  289. printf(" %s \\\n", s);
  290. do_config_file(s);
  291. }
  292. m = p + 1;
  293. }
  294. printf("\n%s: $(deps_%s)\n\n", target, target);
  295. printf("$(deps_%s):\n", target);
  296. }
  297. void print_deps(void)
  298. {
  299. struct stat st;
  300. int fd;
  301. void *map;
  302. fd = open(depfile, O_RDONLY);
  303. if (fd < 0) {
  304. fprintf(stderr, "fixdep: ");
  305. perror(depfile);
  306. exit(2);
  307. }
  308. fstat(fd, &st);
  309. if (st.st_size == 0) {
  310. fprintf(stderr,"fixdep: %s is empty\n",depfile);
  311. close(fd);
  312. return;
  313. }
  314. map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  315. if ((long) map == -1) {
  316. perror("fixdep: mmap");
  317. close(fd);
  318. return;
  319. }
  320. parse_dep_file(map, st.st_size);
  321. munmap(map, st.st_size);
  322. close(fd);
  323. }
  324. void traps(void)
  325. {
  326. static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
  327. if (*(int *)test != INT_CONF) {
  328. fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianess? %#x\n",
  329. *(int *)test);
  330. exit(2);
  331. }
  332. }
  333. int main(int argc, char *argv[])
  334. {
  335. traps();
  336. if (argc != 4)
  337. usage();
  338. depfile = argv[1];
  339. target = argv[2];
  340. cmdline = argv[3];
  341. print_cmdline();
  342. print_deps();
  343. return 0;
  344. }