1 // SPDX-License-Identifier: GPL-2.0
3 * "Optimize" a list of dependencies as spit out by gcc -MD
4 * for the build framework.
7 * Copyright 2002 by Kai Germaschewski <kai.germaschewski@gmx.de>
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.
14 #include <sys/types.h>
28 static void usage(void)
30 fprintf(stderr
, "Usage: fixdep <depfile> <target> <cmdline>\n");
35 * Print out the commandline prefixed with cmd_<target filename> :=
37 static void print_cmdline(void)
39 printf("cmd_%s := %s\n\n", target
, cmdline
);
43 * Important: The below generated source_foo.o and deps_foo.o variable
44 * assignments are parsed not only by make, but also by the rather simple
45 * parser in scripts/mod/sumversion.c.
47 static void parse_dep_file(void *map
, size_t len
)
53 int is_target
, has_target
= 0;
54 int saw_any_target
= 0;
58 /* Skip any "white space" */
59 while (m
< end
&& (*m
== ' ' || *m
== '\\' || *m
== '\n'))
61 /* Find next "white space" */
63 while (p
< end
&& *p
!= ' ' && *p
!= '\\' && *p
!= '\n')
65 /* Is the token we found a target name? */
66 is_target
= (*(p
-1) == ':');
67 /* Don't write any target names into the dependency file */
69 /* The /next/ file is the first dependency */
72 } else if (has_target
) {
73 /* Save this token/filename */
78 * Do not list the source file as dependency,
79 * so that kbuild is not confused if a .c file
80 * is rewritten into .S or vice versa. Storing
81 * it in source_* is needed for modpost to
82 * compute srcversions.
86 * If processing the concatenation of
87 * multiple dependency files, only
88 * process the first target name, which
89 * will be the original source name,
90 * and ignore any other target names,
91 * which will be intermediate temporary
94 if (!saw_any_target
) {
96 printf("source_%s := %s\n\n",
98 printf("deps_%s := \\\n",
103 printf(" %s \\\n", s
);
106 * Start searching for next token immediately after the first
107 * "whitespace" character that follows this token.
112 if (!saw_any_target
) {
113 fprintf(stderr
, "fixdep: parse error; no targets found\n");
117 printf("\n%s: $(deps_%s)\n\n", target
, target
);
118 printf("$(deps_%s):\n", target
);
121 static void print_deps(void)
127 fd
= open(depfile
, O_RDONLY
);
129 fprintf(stderr
, "fixdep: error opening depfile: ");
133 if (fstat(fd
, &st
) < 0) {
134 fprintf(stderr
, "fixdep: error fstat'ing depfile: ");
138 if (st
.st_size
== 0) {
139 fprintf(stderr
, "fixdep: %s is empty\n", depfile
);
143 map
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
144 if ((long) map
== -1) {
145 perror("fixdep: mmap");
150 parse_dep_file(map
, st
.st_size
);
152 munmap(map
, st
.st_size
);
157 int main(int argc
, char **argv
)