Commit Graph

98 Commits

Author SHA1 Message Date
Daniel Foster 24d7c347f5 compilers/rust: fix syntax of has_argument checks 2025-05-11 03:04:52 -04:00
Paolo Bonzini 8e564f16ae compilers: introduce common helper for sanity checks
Avoid reinventing the wheel and instead use a single helper, taking care
of logging and cross compilation.

Fixes: #14373
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-04-30 00:14:44 +03:00
Kacper Michajłow ce7e187630 compilers/rust: remove CRT selection from native_static_libs args
This will be handled by target binary link. And if it's not compatible
with what Rust uses, it wouldn't work anyway.
2025-04-16 13:02:28 -04:00
Dylan Baker 390ea4624c coredata: move MutableKeyedOptionDict to options 2025-04-08 10:00:16 -07:00
Paolo Bonzini ff0c758b2a compilers: move -std options to get_option_std_args, special-casing CUDA
Move building the -std option to the new get_option_std_args method,
special casing CUDA to never include the option from the host compiler.
This fixes again #8523, which was broken by the option refactoring
(unsurprisingly, since the fix was ripped out unceremoniously without
a replacement).

Fixes: #14365
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-04-04 12:46:55 +03:00
Kacper Michajłow 5d9b9739dd compilers/rust: fix sanity_check for Windows targets
Windows toolchains append `.exe` to executables. When cross-compiling on
Linux, attempting to run `./rusttest` will not execute the generated
`rusttest.exe`.

Fix this by always appending `.exe`, which is a valid filename on all
supported platforms. This is what the `CLikeCompiler` class does too.

While reviewing `rust.py`, there are opportunities for improvements and
better unification with the rest of the Meson code. However, this commit
focuses on fixing cross-compilation with minimal changes.

Fixes: #14374
2025-04-03 11:49:37 +03:00
Paolo Bonzini d9af002fa2 compilers: rust: fix derivation of RustdocTestCompiler
Pass down the full_version, otherwise assigning "self.is_beta"
fails.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-04-02 08:44:37 -07:00
Paolo Bonzini 54118b6fbf ninjabackend: generate command line for rust doctests
Adjust get_rust_compiler_args() to accept the crate-type externally, because
rustdoc tests are an executable but are compiled with the parent target's
--crate-type.

Apart from that, the rustdoc arguments are very similar to the parent target, and
are handled by the same functions that were split out of generate_rust_target.

This concludes the backend implementation of doctests, only leaving the
implementation of a doctest() function in the rust module.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-04-02 08:44:37 -07:00
Paolo Bonzini 2c10d77c3c compilers: introduce get_exe() and get_exe_args()
This will be used by rustdoc tests because the Test objects takes a
single string for the command and everything else goes in the args.
But apart from this, the need to split the executable from the
arguments is common so create new methods to do it.

While at it, fix brokenness in the handling of the zig compiler, which
is checking against "zig" but failing to detect e.g. "/usr/bin/zig".

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-04-02 08:44:37 -07:00
Paolo Bonzini dfefd838a8 ninjabackend: split out generation of rustc arguments
Allow reusing the code for doctests.  In particular, the sources are
shared between the two cases.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-04-02 08:44:37 -07:00
Dylan Baker f39637e164 compilers/rust: implement has_argument checks
We're about to convert the `b_sanitize` option into a free-form array
whose value gets verified via a compiler check. This conversion will
also impact the Rust toolchain, which does not yet know to check for
multiple arguments at once.

Implement both `has_multi_arguments()` and `has_multi_link_arguments()`
to prepare the code accordingly.
2025-03-09 18:06:14 -04:00
Dylan Baker 8ebc8c1878 modules/rust: use 'nightly' as the bindgen version if the compiler is nightly
This adds tracking for both nightly and beta to the rust compiler.
2025-02-27 15:27:42 -05:00
Jussi Pakkanen d37d649b08 Make all Meson level options overridable per subproject. 2025-02-13 23:57:48 +02:00
Dylan Baker ba3460eb11 options: Add an EnumeratedUserOption class
This will allow us to take choices out of the UserOption class, which
doesn't actually use this attribute.
2025-02-05 17:45:38 +02:00
Dylan Baker 0e11b90d6f options: use dataclasses for UserOption
This reduces code, makes this clearer, and will be a nice step toward
the goal of getting everything typesafe.

For `UserIntegerOption` this makes a fairly nice, but substantial change
in that the old method used a tuple of `(min, value, max)` to pass to the
initializer, while all other types just passed `value`. The new
`UserIntegerOption` does the same, with keyword arguments for the min
and max values.
2025-02-05 17:45:38 +02:00
Dylan Baker fe9f8de1ab compilers: remove Compiler.create_option
This saves a *tiny* bit of typing, but at the cost of requiring either
the current solution of throwing up our hands and saying "typing is too
hard, better to have bugs!" or an extensive amount of `TypedDict`s,
`overloads`, and a very new version of mypy. Let's get our type safety
back, even if it means writing a little bit more code.
2025-02-05 17:45:38 +02:00
Paolo Bonzini d34c37f8a0
rust: use f-string to build rpath arguments
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-02-03 20:02:18 -05:00
Paolo Bonzini a19df7da15
ninjabackend: start adjusting for differences between rustc and rustdoc
Add functions to RustCompiler() to account for differences
between rustc and "rustdoc --test": rustdoc always generates
a binary, does not support -g, and does not need --emit.

Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-02-03 20:01:37 -05:00
Paolo Bonzini 103501c274
ninjabackend: unify building rpath args
Implement RustCompiler.build_rpath_args, so that more code can
be shared between non-Rust and Rust targets.  Then, RustCompiler
can override it to convert the arguments to "-C link-arg=" and
add the rustup sysroot.

Reviewed-by: Dylan Baker <dylan@pnwbakers.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-02-03 20:01:37 -05:00
Paolo Bonzini b0827fc1e2 rust: add 2024 edition
Rust edition 2024 is being released in February, add support for rust_std=2024.

Resolves: #14074
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-01-08 16:33:49 +02:00
Paolo Bonzini f2b22f87a1 rust: change warning_level=0 to "--cap-lints allow"
This is a better and more backwards-compatible way to disable all warnings,
compared to "-A warnings".  The Rust RFC (https://rust-lang.github.io/rfcs/1193-cap-lints.html)
explains the rationale:

> We would very much like to be able to modify lints, however. For example
> rust-lang/rust#26473 updated the missing_docs lint to also look for missing
> documentation on const items. This ended up breaking some crates in the
> ecosystem due to their usage of #![deny(missing_docs)].

While at it, document that Rust deviates from the other languages in its
interpretation of warning_level=0.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2025-01-08 12:07:31 +02:00
Paolo Bonzini 27c567de5d scripts: add "clippy" internal tool
Similar to the "ninja scan-build" target for C, add a clippy internal
tool that runs clippy-driver on all crates in the project.

The approach used is more efficient than with "ninja scan-build", and
does not require rerunning Meson in a separate build directory; it
uses the introspection data to find the compiler arguments for the
target and invokes clippy-driver with a slightly modified command
line.

This could actually be applied to scan-build as well, reusing the
run_tool_on_targets() function.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2024-12-19 09:25:20 -08:00
Paolo Bonzini 9f3f88feed rust: fix computation of library directory
Using a rustup-based toolchain fails the "rust/2 sharedlib" test for me:

./prog: error while loading shared libraries: libstd-211931512faabf29.so: cannot open shared object file: No such file or directory

This happens because recent rustup places the standard library under
SYSROOT/lib/rustlib/TARGET/lib.  Retrieve the right directory using
"--print target-libdir".  This also provides a more accurate version
for rustc installed in /usr.

Before:
  $ echo $(/usr/bin/rustc --print sysroot)/lib
  /usr/lib

After:
  $ /usr/bin/rustc --print target-libdir
  /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib

While at it, cache the value to avoid repeated process invocation.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2024-11-20 10:19:33 -08:00
Florent Valette 1aac6cc1ec compiler,rust: fix sanity check and internal deps for baremetal rust project/target
Sanity check for bare metal rust wasn't working for a while and there is a pending PR (#12175).
To workaround this problem, we used to let sanity check for build machine and manually defined
rustc target.

Commit 18f8aeda8b breaks this workaround as, even without an
exe_wrapper, native_static_libs are appends as internal deps.

This behaviour makes sense for cross compiled rust in a rich environment but not any for
no-std rust. As said in comments, one can't tell if the code is no-std or not because this is
an annotation from sources. From our point of view, it is pretty clear that building a no-std
rust target means that one has to define system='bare metal' and kernel='none' in his cross-file.

According to that, sanity_check for rust compiler is modified to handle kernel == 'none' case
by building a specific no-std rust snippet, with an extra args if rust_ld is ls.bfd (in order
to prevent the linker to link with a potentially non existing startfile for the given target).
'native_static_libs' is also leave empty in that very case.

This commit fix the spurious native static libs for no-std case and allow us to remove
our dirty workaround which by-passed non working sanity check for bare metal rust.

One who wants to use meson for baremetal Rust project only have to define the rust target
in their cross file.
 e.g.
   rust = ['rustc', '--target', '<rustc valid target>']
2024-09-24 14:56:01 -07:00
Christoph Reiter d9e2dd6c80 Revert various cross compile changes
c1076241af changed the logic in multiple
places, in particular it looks like it was assumed that is_cross is always
the same as need_exe_wrapper(), but that's not true.

Also the commit only talks about mypy, so this was definitely not intended.

This reverts all the cases where need_exe_wrapper() was introduced back to
is_cross.

The change in backends.py could be a correct simplification, but I don't know
the code base enough, so reverting that too.

See #13403 and #13410
2024-07-16 02:01:21 -04:00
Jussi Pakkanen f15963194f Rename langopt method
The public facing name of language options is compiler option, so
let's standardise on that.
2024-07-12 15:53:15 +03:00
Jussi Pakkanen 0d7bb776e2 Move OptionKey in the option source file. 2024-07-11 11:53:39 +03:00
Jussi Pakkanen 9a6fcd4d9a Replace direct indexing with named methods. 2024-06-14 17:19:53 +03:00
Jussi Pakkanen 4eb1eb3c4a Use helper method in Rust compiler class. 2024-06-08 21:12:56 +03:00
Jussi Pakkanen 5365d9a842 Refactor option classes to their own file. 2024-05-23 21:26:45 +03:00
Xavier Claessens 3a94aef38d rust: Fix warning_level=everything case
Fixes: #13100
2024-04-24 22:07:45 -07:00
Sam James 5bd28febf7
Pass Environment down from Backend
We'll need it in a moment for get_base_compile_args -> get_assert_args.

Bug: https://github.com/mesonbuild/meson/issues/12962
Signed-off-by: Sam James <sam@gentoo.org>
Signed-off-by: Eli Schwartz <eschwartz93@gmail.com>
2024-03-28 00:52:25 -04:00
Charles Brunet dacb25db10 Improve error messages for invalid option values
By adding the option name to UserOption object, it is now possible to
display the name of the affected option when the given option value is
not valid.

Fixes #12635
2024-03-15 09:23:46 -07:00
Xavier Claessens 18f8aeda8b rust: Get native-static-libs even when it cannot run host binaries
When rustc cannot run host binaries it does an early return which
skipped getting native-static-libs. Move that code earlier to always run
it.

While at it, failing to determine those libs is a fatal error. We would
crash later when trying to access rustc.native_static_libs attribute
otherwise.
2024-03-15 11:38:54 -04:00
Xavier Claessens c1076241af compilers: No need to pass exe_wrapper everywhere
Places where compiler needs it already have access to Environment object
and can use it directly.

This fixes mypy complaining that not all compilers have self.exe_wrapper
in run() method that got moved to base class.
2024-03-15 11:38:54 -04:00
Alyssa Ross 1ca2c74d16 rust: fix linking static executables
For the same reason as for static libraries, we have to use -l when
generating static executables.

Fixes: https://github.com/mesonbuild/meson/issues/12585
2024-01-17 13:36:15 -08:00
Charles Brunet 4761e4cad9 Remove `get_buildtype_args` function
This is a first step to make `buildtype` a true alias of `debug` and
`optimization` options.

See #10808.

Relates to:
- #11645
- #12096
- #5920
- #5814
- #8220
- #8493
- #9540
- #10487
- #12265
- #8308
- #8214
- #7194
- #11732
2023-12-23 13:32:49 +02:00
Dylan Baker e991c4d454 Use SPDX-License-Identifier consistently
This replaces all of the Apache blurbs at the start of each file with an
`# SPDX-License-Identifier: Apache-2.0` string. It also fixes existing
uses to be consistent in capitalization, and to be placed above any
copyright notices.

This removes nearly 3000 lines of boilerplate from the project (only
python files), which no developer cares to look at.

SPDX is in common use, particularly in the Linux kernel, and is the
recommended format for Meson's own `project(license: )` field
2023-12-13 15:19:21 -05:00
Eli Schwartz 39ecfc2d54
compilers: drop dead code
no_warn_args is unused. Its only purpose was to implement automatic
hiding of UB in transpiled code, and it was not used at all in languages
other than C/C++ -- specifically when the C/C++ source files were
created by transpiling from vala or cython.
2023-12-05 19:52:23 -05:00
Xavier Claessens e2a87afa52 rust: Always link dll and exe with the correct vscrt
This fixes missing flags in the link_whole case and link failure for
static libraries.
2023-10-19 08:13:57 -04:00
Xavier Claessens 7321f01678 Rust: Use Popen_safe_logged in sanity checks 2023-09-19 13:54:49 -04:00
Eli Schwartz 90ce084144
treewide: automatic rewriting of all comment-style type annotations
Performed using https://github.com/ilevkivskyi/com2ann

This has no actual effect on the codebase as type checkers (still)
support both and negligible effect on runtime performance since
__future__ annotations ameliorates that. Technically, the bytecode would
be bigger for non function-local annotations, of which we have many
either way.

So if it doesn't really matter, why do a large-scale refactor? Simple:
because people keep wanting to, but it's getting nickle-and-dimed. If
we're going to do this we might as well do it consistently in one shot,
using tooling that guarantees repeatability and correctness.

Repeat with:

```
com2ann mesonbuild/
```
2023-08-11 13:41:03 -04:00
Dylan Baker 76eba99850 rust: disable overflow-checks by default
These result in very large binaries when linked, and are not generally
useful. A user can turn them back on by passing `-C overflow-checks=yes`
manually via `-Drust_args` or the `RUSTFLAGS` environment variable

fixes: #11785
2023-07-15 12:22:30 -07:00
Eli Schwartz a1ef957e34
linkers: delay implementations import until detect is run
This saves on a 1500-line import at startup and may be skipped entirely
if no compiled languages are used. In exchange, we move the
implementation to a new file that is imported instead.

Followup to commit ab20eb5bbc.
2023-06-26 13:10:33 -04:00
Xavier Claessens 6c480085d5 rust: PIC is always enabled
rustc enables PIC by default and Meson currently has no way to force
disabling PIC, it can only force enable or use compiler's default.
2023-06-07 13:18:58 -04:00
Xavier Claessens c6db870fc7 rust: Add system libs used by rust std to staticlib external deps 2023-05-20 10:35:42 -04:00
Xavier Claessens 01bb756484 rust: Use Popen_safe() for sanity checks 2023-05-20 10:35:42 -04:00
Eli Schwartz 057ac1f576 rust compiler: use better sanity check logging comparable to the clike one
Don't spew debug-level info. Log the actual command line run.
2023-05-16 11:19:43 -04:00
Dylan Baker c62989ce80 rust: add support for b_ndebug
Rust has a `debug_assert!()` macro, which is designed to be toggled on
the command line. It is on by default in debug builds, and off by
default in release builds, in cargo. This matches what meson's b_ndebug
option does in `if-release` mode.
2023-04-21 15:18:56 +05:30
Sebastian Dröge 49e62877d1 rust: Don't pass dependency compile arguments to the compiler
Rust doesn't have a concept of dependency compile arguments, i.e.
something like headers. Dependencies are linked in and all required
metadata is provided by the linker flags.
2023-04-14 06:11:44 -04:00