Import buildifier

This patch sets up buildifier to run as part of CI. It can also be run
manually via `//tools/lint:buildifier`.

As a consequence, I needed to make the whole repo conform.

Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: Ic41c97b17255813b6c21aef40ab2f8a216683a2c
diff --git a/tools/lint/BUILD b/tools/lint/BUILD
index 80b4aa1..d33d786 100644
--- a/tools/lint/BUILD
+++ b/tools/lint/BUILD
@@ -12,11 +12,24 @@
 )
 
 sh_binary(
+    name = "buildifier",
+    srcs = ["buildifier.sh"],
+    data = [
+        "@com_github_bazelbuild_buildtools//buildifier",
+    ],
+    target_compatible_with = ["@platforms//cpu:x86_64"],
+    deps = [
+        "@bazel_tools//tools/bash/runfiles",
+    ],
+)
+
+sh_binary(
     name = "run-ci",
     srcs = [
         "run-ci.sh",
     ],
     data = [
+        ":buildifier",
         ":gofmt",
         "//:gazelle-runner",
         "//tools/go:tweak_gazelle_go_deps",
@@ -28,6 +41,7 @@
         #   /var/lib/buildkite-agent/.cache/go-build: permission denied
         "RUNNING_IN_CI": "1" if RUNNING_IN_CI else "0",
     },
+    target_compatible_with = ["@platforms//cpu:x86_64"],
     deps = [
         "@bazel_tools//tools/bash/runfiles",
     ],
diff --git a/tools/lint/buildifier.sh b/tools/lint/buildifier.sh
new file mode 100755
index 0000000..c6bbd5d
--- /dev/null
+++ b/tools/lint/buildifier.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+
+# --- begin runfiles.bash initialization v2 ---
+# Copy-pasted from the Bazel Bash runfiles library v2.
+set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
+source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
+  source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
+  source "$0.runfiles/$f" 2>/dev/null || \
+  source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+  source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
+  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
+# --- end runfiles.bash initialization v2 ---
+
+readonly BUILDIFIER="$(rlocation com_github_bazelbuild_buildtools/buildifier/buildifier_/buildifier)"
+
+# Run everything from the root of the tree.
+cd "${BUILD_WORKSPACE_DIRECTORY}"
+
+# Find all the Starlark files in the repo.
+starlark_files=($(git ls-tree --name-only --full-tree -r @ \
+    | grep -v '^third_party/' \
+    | (grep \
+        -e '\.bzl$' \
+        -e '\.BUILD$' \
+        -e '^BUILD$' \
+        -e '/BUILD$' \
+        -e '/BUILD.bazel$' \
+        -e '^WORKSPACE$' \
+        || :)))
+
+# If we have any Starlark files, format them.
+if ((${#starlark_files[@]} > 0)); then
+    "${BUILDIFIER}" --lint=fix "${starlark_files[@]}"
+    "${BUILDIFIER}" --lint=warn \
+        --warnings=-module-docstring,-function-docstring,-function-docstring-args,-rule-impl-return,-no-effect,-provider-params,-unnamed-macro,-positional-args \
+        "${starlark_files[@]}"
+fi
diff --git a/tools/lint/run-ci.sh b/tools/lint/run-ci.sh
index 48c0960..6228552 100755
--- a/tools/lint/run-ci.sh
+++ b/tools/lint/run-ci.sh
@@ -50,6 +50,10 @@
     "${tweaker}" ./go_deps.bzl
 }
 
+buildifier() {
+    ./tools/lint/buildifier
+}
+
 git_status_is_clean() {
     cd "${BUILD_WORKSPACE_DIRECTORY}"
     if ! git diff --quiet; then
@@ -65,6 +69,7 @@
     update_repos
     gazelle
     tweak_gazelle_go_deps
+    buildifier
     git_status_is_clean  # This must the last linter.
 )