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

lto cannot be used for proc-macro crate type without -Zdylib-lto #3143

Closed
marvin-hansen opened this issue Dec 30, 2024 · 7 comments · Fixed by #3147
Closed

lto cannot be used for proc-macro crate type without -Zdylib-lto #3143

marvin-hansen opened this issue Dec 30, 2024 · 7 comments · Fixed by #3147

Comments

@marvin-hansen
Copy link
Contributor

The last release, 0.56, introduced a new setting for LTO (PR #3104).

However, this conflicts with the official Bzlmod LTO example and throws an error
that embed-bitcode does contain symbols for LTO. That is consistently the case on my CI.

Okay, when I enable the new LTO setting in my .bazelrc via

build --@rules_rust//rust/settings:lto=thin

bazel build throws then an error, both locally on MacOS and on CI / Linux:

error: lto cannot be used for proc-macrocrate type without-Zdylib-lto``

Indeed, I have a bunch of proc macros in my repo.

Currently, the previous LTO config throws an error and the new LTO setting also throws an error.

How do you advice to setup LTO?
Where do I set the Zdylib-lto flag?

havasd added a commit to havasd/rules_rust that referenced this issue Dec 31, 2024
Proc-macro crates can not be linked with lto and we should not emit bitcode either.
This fixes bazelbuild#3143
@havasd
Copy link
Contributor

havasd commented Dec 31, 2024

I have fix for it in #3147

@marvin-hansen
Copy link
Contributor Author

Thank you, appreciate your effort to get this solved.

havasd added a commit to havasd/rules_rust that referenced this issue Dec 31, 2024
Proc-macro crates can not be linked with lto and we should not emit bitcode either.
This fixes bazelbuild#3143
havasd added a commit to havasd/rules_rust that referenced this issue Dec 31, 2024
Proc-macro crates can not be linked with lto and we should not emit bitcode either.
This fixes bazelbuild#3143
github-merge-queue bot pushed a commit that referenced this issue Jan 2, 2025
…#3147)

I would like to make lto setting public in toolchain so that when we are
providing custom toolchains it would be easier to make configurations
for LTO with `select` statements.

The logic which determines the flags based on cargo outputs.

Proc-macro crates can not be linked with lto and we should not emit
bitcode either. This fixes #3143

---------

Co-authored-by: Daniel Wagner-Hall <dwagnerhall@apple.com>
@marvin-hansen
Copy link
Contributor Author

@havasd Okay, now as the fix is merged, how am I supposed to use this one?

@havasd
Copy link
Contributor

havasd commented Jan 3, 2025

you can use, git_override to pick up the latest version if you use MODULE.bazel. Ideally it should work out of the box. If you use workspace you need to pick download the main branch in http_archive with https://github.com/bazelbuild/rules_rust/archive/refs/heads/main.zip.

bazel_dep(name = "rules_rust", version = "0.56.0")
git_override(
  module_name = "rules_rust",
  commit = "568bb7b70f32d52e6626c313fc1d3dc18a0757af",
  remote = "https://github.com/bazelbuild/rules_rust.git",
)

@marvin-hansen
Copy link
Contributor Author

marvin-hansen commented Jan 3, 2025 via email

@havasd
Copy link
Contributor

havasd commented Jan 3, 2025

I see, I misunderstood your question.

With the original implementation you can do this --@rules_rust//rust/settings/lto=thin
this can be put to .bazelrc or passed to CLI. Also if you specify --@rules_rust//rust/settings/lto=manual your original settings should work without any change.

With my latest changes ideally you could make it work by creating your own toolchain and there specifying the lto flag.

load("//rust/private:lto.bzl", "rust_lto_flag")

rust_lto_flag(
    name = "lto_thin",
    build_setting_default = "thin",
    visibility = ["//visibility:public"],
)

rust_lto_flag(
    name = "lto_off",
    build_setting_default = "unspecified",
    visibility = ["//visibility:public"],
)

load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")

rust_toolchain(
  ....
  lto = select({
          "//:release": ":lto_thin",
          "//conditions:default": ":lto_off",
  }),
  ...
)

Now how you prepare a custom toolchain that is a different story.

@marvin-hansen
Copy link
Contributor Author

Hey, would it possible to update the official comp_opt example with the new toolchain approach?

https://github.com/bazelbuild/rules_rust/tree/main/examples/compile_opt

I am asking b/c right now, the official LTO/compiler optimization example is pinned to v0.46 of rules_rust and I am pretty certain once the new release comes out with LTO config changed again, people will ask how to set this up just to figure out that the official code example is basically pinned to an old version of the rules.

https://github.com/bazelbuild/rules_rust/blob/main/examples/compile_opt/MODULE.bazel

github-merge-queue bot pushed a commit that referenced this issue Jan 3, 2025
Fixes comment mentioned in
[3143#issuecomment-2568947858](#3143 (comment))

---------

Co-authored-by: UebelAndre <github@uebelandre.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants