diff --git a/defs.bzl b/defs.bzl index b3f5441bcd6..0002bdd2aaf 100644 --- a/defs.bzl +++ b/defs.bzl @@ -4,7 +4,7 @@ load("@rules_rust//rust:repositories.bzl", "rust_repositories") load("@net_ankiweb_anki//cargo:crates.bzl", "raze_fetch_remote_crates") load(":python.bzl", "setup_local_python") load(":protobuf.bzl", "setup_protobuf_binary") -load("//rslib:clang_format.bzl", "setup_clang_format") +load("//proto:format.bzl", "setup_clang_format") load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install") load("@io_bazel_rules_sass//:defs.bzl", "sass_repositories") load("@com_github_ali5h_rules_pip//:defs.bzl", "pip_import") diff --git a/rslib/.clang-format b/proto/.clang-format similarity index 100% rename from rslib/.clang-format rename to proto/.clang-format diff --git a/proto/BUILD.bazel b/proto/BUILD.bazel new file mode 100644 index 00000000000..da881c35254 --- /dev/null +++ b/proto/BUILD.bazel @@ -0,0 +1,18 @@ +# Copyright: Ankitects Pty Ltd and contributors +# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +load("@rules_proto//proto:defs.bzl", "proto_library") +load("//proto:clang_format.bzl", "proto_format") + +proto_format( + name = "format", + srcs = ["backend.proto"], +) + +proto_library( + name = "backend_proto_lib", + srcs = ["backend.proto"], + visibility = ["//visibility:public"], +) + +exports_files(["backend.proto"]) diff --git a/rslib/backend.proto b/proto/backend.proto similarity index 100% rename from rslib/backend.proto rename to proto/backend.proto diff --git a/rslib/clang_format.bzl b/proto/clang_format.bzl similarity index 98% rename from rslib/clang_format.bzl rename to proto/clang_format.bzl index 3591a82c085..165e82bd277 100644 --- a/rslib/clang_format.bzl +++ b/proto/clang_format.bzl @@ -68,7 +68,7 @@ def proto_format(name, srcs, **kwargs): py_test( name = name, srcs = [ - "proto_format.py", + "format.py", ], data = ["@clang_format//:clang_format"] + srcs, args = ["$(location @clang_format//:clang_format)"] + [native.package_name() + "/" + f for f in srcs], diff --git a/proto/format.bzl b/proto/format.bzl new file mode 100644 index 00000000000..165e82bd277 --- /dev/null +++ b/proto/format.bzl @@ -0,0 +1,76 @@ +# Copyright: Ankitects Pty Ltd and contributors +# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +""" +Exposes a clang-format binary for formatting protobuf. +""" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") +load("@rules_python//python:defs.bzl", "py_test") + +def _impl(rctx): + rctx.file("BUILD.bazel", """ +alias( + name = "clang_format", + actual = select({ + "@net_ankiweb_anki//platforms:windows_x86_64": "@clang_format_windows_x86_64//:clang-format.exe", + "@net_ankiweb_anki//platforms:macos_x86_64": "@clang_format_macos_x86_64//:clang-format", + "@net_ankiweb_anki//platforms:linux_x86_64": "@clang_format_linux_x86_64//:clang-format", + }), + visibility = ["//visibility:public"] +) +""") + +_setup_clang_format = repository_rule( + attrs = {}, + local = True, + implementation = _impl, +) + +def setup_clang_format(name): + maybe( + http_archive, + name = "clang_format_macos_x86_64", + build_file_content = """exports_files(["clang-format"])""", + sha256 = "238be68d9478163a945754f06a213483473044f5a004c4125d3d9d8d3556466e", + urls = [ + "https://github.com/ankitects/clang-format-binaries/releases/download/anki-2021-01-09/clang-format_macos_x86_64.zip", + ], + ) + + maybe( + http_archive, + name = "clang_format_linux_x86_64", + build_file_content = """exports_files(["clang-format"])""", + sha256 = "64060bc4dbca30d0d96aab9344e2783008b16e1cae019a2532f1126ca5ec5449", + urls = [ + "https://github.com/ankitects/clang-format-binaries/releases/download/anki-2021-01-09/clang-format_linux_x86_64.zip", + ], + ) + + maybe( + http_archive, + name = "clang_format_windows_x86_64", + build_file_content = """exports_files(["clang-format.exe"])""", + sha256 = "7d9f6915e3f0fb72407830f0fc37141308d2e6915daba72987a52f309fbeaccc", + urls = [ + "https://github.com/ankitects/clang-format-binaries/releases/download/anki-2021-01-09/clang-format_windows_x86_64.zip", + ], + ) + + if not native.existing_rule(name): + _setup_clang_format( + name = name, + ) + +def proto_format(name, srcs, **kwargs): + py_test( + name = name, + srcs = [ + "format.py", + ], + data = ["@clang_format//:clang_format"] + srcs, + args = ["$(location @clang_format//:clang_format)"] + [native.package_name() + "/" + f for f in srcs], + **kwargs + ) diff --git a/rslib/proto_format.py b/proto/format.py similarity index 100% rename from rslib/proto_format.py rename to proto/format.py diff --git a/pylib/anki/_backend/BUILD.bazel b/pylib/anki/_backend/BUILD.bazel index 2cba311d5ea..8a2fdf4ee64 100644 --- a/pylib/anki/_backend/BUILD.bazel +++ b/pylib/anki/_backend/BUILD.bazel @@ -6,7 +6,7 @@ load("@bazel_skylib//lib:selects.bzl", "selects") py_proto_library_typed( name = "backend_pb2", - src = "//rslib:backend.proto", + src = "//proto:backend.proto", visibility = [ "//visibility:public", ], diff --git a/rslib/BUILD.bazel b/rslib/BUILD.bazel index 988f9f836cb..ccfb727ee37 100644 --- a/rslib/BUILD.bazel +++ b/rslib/BUILD.bazel @@ -1,11 +1,9 @@ # Copyright: Ankitects Pty Ltd and contributors # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -load("@rules_proto//proto:defs.bzl", "proto_library") load("@rules_rust//rust:rust.bzl", "rust_binary", "rust_library", "rust_test") load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script") load(":rustfmt.bzl", "rustfmt_fix", "rustfmt_test") -load(":clang_format.bzl", "proto_format") load("//ts:sql_format.bzl", "sql_format") # Build script @@ -15,7 +13,7 @@ cargo_build_script( name = "build_script", srcs = glob(["build/*.rs"]), build_script_env = { - "BACKEND_PROTO": "$(location backend.proto)", + "BACKEND_PROTO": "$(location //proto:backend.proto)", "PROTOC": "$(location @com_google_protobuf//:protoc)", "RSLIB_FTL_ROOT": "$(location @rslib_ftl//:l10n.toml)", "EXTRA_FTL_ROOT": "$(location @extra_ftl//:l10n.toml)", @@ -24,7 +22,7 @@ cargo_build_script( crate_root = "build/main.rs", data = [ "//ftl", - "backend.proto", + "//proto:backend.proto", "@com_google_protobuf//:protoc", # bazel requires us to list these out separately "@rslib_ftl//:l10n.toml", @@ -163,19 +161,3 @@ sql_format( name = "sql_format", srcs = glob(["**/*.sql"]), ) - -proto_format( - name = "proto_format", - srcs = ["backend.proto"], -) - -# backend.proto -####################### - -proto_library( - name = "backend_proto_lib", - srcs = ["backend.proto"], - visibility = ["//visibility:public"], -) - -exports_files(["backend.proto"]) diff --git a/rslib/build/protobuf.rs b/rslib/build/protobuf.rs index 42c0440a33d..23f05628d62 100644 --- a/rslib/build/protobuf.rs +++ b/rslib/build/protobuf.rs @@ -80,7 +80,7 @@ pub fn write_backend_proto_rs() { proto_dir = backend_proto.parent().unwrap().to_owned(); } else { backend_proto = PathBuf::from("backend.proto"); - proto_dir = PathBuf::from("."); + proto_dir = PathBuf::from("../proto"); } println!("cargo:rerun-if-changed={}", backend_proto.to_str().unwrap()); diff --git a/ts/lib/BUILD.bazel b/ts/lib/BUILD.bazel index 6f0f913661f..ba27d66afef 100644 --- a/ts/lib/BUILD.bazel +++ b/ts/lib/BUILD.bazel @@ -9,7 +9,7 @@ load("//ts:jest.bzl", "jest_test") protobufjs_library( name = "backend_proto", - proto = "//rslib:backend_proto_lib", + proto = "//proto:backend_proto_lib", visibility = ["//visibility:public"], )