D rules for Bazel
We make sure this repository works with the latest version of Bazel, but no
other development is planned.
Volunteers are welcome. If you want to use the rules, consider contributing to
this repository and becoming a maintainer.
To use the D rules, add the following to your WORKSPACE
file to add the
external repositories for the D toolchain:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_d",
urls = ["https://github.com/bazelbuild/rules_d/archive/bcf137e3c9381545ce54715632bc1d31c51ee4da.tar.gz"],
sha256 = "a32847bf2ae634563dece49c4dc8353956b64ba5c2d01ce811ea243e1a21b5b7",
strip_prefix = "rules_d-bcf137e3c9381545ce54715632bc1d31c51ee4da",
)
load("@io_bazel_rules_d//d:d.bzl", "d_repositories")
d_repositories()
ddox
d_docs
rule.
d_library(name, srcs, deps, includes, linkopts, versions)
Attributes | |
---|---|
name | Name, required A unique name for this rule.
|
srcs | List of labels, required List of D |
deps | List of labels, optional List of libraries to be linked to this library target.
|
imports | List of strings, optional List of import dirs to add to the compile line.
|
linkopts | List of strings, optional List of flags that are added to the D linker command.
|
versions | List of strings, optional List of versions to be defined during compilation.
|
Suppose you have the following directory structure for a D project:
[workspace]/
WORKSPACE
foo/
BUILD
foo.d
bar.d
baz.d
The library foo
is built using a d_library
target:
foo/BUILD
:
load("@io_bazel_rules_d//d:d.bzl", "d_library")
d_library(
name = "foo",
srcs = [
"foo.d",
"bar.d",
"baz.d",
],
)
d_source_library(name, srcs, deps, includes, linkopts, versions)
Attributes | |
---|---|
name | Name, required A unique name for this rule. |
srcs | List of labels, required
|
deps | List of labels, optional List of library targets depended on by this target.
|
imports | List of strings, optional List of import dirs to add to the compile line.
|
linkopts | List of strings, optional List of flags that are added to the D linker command.
|
versions | List of strings, optional List of version flags to be defined during compilation.
|
Suppose you have the following directory structure for a project building a
C library and a D interface for the C
library:
[workspace]/
WORKSPACE
greeter/
BUILD
native_greeter.c
native_greeter.h
native_greeter.d
hello_world
BUILD
hello_world.d
Build the C library using the cc_library
rule and then use thed_source_library
to define the target for the D interface for the Cnative_greeter
library:
greeter/BUILD
:
load("@io_bazel_rules_d//d:d.bzl", "d_source_library")
cc_library(
name = "native_greeter_lib",
srcs = ["native_greeter.c"],
hdrs = ["native_greeter.h"],
)
d_source_library(
name = "native_greeter",
srcs = ["native_greeter.d"],
deps = [":native_greeter_lib"],
)
Other targets can directly depend on the d_source_library
target to link
the C library:
hello_world/BUILD
:
load("@io_bazel_rules_d//d:d.bzl", "d_source_library")
d_binary(
name = "hello_world",
srcs = ["hello_world.d"],
deps = ["//greeter:native_greeter"],
)
d_binary(name, srcs, deps, includes, linkopts, versions)
Attributes | |
---|---|
name | Name, required A unique name for this rule.
|
srcs | List of labels, required List of D |
deps | List of labels, optional List of libraries to be linked to this binary target.
|
imports | List of strings, optional List of import dirs to add to the compile line.
|
linkopts | List of strings, optional List of flags that are added to the D linker command.
|
versions | List of strings, optional List of versions to be defined during compilation.
|
Suppose you have the following directory structure for a D project:
[workspace]/
WORKSPACE
hello_lib/
BUILD
greeter.d
hello_world
BUILD
hello_world.d
The source file hello_lib/greeter.d
defines a module greeter
:
module greeter;
...
The hello_lib
library is built using a d_library
target:
hello_lib/BUILD
:
load("@io_bazel_rules_d//d:d.bzl", "d_library")
d_library(
name = "hello_lib",
srcs = ["greeter.d"],
)
By default, import paths are from the root of the workspace. Thus, the source
for the hello_world
binary, hello_world.d
, would import the greeter
module as follows:
import hello_lib.greeter;
However, this can be changed via the imports
attribute on the d_library
rule.
The hello_world
binary is built using a d_binary
target:
hello_world/BUILD
:
load("@io_bazel_rules_d//d:d.bzl", "d_library")
d_binary(
name = "hello_world",
srcs = ["hello_world.d"],
deps = ["//hello_lib"],
)
d_test(name, srcs, deps, includes, linkopts, versions)
Attributes | |
---|---|
name | Name, required A unique name for this rule.
|
srcs | List of labels, required List of D |
deps | List of labels, optional List of libraries to be linked to this test target.
|
imports | List of strings, optional List of import dirs to add to the compile line.
|
linkopts | List of strings, optional List of flags that are added to the D linker command.
|
versions | List of strings, optional List of versions to be defined during compilation.
|
Suppose you have the following directory structure for a D project:
[workspace]/
WORKSPACE
hello_lib/
BUILD
greeter.d
greeter_test.d
hello_lib/greeter.d
:
module greeter;
import std.stdio;
import std.string;
class Greeter {
private string greeting;
public:
this(in string greeting) {
this.greeting = greeting.dup;
}
string makeGreeting(in immutable string thing) {
return format("%s %s!", this.greeting, thing);
}
void greet(in immutable string thing) {
writeln(makeGreeting(thing));
}
}
hello_lib/greeter_test.d
:
import hello_lib.greeter;
unittest {
auto greeter = new Greeter("Hello");
assert(greeter.makeGreeting("world") == "Hello world!");
}
void main() {}
To build the library and unit test:
hello_lib/BUILD
:
load("@io_bazel_rules_d//d:d.bzl", "d_library", "d_test")
d_library(
name = "greeter",
srcs = ["greeter.d"],
)
d_test(
name = "greeter_test",
srcs = ["greeter_test.d"],
deps = [":greeter"],
)
The unit test can then be run using:
bazel test //hello_lib:greeter_test
d_docs(name, dep)
Attributes | |
---|---|
name | Name, required A unique name for this rule. |
dep | Label, required The label of the target to generate code documentation for.
|
Suppose you have the following directory structure for a D project:
[workspace]/
WORKSPACE
foo/
BUILD
foo.d
bar.d
baz.d
The foo/
directory contains the sources for the d_library
foo
. To
generate HTML documentation for the foo
library, define a d_docs
target
that takes the d_library
foo
as its dependency:
foo/BUILD
:
load("@io_bazel_rules_d//d:d.bzl", "d_library", "d_docs")
d_library(
name = "foo",
srcs = [
"foo.d",
"bar.d",
"baz.d",
],
)
d_docs(
name = "foo_docs",
dep = ":foo",
)
Running bazel build //foo:foo_docs
will generate a zip file containing the
HTML documentation generated from the source files. See the official D language
documentation on the Documentation Generator for
more information on the conventions for source documentation.