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 tools attr with exec transition for cargo_build_script #885

Merged
merged 9 commits into from
Aug 25, 2021

Conversation

djmarcin
Copy link
Contributor

If tools are passed in data, they must be built in the exec configuration in order to run on the host machine when the target != host.

I think this should be a fairly safe change, unless someone is doing something with the tools passed to the build script that would require them to run on the target configuration, but short of copying these tools into the out_dir I don't know what that would be. And -- that would be better handled by depending directly on these tools in the data attr of the library or binary anyway.

@google-cla google-cla bot added the cla: yes label Aug 10, 2021
@djmarcin djmarcin force-pushed the cargo-build-script-tools-exec branch from b360e41 to c42f81d Compare August 10, 2021 23:50
@UebelAndre
Copy link
Collaborator

This is actually a very interesting find. Lets consider the case of openssl-sys. Which we currently have an example of that builds openssl and passes it to the build script:

cargo_build_script(
name = "openssl_sys_build_script",
srcs = glob(["**/*.rs"]),
build_script_env = {
"OPENSSL_DIR": "../openssl/openssl",
},
crate_features = [
],
crate_root = "build/main.rs",
data = glob(["**"]) + [
"@openssl",
],
edition = "2015",
links = "openssl",
rustc_flags = [
"--cap-lints=allow",
],
tags = [
"cargo-raze",
"manual",
],
version = "0.9.60",
visibility = ["//visibility:private"],
deps = [
"@rules_rust_examples_complex_sys__autocfg__1_0_1//:autocfg",
"@rules_rust_examples_complex_sys__cc__1_0_66//:cc",
"@rules_rust_examples_complex_sys__pkg_config__0_3_19//:pkg_config",
] + selects.with_or({
# cfg(target_env = "msvc")
(
"@rules_rust//rust/platform:i686-pc-windows-msvc",
"@rules_rust//rust/platform:x86_64-pc-windows-msvc",
): [
"@rules_rust_examples_complex_sys__vcpkg__0_2_11//:vcpkg",
],
"//conditions:default": [],
}),
)

Assuming rules_foreign_cc supports cross compilation, would this crate expect openssl to be built in the target or exec configuration?

@UebelAndre UebelAndre self-requested a review August 10, 2021 23:58
@djmarcin
Copy link
Contributor Author

I suppose the question is what is the openssl-sys build script doing with the openssl binary? For what it's worth, we do use openssl-sys in our build and it doesn't seem broken to me (yet...)

@djmarcin
Copy link
Contributor Author

This (or some other mechanism for passing tools in the exec configuration) would solve #884

@illicitonion
Copy link
Collaborator

How about if we added a compile_data attribute to cargo_build_script as per #516 ?

@djmarcin
Copy link
Contributor Author

djmarcin commented Aug 11, 2021 via email

@illicitonion
Copy link
Collaborator

Yeah, tools works for me!

@hlopko
Copy link
Member

hlopko commented Aug 12, 2021

tools sgtm too :)

@djmarcin djmarcin changed the title Add exec transition for cargo_build_script data Add tools attr with exec transition for cargo_build_script Aug 12, 2021
@djmarcin
Copy link
Contributor Author

Updated to add a tools attr instead. This is potentially more fixup work in other tools such as cargo raze if they are using data to pass tools that should be built for the exec platform, but certainly better from the perspective of ensuring backwards compatibility.

@illicitonion
Copy link
Collaborator

Fantastic - LGTM

One last piece - any chance of a test? I know that in our current execution environments on CI we won't see a difference, but it'd be handy for the future. I imagine adding a one-line rust_binary as a tools to a target and running it in the build script? @hlopko I'm not sure if the unit testing stuff is set up well to test this kind of thing?

Copy link
Collaborator

@UebelAndre UebelAndre left a comment

Choose a reason for hiding this comment

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

Looks good to me! I wonder if tools should be wired into compile_data for the rust_binary? Leaning towards not but would like some other opinions as well 😄

Either way, thank you!

@hlopko
Copy link
Member

hlopko commented Aug 12, 2021

The PR looks good to me, and as for the test, I guess it would be enough to have a genrule that touches a file (using for example this rule: https://github.com/bazelbuild/bazel-skylib/blob/main/rules/write_file.bzl), and then in the unit test we would check the exec path of the file passed in data and in tools. Like you do in a different PR, exec path of the file in tools should have "exec" in it.

@djmarcin djmarcin force-pushed the cargo-build-script-tools-exec branch from 4f2e05d to bd7ebf7 Compare August 20, 2021 22:44
@djmarcin
Copy link
Contributor Author

djmarcin commented Aug 20, 2021

Test added, merged main, and documentation re-updated.

@djmarcin djmarcin force-pushed the cargo-build-script-tools-exec branch from bd7ebf7 to d51da84 Compare August 20, 2021 23:02
@UebelAndre
Copy link
Collaborator

@hlopko do these tests look good to you?

@UebelAndre UebelAndre requested a review from hlopko August 23, 2021 20:38
Copy link
Collaborator

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

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

LGTM, thanks! Over to @hlopko

Copy link
Member

@hlopko hlopko left a comment

Choose a reason for hiding this comment

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

Looks good, thank you!

@hlopko
Copy link
Member

hlopko commented Aug 24, 2021

(please rebase and somebody will merge this right away, thank you!)

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

4 participants