Skip to content

Commit

Permalink
Clean up the rules examples, part 1.
Browse files Browse the repository at this point in the history
1. An final goal would be to rename this to something more informative,
like "basic_rules", or "rules/basics". We are not doing that in this
PR because it will break all the pointers in docs.

Instead, this change starts to enable that by changing the absolute
references to targets within the examples to relative ones. That makes
it easier for people to grab the example and copy it into their own
code while having it still work.

2. Move starlark_configurations out of rules and into 'configurations'.
Someone should have learned the basics first before moving into
advanced topics.  I don't actually care about the final name for this,
because the short term goal is to get it out of the rules tree so
that I we can unblock CI for rules while trying to fix this. My only
concern for the name is that we don't call it 'starlark_configuration'
because that name only makes sense from the historicial point of view
of the bazel team, where we had native configuration and transitions.
From most users' point of view.  This is simply about defininging the
build configuration. It happens to be done on starlark because that
is the configuration language.

3. From 1 and 2... get the tests passing again.
  • Loading branch information
aiuto committed Apr 19, 2022
1 parent c653b1d commit e5e9d05
Show file tree
Hide file tree
Showing 51 changed files with 37 additions and 20 deletions.
File renamed without changes.
16 changes: 16 additions & 0 deletions configurations/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
workspace(name = "examples")

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

http_archive(
name = "bazel_skylib",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
],
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
)

load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions rules/actions_run/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ concat(
"footer.html",
],
merge_tool = select({
"//conditions:default": "//actions_run:merge_on_linux",
"on_linux": "//actions_run:merge_on_linux",
"on_windows": "//actions_run:merge_on_windows.bat",
"//conditions:default": ":merge_on_linux",
"on_linux": ":merge_on_linux",
"on_windows": ":merge_on_windows.bat",
}),
)

Expand Down
4 changes: 3 additions & 1 deletion rules/aspect/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ cc_library(
name = "lib",
srcs = [
"lib.cc",
],
hdrs = [
"lib.h",
],
)
Expand All @@ -14,7 +16,7 @@ cc_binary(
srcs = [
"app.cc",
],
deps = ["lib"],
deps = [":lib"],
)

file_collector(
Expand Down
2 changes: 1 addition & 1 deletion rules/aspect/app.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "aspect/lib.h"
#include "lib.h"
#include <iostream>

int main() {
Expand Down
2 changes: 2 additions & 0 deletions rules/aspect/lib.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "lib.h"

int number() {
return 42;
}
2 changes: 1 addition & 1 deletion rules/attributes/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//attributes:printer.bzl", "printer")
load(":printer.bzl", "printer")

# Run 'bazel build :some_target' to print its information.
printer(
Expand Down
2 changes: 1 addition & 1 deletion rules/computed_dependencies/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//computed_dependencies:hash.bzl", "md5_sum")
load(":hash.bzl", "md5_sum")

# Run 'bazel build :hash_no_space' to compute the md5 sum of 'hello.txt'
# ignoring all spaces.
Expand Down
2 changes: 1 addition & 1 deletion rules/depsets/foo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ _foo_binary = rule(
"srcs": attr.label_list(allow_files = True),
"deps": attr.label_list(),
"_foocc": attr.label(
default = Label("//depsets:foocc"),
default = ":foocc",
allow_files = True,
executable = True,
cfg = "exec",
Expand Down
2 changes: 1 addition & 1 deletion rules/empty/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//empty:empty.bzl", "empty")
load(":empty.bzl", "empty")

# Run 'bazel build :nothing'. The target will be successfully analyzed,
# but it does nothing.
Expand Down
2 changes: 1 addition & 1 deletion rules/executable/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//executable:fortune.bzl", "haiku_fortune")
load(":fortune.bzl", "haiku_fortune")

haiku_fortune(
name = "bazel_haikus",
Expand Down
2 changes: 1 addition & 1 deletion rules/expand_template/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("//expand_template:hello.bzl", "hello")
load(":hello.bzl", "hello")

# Generates a C++ source file that says "Hello John!".
hello(
Expand Down
5 changes: 1 addition & 4 deletions rules/expand_template/hello.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ It is much more memory-efficient to use a template file than creating the whole
content during the analysis phase.
"""

# Label of the template file to use.
_TEMPLATE = "//expand_template:hello.cc"

def hello(**kwargs):
_hello(
source_file = "{name}.cc".format(**kwargs),
Expand All @@ -27,7 +24,7 @@ _hello = rule(
attrs = {
"firstname": attr.string(mandatory = True),
"_template": attr.label(
default = Label(_TEMPLATE),
default = ":hello.cc",
allow_single_file = True,
),
"source_file": attr.output(mandatory = True),
Expand Down
2 changes: 1 addition & 1 deletion rules/predeclared_outputs/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//predeclared_outputs:hash.bzl", "word_hashes")
load(":hash.bzl", "word_hashes")

# Run any of the following:
# bazel build //predeclared_outputs:animal_hashes
Expand Down
6 changes: 3 additions & 3 deletions rules/runfiles/complex_tool.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ sub_tool = rule(
"command": attr.string(),
"_data": attr.label(
allow_files = True,
default = "//runfiles:data.txt",
default = ":data.txt",
),
},
)
Expand Down Expand Up @@ -86,11 +86,11 @@ complex_tool = rule(
"command": attr.string(),
"_subtool": attr.label(
allow_files = True,
default = "//runfiles:subtool",
default = ":subtool",
),
"_data": attr.label(
allow_files = True,
default = "//runfiles:complex_tool_data.txt",
default = ":complex_tool_data.txt",
),
},
)
2 changes: 1 addition & 1 deletion rules/runfiles/tool.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ tool = rule(
attrs = {
"_data": attr.label(
allow_files = True,
default = "//runfiles:data.txt",
default = ":data.txt",
),
},
)
Expand Down

2 comments on commit e5e9d05

@Artem-B
Copy link

@Artem-B Artem-B commented on e5e9d05 Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI. moving starlark_configurations out of rules and into 'configurations' broke links to examples in the public docs. E.g. https://docs.bazel.build/versions/main/skylark/config.html#defining-build-settings still links to now missing https://github.com/bazelbuild/examples/tree/HEAD/rules/starlark_configurations/basic_build_setting

@Artem-B
Copy link

@Artem-B Artem-B commented on e5e9d05 Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind. the doc I was looking at was itself out of date. The updated docs on https://bazel.build/rules/config are fine.

Please sign in to comment.