fixdep.c 3.7 KB

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