From 9f1bd145272f109e0453bccab8107367a93abdcd Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 23 Dec 2020 17:10:37 +1000 Subject: [PATCH] use protobuf binaries to reduce initial compile times --- defs.bzl | 6 ++--- platforms/README.md | 4 +-- protobuf.bzl | 63 +++++++++++++++++++++++++++++++++++++++++++++ repos.bzl | 13 ---------- 4 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 protobuf.bzl diff --git a/defs.bzl b/defs.bzl index ce6577804..a2c3ed872 100644 --- a/defs.bzl +++ b/defs.bzl @@ -3,10 +3,10 @@ load("@bazel_skylib//lib:versions.bzl", "versions") load("@io_bazel_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("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install") load("@io_bazel_rules_sass//:defs.bzl", "sass_repositories") load("@build_bazel_rules_svelte//:defs.bzl", "rules_svelte_dependencies") -load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") load("@com_github_ali5h_rules_pip//:defs.bzl", "pip_import") load("//pip/pyqt5:defs.bzl", "install_pyqt5") @@ -26,6 +26,8 @@ def setup_deps(): setup_local_python(name = "python") + setup_protobuf_binary(name = "com_google_protobuf") + native.register_toolchains("@python//:python3_toolchain") pip_import( @@ -50,5 +52,3 @@ def setup_deps(): sass_repositories() rules_svelte_dependencies() - - protobuf_deps() diff --git a/platforms/README.md b/platforms/README.md index 651ff8de4..c55b72f75 100644 --- a/platforms/README.md +++ b/platforms/README.md @@ -1,2 +1,2 @@ -Platform constraints. Currently only used to control wheel filenames -on a per-platform basis. +Platform constraints, used for things like controlling wheel filenames +on a per-platform basis, and selecting the correct protobuf binary. diff --git a/protobuf.bzl b/protobuf.bzl new file mode 100644 index 000000000..13cd957c0 --- /dev/null +++ b/protobuf.bzl @@ -0,0 +1,63 @@ +""" +Replace @com_google_protobuf with a platform binary. + +Avoids the long initial compile, but will fail if anything depends on the protobuf library. +""" + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") +load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") + +def _impl(rctx): + rctx.file("BUILD.bazel", """ +alias( + name = "protoc", + actual = select({ + "@net_ankiweb_anki//platforms:windows_x86_64": "@protoc_bin_windows//:bin/protoc.exe", + "@net_ankiweb_anki//platforms:macos_x86_64": "@protoc_bin_macos//:bin/protoc", + "@net_ankiweb_anki//platforms:linux_x86_64": "@protoc_bin_linux//:bin/protoc" + }), + visibility = ["//visibility:public"] +) +""") + +_setup_protoc = repository_rule( + implementation = _impl, + local = True, + attrs = {}, +) + +def setup_protobuf_binary(name): + maybe( + http_archive, + name = "protoc_bin_macos", + urls = [ + "https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/protoc-3.14.0-osx-x86_64.zip", + ], + sha256 = "699ceee7ef0988ecf72bf1c146dee5d9d89351a19d4093d30ebea3c04008bb8c", + build_file_content = """exports_files(["bin/protoc"])""", + ) + + maybe( + http_archive, + name = "protoc_bin_linux", + urls = [ + "https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/protoc-3.14.0-linux-x86_64.zip", + ], + sha256 = "a2900100ef9cda17d9c0bbf6a3c3592e809f9842f2d9f0d50e3fba7f3fc864f0", + build_file_content = """exports_files(["bin/protoc"])""", + ) + + maybe( + http_archive, + name = "protoc_bin_windows", + urls = [ + "https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/protoc-3.14.0-win64.zip", + ], + sha256 = "642554ed4dd2dba94e1afddcccdd7d832999cea309299cc5952f13db389894f8", + build_file_content = """exports_files(["bin/protoc.exe"])""", + ) + + if not native.existing_rule(name): + _setup_protoc( + name = name, + ) diff --git a/repos.bzl b/repos.bzl index 68da262b9..1464287ff 100644 --- a/repos.bzl +++ b/repos.bzl @@ -22,19 +22,6 @@ def register_repos(): ], ) - # protobuf - ############ - - maybe( - http_archive, - name = "com_google_protobuf", - sha256 = "6dd0f6b20094910fbb7f1f7908688df01af2d4f6c5c21331b9f636048674aebf", - strip_prefix = "protobuf-3.14.0", - urls = [ - "https://github.com/protocolbuffers/protobuf/releases/download/v3.14.0/protobuf-all-3.14.0.tar.gz", - ], - ) - # rust ########