Skip to content

Commit

Permalink
🪄 Add relative inclusion magic
Browse files Browse the repository at this point in the history
  • Loading branch information
neqochan committed Jun 12, 2022
1 parent 525b7b8 commit 9141b6c
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 42 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module(
name = "rules_ll",
version = "20220609.1",
version = "20220612.0",
execution_platforms_to_register = [
"@rules_ll//ll:ll_linux_exec_platform",
],
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ workspace:
touch WORKSPACE.bazel .bazelrc
echo 6.0.0-pre.20220526.1 >> .bazelversion
echo 'bazel_dep(name="rules_ll", version="20220609.1")' >> MODULE.bazel
echo 'bazel_dep(name="rules_ll", version="20220612.0")' >> MODULE.bazel
Copy the following lines into the just created ``.bashrc`` file::

Expand Down
2 changes: 1 addition & 1 deletion examples/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
bazel_dep(name="rules_ll", version="20220609.1")
bazel_dep(name="rules_ll", version="20220612.0")
local_path_override(module_name = "rules_ll", path = "..")
42 changes: 42 additions & 0 deletions ll/attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ DEFAULT_ATTRS = {
Only used for this target.
""",
),
"relative_includes": attr.string_list(
doc = """Additional quoted include paths, relative to the target
workspace.
This attribute is useful if we require custom include prefix stripping,
but have dynamic paths, such as ones generated by ``bzlmod``. So instead
of using ``includes = ["external/mydep.someversion/include"]`` we can
use ``relative_includes = ["include"]``, and the path to the workspace
will be added automatically.
Only used for this target.
""",
),
"angled_includes": attr.string_list(
doc = """Additional angled include paths for this target.
Expand All @@ -75,6 +88,19 @@ DEFAULT_ATTRS = {
Only used for this target.
""",
),
"relative_angled_includes": attr.string_list(
doc = """Additional angled include paths, relative to the target
workspace.
This attribute is useful if we require custom include prefix stripping,
but have dynamic paths, such as ones generated by ``bzlmod``. So instead
of using ``angled_includes = ["external/mydep.someversion/include"]`` we
can use ``relative_angled_includes = ["include"]``, and the path to the
workspace will be added automatically.
Only used for this target.
""",
),
"compile_flags": attr.string_list(
doc = """Additional flags for the compiler.
Expand Down Expand Up @@ -169,9 +195,25 @@ LIBRARY_ATTRS = {
arguments for all downstream targets.
""",
),
"transitive_relative_includes": attr.string_list(
doc = """Additional transitive include paths, relative to the original
target workspace.
Includes in this attribute will be added to the compile command line
arguments for all downstream targets.
""",
),
"transitive_angled_includes": attr.string_list(
doc = """Additional transitive angled include paths for this target.
Includes in this attribute will be added to the compile command line
arguments for all downstream targets.
""",
),
"transitive_relative_angled_includes": attr.string_list(
doc = """Additional transitive angled include paths, relative to the
original target workspace.
Includes in this attribute will be added to the compile command line
arguments for all downstream targets.
""",
Expand Down
4 changes: 2 additions & 2 deletions ll/bootstrap_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _ll_bootstrap_library_impl(ctx):
defines,
includes,
angled_includes,
transitive_headers,
transitive_hdrs,
transitive_defines,
transitive_includes,
transitive_angled_includes,
Expand Down Expand Up @@ -48,7 +48,7 @@ def _ll_bootstrap_library_impl(ctx):
return [
DefaultInfo(files = depset([out_file])),
LlInfo(
transitive_headers = transitive_headers,
transitive_hdrs = transitive_hdrs,
transitive_defines = transitive_defines,
transitive_includes = transitive_includes,
transitive_angled_includes = transitive_angled_includes,
Expand Down
4 changes: 2 additions & 2 deletions ll/inputs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def compile_object_inputs(ctx, headers, toolchain_type):
transitive = [
headers,
ctx.toolchains[toolchain_type].cpp_stdhdrs.files,
ctx.toolchains[toolchain_type].cpp_abi[LlInfo].transitive_headers,
ctx.toolchains[toolchain_type].cpp_abi[LlInfo].transitive_hdrs,
],
)
elif toolchain_type == "//ll:heterogeneous_toolchain_type":
Expand All @@ -44,7 +44,7 @@ def compile_object_inputs(ctx, headers, toolchain_type):
transitive = [
headers,
ctx.toolchains[toolchain_type].cpp_stdhdrs.files,
ctx.toolchains[toolchain_type].cpp_abi[LlInfo].transitive_headers,
ctx.toolchains[toolchain_type].cpp_abi[LlInfo].transitive_hdrs,
heterogeneous_deps,
],
)
Expand Down
107 changes: 77 additions & 30 deletions ll/internal_functions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,71 @@ Internal functions used by `ll_binary` and `ll_library`.
"""

load("//ll:providers.bzl", "LlInfo")
load("@bazel_skylib//lib:paths.bzl", "paths")

def resolve_binary_deps(ctx):
dep_headers = [dep[LlInfo].transitive_headers for dep in ctx.attr.deps]
dep_defines = [dep[LlInfo].transitive_defines for dep in ctx.attr.deps]
dep_includes = [dep[LlInfo].transitive_includes for dep in ctx.attr.deps]
dep_angled_includes = [
dep[LlInfo].transitive_angled_includes
for dep in ctx.attr.deps
]
def get_transitive_hdrs(ctx, transitive_hdrs):
return depset(
transitive_hdrs,
transitive = [dep[LlInfo].transitive_hdrs for dep in ctx.attr.deps],
)

def get_transitive_defines(ctx, transitive_defines):
return depset(
transitive_defines,
transitive = [dep[LlInfo].transitive_defines for dep in ctx.attr.deps],
)

def get_transitive_includes(
ctx,
transitive_includes,
transitive_relative_includes):
return depset(
transitive_includes + [
paths.join(ctx.label.workspace_root, suffix)
for suffix in transitive_relative_includes
],
transitive = [dep[LlInfo].transitive_includes for dep in ctx.attr.deps],
)

def get_transitive_angled_includes(
ctx,
transitive_angled_includes,
transitive_relative_angled_includes):
return depset(
transitive_angled_includes + [
paths.join(ctx.label.workspace_root, suffix)
for suffix in transitive_relative_angled_includes
],
transitive = [
dep[LlInfo].transitive_angled_includes
for dep in ctx.attr.deps
],
)

def resolve_binary_deps(ctx):
# Headers.
headers = depset(ctx.files.hdrs, transitive = dep_headers)
transitive_hdrs = get_transitive_hdrs(ctx, [])
headers = depset(ctx.files.hdrs, transitive = [transitive_hdrs])

# Defines.
defines = depset(ctx.attr.defines, transitive = dep_defines)
transitive_defines = get_transitive_defines(ctx, [])
defines = depset(ctx.attr.defines, transitive = [transitive_defines])

# Includes.
includes = depset(ctx.attr.includes, transitive = dep_includes)
transitive_includes = get_transitive_includes(ctx, [], [])
includes = depset(
ctx.attr.includes + [
paths.join(ctx.label.workspace_root, suffix)
for suffix in ctx.attr.relative_includes
],
transitive = [transitive_includes],
)

# Angled includes.
transitive_angled_includes = get_transitive_angled_includes(ctx, [], [])
angled_includes = depset(
ctx.attr.angled_includes,
transitive = dep_angled_includes,
transitive = [transitive_angled_includes],
)

return (
Expand All @@ -37,42 +79,47 @@ def resolve_binary_deps(ctx):
)

def resolve_library_deps(ctx):
dep_headers = [dep[LlInfo].transitive_headers for dep in ctx.attr.deps]
dep_defines = [dep[LlInfo].transitive_defines for dep in ctx.attr.deps]
dep_includes = [dep[LlInfo].transitive_includes for dep in ctx.attr.deps]
dep_angled_includes = [
dep[LlInfo].transitive_angled_includes
for dep in ctx.attr.deps
]

# Headers.
transitive_headers = depset(
ctx.files.transitive_hdrs,
transitive = dep_headers,
)
headers = depset(ctx.files.hdrs, transitive = [transitive_headers])
transitive_hdrs = get_transitive_hdrs(ctx, ctx.files.transitive_hdrs)
headers = depset(ctx.files.hdrs, transitive = [transitive_hdrs])

# Defines.
transitive_defines = depset(
transitive_defines = get_transitive_defines(
ctx,
ctx.attr.transitive_defines,
transitive = dep_defines,
)
defines = depset(ctx.attr.defines, transitive = [transitive_defines])

# Includes.
transitive_includes = depset(
transitive_includes = get_transitive_includes(
ctx,
ctx.attr.transitive_includes,
transitive = dep_includes,
ctx.attr.transitive_relative_includes,
)
includes = depset(
ctx.attr.includes + [
paths.join(ctx.label.workspace_root, suffix)
for suffix in ctx.attr.relative_includes
],
transitive = [transitive_includes],
)
includes = depset(ctx.attr.includes, transitive = [transitive_includes])

# Angled includes.
transitive_angled_includes = depset(
ctx.attr.angled_includes,
transitive = dep_angled_includes,
transitive_angled_includes = get_transitive_angled_includes(
ctx,
ctx.attr.transitive_angled_includes,
ctx.attr.transitive_relative_angled_includes,
)
angled_includes = depset(
ctx.attr.angled_includes,
ctx.attr.angled_includes + [
paths.join(ctx.label.workspace_root, suffix)
for suffix in ctx.attr.relative_angled_includes
],
transitive = [transitive_angled_includes],
)

Expand All @@ -81,7 +128,7 @@ def resolve_library_deps(ctx):
defines,
includes,
angled_includes,
transitive_headers,
transitive_hdrs,
transitive_defines,
transitive_includes,
transitive_angled_includes,
Expand Down
4 changes: 2 additions & 2 deletions ll/ll.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _ll_library_impl(ctx):
defines,
includes,
angled_includes,
transitive_headers,
transitive_hdrs,
transitive_defines,
transitive_includes,
transitive_angled_includes,
Expand Down Expand Up @@ -77,7 +77,7 @@ def _ll_library_impl(ctx):
files = depset(out_files),
),
LlInfo(
transitive_headers = transitive_headers,
transitive_hdrs = transitive_hdrs,
transitive_defines = transitive_defines,
transitive_includes = transitive_includes,
transitive_angled_includes = transitive_angled_includes,
Expand Down
4 changes: 1 addition & 3 deletions ll/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ Providers used by `rules_ll`.
LlInfo = provider(
doc = "Provider returned by ll targets.",
fields = {
"exported_headers": "A directory containing exported header files.",
"transitive_headers": "A depset containing header files. These header files are carried to all depending targets.",
"libraries": "A depset containing object files.",
"transitive_hdrs": "A depset containing header files. These header files are carried to all depending targets.",
"transitive_defines": "A depset containing defines. These defines are carried to all depending targets.",
"transitive_includes": "A depset containing include paths. These include paths are carried to all depending targets.",
"transitive_angled_includes": "A depset containing angled include paths. These include paths are carried to all depending targets.",
Expand Down

0 comments on commit 9141b6c

Please sign in to comment.