build/analysis: replace nogo staticcheck adapter

The upstream repo is not very active and requires us to override the rules_go
dependency name. By building our own adapter, we don't need it anymore.

Change-Id: I6e539881a23a1934d0bf6ebc2d79d02469bd4c6a
Reviewed-on: https://review.monogon.dev/c/monogon/+/4481
Tested-by: Jenkins CI
Reviewed-by: Leopold Schabel <leo@monogon.tech>
diff --git a/build/analysis/staticcheck/BUILD.bazel b/build/analysis/staticcheck/BUILD.bazel
new file mode 100644
index 0000000..589f7c1
--- /dev/null
+++ b/build/analysis/staticcheck/BUILD.bazel
@@ -0,0 +1,25 @@
+load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
+load("@io_bazel_rules_go//go:def.bzl", "go_library")
+
+go_library(
+    name = "staticcheck_lib",
+    srcs = ["staticcheck.go"],
+    importpath = "source.monogon.dev/build/analysis/staticcheck",
+    visibility = ["//build/analysis/staticcheck:__subpackages__"],
+    deps = [
+        "@co_honnef_go_tools//analysis/lint",
+        "@co_honnef_go_tools//quickfix",
+        "@co_honnef_go_tools//simple",
+        "@co_honnef_go_tools//staticcheck",
+        "@co_honnef_go_tools//stylecheck",
+        "@co_honnef_go_tools//unused",
+        "@org_golang_x_tools//go/analysis",
+    ],
+)
+
+write_source_files(
+    name = "staticcheck_analyzers",
+    files = {
+        "analyzers.bzl": "//build/analysis/staticcheck/generate_analyzers:analyzers",
+    },
+)
diff --git a/build/analysis/staticcheck/README.md b/build/analysis/staticcheck/README.md
new file mode 100644
index 0000000..4a63402
--- /dev/null
+++ b/build/analysis/staticcheck/README.md
@@ -0,0 +1,3 @@
+# Staticcheck Nogo Compat
+This package is a replacement for https://github.com/sluongng/nogo-analyzer/, 
+which we previously used. 
\ No newline at end of file
diff --git a/build/analysis/staticcheck/analyzer/BUILD.bazel b/build/analysis/staticcheck/analyzer/BUILD.bazel
new file mode 100644
index 0000000..be9afe8
--- /dev/null
+++ b/build/analysis/staticcheck/analyzer/BUILD.bazel
@@ -0,0 +1,27 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_library")
+load("//build/analysis/staticcheck:analyzers.bzl", "ANALYZER_NAMES")
+
+[
+    go_library(
+        name = analyzer,
+        srcs = ["analyzer.go"],
+        importpath = "source.monogon.dev/build/analysis/staticcheck/analyzer/" + analyzer,
+        visibility = ["//visibility:public"],
+        x_defs = {
+            "name": analyzer,
+        },
+        deps = ["//build/analysis/staticcheck:staticcheck_lib"],
+    )
+    for analyzer in ANALYZER_NAMES
+]
+
+go_library(
+    name = "analyzer",
+    srcs = ["analyzer.go"],
+    importpath = "source.monogon.dev/build/analysis/staticcheck/analyzer",
+    # Gazelle generated target that doesn't do anything,
+    # disable via incompatible.
+    target_compatible_with = ["@platforms//:incompatible"],
+    visibility = ["//visibility:private"],
+    deps = ["//build/analysis/staticcheck:staticcheck_lib"],
+)
diff --git a/build/analysis/staticcheck/analyzer/analyzer.go b/build/analysis/staticcheck/analyzer/analyzer.go
new file mode 100644
index 0000000..b082533
--- /dev/null
+++ b/build/analysis/staticcheck/analyzer/analyzer.go
@@ -0,0 +1,21 @@
+// Copyright The Monogon Project Authors.
+// SPDX-License-Identifier: Apache-2.0
+
+package staticcheck
+
+import (
+	"fmt"
+
+	"source.monogon.dev/build/analysis/staticcheck"
+)
+
+var (
+	name     string
+	Analyzer = staticcheck.Analyzers[name]
+)
+
+func init() {
+	if Analyzer == nil {
+		panic(fmt.Sprintf("staticcheck analyzer %q not found", name))
+	}
+}
diff --git a/build/analysis/staticcheck/analyzers.bzl b/build/analysis/staticcheck/analyzers.bzl
new file mode 100644
index 0000000..5f97fd0
--- /dev/null
+++ b/build/analysis/staticcheck/analyzers.bzl
@@ -0,0 +1,166 @@
+# Generated by //build/analysis/staticcheck/generate_analyzers
+# Do not modify!
+
+ANALYZER_NAMES = [
+    "QF1001",
+    "QF1002",
+    "QF1003",
+    "QF1004",
+    "QF1005",
+    "QF1006",
+    "QF1007",
+    "QF1008",
+    "QF1009",
+    "QF1010",
+    "QF1011",
+    "QF1012",
+    "S1000",
+    "S1001",
+    "S1002",
+    "S1003",
+    "S1004",
+    "S1005",
+    "S1006",
+    "S1007",
+    "S1008",
+    "S1009",
+    "S1010",
+    "S1011",
+    "S1012",
+    "S1016",
+    "S1017",
+    "S1018",
+    "S1019",
+    "S1020",
+    "S1021",
+    "S1023",
+    "S1024",
+    "S1025",
+    "S1028",
+    "S1029",
+    "S1030",
+    "S1031",
+    "S1032",
+    "S1033",
+    "S1034",
+    "S1035",
+    "S1036",
+    "S1037",
+    "S1038",
+    "S1039",
+    "S1040",
+    "SA1000",
+    "SA1001",
+    "SA1002",
+    "SA1003",
+    "SA1004",
+    "SA1005",
+    "SA1006",
+    "SA1007",
+    "SA1008",
+    "SA1010",
+    "SA1011",
+    "SA1012",
+    "SA1013",
+    "SA1014",
+    "SA1015",
+    "SA1016",
+    "SA1017",
+    "SA1018",
+    "SA1019",
+    "SA1020",
+    "SA1021",
+    "SA1023",
+    "SA1024",
+    "SA1025",
+    "SA1026",
+    "SA1027",
+    "SA1028",
+    "SA1029",
+    "SA1030",
+    "SA1031",
+    "SA1032",
+    "SA2000",
+    "SA2001",
+    "SA2002",
+    "SA2003",
+    "SA3000",
+    "SA3001",
+    "SA4000",
+    "SA4001",
+    "SA4003",
+    "SA4004",
+    "SA4005",
+    "SA4006",
+    "SA4008",
+    "SA4009",
+    "SA4010",
+    "SA4011",
+    "SA4012",
+    "SA4013",
+    "SA4014",
+    "SA4015",
+    "SA4016",
+    "SA4017",
+    "SA4018",
+    "SA4019",
+    "SA4020",
+    "SA4021",
+    "SA4022",
+    "SA4023",
+    "SA4024",
+    "SA4025",
+    "SA4026",
+    "SA4027",
+    "SA4028",
+    "SA4029",
+    "SA4030",
+    "SA4031",
+    "SA4032",
+    "SA5000",
+    "SA5001",
+    "SA5002",
+    "SA5003",
+    "SA5004",
+    "SA5005",
+    "SA5007",
+    "SA5008",
+    "SA5009",
+    "SA5010",
+    "SA5011",
+    "SA5012",
+    "SA6000",
+    "SA6001",
+    "SA6002",
+    "SA6003",
+    "SA6005",
+    "SA6006",
+    "SA9001",
+    "SA9002",
+    "SA9003",
+    "SA9004",
+    "SA9005",
+    "SA9006",
+    "SA9007",
+    "SA9008",
+    "SA9009",
+    "ST1000",
+    "ST1001",
+    "ST1003",
+    "ST1005",
+    "ST1006",
+    "ST1008",
+    "ST1011",
+    "ST1012",
+    "ST1013",
+    "ST1015",
+    "ST1016",
+    "ST1017",
+    "ST1018",
+    "ST1019",
+    "ST1020",
+    "ST1021",
+    "ST1022",
+    "ST1023",
+    "U1000",
+]
diff --git a/build/analysis/staticcheck/def.bzl b/build/analysis/staticcheck/def.bzl
new file mode 100644
index 0000000..97509dc
--- /dev/null
+++ b/build/analysis/staticcheck/def.bzl
@@ -0,0 +1,21 @@
+load(":analyzers.bzl", _ANALYZER_NAMES = "ANALYZER_NAMES")
+
+def _name_to_target(name):
+    return Label("//build/analysis/staticcheck/analyzer:" + name)
+
+def filtered_analyzers(filter):
+    """Returns a list of all analyzers without the ones provided as list."""
+    filtered = {
+        name: _name_to_target(name)
+        for name in _ANALYZER_NAMES
+    }
+    for name in filter:
+        filtered.pop(name)
+    return filtered.values()
+
+ANALYZER_NAMES = _ANALYZER_NAMES
+
+ANALYZER_TARGETS = [
+    _name_to_target(name)
+    for name in _ANALYZER_NAMES
+]
diff --git a/build/analysis/staticcheck/generate_analyzers/BUILD.bazel b/build/analysis/staticcheck/generate_analyzers/BUILD.bazel
new file mode 100644
index 0000000..0280cdb
--- /dev/null
+++ b/build/analysis/staticcheck/generate_analyzers/BUILD.bazel
@@ -0,0 +1,21 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
+load(":def.bzl", "generate_analyzers")
+
+generate_analyzers(
+    name = "analyzers",
+    visibility = ["//build/analysis/staticcheck:__pkg__"],
+)
+
+go_library(
+    name = "generate_analyzers_lib",
+    srcs = ["main.go"],
+    importpath = "source.monogon.dev/build/analysis/staticcheck/generate_analyzers",
+    visibility = ["//visibility:private"],
+    deps = ["//build/analysis/staticcheck:staticcheck_lib"],
+)
+
+go_binary(
+    name = "generate_analyzers",
+    embed = [":generate_analyzers_lib"],
+    visibility = ["//visibility:public"],
+)
diff --git a/build/analysis/staticcheck/generate_analyzers/def.bzl b/build/analysis/staticcheck/generate_analyzers/def.bzl
new file mode 100644
index 0000000..2356103
--- /dev/null
+++ b/build/analysis/staticcheck/generate_analyzers/def.bzl
@@ -0,0 +1,25 @@
+def _generate_analyzers_impl(ctx):
+    analyzers_file = ctx.actions.declare_file("analyzers.bzl")
+
+    args = ctx.actions.args()
+    args.add("-out")
+    args.add(analyzers_file)
+
+    ctx.actions.run(
+        outputs = [analyzers_file],
+        executable = ctx.executable._generate_analyzers,
+        arguments = [args],
+    )
+
+    return [DefaultInfo(files = depset([analyzers_file]), runfiles = ctx.runfiles(files = [analyzers_file]))]
+
+generate_analyzers = rule(
+    implementation = _generate_analyzers_impl,
+    attrs = {
+        "_generate_analyzers": attr.label(
+            default = ":generate_analyzers",
+            executable = True,
+            cfg = "exec",
+        ),
+    },
+)
diff --git a/build/analysis/staticcheck/generate_analyzers/main.go b/build/analysis/staticcheck/generate_analyzers/main.go
new file mode 100644
index 0000000..bcf96ff
--- /dev/null
+++ b/build/analysis/staticcheck/generate_analyzers/main.go
@@ -0,0 +1,44 @@
+// Copyright The Monogon Project Authors.
+// SPDX-License-Identifier: Apache-2.0
+
+package main
+
+import (
+	"flag"
+	"fmt"
+	"log"
+	"maps"
+	"os"
+	"slices"
+	"sort"
+
+	"source.monogon.dev/build/analysis/staticcheck"
+)
+
+func main() {
+	out := flag.String("out", "", "The output file to write the list to")
+	flag.Parse()
+
+	if *out == "" {
+		log.Fatal("-out argument is required")
+	}
+
+	outFile, err := os.OpenFile(*out, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0655)
+	if err != nil {
+		log.Fatalf("failed opening file: %v", err)
+	}
+	defer outFile.Close()
+
+	const fileHeader = "# Generated by //build/analysis/staticcheck/generate_analyzers\n# Do not modify!\n\nANALYZER_NAMES = [\n"
+	fmt.Fprint(outFile, fileHeader)
+
+	analyzerNames := slices.Collect(maps.Keys(staticcheck.Analyzers))
+	sort.Strings(analyzerNames)
+
+	for _, name := range analyzerNames {
+		fmt.Fprintf(outFile, "    %q,\n", name)
+	}
+
+	const fileFooter = "]\n"
+	fmt.Fprint(outFile, fileFooter)
+}
diff --git a/build/analysis/staticcheck/staticcheck.go b/build/analysis/staticcheck/staticcheck.go
new file mode 100644
index 0000000..02c5b10
--- /dev/null
+++ b/build/analysis/staticcheck/staticcheck.go
@@ -0,0 +1,32 @@
+// Copyright The Monogon Project Authors.
+// SPDX-License-Identifier: Apache-2.0
+
+package staticcheck
+
+import (
+	"golang.org/x/tools/go/analysis"
+	"honnef.co/go/tools/analysis/lint"
+	"honnef.co/go/tools/quickfix"
+	"honnef.co/go/tools/simple"
+	"honnef.co/go/tools/staticcheck"
+	"honnef.co/go/tools/stylecheck"
+	"honnef.co/go/tools/unused"
+)
+
+// Analyzers contains all staticcheck analyzer passes
+// Copied from https://github.com/sluongng/nogo-analyzer/ under Apache 2.0
+var Analyzers = func() map[string]*analysis.Analyzer {
+	m := make(map[string]*analysis.Analyzer)
+	for _, analyzers := range [][]*lint.Analyzer{
+		quickfix.Analyzers,
+		simple.Analyzers,
+		staticcheck.Analyzers,
+		stylecheck.Analyzers,
+		{unused.Analyzer},
+	} {
+		for _, a := range analyzers {
+			m[a.Analyzer.Name] = a.Analyzer
+		}
+	}
+	return m
+}()