fixdep.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * "Optimize" a list of dependencies as spit out by gcc -MD
  3. * for the build framework.
  4. *
  5. * Original author:
  6. * Copyright 2002 by Kai Germaschewski <kai.germaschewski@gmx.de>
  7. *
  8. * This code has been borrowed from kbuild's fixdep (scripts/basic/fixdep.c),
  9. * Please check it for detailed explanation. This fixdep borow only the
  10. * base transformation of dependecies without the CONFIG mangle.
  11. */
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <sys/mman.h>
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <limits.h>
  21. char *target;
  22. char *depfile;
  23. char *cmdline;
  24. static void usage(void)
  25. {
  26. fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
  27. exit(1);
  28. }
  29. /*
  30. * Print out the commandline prefixed with cmd_<target filename> :=
  31. */
  32. static void print_cmdline(void)
  33. {
  34. printf("cmd_%s := %s\n\n", target, cmdline);
  35. }
  36. /*
  37. * Important: The below generated source_foo.o and deps_foo.o variable
  38. * assignments are parsed not only by make, but also by the rather simple
  39. * parser in scripts/mod/sumversion.c.
  40. */
  41. static void parse_dep_file(void *map, size_t len)
  42. {
  43. char *m = map;
  44. char *end = m + len;
  45. char *p;
  46. char s[PATH_MAX];
  47. int is_target;
  48. int saw_any_target = 0;
  49. int is_first_dep = 0;
  50. while (m < end) {
  51. /* Skip any "white space" */
  52. while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
  53. m++;
  54. /* Find next "white space" */
  55. p = m;
  56. while (p < end && *p != ' ' && *p != '\\' && *p != '\n')
  57. p++;
  58. /* Is the token we found a target name? */
  59. is_target = (*(p-1) == ':');
  60. /* Don't write any target names into the dependency file */
  61. if (is_target) {
  62. /* The /next/ file is the first dependency */
  63. is_first_dep = 1;
  64. } else {
  65. /* Save this token/filename */
  66. memcpy(s, m, p-m);
  67. s[p - m] = 0;
  68. /*
  69. * Do not list the source file as dependency,
  70. * so that kbuild is not confused if a .c file
  71. * is rewritten into .S or vice versa. Storing
  72. * it in source_* is needed for modpost to
  73. * compute srcversions.
  74. */
  75. if (is_first_dep) {
  76. /*
  77. * If processing the concatenation of
  78. * multiple dependency files, only
  79. * process the first target name, which
  80. * will be the original source name,
  81. * and ignore any other target names,
  82. * which will be intermediate temporary
  83. * files.
  84. */
  85. if (!saw_any_target) {
  86. saw_any_target = 1;
  87. printf("source_%s := %s\n\n",
  88. target, s);
  89. printf("deps_%s := \\\n",
  90. target);
  91. }
  92. is_first_dep = 0;
  93. } else
  94. printf(" %s \\\n", s);
  95. }
  96. /*
  97. * Start searching for next token immediately after the first
  98. * "whitespace" character that follows this token.
  99. */
  100. m = p + 1;
  101. }
  102. if (!saw_any_target) {
  103. fprintf(stderr, "fixdep: parse error; no targets found\n");
  104. exit(1);
  105. }
  106. printf("\n%s: $(deps_%s)\n\n", target, target);
  107. printf("$(deps_%s):\n", target);
  108. }
  109. static void print_deps(void)
  110. {
  111. struct stat st;
  112. int fd;
  113. void *map;
  114. fd = open(depfile, O_RDONLY);
  115. if (fd < 0) {
  116. fprintf(stderr, "fixdep: error opening depfile: ");
  117. perror(depfile);
  118. exit(2);
  119. }
  120. if (fstat(fd, &st) < 0) {
  121. fprintf(stderr, "fixdep: error fstat'ing depfile: ");
  122. perror(depfile);
  123. exit(2);
  124. }
  125. if (st.st_size == 0) {
  126. fprintf(stderr, "fixdep: %s is empty\n", depfile);
  127. close(fd);
  128. return;
  129. }
  130. map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  131. if ((long) map == -1) {
  132. perror("fixdep: mmap");
  133. close(fd);
  134. return;
  135. }
  136. parse_dep_file(map, st.st_size);
  137. munmap(map, st.st_size);
  138. close(fd);
  139. }
  140. int main(int argc, char **argv)
  141. {
  142. if (argc != 4)
  143. usage();
  144. depfile = argv[1];
  145. target = argv[2];
  146. cmdline = argv[3];
  147. print_cmdline();
  148. print_deps();
  149. return 0;
  150. }