fixdep.c 9.5 KB

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