-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rule depending on target #379
Comments
The compiler needs to come into the my_lang_binary rule as an execution dependency (attribute). The cycle isn't going to be an issue. # BUCK
load("//:rules.bzl", "my_lang_binary")
cxx_binary(
name = "compiler",
srcs = ["compiler.cpp"],
)
my_lang_binary(
name = "main",
srcs = ["main.my_lang"],
) # rules.bzl
def _my_lang_binary_impl(ctx: AnalysisContext) -> list[Provider]:
output = ctx.actions.declare_output("compiled")
ctx.actions.run(
[
ctx.attrs.compiler[RunInfo],
output.as_output(),
ctx.attrs.srcs,
],
category = "my_lang",
)
return [DefaultInfo(default_output = output)]
my_lang_binary = rule(
impl = _my_lang_binary_impl,
attrs = {
"compiler": attrs.default_only(attrs.exec_dep(providers = [RunInfo], default = "//:compiler")),
"srcs": attrs.list(attrs.source(), default = []),
},
) // compiler.cpp
#include <fstream>
int main(int argc, char *argv[]) {
if (argc < 2) {
return 1;
}
std::ofstream ofs{argv[1], std::ios::binary};
for (int i = 2; i < argc; ++i) {
std::ifstream ifs{argv[i], std::ios::binary};
ofs << ifs.rdbuf();
ifs.close();
if (ifs.fail()) {
return 1;
}
}
ofs.close();
if (ofs.fail()) {
return 1;
}
} To build the compiler and run it on main.my_lang: $ buck2 build :main --show-output |
Thank you so much for your time and detailed answer, I've just reproduced it and it works flawlessly. |
Hi,
I'm trying to write a rule that calls a binary that is an artifact of a buck2 target: the target is a compiler written in C++, the rule is meant to create targets built using that compiler.
To make things more concrete, I have the following (simplified) structure:
compiler.cpp
file, which can be built to get amy_lang
compilerrules.bzl
in which I define amy_lang_binary
rule for buildingmy_lang
filesBUCK
file in which I define acompiler
target usingcxx_binary
, as well as a target using mymy_lang_binary
rulemain.my_lang
which I would like to build usingmy_lang_binary
my_lang_binary
currently only creates an empty file usingtouch
. Instead I would like it to call the output binary of thecompiler
target defined in myBUCK
file, but I haven't found a way for a.bzl
file to depend on/load aBUCK
file. Is this doable? If so how? If not, is there a suggested alternative approach that doesn't require e.g. first installing my compiler in my PATH and relying on that?As a secondary issue, will buck2 be able to deal with the cyclic dependency (compiler defined in
BUCK
, custom rule defined inrules.bzl
and depending on the target fromBUCK
,BUCK
also calling this custom rule)? If not, I suppose that I'll have to create a subdirectory for the targets that use my custommy_lang_binary
rule, as there can only be a singleBUCK
file per directory, right?Thanks for your time, any pointers would be greatly appreciated!
The text was updated successfully, but these errors were encountered: