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

Provide CARGO_PKG_NAME and CARGO_PKG_VERSION* for build scripts #366

Merged
merged 3 commits into from
Jul 7, 2020

Conversation

a1ph
Copy link
Contributor

@a1ph a1ph commented Jul 3, 2020

This is used e.g. by the protobuf crate build script.

@damienmg
Copy link
Collaborator

damienmg commented Jul 3, 2020

Hi,

Although I think it make senses to have CARGO_PKG_NAME since it does not extend the API surface, build_script_env should be used to provide CARGO_PKG_VERSION. There is plenty of env that does not matter for us, unless they make sense in the rust_binary I would prefer not to add a dedicated support for it.

@a1ph
Copy link
Contributor Author

a1ph commented Jul 3, 2020

@damienmg thanks for looking into it.

My goal is to make all the crates compilable without having a need to put anything into the raze.crates section. Finding the right set of settings is a pain for developers, see e.g. google/cargo-raze#41 thread.

Besides that it is hard to maintain the settings in this section, as it requires putting exact crate versions that in fact may change on every cargo-raze run and developer needs to manually keep the versions up to date there.

@illicitonion
Copy link
Collaborator

@a1ph I think what @damienmg is suggesting is that instead of teaching cargo-raze to generate:

cargo_build_script(
    ...
    version = "2.1.0",
    ...
)

and having cargo_build_script turn that into an env var, we should teach cargo-raze to generate:

cargo_build_script(
    ...
    build_script_env = {
        "CARGO_PKG_VERSION": "2.1.0",
        "CARGO_PKG_VERSION_MAJOR": "2",
        ...
    },
    ...
)

which will Just Work with no modification to the rules.

I guess there's a philosophical question as to whether cargo-raze should be the thing translating cargo concepts to bazel ones, or cargo_build_script should be. I'm inclined to agree that for this one, setting version = "2.1.0" in a BUILD file is much nicer, because of how it expands out to multiple redundant environment variables.

I have no real preference for whether the version -> 4 env vars happens in the bzl for the rule, or the runner binary, though.

@damienmg
Copy link
Collaborator

damienmg commented Jul 3, 2020

Yes @illicitonion explained well what I was thinking. I agree that version is nicer but however this is a duplicate feature, since it is always easier to add feature than to remove them, I would err on the side of not adding a feature.

So I would prefer if this PR would just add the CARGO_PKG_NAME here and inject the CARGO_PKG_VERSION from cargo-raze. There is one case where I see that adding a version parameter make sense if we were to fetch it, but that's not the responsibility of cargo_build_script.

@a1ph
Copy link
Contributor Author

a1ph commented Jul 4, 2020

I'm ok with moving the logic to cargo-raze. However please note it already generates version in cargo_build_script invocation, that I believe is required by rust_binary.

@damienmg
Copy link
Collaborator

damienmg commented Jul 4, 2020 via email

Copy link
Collaborator

@damienmg damienmg left a comment

Choose a reason for hiding this comment

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

Sorry for all the push back, I totally missed this was a feature of rust_binary. This does not change any API in fact so LGTM.

Comment on lines 44 to 49
if ctx.attr.version:
version = ctx.attr.version.split(".")
env["CARGO_PKG_VERSION"] = ctx.attr.version
env["CARGO_PKG_VERSION_MAJOR"] = version[0]
env["CARGO_PKG_VERSION_MINOR"] = version[1] if len(version) > 1 else ""
env["CARGO_PKG_VERSION_PATCH"] = version[2] if len(version) > 2 else ""
Copy link
Collaborator

Choose a reason for hiding this comment

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

What about when there is a label (e.g. 1.2.3-blah or 2.3.4+bleh)?

Suggested change
if ctx.attr.version:
version = ctx.attr.version.split(".")
env["CARGO_PKG_VERSION"] = ctx.attr.version
env["CARGO_PKG_VERSION_MAJOR"] = version[0]
env["CARGO_PKG_VERSION_MINOR"] = version[1] if len(version) > 1 else ""
env["CARGO_PKG_VERSION_PATCH"] = version[2] if len(version) > 2 else ""
if ctx.attr.version:
version = ctx.attr.version.split("+")[0].split("-")[0].split(".")
env["CARGO_PKG_VERSION"] = ctx.attr.version
env["CARGO_PKG_VERSION_MAJOR"] = version[0]
env["CARGO_PKG_VERSION_MINOR"] = version[1] if len(version) > 1 else ""
env["CARGO_PKG_VERSION_PATCH"] = version[2] if len(version) > 2 else ""

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done. slightly different though.

Copy link
Collaborator

@damienmg damienmg left a comment

Choose a reason for hiding this comment

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

Thanks!

@damienmg damienmg merged commit 3dffbab into bazelbuild:master Jul 7, 2020
@a1ph a1ph deleted the cargo-pkg branch July 7, 2020 08:29
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