fixdep.c 3.7 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, has_target = 0;
  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. has_target = 1;
  65. } else if (has_target) {
  66. /* Save this token/filename */
  67. memcpy(s, m, p-m);
  68. s[p - m] = 0;
  69. /*
  70. * Do not list the source file as dependency,
  71. * so that kbuild is not confused if a .c file
  72. * is rewritten into .S or vice versa. Storing
  73. * it in source_* is needed for modpost to
  74. * compute srcversions.
  75. */
  76. if (is_first_dep) {
  77. /*
  78. * If processing the concatenation of
  79. * multiple dependency files, only
  80. * process the first target name, which
  81. * will be the original source name,
  82. * and ignore any other target names,
  83. * which will be intermediate temporary
  84. * files.
  85. */
  86. if (!saw_any_target) {
  87. saw_any_target = 1;
  88. printf("source_%s := %s\n\n",
  89. target, s);
  90. printf("deps_%s := \\\n",
  91. target);
  92. }
  93. is_first_dep = 0;
  94. } else
  95. printf(" %s \\\n", s);
  96. }
  97. /*
  98. * Start searching for next token immediately after the first
  99. * "whitespace" character that follows this token.
  100. */
  101. m = p + 1;
  102. }
  103. if (!saw_any_target) {
  104. fprintf(stderr, "fixdep: parse error; no targets found\n");
  105. exit(1);
  106. }
  107. printf("\n%s: $(deps_%s)\n\n", target, target);
  108. printf("$(deps_%s):\n", target);
  109. }
  110. static void print_deps(void)
  111. {
  112. struct stat st;
  113. int fd;
  114. void *map;
  115. fd = open(depfile, O_RDONLY);
  116. if (fd < 0) {
  117. fprintf(stderr, "fixdep: error opening depfile: ");
  118. perror(depfile);
  119. exit(2);
  120. }
  121. if (fstat(fd, &st) < 0) {
  122. fprintf(stderr, "fixdep: error fstat'ing depfile: ");
  123. perror(depfile);
  124. exit(2);
  125. }
  126. if (st.st_size == 0) {
  127. fprintf(stderr, "fixdep: %s is empty\n", depfile);
  128. close(fd);
  129. return;
  130. }
  131. map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  132. if ((long) map == -1) {
  133. perror("fixdep: mmap");
  134. close(fd);
  135. return;
  136. }
  137. parse_dep_file(map, st.st_size);
  138. munmap(map, st.st_size);
  139. close(fd);
  140. }
  141. int main(int argc, char **argv)
  142. {
  143. if (argc != 4)
  144. usage();
  145. depfile = argv[1];
  146. target = argv[2];
  147. cmdline = argv[3];
  148. print_cmdline();
  149. print_deps();
  150. return 0;
  151. }