postjevsql.git / build / defs.bzl
1# The extension version. The control file's default_version, the
2# generated install script's name and the crate's CARGO_PKG_VERSION must
3# agree; this is the one place it is written.
4VERSION = "0.0.1"
5
6# The PostgreSQL majors the extension supports, oldest first. The
7# generated Cargo manifests declare one `pgNN` feature per entry and make
8# the last the default (tools/cargo-gen). third-party/Cargo.toml selects
9# the one buck builds.
10PG_MAJORS = ["17", "18"]
11
12# The major a target builds for is a configuration, not a buckconfig
13# value: `//platforms:pgNN` sets the `pg_major` constraint, and every
14# major-dependent attribute branches on it through `pg_major_select`, so
15# one graph builds (and tests) every major. Anything configured without
16# the constraint (the default target platform, the execution platform)
17# gets the newest. Code that differs between majors branches on the
18# `feature = "pgNN"` cfg, which every pgrx-reaching crate gets, as under
19# cargo's generated `pgNN` features.
20def pg_major_select(value_for):
21    """A select over the `pg_major` constraint: `value_for(major)` per major."""
22    branches = {"root//platforms:pg_major_" + m: value_for(m) for m in PG_MAJORS}
23    branches["DEFAULT"] = value_for(PG_MAJORS[-1])
24    return select(branches)
25
26def _pg_major_features():
27    return pg_major_select(lambda m: ["pg" + m])
28
29# The Rust edition of every first-party crate, for buck (toolchains/BUCK)
30# and the generated Cargo manifests alike.
31RUST_EDITION = "2024"
32
33# First-party Rust targets. Each one also gets a `<name>-lint` test that
34# fails when clippy (which includes every rustc warning) reports
35# anything: buck prints no compiler output for actions that succeed, so
36# without this a warning is invisible. Third-party crates are built with
37# --cap-lints=allow by reindeer's rules and are not linted.
38
39def _lint_test(name):
40    native.rust_test(
41        name = name + "-lint",
42        mapped_srcs = {"//build:lint.rs": "lint.rs"},
43        crate_root = "lint.rs",
44        env = {"LINT_REPORT": "$(location :{}[clippy.txt])".format(name)},
45        labels = ["lint"],
46    )
47
48# Every first-party target also records what tools/cargo-gen needs to
49# write its Cargo manifest: a `<name>-cargo` JSON file of its kind, crate
50# root and deps, and a `<name>-manifest` filegroup of the manifest
51# committed beside it, which the drift test compares.
52def _cargo_meta(name, kind, crate_root, deps, pgrx_extension = False):
53    meta = {
54        "package": native.package_name(),
55        "name": name,
56        "kind": kind,
57        "crate_root": crate_root,
58        "deps": deps,
59        "pgrx_extension": pgrx_extension,
60    }
61    native.genrule(
62        name = name + "-cargo",
63        out = name + ".json",
64        cmd = "cat > $OUT <<'CARGO_META_EOF'\n" + json.encode(meta) + "\nCARGO_META_EOF",
65        visibility = ["PUBLIC"],
66    )
67    native.filegroup(
68        name = name + "-manifest",
69        srcs = native.glob(["Cargo.toml", "src/bin/pgrx_embed.rs"]),
70        visibility = ["PUBLIC"],
71    )
72
73def first_party_library(name, pgrx_extension = False, pg_major = False, **kwargs):
74    if pg_major:
75        kwargs["features"] = kwargs.get("features", []) + _pg_major_features()
76    native.rust_library(name = name, **kwargs)
77    _cargo_meta(name, "library", kwargs["crate_root"], kwargs.get("deps", []), pgrx_extension)
78    _lint_test(name)
79
80def first_party_binary(name, **kwargs):
81    native.rust_binary(name = name, **kwargs)
82    _cargo_meta(name, "binary", kwargs["crate_root"], kwargs.get("deps", []))
83    _lint_test(name)
84
85def first_party_test(name, **kwargs):
86    native.rust_test(name = name, **kwargs)
87
88    # The same test on every older major: `<name>-pgNN` is configured for
89    # //platforms:pgNN, so its whole graph (pgrx's features, bindgen's
90    # pg_config, the server it starts) is that major's.
91    for m in PG_MAJORS[:-1]:
92        native.rust_test(
93            name = name + "-pg" + m,
94            default_target_platform = "root//platforms:pg" + m,
95            **kwargs
96        )
97    _cargo_meta(name, "test", kwargs["crate_root"], kwargs.get("deps", []))
98    _lint_test(name)