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