Merge changes I6b780c1d,I238d963a,I32076c65 into main am: 51f1006440 am: dc59b8c5ce am: 15ee7e75ee

Original change: https://android-review.googlesource.com/c/platform/external/dtc/+/2784358

Change-Id: I2aefd49fb0818eca8010a53d8b44146c3654392c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/Android.bp b/Android.bp
index 85c1b22..2d121ec 100644
--- a/Android.bp
+++ b/Android.bp
@@ -62,6 +62,7 @@
         "-DNO_YAML"
     ],
 
+    generated_headers: ["dtc_version_gen.h"],
     shared_libs: ["libfdt"],
 
     stl: "none",
@@ -117,3 +118,17 @@
         "util.c",
     ],
 }
+
+genrule {
+    name: "dtc_version_gen.h",
+    out: ["version_gen.h"],
+    srcs: ["version_gen.h.in"],
+    tool_files: [
+        "METADATA",
+        "METADATA_version.sed",
+    ],
+    cmd: "version=$$("
+       +     "sed -f $(location METADATA_version.sed) -n $(location METADATA)"
+       + ")-Android-build;"
+       + "sed s/@VCS_TAG@/$${version}/ $(in) > $(out)",
+}
diff --git a/BUILD.bazel b/BUILD.bazel
index d9fdb9a..56aeed2 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -18,68 +18,67 @@
 )
 
 genrule(
-    name = "lexer",
-    srcs = [
-        "dtc-lexer.l",
-        ":parser",
-    ],
+    name = "dtc_lexer_srcs",
+    srcs = ["dtc-lexer.l"],
     outs = ["dtc-lexer.lex.c"],
-    cmd = "lex -o$@ $(location dtc-lexer.l)",
+    cmd = "lex -o $@ $<",
 )
 
 genrule(
-    name = "parser",
+    name = "dtc_parser_srcs",
     srcs = ["dtc-parser.y"],
     outs = [
         "dtc-parser.c",
         "dtc-parser.h",
     ],
-    cmd = """
-      bison -b dtc-parser -d $(location dtc-parser.y)
-      cp ./*.c $(location dtc-parser.c)
-      cp ./*.h $(location dtc-parser.h)
-    """,
+    cmd = "bison -d -o $(location dtc-parser.c) $(location dtc-parser.y)",
 )
 
-cc_library(
-    name = "dtc_gen",
+UTILS = [
+    "util.c",
+    "util.h",
+    ":version_gen_header",
+]
+
+genrule(
+    name = "version_gen_header",
+    outs = ["version_gen.h"],
     srcs = [
-        ":lexer",
-        ":parser",
+        "METADATA",
+        "METADATA_version.sed",
+        "version_gen.h.in",
     ],
-    hdrs = glob(["*.h"]),
-    copts = COPTS,
-    deps = [":libfdt"],
+    cmd = """
+      version="$$(sed -f $(location METADATA_version.sed) -n $(location METADATA))-Android-build"
+      sed s/@VCS_TAG@/$${version}/ $(location version_gen.h.in) > $@
+    """,
 )
 
 cc_binary(
     name = "dtc",
-    srcs = [
+    srcs = UTILS + [
+        ":dtc_lexer_srcs",
+        ":dtc_parser_srcs",
         "checks.c",
         "data.c",
         "dtc.c",
+        "dtc.h",
         "flattree.c",
         "fstree.c",
         "livetree.c",
         "srcpos.c",
+        "srcpos.h",
         "treesource.c",
-        "util.c",
     ],
     copts = COPTS,
     defines = ["NO_YAML"],
-    deps = [
-        ":dtc_gen",
-        ":libfdt",
-    ],
+    deps = [":libfdt"],
 )
 
 cc_binary(
     name = "fdtget",
-    srcs = [
+    srcs = UTILS + [
         "fdtget.c",
-        "util.c",
-        "util.h",
-        "version_non_gen.h",
     ],
     copts = COPTS,
     defines = ["NO_YAML"],
@@ -88,11 +87,8 @@
 
 cc_binary(
     name = "fdtput",
-    srcs = [
+    srcs = UTILS + [
         "fdtput.c",
-        "util.c",
-        "util.h",
-        "version_non_gen.h",
     ],
     copts = COPTS,
     defines = ["NO_YAML"],
@@ -101,11 +97,8 @@
 
 cc_binary(
     name = "fdtdump",
-    srcs = [
+    srcs = UTILS + [
         "fdtdump.c",
-        "util.c",
-        "util.h",
-        "version_non_gen.h",
     ],
     copts = COPTS,
     defines = ["NO_YAML"],
@@ -114,11 +107,8 @@
 
 cc_binary(
     name = "fdtoverlay",
-    srcs = [
+    srcs = UTILS + [
         "fdtoverlay.c",
-        "util.c",
-        "util.h",
-        "version_non_gen.h",
     ],
     copts = COPTS,
     defines = ["NO_YAML"],
diff --git a/METADATA_version.sed b/METADATA_version.sed
new file mode 100644
index 0000000..9e7ea54
--- /dev/null
+++ b/METADATA_version.sed
@@ -0,0 +1 @@
+s/^[[:space:]]*version:[[:space:]]*"v\([[:digit:]][[:digit:]]*.[[:digit:]][[:digit:]]*.[[:digit:]][[:digit:]]*\)"[[:space:]]*/\1/p
diff --git a/util.c b/util.c
index 197fb19..507f012 100644
--- a/util.c
+++ b/util.c
@@ -21,7 +21,7 @@
 
 #include "libfdt.h"
 #include "util.h"
-#include "version_non_gen.h"
+#include "version_gen.h"
 
 char *xstrdup(const char *s)
 {
diff --git a/version_non_gen.h b/version_non_gen.h
deleted file mode 100644
index 3376b35..0000000
--- a/version_non_gen.h
+++ /dev/null
@@ -1 +0,0 @@
-#define DTC_VERSION "DTC 1.6.0-Android-build"