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

Rust: Replace rust_crate_type with rust_abi #11714

Merged
merged 9 commits into from
Sep 19, 2023

Conversation

xclaesse
Copy link
Member

@xclaesse xclaesse commented Apr 21, 2023

  • Rust: Fix both_libraries() case
Rustc does not produce object files we can reuse to build both
libraries. Ideally this should be done with a single target that has
both `--crate-type` arguments instead of having 2 different build rules.

As temporary workaround, build twice and ensure they don't get conflicts
in intermediary files created by rustc by passing target's private
directory as --out-dir.

See https://github.com/rust-lang/rust/issues/111083.
  • Rust: Use Popen_safe_logged in sanity checks

  • Rust: Fix proc-macro usage when cross compiling

Force BUILD machine and allow to link with HOST machine.
  • Rust: Prevent linking Rust ABI with C library/executable

  • Rust: Remove unit test already covered in "rust/4 polyglot"

  • Rust: Replace rust_crate_type with rust_abi

Meson already knows if it's a shared or static library, user only need
to specify the ABI (Rust or C).
  • Rust: Add a rust.proc_macro() method

  • interpreter: Use common definition for sources type

  • interpreter: Allow regex matching in expect_error()

@xclaesse xclaesse requested a review from jpakkane as a code owner April 21, 2023 20:40
@xclaesse
Copy link
Member Author

@xclaesse
Copy link
Member Author

xclaesse commented Apr 21, 2023

I'm wondering if we should even deprecate cdylib, dylib, staticlib and rlib values. Meson only needs to know which ABI (Rust or C) and already knows dynamic vs static. Only lib, clib and proc-macro still make sense IMHO.

@sdroege
Copy link
Contributor

sdroege commented Apr 21, 2023

That sounds like an option, just means that things don't map 1:1 to rustc (or cargo) terminology anymore and more documentation would be needed.

@xclaesse
Copy link
Member Author

While at it, I fixed usage of both_libraries and default_library=both, and added unittest.

That sounds like an option, just means that things don't map 1:1 to rustc (or cargo) terminology anymore and more documentation would be needed.

Yes, but the alternative is everyone is going to write library(..., rust_crate_type: 'cdylib') because it works by default, just like you did in your experiment, and that will have to be fixed when someone wants e.g. --default-library=static. But deprecating those values can be done later, after @dcbaker lands its type checking that will make that easier afaik.

@sdroege
Copy link
Contributor

sdroege commented Apr 23, 2023

I guess that makes sense, just make sure the deprecation warning makes it clear what should be done instead :)

docs/yaml/functions/_build_target_base.yaml Outdated Show resolved Hide resolved
docs/yaml/functions/_build_target_base.yaml Outdated Show resolved Hide resolved
docs/yaml/functions/_build_target_base.yaml Outdated Show resolved Hide resolved
mesonbuild/backend/ninjabackend.py Outdated Show resolved Hide resolved
mesonbuild/build.py Outdated Show resolved Hide resolved
mesonbuild/build.py Outdated Show resolved Hide resolved
test cases/rust/4 polyglot/meson.build Outdated Show resolved Hide resolved
@xclaesse xclaesse force-pushed the rust-crate-type branch 6 times, most recently from a9a4446 to bd9f871 Compare April 24, 2023 17:02
@xclaesse
Copy link
Member Author

@sdroege
Copy link
Contributor

sdroege commented Apr 24, 2023

@sdroege any idea what could be causing that CI failure? https://github.com/mesonbuild/meson/actions/runs/4789026887/jobs/8516401795?pr=11714#step:4:2263

Exciting. I have no idea :) Can you reproduce it locally?

@xclaesse
Copy link
Member Author

xclaesse commented Apr 24, 2023

Exciting. I have no idea :) Can you reproduce it locally?

no, works fine here, maybe more recent gcc and/or rustc. Maybe rust-lang/cargo#8239.

There is also that failure: https://github.com/mesonbuild/meson/actions/runs/4789026887/jobs/8516402494?pr=11714#step:4:2166. I guess we need -pthread, but it does not seems to be needed locally, so maybe also caused by a different rustc version.

@sdroege
Copy link
Contributor

sdroege commented Apr 24, 2023

You could try with a newer version, I guess. There's some code in ninjabackend.py that is only enabled with 1.66 and before that it's luck if things work correctly in certain configurations or not (and you're running here with 1.65)

@xclaesse
Copy link
Member Author

@sdroege
Copy link
Contributor

sdroege commented Apr 24, 2023

On Windows that's missing to link to ws2_32.dll, not sure where the linker should get that from here and why it usually works. You found an interesting testcase :)

@dcbaker
Copy link
Member

dcbaker commented Apr 24, 2023

I had thought about this too... Instead of rust_crate_type though, maybe something more generic like abi_format? Rust really isn't the only language that we could have this problem with. If we ever got around to adding Zig we would have this problem. D actually has a "BetterC" mode where you write D code, but the compiler disables a bunch of features to ensure C ABI compatibility with no external D runtime usage, which might be a another use case for this.

Then we could move proc_macro into the Rust module instead of as a normal target, since it has a bunch of special restrictions that doesn't exist for other crate types.

Just throwing out ideas that I've pondered about this.

@xclaesse
Copy link
Member Author

@dcbaker while I like the idea of having something more generic, I don't think we are there yet. We can rethink then when/if we actually do zig and/or D.

@xclaesse xclaesse force-pushed the rust-crate-type branch 3 times, most recently from 975b2f2 to 3be2e0e Compare April 25, 2023 15:55
@xclaesse
Copy link
Member Author

@dcbaker I added comments on c0928a3, not sure you get notified if it's not a PR?

@sdroege
Copy link
Contributor

sdroege commented Aug 22, 2023

@dcbaker Should rust_abi be string or boolean? There is only 2 possible values: Rust or C, right? Maybe rust_abi should default to True and rust_abi: false means C ?

There will very likely be more in the future

@xclaesse xclaesse changed the title Rust: Add "clib" crate type Rust: Replace rust_crate_type with rust_abi Aug 23, 2023
@xclaesse
Copy link
Member Author

@dcbaker @sdroege I updated this PR to add rust_abi, can you please give it a review and publish a PR to add rust.proc_macro()?

@xclaesse xclaesse force-pushed the rust-crate-type branch 4 times, most recently from cdb7f71 to f4c7c26 Compare August 24, 2023 17:54
@xclaesse
Copy link
Member Author

@dcbaker @sdroege I included the new rust.proc_macro() method in this PR. It is getting big and I really would like to get things moving forward, could you please do a review asap?

xclaesse and others added 9 commits September 18, 2023 14:11
Meson already knows if it's a shared or static library, user only need
to specify the ABI (Rust or C).
Force BUILD machine and allow to link with HOST machine.
Rustc does not produce object files we can reuse to build both
libraries. Ideally this should be done with a single target that has
both `--crate-type` arguments instead of having 2 different build rules.

As temporary workaround, build twice and ensure they don't get conflicts
in intermediary files created by rustc by passing target's private
directory as --out-dir.

See rust-lang/rust#111083.
Copy link
Member

@dcbaker dcbaker left a comment

Choose a reason for hiding this comment

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

Thanks for doing this, and thank you for bearing with me on the keyword argument name. This looks great

@xclaesse xclaesse merged commit dbf081c into mesonbuild:master Sep 19, 2023
@xclaesse xclaesse deleted the rust-crate-type branch September 19, 2023 17:54
@eli-schwartz
Copy link
Member

The https://github.com/nvinson/refivar project no longer builds in meson from git master:

meson.build:113:5: ERROR: Invalid rust_crate_type: must be "bin" for executables.

The failing code:

lib_refivar = static_library(
  'efivar',

[...]
  rust_crate_type: 'lib',
[...]
)

rust.test('unit tests', lib_refivar, rust_args:[])

Bisecting points to here:

10dcd87d002f6f36b1f60371dc807b8d9959d97b is the first bad commit
commit 10dcd87d002f6f36b1f60371dc807b8d9959d97b
Author: Xavier Claessens <xavier.claessens@collabora.com>
Date:   Fri Apr 21 16:21:48 2023 -0400

    Rust: Replace rust_crate_type with rust_abi
    
    Meson already knows if it's a shared or static library, user only need
    to specify the ABI (Rust or C).

 docs/markdown/snippets/rust_crate_type.md   | 11 ++++
 docs/yaml/functions/_build_target_base.yaml |  5 ++
 docs/yaml/functions/library.yaml            | 11 ++++
 docs/yaml/functions/shared_library.yaml     |  8 +++
 docs/yaml/functions/shared_module.yaml      |  8 +++
 docs/yaml/functions/static_library.yaml     |  8 +++
 mesonbuild/backend/ninjabackend.py          | 11 +---
 mesonbuild/build.py                         | 78 +++++++++++++-----------
 mesonbuild/interpreter/type_checking.py     | 25 ++++++++
 test cases/rust/4 polyglot/meson.build      | 51 +++++++++++++++-
 test cases/rust/4 polyglot/proc.rs          |  7 +++
 test cases/rust/4 polyglot/stuff.rs         |  2 -
 test cases/rust/4 polyglot/test.json        | 94 +++++++++++++++++++++++++++--
 13 files changed, 263 insertions(+), 56 deletions(-)
 create mode 100644 docs/markdown/snippets/rust_crate_type.md
 create mode 100644 test cases/rust/4 polyglot/proc.rs

/cc @nvinson

@dcbaker
Copy link
Member

dcbaker commented Oct 16, 2023

@eli-schwartz, @xclaesse: #12383

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

Successfully merging this pull request may close these issues.

4 participants