bazelbuild / bazel-skylib

Common useful functions and rules for Bazel
https://bazel.build/
Apache License 2.0
376 stars 179 forks source link

Add bazel rules for handling directories #494

Closed matts1 closed 2 months ago

matts1 commented 3 months ago

I first created this rule to better support sysroots inside rules_cc (https://github.com/bazelbuild/bazel/issues/20334).

However, upon review, @t-rad679 wrote "This issue of wanting packages (directories) instead of individual target labels is coming up frequently for me. Maybe we could reuse this provider for use cases other than sysroot?".

I think this is sufficiently broadly applicable to work with anything. For example, here is some things I plan to do with it down the line:

# arm_sysroot/BUILD.bazel
directory(
    name = "sysroot",
    srcs = glob(["**"], exclude = ["BUILD.bazel"])
)

# toolchain/BUILD.bazel
alias(
    name = "sysroot",
    actual = select({
       ":is_arm": "@arm_sysroot//:sysroot",
       "//conditions:default": "@amd64_sysroot//:sysroot",
    },
)

# Will be implemented in bazel-skylib in a later PR
filter_directory(
   name = "clang_files"
   directory = ":sysroot",
   srcs = ["usr/bin/clang"],
   data = ["lib/libc.so.6"]
)

# Already implemented in rules_cc
cc_tool(
   name = "clang_files",
   executable = ":clang",
)

subdirectory(
    name = "header_dir",
    directory = ":sysroot",
    srcs = ["usr/include"],
)

# Will be implemented in bazel-skylib in a later PR
glob_directory(
    name = "header_files",
    directory = ":header_dir",
    srcs = ["*.h"],
)

# Already implemented in rules_cc
cc_action_type_config(
    name = "c_compile",
    actions = ["@rules_cc//cc/toolchains/actions:c_compile"],
    tools = [":clang"],
    data = [":header_files"],
)

# Already mostly implemented in rules_cc
cc_toolchain(
    name = "cc_toolchain",
    action_type_configs = [":c_compile", ...]
    # At the moment these two params are strings. This PR will allow me to change them to labels.
    sysroot = ":sysroot",
    cxx_builtin_include_dirs = [":header_dir"],
)

@armandomontanez FYI

Note: for now, I haven't written any documentation, but I can add them once the concept gets approval.

matts1 commented 3 months ago

Build failures appear to be for an unrelated reason.

fmeum commented 3 months ago

It's not unrelated, the test runs on skylib itself and you thus need to add the new directory directories you created.

matts1 commented 3 months ago

Ah, thanks, I was in a hurry when I looked at it last and didn't get a chance to fully check it out. Should be fixed now.

armandomontanez commented 2 months ago

The handling of this would be a lot simpler if this Bazel bug was fixed: https://github.com/bazelbuild/bazel/issues/12954

Would it make sense to have a single directory rule instead?

directory(
    name = "root",
    dir = ":__pkg__",  # special-case "__pkg__" to match the existing special-cased label.
    srcs = glob(["**"]),
)

directory(
    name = "subdir",
    dir = ":a",
    srcs = glob(["a/**"]),
)