Skip to content
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

Add a cargo_build_script_run rule #320

Merged
merged 18 commits into from
Jun 5, 2020

Conversation

damienmg
Copy link
Collaborator

@damienmg damienmg commented May 18, 2020

This rule allows to run a build.rs file (cf. https://doc.rust-lang.org/cargo/reference/build-scripts.html)
get its output (environment variable, rust flags and generated flags) and pass it to
rust_binary and rust_library in order to build crate with build script.

@damienmg
Copy link
Collaborator Author

cc @smklein

I finally got time to update my build script PR.

Would that works for you? We could also add an additional rule to provide the various file directly. If we do so I would be up to deprecate also the out_dir attribute for rules_rust, as they would conflict (and the logic there is bit hairy to be honest).

@damienmg damienmg force-pushed the cargo_build_script branch 2 times, most recently from 9322fbb to 0cac7ee Compare May 18, 2020 18:55
cargo/cargo_build_script.bzl Outdated Show resolved Hide resolved
examples/cargo/BUILD Outdated Show resolved Hide resolved
cargo/cargo_build_script.bzl Outdated Show resolved Hide resolved
@mobileink
Copy link

A possible alternative: println! is a macro; can we just redefine it to write to a file in OUT_DIR? (I don't know Rust very well so it seems like it should be possible, but I'm not sure how to do it.)

@damienmg
Copy link
Collaborator Author

damienmg commented Jun 2, 2020

A possible alternative: println! is a macro; can we just redefine it to write to a file in OUT_DIR? (I don't know Rust very well so it seems like it should be possible, but I'm not sure how to do it.)

There is a bit more than that to do. To fix the data access, we have to change also the root directory, and adjust all the path to be absolute. I would expect also to do some wrapping code to redefine the println output, so basically the same as what it is doing (in addition to the need to parse the output nevertheless).

@damienmg damienmg requested review from smklein and removed request for acmcarther and mfarrugi June 2, 2020 13:48
examples/cargo/build_script.rs Outdated Show resolved Hide resolved
examples/cargo/BUILD Show resolved Hide resolved
examples/cargo/BUILD Show resolved Hide resolved
cargo/cargo_build_script_runner/lib.rs Outdated Show resolved Hide resolved
cargo/cargo_build_script_runner/bin.rs Show resolved Hide resolved
cargo/cargo_build_script_runner/lib.rs Outdated Show resolved Hide resolved
cargo/cargo_build_script_runner/lib.rs Show resolved Hide resolved
rust/private/rustc.bzl Outdated Show resolved Hide resolved
This rule allows to run a build.rs file (cf. https://doc.rust-lang.org/cargo/reference/build-scripts.html)a
get its output (environment variable, rust flags and generated flags) and pass it to
`rust_binary` and `rust_library` in order to build crate with build script.
This is not logical to allow multiple of those and create a hairy logic.
The binary will now run from the workspace root of the workspace they
belong too which is also the root of the cargo package when importing
with cargo raze.
This create a simple API for executing a build_script by just exposing
`cargo_build_script` which behaves like a rust_binary except creating
a

This creates an intermediate target `<name>_script_` that compiles
the binary and the final target is for executing the binary. This
could be joined as one rule by calling the `_rust_binary_impl` in
addition to the `_cargo_build_script_run`.
fs::canonalize convert a relative path to an absolute one so there is
no need for a custom method.

Addressing review comment.
This might be a bit verbose for directive that are voluntarely ignored
but will be better for catching not yet supported directive.

Note that directive that are currently ignored on purposely are still
silently ignored.
Also removed the unused popd argument (code review).
@damienmg
Copy link
Collaborator Author

damienmg commented Jun 5, 2020

Ok this is now passing all the tests having not received any more comment I'll go ahead and merge it.

@damienmg damienmg merged commit ddd8dae into bazelbuild:master Jun 5, 2020
@illicitonion
Copy link
Collaborator

This is super exciting, thanks for putting it together! (I found this after writing something very similar and preparing to make a PR for it, but yours is much better than mine was going to be!)

Do you have any plans to modify cargo-raze to generate this instead of its existing rust_binary + genrule situation?

@damienmg
Copy link
Collaborator Author

This is the plan. The actual plan I had originally was to totally replace cargo raze but at the time there was no correct way to handle feature propagation so I kind of side-track this effort until @smklein sent out a change that was in that direction.

If you want to go ahead and send a PR to cargo-raze I would be more than willing to let you do it ;-)

@smklein
Copy link
Contributor

smklein commented Jun 10, 2020

This is the plan. The actual plan I had originally was to totally replace cargo raze but at the time there was no correct way to handle feature propagation so I kind of side-track this effort until @smklein sent out a change that was in that direction.

If you want to go ahead and send a PR to cargo-raze I would be more than willing to let you do it ;-)

I'm familiar with the cargo-raze codebase; happy to take this on!

illicitonion added a commit to illicitonion/cargo-raze that referenced this pull request Jun 10, 2020
@illicitonion
Copy link
Collaborator

This is the plan. The actual plan I had originally was to totally replace cargo raze but at the time there was no correct way to handle feature propagation so I kind of side-track this effort until @smklein sent out a change that was in that direction.
If you want to go ahead and send a PR to cargo-raze I would be more than willing to let you do it ;-)

I'm familiar with the cargo-raze codebase; happy to take this on!

I've actually just put this together - see google/cargo-raze#166 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants