// SPDX-License-Identifier: GPL-2.01/*2* "Optimize" a list of dependencies as spit out by gcc -MD3* for the build framework.4*5* Original author:6* Copyright 2002 by Kai Germaschewski <[email protected]>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 the10* base transformation of dependecies without the CONFIG mangle.11*/1213#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>2223char *target;24char *depfile;25char *cmdline;2627static void usage(void)28{29fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");30exit(1);31}3233/*34* Print out the commandline prefixed with cmd_<target filename> :=35*/36static void print_cmdline(void)37{38printf("cmd_%s := %s\n\n", target, cmdline);39}4041/*42* Important: The below generated source_foo.o and deps_foo.o variable43* assignments are parsed not only by make, but also by the rather simple44* parser in scripts/mod/sumversion.c.45*/46static void parse_dep_file(void *map, size_t len)47{48char *m = map;49char *end = m + len;50char *p;51char s[PATH_MAX];52int is_target, has_target = 0;53int saw_any_target = 0;54int is_first_dep = 0;5556while (m < end) {57/* Skip any "white space" */58while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))59m++;60/* Find next "white space" */61p = m;62while (p < end && *p != ' ' && *p != '\\' && *p != '\n')63p++;64/* Is the token we found a target name? */65is_target = (*(p-1) == ':');66/* Don't write any target names into the dependency file */67if (is_target) {68/* The /next/ file is the first dependency */69is_first_dep = 1;70has_target = 1;71} else if (has_target) {72/* Save this token/filename */73memcpy(s, m, p-m);74s[p - m] = 0;7576/*77* Do not list the source file as dependency,78* so that kbuild is not confused if a .c file79* is rewritten into .S or vice versa. Storing80* it in source_* is needed for modpost to81* compute srcversions.82*/83if (is_first_dep) {84/*85* If processing the concatenation of86* multiple dependency files, only87* process the first target name, which88* will be the original source name,89* and ignore any other target names,90* which will be intermediate temporary91* files.92*/93if (!saw_any_target) {94saw_any_target = 1;95printf("source_%s := %s\n\n",96target, s);97printf("deps_%s := \\\n",98target);99}100is_first_dep = 0;101} else102printf(" %s \\\n", s);103}104/*105* Start searching for next token immediately after the first106* "whitespace" character that follows this token.107*/108m = p + 1;109}110111if (!saw_any_target) {112fprintf(stderr, "fixdep: parse error; no targets found\n");113exit(1);114}115116printf("\n%s: $(deps_%s)\n\n", target, target);117printf("$(deps_%s):\n", target);118}119120static void print_deps(void)121{122struct stat st;123int fd;124void *map;125126fd = open(depfile, O_RDONLY);127if (fd < 0) {128fprintf(stderr, "fixdep: error opening depfile: ");129perror(depfile);130exit(2);131}132if (fstat(fd, &st) < 0) {133fprintf(stderr, "fixdep: error fstat'ing depfile: ");134perror(depfile);135exit(2);136}137if (st.st_size == 0) {138fprintf(stderr, "fixdep: %s is empty\n", depfile);139close(fd);140return;141}142map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);143if ((long) map == -1) {144perror("fixdep: mmap");145close(fd);146return;147}148149parse_dep_file(map, st.st_size);150151munmap(map, st.st_size);152153close(fd);154}155156int main(int argc, char **argv)157{158if (argc != 4)159usage();160161depfile = argv[1];162target = argv[2];163cmdline = argv[3];164165print_cmdline();166print_deps();167168return 0;169}170171172