third_party: add libpg_query and sqlc

This adds sqlc, a SQL query code generator for Go (and other languages).
It in turn requires pganalyze's libpg_query, which is a C library for
parsing PostgreSQL queries.

To test:

   $ bazel build @com_github_kyleconroy_sqlc//cmd/sqlc

In the future this will be used by Bazel rules to generate sources at
build time.

Change-Id: I369c9ab503e8ce6952fd3f73c233dd3d59922358
Reviewed-on: https://review.monogon.dev/c/monogon/+/882
Tested-by: Jenkins CI
Reviewed-by: Leopold Schabel <leo@monogon.tech>
diff --git a/third_party/libpg_query/BUILD.bazel b/third_party/libpg_query/BUILD.bazel
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/third_party/libpg_query/BUILD.bazel
diff --git a/third_party/libpg_query/README.md b/third_party/libpg_query/README.md
new file mode 100644
index 0000000..d86b7d4
--- /dev/null
+++ b/third_party/libpg_query/README.md
@@ -0,0 +1,19 @@
+libpg\_query
+===
+
+This library provides a C API to parse postgres queries. It consists of some vendored PostgreSQL source code and wrapper header/sources.
+
+Licensing
+---
+
+ * libpg\_query itself: BSD 3-clause
+ * xxhash: BSD 2-clause
+ * protobuf-c: BSD 2-clause (not named, but terms are equal)
+ * PostgreSQL: PostgreSQL license (similar to MIT)
+
+Known Issues
+---
+
+This library has a very wide include path requirement, that includes its own vendor directories (which contain postgres, xxhash and protobuf-c). These includes are pulled into all dependendents of this library and might break anything that wants eg. both libpg\_query and xxhash. When this happens, we should patch the library to always use absolute includes instead, thereby cleaning up the include directives. We technically have `bazel_cc_fix` for that.
+
+We could also unvendor xxhash, protobuf-c and even postgres. But that might not be worth the effort right now.
diff --git a/third_party/libpg_query/external.bzl b/third_party/libpg_query/external.bzl
new file mode 100644
index 0000000..de779ca
--- /dev/null
+++ b/third_party/libpg_query/external.bzl
@@ -0,0 +1,13 @@
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+
+def libpg_query_external(name, version):
+    sums = {
+        "13-2.1.2": "101a7851ee065d824fe06e300b78355a79bd5411864de707761282a0c57a0a97",
+    }
+    http_archive(
+        name = name,
+        build_file = "//third_party/libpg_query/external:BUILD.repo",
+        sha256 = sums[version],
+        strip_prefix = "libpg_query-" + version,
+        urls = ["https://github.com/pganalyze/libpg_query/archive/refs/tags/%s.tar.gz" % version],
+    )
diff --git a/third_party/libpg_query/external/BUILD.bazel b/third_party/libpg_query/external/BUILD.bazel
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/third_party/libpg_query/external/BUILD.bazel
diff --git a/third_party/libpg_query/external/BUILD.repo b/third_party/libpg_query/external/BUILD.repo
new file mode 100644
index 0000000..c0f98a7
--- /dev/null
+++ b/third_party/libpg_query/external/BUILD.repo
@@ -0,0 +1,63 @@
+filegroup(
+    name = "all",
+    srcs = glob(["**"]),
+    visibility = ["//visibility:public"],
+)
+
+cc_library(
+    name = "libpg_query",
+    srcs = glob([
+        "src/*.c",
+        "src/*.h",
+
+        "vendor/protobuf-c/protobuf-c.h",
+        "vendor/protobuf-c/protobuf-c.c",
+
+        "vendor/xxhash/xxhash.c",
+
+        "protobuf/pg_query.pb-c.c",
+        "protobuf/pg_query.pb-c.h",
+
+        "src/postgres/include/*.h",
+        "src/postgres/include/**/*.h",
+    ], [
+        "src/pg_query_enum_defs.c",
+        "src/pg_query_fingerprint_defs.c",
+        "src/pg_query_fingerprint_conds.c",
+        "src/pg_query_outfuncs_defs.c",
+        "src/pg_query_outfuncs_conds.c",
+        "src/pg_query_readfuncs_defs.c",
+        "src/pg_query_readfuncs_conds.c",
+        "src/pg_query_json_helper.c",
+    ]),
+    textual_hdrs = [
+        "src/pg_query_enum_defs.c",
+        "src/pg_query_fingerprint_defs.c",
+        "src/pg_query_fingerprint_conds.c",
+        "src/pg_query_outfuncs_defs.c",
+        "src/pg_query_outfuncs_conds.c",
+        "src/pg_query_readfuncs_defs.c",
+        "src/pg_query_readfuncs_conds.c",
+        "src/pg_query_json_helper.c",
+    ],
+    hdrs = [
+        "pg_query.h",
+        "vendor/xxhash/xxhash.h",
+    ],
+    # Unfortunate. We should patch this library so that this doesn't pollute
+    # all dependents.
+    includes = [
+        "vendor/xxhash",
+        "src/postgres/include",
+        "vendor",
+        "vendor/protobuf-c",
+        "src",
+    ],
+    copts = [
+        "-Iexternal/libpg_query/protobuf",
+        "-Iexternal/libpg_query/vendor/xxhash",
+    ],
+    visibility = [
+        "@com_github_pganalyze_pg_query_go_v2//:__subpackages__",
+    ],
+)