Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "aspect_bazel_lib", version = "2.9.1")
bazel_dep(name = "bazel_features", version = "1.32.0")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "package_metadata", version = "0.0.5")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "rules_cc", version = "0.0.10")

bazel_dep(name = "aspect_bazel_lib", version = "2.9.1", dev_dependency = True)
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.7.1", dev_dependency = True)
bazel_dep(name = "buildifier_prebuilt", version = "8.0.3", dev_dependency = True)
bazel_dep(name = "gazelle", version = "0.42.0", dev_dependency = True, repo_name = "bazel_gazelle")
Expand Down
2 changes: 1 addition & 1 deletion d/private/resolved_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def _resolved_toolchain_impl(ctx):
return [
toolchain_info,
toolchain_info.default,
toolchain_info.dinfo,
toolchain_info.d_toolchain_info,
toolchain_info.template_variables,
]

Expand Down
2 changes: 2 additions & 0 deletions d/private/rules/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ bzl_library(
],
deps = [
"//d/private:providers",
"@aspect_bazel_lib//lib:expand_make_vars",
"@bazel_skylib//lib:dicts",
"@bazel_skylib//lib:paths",
"@rules_cc//cc/common",
],
Expand Down
4 changes: 2 additions & 2 deletions d/private/rules/binary.bzl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""D test rule for compiling binaries."""

load("//d/private/rules:common.bzl", "TARGET_TYPE", "common_attrs", "compilation_action")
load("//d/private/rules:common.bzl", "TARGET_TYPE", "compilation_action", "runnable_attrs")

def _d_binary_impl(ctx):
"""Implementation of d_binary rule."""
return compilation_action(ctx, target_type = TARGET_TYPE.BINARY)

d_binary = rule(
implementation = _d_binary_impl,
attrs = common_attrs,
attrs = runnable_attrs,
toolchains = ["//d:toolchain_type"],
executable = True,
)
45 changes: 36 additions & 9 deletions d/private/rules/common.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Common definitions for D rules."""

load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_variables")
load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load("//d/private:providers.bzl", "DInfo")
Expand Down Expand Up @@ -30,6 +32,21 @@ common_attrs = {
"_windows_constraint": attr.label(default = "@platforms//os:windows", doc = "Windows platform constraint"),
}

runnable_attrs = dicts.add(
common_attrs,
{
"env": attr.string_dict(doc = "Environment variables for the binary at runtime. Subject of location and make variable expansion."),
"data": attr.label_list(allow_files = True, doc = "List of files to be made available at runtime."),
},
)

library_attrs = dicts.add(
common_attrs,
{
"source_only": attr.bool(doc = "If true, the source files are compiled, but not library is produced."),
},
)

def _get_os(ctx):
if ctx.target_platform_has_constraint(ctx.attr._linux_constraint[platform_common.ConstraintValueInfo]):
return "linux"
Expand All @@ -38,7 +55,7 @@ def _get_os(ctx):
elif ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]):
return "windows"
else:
fail("Unsupported OS: %s" % ctx.label)
fail("OS not supported")

def _binary_name(ctx, name):
"""Returns the name of the binary based on the OS.
Expand Down Expand Up @@ -104,13 +121,13 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
d_deps = [d[DInfo] for d in ctx.attr.deps if DInfo in d]
compiler_flags = depset(ctx.attr.dopts, transitive = [d.compiler_flags for d in d_deps])
imports = depset(
[paths.join(ctx.label.package, imp) for imp in ctx.attr.imports],
[paths.join(ctx.label.workspace_root, ctx.label.package, imp) for imp in ctx.attr.imports],
transitive = [d.imports for d in d_deps],
)
linker_flags = depset(ctx.attr.linkopts, transitive = [d.linker_flags for d in d_deps])
string_imports = depset(
([ctx.label_package] if ctx.files.string_srcs else []) +
[paths.join(ctx.label.package, imp) for imp in ctx.attr.string_imports],
([paths.join(ctx.label.workspace_root, ctx.label.package)] if ctx.files.string_srcs else []) +
[paths.join(ctx.label.workspace_root, ctx.label.package, imp) for imp in ctx.attr.string_imports],
transitive = [d.string_imports for d in d_deps],
)
versions = depset(ctx.attr.versions, transitive = [d.versions for d in d_deps])
Expand Down Expand Up @@ -162,8 +179,8 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
DInfo(
compiler_flags = compiler_flags,
imports = depset(
[ctx.label.package] +
[paths.join(ctx.label.package, imp) for imp in ctx.attr.imports],
[paths.join(ctx.label.workspace_root, ctx.label.package)] +
[paths.join(ctx.label.workspace_root, ctx.label.package, imp) for imp in ctx.attr.imports],
transitive = [d.imports for d in d_deps],
),
interface_srcs = depset(
Expand All @@ -178,12 +195,22 @@ def compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY):
),
linker_flags = linker_flags,
string_imports = depset(
([ctx.label.package] if ctx.files.string_srcs else []) +
[paths.join(ctx.label.package, imp) for imp in ctx.attr.string_imports],
([paths.join(ctx.label.workspace_root, ctx.label.package)] if ctx.files.string_srcs else []) +
[paths.join(ctx.label.workspace_root, ctx.label.package, imp) for imp in ctx.attr.string_imports],
transitive = [d.string_imports for d in d_deps],
),
versions = versions,
),
]
else:
return [DefaultInfo(executable = output)]
env_with_expansions = {
k: expand_variables(ctx, ctx.expand_location(v, ctx.files.data), [output], "env")
for k, v in ctx.attr.env.items()
}
return [
DefaultInfo(
executable = output,
runfiles = ctx.runfiles(files = ctx.files.data),
),
RunEnvironmentInfo(environment = env_with_expansions),
]
11 changes: 2 additions & 9 deletions d/private/rules/library.bzl
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
"""Rule for compiling D libraries."""

load("//d/private:providers.bzl", "DInfo")
load("//d/private/rules:common.bzl", "TARGET_TYPE", "common_attrs", "compilation_action")
load("//d/private/rules:common.bzl", "TARGET_TYPE", "compilation_action", "library_attrs")

def _d_library_impl(ctx):
"""Implementation of d_library rule."""
return compilation_action(ctx, target_type = TARGET_TYPE.LIBRARY)

d_library = rule(
implementation = _d_library_impl,
attrs = dict(
common_attrs.items() +
{
"source_only": attr.bool(
doc = "If true, the source files are compiled, but not library is produced.",
),
}.items(),
),
attrs = library_attrs,
toolchains = ["//d:toolchain_type"],
provides = [DInfo],
)
4 changes: 2 additions & 2 deletions d/private/rules/test.bzl
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""D test rule for compiling and running D unit tests."""

load("//d/private/rules:common.bzl", "TARGET_TYPE", "common_attrs", "compilation_action")
load("//d/private/rules:common.bzl", "TARGET_TYPE", "compilation_action", "runnable_attrs")

def _d_test_impl(ctx):
"""Implementation of d_test rule."""
return compilation_action(ctx, target_type = TARGET_TYPE.TEST)

d_test = rule(
implementation = _d_test_impl,
attrs = common_attrs,
attrs = runnable_attrs,
toolchains = ["//d:toolchain_type"],
test = True,
)
9 changes: 7 additions & 2 deletions docs/rules.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 1 addition & 12 deletions e2e/smoke/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
"""Provides a simple way to test your rules as an external workspace."""

load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@rules_d//d:defs.bzl", "d_binary", "d_test")

d_binary(
name = "fiber_thread_usage",
srcs = ["fiber_thread_usage.d"],
)

d_test(
name = "fiber_thread_usage_test",
srcs = ["fiber_thread_usage.d"],
)

build_test(
name = "smoke_test",
targets = [
":fiber_thread_usage",
"//tests/std_lib:fiber_thread_usage",
],
)
13 changes: 13 additions & 0 deletions e2e/smoke/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ local_repository(

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "aspect_bazel_lib",
sha256 = "f93d386d8d0b0149031175e81df42a488be4267c3ca2249ba5321c23c60bc1f0",
strip_prefix = "bazel-lib-2.9.1",
url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v2.9.1/bazel-lib-v2.9.1.tar.gz",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "aspect_bazel_lib_register_toolchains")

aspect_bazel_lib_dependencies()

aspect_bazel_lib_register_toolchains()

http_archive(
name = "bazel_features",
sha256 = "07bd2b18764cdee1e0d6ff42c9c0a6111ffcbd0c17f0de38e7f44f1519d1c0cd",
Expand Down
12 changes: 12 additions & 0 deletions e2e/smoke/tests/std_lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@rules_d//d:defs.bzl", "d_binary", "d_test")

d_binary(
name = "fiber_thread_usage",
srcs = ["fiber_thread_usage.d"],
visibility = ["//visibility:public"],
)

d_test(
name = "fiber_thread_usage_test",
srcs = ["fiber_thread_usage.d"],
)
17 changes: 13 additions & 4 deletions tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_gazelle//:def.bzl", "gazelle", "gazelle_binary")
load("@rules_d//d:defs.bzl", "d_binary")
load("@rules_d//d:defs.bzl", "d_binary", "d_library")

gazelle_binary(
name = "gazelle_bin",
Expand All @@ -11,12 +11,21 @@ gazelle(
gazelle = "gazelle_bin",
)

d_library(
name = "d_utils",
srcs = [
"curl_downloader.d",
"integrity_hash.d",
],
linkopts = ["-lcurl"],
visibility = ["//dub:__subpackages__"],
)

d_binary(
name = "generate_compiler_versions_bzl",
srcs = [
"curl_downloader.d",
"generate_compiler_versions_bzl.d",
],
linkopts = ["-lcurl"],
tags = ["manual"],
tags = ["manual"], # Not built by default; 'curl' dependency may not be available on all platforms.
deps = [":d_utils"],
)
Loading
Loading