From 13b2442fed56eebaa482c474e0cec87a3516804c Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Fri, 8 Apr 2022 15:58:33 +0200 Subject: [PATCH] Bazel: code reorganization --- WORKSPACE.bazel | 4 ++-- defs.bzl | 14 -------------- misc/bazel/BUILD.bazel | 0 misc/bazel/source_dir.bzl.tpl | 11 +++++++++++ misc/bazel/workspace.bzl | 15 +++++++++++++++ swift/install/BUILD.bazel | 4 ++-- 6 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 misc/bazel/BUILD.bazel create mode 100644 misc/bazel/source_dir.bzl.tpl create mode 100644 misc/bazel/workspace.bzl diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index bc27ed44c180..4d485742b288 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -27,6 +27,6 @@ http_archive( ], ) -load("@ql//:defs.bzl", "ql_utils") +load("@ql//misc/bazel:workspace.bzl", "ql_workspace") -ql_utils(name = "utils") +ql_workspace() diff --git a/defs.bzl b/defs.bzl index de0179d0cd1c..d6748d831761 100644 --- a/defs.bzl +++ b/defs.bzl @@ -3,17 +3,3 @@ codeql_platform = select({ "@platforms//os:macos": "osx64", "@platforms//os:windows": "win64", }) - -_paths_bzl = """ -def source_dir(): - return '%s/' + native.package_name() -""" - -def _ql_utils_impl(repository_ctx): - root = repository_ctx.path(Label("@ql//:WORKSPACE.bazel")).realpath.dirname - repository_ctx.file("BUILD.bazel") - repository_ctx.file("paths.bzl", content = _paths_bzl % root) - -ql_utils = repository_rule( - implementation = _ql_utils_impl, -) diff --git a/misc/bazel/BUILD.bazel b/misc/bazel/BUILD.bazel new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/misc/bazel/source_dir.bzl.tpl b/misc/bazel/source_dir.bzl.tpl new file mode 100644 index 000000000000..245ccf6e8b8e --- /dev/null +++ b/misc/bazel/source_dir.bzl.tpl @@ -0,0 +1,11 @@ +def root_source_dir(): + """ get absolute path to the root source directory + + This can break hermeticity if used in a build step""" + return '{root}' + +def current_source_dir(): + """ get absolute path to the source directory of this bazel package + + This can break hermeticity if used in a build step""" + return root_source_dir() + '/' + native.package_name() diff --git a/misc/bazel/workspace.bzl b/misc/bazel/workspace.bzl new file mode 100644 index 000000000000..b6f9bacd2018 --- /dev/null +++ b/misc/bazel/workspace.bzl @@ -0,0 +1,15 @@ +def _ql_utils_impl(repository_ctx): + root = repository_ctx.path(Label("//:WORKSPACE.bazel")).realpath.dirname + repository_ctx.file("BUILD.bazel") + repository_ctx.template( + "source_dir.bzl", + Label("@ql//misc/bazel:source_dir.bzl.tpl"), + substitutions = {"{root}": str(root)}, + ) + +_ql_utils = repository_rule( + implementation = _ql_utils_impl, +) + +def ql_workspace(): + _ql_utils(name = "utils") diff --git a/swift/install/BUILD.bazel b/swift/install/BUILD.bazel index b270fa8f79d8..5064a265eecc 100644 --- a/swift/install/BUILD.bazel +++ b/swift/install/BUILD.bazel @@ -1,4 +1,4 @@ -load("@utils//:paths.bzl", "source_dir") +load("@utils//:source_dir.bzl", "current_source_dir") load("@rules_pkg//:install.bzl", "pkg_install") pkg_install( @@ -6,6 +6,6 @@ pkg_install( srcs = ["//swift:extractor-pack"], args = [ "--destdir", - source_dir() + "/../extractor_pack", + current_source_dir() + "/../extractor_pack", ], )