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

Breaking change to target names containing dashes with Rust 1.79 #501

Closed
jschwe opened this issue Apr 3, 2024 · 3 comments
Closed

Breaking change to target names containing dashes with Rust 1.79 #501

jschwe opened this issue Apr 3, 2024 · 3 comments

Comments

@jschwe
Copy link
Collaborator

jschwe commented Apr 3, 2024

cargo-metadata output for target names changes with Rust 1.79 due to PR rust-lang/cargo#12783 fixing issue rust-lang/cargo#12780. (TLDR: dashes in target names are replaced with underscores starting with Rust 1.79)

Users would notice this by receiving errors that CMake can't find Rust targets with dashes in the name anymore, e.g.:

CMake Error at /Users/jschwender/Dev/corrosion/cmake/Corrosion.cmake:1172 (set_property):
  set_property could not find TARGET rustflag-test-lib.  Perhaps it has not
  yet been created.
Call Stack (most recent call first):
  CMakeLists.txt:11 (corrosion_add_target_rustflags)

Example of an affected Cargo.toml file:

[package]
name = "flags-lib"
version = "0.1.0"
edition = "2018"

[lib]
# Note how there is no explicit `name` field here.
crate-type=["staticlib"]

With Rust 1.79, corrosion would now import a crate named flags_lib, i.e. the CMake target name changed.

Affected crates

  • Cargo packages which use dashes in their package name AND
  • do not specify an explicit name in the [lib] section
  • bin crates are NOT affected.

Solution / Required actions

  • Corrosion v0.5 and later replace all dashes in imported target names with underscores, to get consistent CMake target names independent of the used Rust version. All users should upgrade from v0.4.x to v0.5.
  • You will need to change the target name of rust libraries in your CMake code to use underscores.
  • If you are not able to update corrosion, users can assign an explicit name in the [lib] or [[bin]] sections of their Cargo.toml manifests, and use that name in their CMake code. (Note: Cargo already forbids dashes in lib and bin target names). You may refer to slint-ui/slint@eeb7bad for an example fix, without updating corrosion.
@jschwe jschwe pinned this issue Apr 3, 2024
@tronical
Copy link
Contributor

tronical commented Apr 3, 2024

We ran into the same issue but worked around it by explicitly setting lib.name (slint-ui/slint@eeb7bad ).

But I guess it also ok to do a _ -> - transformation unconditionally in corrosion.

@jschwe
Copy link
Collaborator Author

jschwe commented Apr 4, 2024

We ran into the same issue but worked around it by explicitly setting lib.name (slint-ui/slint@eeb7bad ).

Thanks - I updated the issue description, and included your commit as an example that other users could follow.
If you have any suggestions to improve the issue description, feel free to edit the description.

But I guess it also ok to do a _ -> - transformation unconditionally in corrosion.

There are also crates which use _ in their package name, so such a transformation would break those users instead. I would just unconditionally transform - to _, so that corrosion matches cargos post 1.79 behavior.

jschwe added a commit to jschwe/corrosion that referenced this issue May 11, 2024
Rust 1.79 and newer replace dashes with underscores in lib names.
This can only happen when the lib name is inherited from the
package name, since explicit lib names have never been allowed
to use dashes.
Since we import our CMake target names by using cargo metadata,
we make the same change as cargo 1.79 does, but also for
older Rust versions, so that Corrosion users can use the
same CMake target name independent of the Rust version.

Fixes corrosion-rs#501
(cherry-picked from f85b242)
@jschwe
Copy link
Collaborator Author

jschwe commented May 13, 2024

Corrosion v0.5 and newer unconditionally replace dashes with underscores in library names, to match the behavior of Rust 1.79 and newer.

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

No branches or pull requests

2 participants