tools build: Add fixdep dependency helper
authorJiri Olsa <jolsa@kernel.org>
Wed, 23 Sep 2015 10:33:58 +0000 (12:33 +0200)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 28 Sep 2015 18:50:55 +0000 (15:50 -0300)
For dependency tracking we currently use targets that fall out of the
gcc -MD command. We store this info in the .cmd file and include as
makefile during the build.

This format put object as target and all the c and header files as
dependencies, like:

  util/abspath.o: util/abspath.c /usr/include/stdc-predef.h util/cache.h \
   /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h \
   ...

If any of those dependency header files (krava.h below) is removed the
build fails on:

  make[1]: *** No rule to make target 'krava.h', needed by 'inc.o'.  Stop.

This patch adds fixdep helper, that is used by kbuild to alter the shape
of the object dependencies like:

  source_util/abspath.o := util/abspath.c

  deps_util/abspath.o := \
    /usr/include/stdc-predef.h \
    util/cache.h \
    ...

  util/abspath.o: $(deps_util/abspath.o)

  $(deps_util/abspath.o):

With this format the header removal won't make the build fail, because
it'll be picked up by the last empty target defined for each header.

As previously mentioned the fixdep tool is taken from kbuild. It's not
complete backport, only the part that alters the standard dependency
info was taken, the part that adds the CONFIG_* dependency logic will be
probably taken later on.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Kai Germaschewski <kai.germaschewski@gmx.de>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Link: http://lkml.kernel.org/r/1443004442-32660-4-git-send-email-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/build/Build [new file with mode: 0644]
tools/build/Makefile [new file with mode: 0644]
tools/build/fixdep.c [new file with mode: 0644]

diff --git a/tools/build/Build b/tools/build/Build
new file mode 100644 (file)
index 0000000..63a6c34
--- /dev/null
@@ -0,0 +1 @@
+fixdep-y := fixdep.o
diff --git a/tools/build/Makefile b/tools/build/Makefile
new file mode 100644 (file)
index 0000000..a930362
--- /dev/null
@@ -0,0 +1,43 @@
+ifeq ($(srctree),)
+srctree := $(patsubst %/,%,$(dir $(shell pwd)))
+srctree := $(patsubst %/,%,$(dir $(srctree)))
+endif
+
+include $(srctree)/tools//scripts/Makefile.include
+
+define allow-override
+  $(if $(or $(findstring environment,$(origin $(1))),\
+            $(findstring command line,$(origin $(1)))),,\
+    $(eval $(1) = $(2)))
+endef
+
+$(call allow-override,CC,$(CROSS_COMPILE)gcc)
+$(call allow-override,LD,$(CROSS_COMPILE)ld)
+
+ifeq ($(V),1)
+  Q =
+else
+  Q = @
+endif
+
+export Q srctree CC LD
+
+MAKEFLAGS := --no-print-directory
+build     := -f $(srctree)/tools/build/Makefile.build dir=. obj
+
+all: fixdep
+
+clean:
+       $(call QUIET_CLEAN, fixdep)
+       $(Q)find . -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d' -delete
+       $(Q)rm -f fixdep
+
+$(OUTPUT)fixdep-in.o: FORCE
+       $(Q)$(MAKE) $(build)=fixdep
+
+$(OUTPUT)fixdep: $(OUTPUT)fixdep-in.o
+       $(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $<
+
+FORCE:
+
+.PHONY: FORCE
diff --git a/tools/build/fixdep.c b/tools/build/fixdep.c
new file mode 100644 (file)
index 0000000..1521d36
--- /dev/null
@@ -0,0 +1,168 @@
+/*
+ * "Optimize" a list of dependencies as spit out by gcc -MD
+ * for the build framework.
+ *
+ * Original author:
+ *   Copyright    2002 by Kai Germaschewski  <kai.germaschewski@gmx.de>
+ *
+ * This code has been borrowed from kbuild's fixdep (scripts/basic/fixdep.c),
+ * Please check it for detailed explanation. This fixdep borow only the
+ * base transformation of dependecies without the CONFIG mangle.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <limits.h>
+
+char *target;
+char *depfile;
+char *cmdline;
+
+static void usage(void)
+{
+       fprintf(stderr, "Usage: fixdep <depfile> <target> <cmdline>\n");
+       exit(1);
+}
+
+/*
+ * Print out the commandline prefixed with cmd_<target filename> :=
+ */
+static void print_cmdline(void)
+{
+       printf("cmd_%s := %s\n\n", target, cmdline);
+}
+
+/*
+ * Important: The below generated source_foo.o and deps_foo.o variable
+ * assignments are parsed not only by make, but also by the rather simple
+ * parser in scripts/mod/sumversion.c.
+ */
+static void parse_dep_file(void *map, size_t len)
+{
+       char *m = map;
+       char *end = m + len;
+       char *p;
+       char s[PATH_MAX];
+       int is_target;
+       int saw_any_target = 0;
+       int is_first_dep = 0;
+
+       while (m < end) {
+               /* Skip any "white space" */
+               while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
+                       m++;
+               /* Find next "white space" */
+               p = m;
+               while (p < end && *p != ' ' && *p != '\\' && *p != '\n')
+                       p++;
+               /* Is the token we found a target name? */
+               is_target = (*(p-1) == ':');
+               /* Don't write any target names into the dependency file */
+               if (is_target) {
+                       /* The /next/ file is the first dependency */
+                       is_first_dep = 1;
+               } else {
+                       /* Save this token/filename */
+                       memcpy(s, m, p-m);
+                       s[p - m] = 0;
+
+                       /*
+                        * Do not list the source file as dependency,
+                        * so that kbuild is not confused if a .c file
+                        * is rewritten into .S or vice versa. Storing
+                        * it in source_* is needed for modpost to
+                        * compute srcversions.
+                        */
+                       if (is_first_dep) {
+                               /*
+                                * If processing the concatenation of
+                                * multiple dependency files, only
+                                * process the first target name, which
+                                * will be the original source name,
+                                * and ignore any other target names,
+                                * which will be intermediate temporary
+                                * files.
+                                */
+                               if (!saw_any_target) {
+                                       saw_any_target = 1;
+                                       printf("source_%s := %s\n\n",
+                                               target, s);
+                                       printf("deps_%s := \\\n",
+                                               target);
+                               }
+                               is_first_dep = 0;
+                       } else
+                               printf("  %s \\\n", s);
+               }
+               /*
+                * Start searching for next token immediately after the first
+                * "whitespace" character that follows this token.
+                */
+               m = p + 1;
+       }
+
+       if (!saw_any_target) {
+               fprintf(stderr, "fixdep: parse error; no targets found\n");
+               exit(1);
+       }
+
+       printf("\n%s: $(deps_%s)\n\n", target, target);
+       printf("$(deps_%s):\n", target);
+}
+
+static void print_deps(void)
+{
+       struct stat st;
+       int fd;
+       void *map;
+
+       fd = open(depfile, O_RDONLY);
+       if (fd < 0) {
+               fprintf(stderr, "fixdep: error opening depfile: ");
+               perror(depfile);
+               exit(2);
+       }
+       if (fstat(fd, &st) < 0) {
+               fprintf(stderr, "fixdep: error fstat'ing depfile: ");
+               perror(depfile);
+               exit(2);
+       }
+       if (st.st_size == 0) {
+               fprintf(stderr, "fixdep: %s is empty\n", depfile);
+               close(fd);
+               return;
+       }
+       map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+       if ((long) map == -1) {
+               perror("fixdep: mmap");
+               close(fd);
+               return;
+       }
+
+       parse_dep_file(map, st.st_size);
+
+       munmap(map, st.st_size);
+
+       close(fd);
+}
+
+int main(int argc, char **argv)
+{
+       if (argc != 4)
+               usage();
+
+       depfile = argv[1];
+       target  = argv[2];
+       cmdline = argv[3];
+
+       print_cmdline();
+       print_deps();
+
+       return 0;
+}