Simple cc_library rule as a Skylark rule - bazel

I have a very simple cc_library() rule that I would like to convert to a Skylark rule.
The cc_library() rule is:
cc_library(
name = 'cc_library_a',
srcs = [ 'a.c' ],
)
The challange I'm facing is concerining compilation flags that get passed through different methods - for one, the command line.
So assume I run the following command line:
# bazel build :a --copt -H
This will add the -H flag to the compilation command. The question here is how can I get the -H flag propagated to a Skylark rule?
I have tried the following but it did not work:
def _my_rule(ctx):
_arguments = [ '-c' ]
_arguments.append(ctx.file.src.path)
_arguments += ctx.fragments.cpp.c_options
_arguments += [ '-o', ctx.outputs.out.path ]
ctx.actions.run(
outputs = [ ctx.outputs.out ],
inputs = [ ctx.file.src ],
arguments = _arguments,
executable = ctx.fragments.cpp.compiler_executable,
)
my_rule = rule(
implementation = _my_rule,
attrs = {
'src': attr.label(allow_single_file = True),
},
outputs = {
'out': '%{name}.o',
},
fragments = [ 'cpp' ],
)
The BUILD file content is the following:
load(':rules.bzl', 'my_rule')
my_rule(
name = 'skylark_a',
src = 'a.c',
)
cc_library(
name = 'cc_library_a',
srcs = [ 'a.c' ],
)
The output of building the Skylark rule shows clearly that the --copt was ignored:
# bazel build skylark_a --copt -H --subcommands
INFO: Found 1 target...
>>>>> # //:skylark_a [action 'SkylarkAction skylark_a.o']
(cd /bazel/jbasila/_bazel_jbasila/4d692aace2e7e1a45eec9fac3922ea8d/execroot/__main__ && \
exec env - \
/usr/bin/gcc -c a.c -o bazel-out/local-fastbuild/bin/skylark_a.o)
INFO: From SkylarkAction skylark_a.o:
a.c: In function 'a':
a.c:3:5: warning: implicit declaration of function 'puts' [-Wimplicit-function-declaration]
puts("a");
^~~~
Target //:skylark_a up-to-date:
bazel-bin/skylark_a.o
INFO: Elapsed time: 0.578s, Critical Path: 0.05s
What am I missing?

Because the --copt -H parameter is generic to C and C++ you need to get the flags using ctx.fragments.cpp.compiler_options([]) instead. The ctx.fragments.cpp.c_options code you where using is just for C specific options.
Likewise, there is also a ctx.fragments.cpp.cxx_options([]) function that provides you C++ specific options.

Related

Erlang Hello World using Bazel as a build system

I want to build an Erlang hello world example using rules_erlang on Ubuntu 22.04.
My setup looks like this:
BUILD.bazel
load("#rules_erlang//:erlang_app.bzl", "erlang_app", "test_erlang_app")
load("#rules_erlang//:xref.bzl", "xref")
load("#rules_erlang//:dialyze.bzl", "dialyze", "plt")
load("#rules_erlang//:ct.bzl", "ct_suite", "assert_suites")
APP_NAME = "hello_world"
APP_VERSION = "0.1.0"
erlang_app(
app_name = APP_NAME,
app_version = APP_VERSION,
)
src/hello_world.erl
-module(hello_world).
-compile(export_all).
hello() ->
io:format("hello world~n").
WORKSPACE.bazel
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "bazel_skylib",
sha256 = "af87959afe497dc8dfd4c6cb66e1279cb98ccc84284619ebfec27d9c09a903de",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.2.0/bazel-skylib-1.2.0.tar.gz",
],
)
load("#bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()
http_archive(
name = "rules_erlang",
sha256 = "5e59800ecc786d5375951028c959c6e6275c94eff2a52f5d53ccb1ad8b2ea20a",
strip_prefix = "rules_erlang-3.8.4",
urls = ["https://github.com/rabbitmq/rules_erlang/archive/refs/tags/3.8.4.zip"],
)
load(
"#rules_erlang//:rules_erlang.bzl",
"erlang_config",
"rules_erlang_dependencies",
)
erlang_config()
rules_erlang_dependencies()
load("#erlang_config//:defaults.bzl", "register_defaults")
register_defaults()
The code can also found here.
When I execute bazel build //... I get
ERROR:
/home/vertexwahn/.cache/bazel/_bazel_vertexwahn/b5f945f94177a8ffa6ac0f7108dfc1cd/external/erlang_config/external/BUILD.bazel:12:16:
Validating otp at /usr failed: (Exit 1): bash failed: error executing
command /bin/bash -c ... (remaining 1 argument skipped)
Use --sandbox_debug to see verbose messages from the sandbox and
retain the sandbox build root for debugging Erlang version mismatch
(Expected UNKNOWN, found 24.2.1)
Any hints to get this working are welcome!
To run it, you can declare a shell rule in your BUILD.bazel, for instance:
load("#rules_erlang//:shell.bzl", "shell")
shell(
name = "repl",
deps = [":erlang_app"],
extra_erl_args = ["-eval", "'hello_world:hello()'"],
)
and then you could bazel run :repl.
Or, you could use an escript rule if you change hello_world.erl so that it exports main/1 instead of hello:
load("#rules_erlang//:escript.bzl", "escript_archive")
escript_archive(
name = "hello_world",
app = ":erlang_app",
)
hello_world.erl:
-module(hello_world).
-export([main/1]).
main(_) ->
io:format("hello world~n").
and run with bazel run :hello_world
bazel build //... --sandbox_debug
gave me
compile: warnings being treated as errors
hello_world.erl:2:2: export_all flag enabled - all functions will be exported
With -export([hello/0]). instead of -compile(export_all). it works
-module(hello_world).
-export([hello/0]).
hello() ->
io:format("hello world~n").
❯ bazel build //...
INFO: Analyzed 3 targets (0 packages loaded, 0 targets configured).
INFO: Found 3 targets...
INFO: Elapsed time: 0,326s, Critical Path: 0,25s
INFO: 2 processes: 1 internal, 1 darwin-sandbox.
INFO: Build completed successfully, 2 total actions

How to simulate generating a source-file in a Bazel action?

Suppose I am writing a custom Bazel rule for foo-compiler.
The user provides a list of source-files to the rule:
foo_library(
name = "hello",
srcs = [ "A.foo", "B.foo" ],
)
To build this without Bazel, the steps would be:
Create a config file config.json that lists the sources:
{
"srcs": [ "./A.foo", "./B.foo" ]
}
Place the config alongside the sources:
$ ls .
A.foo
B.foo
config.json
Call foo-compiler in that directory:
$ foo-compiler .
Now, in my Bazel rule implementation I can declare a file like this:
config_file = ctx.actions.declare_file("config.json")
ctx.actions.write(
output = config_file,
content = json_for_srcs(ctx.files.srcs),
)
The file is created and it has the right content.
However, Bazel does not place config.json alongside the srcs.
Is there a way to tell Bazel where to place the file?
Or perhaps I need to copy each source-file alongside the config?
You can do this with ctx.actions.symlink e.g.
srcs = []
# Declare a symlink for each src files in the same directory as the declared
# config file.Then write that symlink.
for f in ctx.files.srcs:
src = ctx.actions.declare_file(f.basename)
srcs.append(src)
ctx.actions.symlink(
output = src,
target_file = f,
)
config_file = ctx.actions.declare_file("config.json")
ctx.actions.write(
output = config_file,
content = json_for_srcs(ctx.files.srcs),
)
# Run compiler
ctx.actions.run(
inputs = srcs + [config_file],
outputs = # TODO: Up to you,
tools = [ctx.file.__compiler], #TODO: Update this to match your rule.
command = ctx.file.__compiler.path,
args = ["."],
#...
)
Note that when you return your provider that you should only return the result of your compilation not the srcs. Otherwise, you'll likely run into problems with duplicate outputs.

Reuse different parts of downloaded package (directory output)?

New to bazel so please bear with me :) I have a genrule which basically downloads and unpacks a a package:
genrule(
name = "extract_pkg",
srcs = ["#deb_pkg//file:pkg.deb"],
outs = ["pkg_dir"],
cmd = "dpkg-deb --extract $< $(#D)/pkg_dir",
)
Naturally pkg_dir here is a directory. There is another rule which uses this rule as input to create executable, but the main point is that I now need to add a rule (or something) which will allow me to use some headers from that package. This rule is used as an input to a cc_library which is then used in other parts of the repository to get access to the headers. Tried like this:
genrule(
name = "pkg_headers",
srcs = [":extract_pkg"],
outs = [
"pkg_dir/usr/include/pkg/h1.h",
"pkg_dir/usr/include/pkg/h2.h"
]
)
But it seems Bazel doesn't like the fact that both rules use the same directory as output, even though the second one doesn't do anything (?):
output file 'pkg_dir' of rule 'extract_pkg' conflicts with output file 'pkg_dir/usr/include/pkg/h1.h' of rule 'pkg_headers'
It works fine if I use different "root" directory for both rules, but I think there must be some better way to do this.
EDIT
I tried to use declare_directory as follows (compiled from different sources):
unpack_deb.bzl:
def _unpack_deb_impl(ctx):
input_deb_file = ctx.file.deb
output_dir = ctx.actions.declare_directory(ctx.attr.name + ".cc")
print(input_deb_file.path)
print(output_dir.path)
ctx.actions.run_shell(
inputs = [ input_deb_file ],
outputs = [ output_dir ],
arguments = [ input_deb_file.path, output_dir.path ],
progress_message = "Unpacking %s to %s" % (input_deb_file.path, output_dir.path),
command = "dpkg-deb --extract \"$1\" \"$2\"",
)
return [DefaultInfo(files = depset([output_dir]))]
unpack_deb = rule(
implementation = _unpack_deb_impl,
attrs = {
"deb": attr.label(
mandatory = True,
allow_single_file = True,
doc = "The .deb file to be unpacked",
),
},
doc = """
Unpacks a .deb file and returns a directory.
""",
)
BUILD.bazel:
load(":unpack_deb.bzl", "unpack_deb")
unpack_deb(
name = "pkg_dir",
deb = "#deb_pkg//file:pkg.deb"
)
cc_library(
name = "headers",
linkstatic = True,
srcs = [ "pkg_dir" ],
hdrs = ["pkg_dir.cc/usr/include/pkg/h1.h",
"pkg_dir.cc/usr/include/pkg/h2.h"],
strip_include_prefix = "pkg_dir.cc/usr/include",
)
The trick with adding .cc so the input can be accepted by cc_library was stolen from this answer. However the command fails on
ERROR: missing input file 'blah/blah/pkg_dir.cc/usr/include/pkg/h1.h'
From the library.
When I run with debug, I can see the command being "executed" (strange thing is that I don't always see this printout):
SUBCOMMAND: # //blah/pkg:pkg_dir [action 'Unpacking tmp/deb_pkg/file/pkg.deb to blah/pkg/pkg_dir.cc', configuration: xxxx]
(cd /home/user/.../execroot/src && \
exec env - \
/bin/bash -c 'dpkg-deb --extract "$1" "$2"' '' tmp/deb_pkg/file/pkg.deb bazel-out/.../pkg/pkg_dir.cc)
After execution, bazel-out/.../pkg/pkg_dir.cc exists but is empty. If I run the command manually it extracts files correctly. What might be the reason? Also, is it correct that there's an empty string directly after bash command line string?
Bazel's genrule doesn't work very well with directory outputs. See https://docs.bazel.build/versions/master/be/general.html#general-advice
Bazel mostly works with individual files, although there's some support for working with directories in Starlark rules with https://docs.bazel.build/versions/master/skylark/lib/actions.html#declare_directory
Your best bet is probably to extract all the files you're interested in in the genrule, then create filegroups for the different groups of files:
genrule(
name = "extract_pkg",
srcs = ["#deb_pkg//file:pkg.deb"],
outs = [
"pkg_dir/usr/include/pkg/h1.h",
"pkg_dir/usr/include/pkg/h2.h",
"pkg_dir/other_files/file1",
"pkg_dir/other_files/file2",
],
cmd = "dpkg-deb --extract $< $(#D)/pkg_dir",
)
filegroup(
name = "pkg_headers",
srcs = [
":pkg_dir/usr/include/pkg/h1.h",
":pkg_dir/usr/include/pkg/h2.h",
],
)
filegroup(
name = "pkg_other_files",
srcs = [
":pkg_dir/other_files/file1",
":pkg_dir/other_files/file2",
],
)
If you've seen glob, you might be tempted to use glob(["pkg_dir/usr/include/pkg/*.h"]) or similar for the srcs of the filegroup, but note that glob works only with "source files", which means files already on disk, not with the outputs of other rules.
There are rules for creating debs, but I'm not aware of rules for importing them. It's possible to write such rules using Starlark:
https://docs.bazel.build/versions/master/skylark/repository_rules.html
With repository rules, it's possible to avoid having to explicitly write out all the files you want to extract, among other things. Might be more work than you want to do though.

Runfiles location substitution

I'm trying to run qemu on the output of a cc_binary rule. For that I have created a custom rule, which is pretty similiar to this example, but instead of the cat command on the txt-file, I want to invoke qemu on the output elf-file (":test_portos.elf") of the cc_binary rule. My files are the following:
run_tests.bzl
def _impl(ctx):
# The path of ctx.file.target.path is:
'bazel-out/cortex-a9-fastbuild/bin/test/test_portos.elf'
target = ctx.file.target.path
command = "qemu-system-arm -M xilinx-zynq-a9 -cpu cortex-a9 -nographic
-monitor null -serial null -semihosting
-kernel %s" % (target)
ctx.actions.write(
output=ctx.outputs.executable,
content=command,
is_executable=True)
return [DefaultInfo(
runfiles=ctx.runfiles(files=[ctx.file.target])
)]
execute = rule(
implementation=_impl,
executable=True,
attrs={
"command": attr.string(),
"target" : attr.label(cfg="data", allow_files=True,
single_file=True, mandatory=True)
},
)
BUILD
load("//make:run_tests.bzl", "execute")
execute(
name = "portos",
target = ":test_portos.elf"
)
cc_binary(
name = "test_portos.elf",
srcs = glob(["*.cc"]),
deps = ["//src:portos",
"#unity//:unity"],
copts = ["-Isrc",
"-Iexternal/unity/src",
"-Iexternal/unity/extras/fixture/src"]
)
The problem is, that in the command (of the custom rule) the location of the ":test_portos.elf" is used and not the location of the runfile. I have also tried, like shown in the example, to use $(location :test_portos.elf) together with ctx.expand_location but the result was the same.
How can I get the location of the "test_portos.elf" runfile and insert it into the command of my custom rule?
Seems that the runfiles are safed according to the short_path of the File, so this was all I needed to change in my run_tests.bzl file:
target = ctx.file.target.short_path

In Bazel, how can I make a C++ library depend on a general rule?

I have a library that depends on graphics files that are generated by a shell script.
I would like the library, when it is compiled, to use the shell script to generate the graphics files, which should be copied as if it were a 'data' statement, but whenever I try to make the library depend on the genrule, I get
in deps attribute of cc_library rule //graphics_assets
genrule rule '//graphics_assets:assets_gen_rule' is misplaced here
(expected cc_inc_library, cc_library, objc_library or
cc_proto_library)
# This is the correct format.
# Here we want to run all the shader .glsl files through the program
# file_utils:archive_tool (which we also build elsewhere) and copy the
# output .arc file to the data.
# 1. List the source files
filegroup(
name = "shader_source",
srcs = glob([
"shaders/*.glsl",
]),
)
# 2. invoke file_utils::archive_tool on the shaders
genrule(
name = "shaders_gen_rule",
srcs = [":shader_source"],
outs = ["shaders.arc"],
cmd = "echo $(locations shader_source) > temp.txt ; \
$(location //common/file_utils:archive_tool) \
--create_from_list=temp.txt \
--archive $(OUTS) ; \
$(location //common/file_utils:archive_tool) \
--ls --archive $(OUTS) ",
tools = ["//common/file_utils:archive_tool"],
)
# 3. when a a binary depends on this tool the arc file will be copied.
# This is the thing I had trouble with
cc_library(
name = "shaders",
srcs = [], # Something
data = [":shaders_gen_rule"],
linkstatic = 1,
)

Resources