Implement ARM PL011 in Rust
Subsystems (2)
v3 cover Implement ARM PL011 in Rust
Changes from v2->v3:
- Addressed minor mistakes (thanks Stefan)
- Setup supported version checks for cargo, rustc and bindgen (thanks everyone who pointed it out / suggested it)
- Fixed problem with bindgen failing if certain system headers where needed by defining an allowlist for headers instead of a blocklist for what we don't want (thanks Alex Bennée for reporting it)
- Cleaned up bindgen target/dependendy definition in meson.build by removing unnecessary bits
Changes from v1->v2:
- Create bindgen target first, then add commit for device (thanks Pierrick)
- Create a special named generated.rs for each target as compilation would fail if more than one targets were defined. The generated.rs target names would clash.
- Add more descriptive commit messages
- Update MAINTAINERS
- Cleanup patch order for better review, hopefully
v2 was:
<[email protected]>
v1 was:
<[email protected]>
Patches can be found online at
https://gitlab.com/epilys/rust-for-qemu/-/tags
Tag/refs:
- rust-pl011-rfc-v3
- rust-pl011-rfc-v2
- rust-pl011-rfc-v1
Manos Pitsidianakis (5):
build-sys: Add rust feature option
rust: add bindgen step as a meson dependency
rust: add PL011 device model
DO NOT MERGE: add rustdoc build for gitlab pages
DO NOT MERGE: replace TYPE_PL011 with x-pl011-rust in arm virt machine
.gitlab-ci.d/buildtest.yml | 64 ++-- MAINTAINERS | 15 + configure | 11 + hw/arm/virt.c | 4 + meson.build | 71 ++++ meson_options.txt | 4 + rust/.cargo/config.toml | 2 + rust/.gitignore | 3 + rust/meson.build | 131 ++++++++ rust/pl011/.gitignore | 2 + rust/pl011/Cargo.lock | 120 +++++++ rust/pl011/Cargo.toml | 66 ++++ rust/pl011/README.md | 42 +++ rust/pl011/build.rs | 44 +++ rust/pl011/deny.toml | 57 ++++ rust/pl011/meson.build | 7 + rust/pl011/rustfmt.toml | 1 + rust/pl011/src/definitions.rs | 95 ++++++ rust/pl011/src/device.rs | 531 ++++++++++++++++++++++++++++++ rust/pl011/src/device_class.rs | 95 ++++++ rust/pl011/src/generated.rs | 5 + rust/pl011/src/lib.rs | 581 +++++++++++++++++++++++++++++++++ rust/pl011/src/memory_ops.rs | 38 +++ rust/rustfmt.toml | 7 + rust/wrapper.h | 39 +++ scripts/cargo_wrapper.py | 289 ++++++++++++++++ scripts/meson-buildoptions.sh | 6 + 27 files changed, 2311 insertions(+), 19 deletions(-) create mode 100644 rust/.cargo/config.toml create mode 100644 rust/.gitignore create mode 100644 rust/meson.build create mode 100644 rust/pl011/.gitignore create mode 100644 rust/pl011/Cargo.lock create mode 100644 rust/pl011/Cargo.toml create mode 100644 rust/pl011/README.md create mode 100644 rust/pl011/build.rs create mode 100644 rust/pl011/deny.toml create mode 100644 rust/pl011/meson.build create mode 120000 rust/pl011/rustfmt.toml create mode 100644 rust/pl011/src/definitions.rs create mode 100644 rust/pl011/src/device.rs create mode 100644 rust/pl011/src/device_class.rs create mode 100644 rust/pl011/src/generated.rs create mode 100644 rust/pl011/src/lib.rs create mode 100644 rust/pl011/src/memory_ops.rs create mode 100644 rust/rustfmt.toml create mode 100644 rust/wrapper.h create mode 100644 scripts/cargo_wrapper.py
base-commit: 01782d6b294f95bcde334386f0aaac593cd28c0d
Signature · 1 line
γαῖα πυρί μιχθήτω
RFC PATCH v3 5/5 DO NOT MERGE: replace TYPE_PL011 with x-pl011-rust in arm virt machine
Convenience patch for testing the rust device.
--- hw/arm/virt.c | 4 ++++ 1 file changed, 4 insertions(+)
hw/arm/virt.c +4 -0
@@ -912,7 +912,11 @@ static void create_uart(const VirtMachineState *vms, int uart,int irq = vms->irqmap[uart];const char compat[] ="arm,pl011\0arm,primecell" ;const char clocknames[] ="uartclk\0apb_pclk" ; +#ifdef CONFIG_WITH_RUST +DeviceState *dev =qdev_new ("x-pl011-rust" ); +#else DeviceState *dev =qdev_new (TYPE_PL011 ); +#endif SysBusDevice *s =SYS_BUS_DEVICE (dev);MachineState *ms =MACHINE (vms);
Signature · 1 line
γαῖα πυρί μιχθήτω
RFC PATCH v3 1/5 build-sys: Add rust feature option
In reply to Manos PitsidianakisAdd options for Rust in meson_options.txt, meson.build, configure to prepare for adding Rust code in the followup commits.
rust is a reserved meson name, so we have to use an alternative. with_rust was chosen.
A cargo_wrapper.py script is added that is heavily based on the work of Marc-André Lureau from 2021.
https://patchew.org/QEMU/[email protected]/
--- MAINTAINERS | 5 + configure | 11 ++ meson.build | 11 ++ meson_options.txt | 4 + scripts/cargo_wrapper.py | 279 ++++++++++++++++++++++++++++++++++ scripts/meson-buildoptions.sh | 6 + 6 files changed, 316 insertions(+) create mode 100644 scripts/cargo_wrapper.py
MAINTAINERS +5 -0
@@ -4223,6 +4223,11 @@ F: docs/sphinx/ F: docs/_templates/ F: docs/devel/docs.rst +Rust build system integration +M: Manos Pitsidianakis <[email protected]> +S: Maintained +F: scripts/cargo_wrapper.py + Miscellaneous ------------- Performance Tools and Tests
configure +11 -0
@@ -302,6 +302,9 @@ else objcc="${objcc-${cross_prefix}clang}" fi +with_rust="auto" +with_rust_target_triple="" + ar="${AR-${cross_prefix}ar}" as="${AS-${cross_prefix}as}" ccas="${CCAS-$cc}"
@@ -760,6 +763,12 @@ for opt do ;; --gdb=*) gdb_bin="$optarg" ;; + --enable-with-rust) with_rust=enabled + ;; + --disable-with-rust) with_rust=disabled + ;; + --with-rust-target-triple=*) with_rust_target_triple="$optarg" + ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;;
@@ -1796,6 +1805,8 @@ if test "$skip_meson" = no; then test -n "${LIB_FUZZING_ENGINE+xxx}" && meson_option_add "-Dfuzzing_engine=$LIB_FUZZING_ENGINE" test "$plugins" = yes && meson_option_add "-Dplugins=true" test "$tcg" != enabled && meson_option_add "-Dtcg=$tcg" + test "$with_rust" != enabled && meson_option_add "-Dwith_rust=$with_rust" + test "$with_rust_target_triple" != "" && meson_option_add "-Dwith_rust_target_triple=$with_rust_target_triple" run_meson() { NINJA=$ninja $meson setup "$@" "$PWD" "$source_path" }
meson.build +11 -0
@@ -290,6 +290,12 @@ foreach lang : all_languages endif endforeach +cargo = not_found +if get_option('with_rust').allowed() + cargo = find_program('cargo', required: get_option('with_rust')) +endif +with_rust = cargo.found() + # default flags for all hosts # We use -fwrapv to tell the compiler that we require a C dialect where # left shift of signed integers is well defined and has the expected
@@ -2066,6 +2072,7 @@ endif config_host_data = configuration_data() +config_host_data.set('CONFIG_WITH_RUST', with_rust) audio_drivers_selected = [] if have_system audio_drivers_available = {
@@ -4190,6 +4197,10 @@ if 'objc' in all_languages else summary_info += {'Objective-C compiler': false} endif +summary_info += {'Rust support': with_rust} +if with_rust and get_option('with_rust_target_triple') != '' + summary_info += {'Rust target': get_option('with_rust_target_triple')} +endif option_cflags = (get_option('debug') ? ['-g'] : []) if get_option('optimization') != 'plain' option_cflags += ['-O' + get_option('optimization')]
meson_options.txt +4 -0
@@ -366,3 +366,7 @@ option('qemu_ga_version', type: 'string', value: '', option('hexagon_idef_parser', type : 'boolean', value : true, description: 'use idef-parser to automatically generate TCG code for the Hexagon frontend') +option('with_rust', type: 'feature', value: 'auto', + description: 'Enable Rust support') +option('with_rust_target_triple', type : 'string', value: '', + description: 'Rust target triple')
scripts/cargo_wrapper.py new file +279 -0
279 lines not on this page · raw message
scripts/meson-buildoptions.sh +6 -0
@@ -79,6 +79,8 @@ meson_options_help() { printf "%s\n" ' auto/sigaltstack/ucontext/windows)' printf "%s\n" ' --with-pkgversion=VALUE use specified string as sub-version of the' printf "%s\n" ' package' + printf "%s\n" ' --with-rust-target-triple=VALUE' + printf "%s\n" ' Specify Rust host target triple' printf "%s\n" ' --with-suffix=VALUE Suffix for QEMU data/modules/config directories' printf "%s\n" ' (can be empty) [qemu]' printf "%s\n" ' --with-trace-file=VALUE Trace file prefix for simple backend [trace]'
@@ -215,6 +217,7 @@ meson_options_help() { printf "%s\n" ' vvfat vvfat image format support' printf "%s\n" ' werror Treat warnings as errors' printf "%s\n" ' whpx WHPX acceleration support' + printf "%s\n" ' with-rust Enable Rust support' printf "%s\n" ' xen Xen backend support' printf "%s\n" ' xen-pci-passthrough' printf "%s\n" ' Xen PCI passthrough support'
@@ -550,6 +553,9 @@ _meson_option_parse() { --disable-werror) printf "%s" -Dwerror=false ;; --enable-whpx) printf "%s" -Dwhpx=enabled ;; --disable-whpx) printf "%s" -Dwhpx=disabled ;; + --enable-with-rust) printf "%s" -Dwith_rust=enabled ;; + --disable-with-rust) printf "%s" -Dwith_rust=disabled ;; + --with-rust-target-triple=*) quote_sh "-Dwith_rust_target_triple=$2" ;; --enable-xen) printf "%s" -Dxen=enabled ;; --disable-xen) printf "%s" -Dxen=disabled ;; --enable-xen-pci-passthrough) printf "%s" -Dxen_pci_passthrough=enabled ;;
Signature · 1 line
γαῖα πυρί μιχθήτω
RFC PATCH v3 2/5 rust: add bindgen step as a meson dependency
In reply to Manos PitsidianakisAdd mechanism to generate rust hw targets that depend on a custom bindgen target for rust bindings to C.
This way bindings will be created before the rust crate is compiled.
The bindings will end up in BUILDDIR/{target}-generated.rs and have the same name as a target:
ninja aarch64-softmmu-generated.rs
The way the bindings are generated is:
1. All required C headers are included in a single file, in our case rust/wrapper.h for convenience. Otherwise we'd have to provide a list of headers every time to the bindgen tool.
2. Meson creates a generated_rs target that runs bindgen making sure the architecture etc header dependencies are present.
3. The generated_rs target takes a list of files, type symbols, function symbols to block from being generated. This is not necessary for the bindings to work, but saves us time and space.
4. Meson creates rust hardware target dependencies from the rust_targets dictionary defined in rust/meson.build.
Since we cannot declare a dependency on generated_rs before it is declared in meson.build, the rust crate targets must be defined after the generated_rs target for each target architecture is defined. This way meson sets up the dependency tree properly.
5. After compiling each rust crate with the cargo_wrapper.py script, its static library artifact is linked as a whole-archive with the final binary.
--- MAINTAINERS | 3 + meson.build | 56 +++++++++++++++++ rust/.gitignore | 3 + rust/meson.build | 129 +++++++++++++++++++++++++++++++++++++++ rust/wrapper.h | 39 ++++++++++++ scripts/cargo_wrapper.py | 10 +++ 6 files changed, 240 insertions(+) create mode 100644 rust/.gitignore create mode 100644 rust/meson.build create mode 100644 rust/wrapper.h
MAINTAINERS +3 -0
@@ -4227,6 +4227,9 @@ Rust build system integration M: Manos Pitsidianakis <[email protected]> S: Maintained F: scripts/cargo_wrapper.py +F: rust/meson.build +F: rust/wrapper.h +F: rust/.gitignore Miscellaneous -------------
meson.build +56 -0
62 lines not on this page · raw message
rust/.gitignore new file +3 -0
@@ -0,0 +1,3 @@ +# Ignore any cargo development build artifacts; for qemu-wide builds, all build +# artifacts will go to the meson build directory. +target
rust/meson.build new file +129 -0
129 lines not on this page · raw message
rust/wrapper.h new file +39 -0
@@ -0,0 +1,39 @@ +/* +* QEMU System Emulator +* +* Copyright (c) 2003-2020 Fabrice Bellard +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "qemu/module.h" +#include "qemu-io.h" +#include "sysemu/sysemu.h" +#include "hw/sysbus.h" +#include "exec/memory.h" +#include "chardev/char-fe.h" +#include "hw/clock.h" +#include "hw/qdev-clock.h" +#include "hw/qdev-properties.h" +#include "hw/qdev-properties-system.h" +#include "hw/irq.h" +#include "qapi/error.h" +#include "migration/vmstate.h" +#include "chardev/char-serial.h"
scripts/cargo_wrapper.py +10 -0
@@ -111,6 +111,8 @@ def get_cargo_rustc(args: argparse.Namespace) -> tuple[Dict[str, Any], List[str] env = os.environ env["CARGO_ENCODED_RUSTFLAGS"] = cfg + env["MESON_BUILD_DIR"] = str(target_dir) + env["MESON_BUILD_ROOT"] = str(args.meson_build_root) return (env, cargo_cmd)
@@ -234,6 +236,14 @@ def main() -> None: required=True, ) parser.add_argument( + "--meson-build-root", + metavar="BUILD_ROOT", + help="meson.project_build_root()", + type=Path, + dest="meson_build_root", + required=True, + ) + parser.add_argument( "--meson-source-dir", metavar="SOURCE_DIR", help="meson.current_source_dir()",
Signature · 1 line
γαῖα πυρί μιχθήτω
RFC PATCH v3 4/5 DO NOT MERGE: add rustdoc build for gitlab pages
In reply to Manos PitsidianakisDeploy the generated rustdocs for my personal rust qemu fork on gitlab.
The URL is:
https://rust-for-qemu-epilys-aebb06ca9f9adfe6584811c14ae44156501d935ba4.gitlab.io/pl011/index.html
--- .gitlab-ci.d/buildtest.yml | 64 +++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 19 deletions(-)
.gitlab-ci.d/buildtest.yml +45 -19
@@ -715,31 +715,57 @@ build-tools-and-docs-debian: # For contributor forks we want to publish from any repo so # that users can see the results of their commits, regardless # of what topic branch they're currently using +# pages: +# extends: .base_job_template +# image: $CI_REGISTRY_IMAGE/qemu/debian:$QEMU_CI_CONTAINER_TAG +# stage: test +# needs: +# - job: build-tools-and-docs-debian +# script: +# - mkdir -p public +# # HTML-ised source tree +# - make gtags +# # We unset variables to work around a bug in some htags versions +# # which causes it to fail when the environment is large +# - CI_COMMIT_MESSAGE= CI_COMMIT_TAG_MESSAGE= htags +# -anT --tree-view=filetree -m qemu_init +# -t "Welcome to the QEMU sourcecode" +# - mv HTML public/src +# # Project documentation +# - make -C build install DESTDIR=$(pwd)/temp-install +# - mv temp-install/usr/local/share/doc/qemu/* public/ +# artifacts: +# when: on_success +# paths: +# - public +# variables: +# QEMU_JOB_PUBLISH: 1 +# The Docker image that will be used to build your app pages: - extends: .base_job_template - image: $CI_REGISTRY_IMAGE/qemu/debian:$QEMU_CI_CONTAINER_TAG - stage: test - needs: - - job: build-tools-and-docs-debian + image: rust:latest script: - - mkdir -p public - # HTML-ised source tree - - make gtags - # We unset variables to work around a bug in some htags versions - # which causes it to fail when the environment is large - - CI_COMMIT_MESSAGE= CI_COMMIT_TAG_MESSAGE= htags - -anT --tree-view=filetree -m qemu_init - -t "Welcome to the QEMU sourcecode" - - mv HTML public/src - # Project documentation - - make -C build install DESTDIR=$(pwd)/temp-install - - mv temp-install/usr/local/share/doc/qemu/* public/ + - rustup component add rustfmt + - DEBIAN_FRONTEND=noninteractive apt-get update -y + - DEBIAN_FRONTEND=noninteractive apt-get install -y python3-venv meson libgcrypt20-dev zlib1g-dev autoconf automake libtool bison flex git libglib2.0-dev libfdt-dev libpixman-1-dev ninja-build make libclang-14-dev + - cargo install bindgen-cli + - mkdir ./build/ + - cd ./build/ + - ../configure --enable-system --disable-kvm --target-list=aarch64-softmmu --enable-with-rust + - ninja "aarch64-softmmu-generated.rs" + - cp ./aarch64-softmmu-generated.rs ../rust/pl011/src/generated.rs.inc + - cd ../rust/pl011/ + - cargo tree --depth 1 -e normal --prefix none | cut -d' ' -f1 | xargs + printf -- '-p %s\n' | xargs cargo doc --no-deps --document-private-items --target x86_64-unknown-linux-gnu + - cd ./../.. + - mv ./rust/pl011/target/x86_64-unknown-linux-gnu/doc ./public artifacts: when: on_success paths: - public - variables: - QEMU_JOB_PUBLISH: 1 + rules: + # This ensures that only pushes to the default branch will trigger + # a pages deploy + - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH coverity: image: $CI_REGISTRY_IMAGE/qemu/fedora:$QEMU_CI_CONTAINER_TAG
Signature · 1 line
γαῖα πυρί μιχθήτω
RFC PATCH v3 3/5 rust: add PL011 device model
In reply to Manos PitsidianakisThis commit adds a re-implementation of hw/char/pl011.c in Rust.
It uses generated Rust bindings (produced by ninja aarch64-softmmu-generated.rs) to
register itself as a QOM type/class.
How to build:
1. Make sure rust, cargo and bindgen (cargo install bindgen-cli) are installed
2. Configure a QEMU build with:
--enable-system --target-list=aarch64-softmmu --enable-with-rust
3. Launching a VM with qemu-system-aarch64 should use the Rust version of the pl011 device (unless it is not set up so in hw/arm/virt.c; the type of the UART device is hardcoded).
To confirm, inspect info qom-tree in the monitor and look for an x-pl011-rust device.
--- MAINTAINERS | 7 + meson.build | 4 + rust/.cargo/config.toml | 2 + rust/meson.build | 2 + rust/pl011/.gitignore | 2 + rust/pl011/Cargo.lock | 120 +++++++ rust/pl011/Cargo.toml | 66 ++++ rust/pl011/README.md | 42 +++ rust/pl011/build.rs | 44 +++ rust/pl011/deny.toml | 57 ++++ rust/pl011/meson.build | 7 + rust/pl011/rustfmt.toml | 1 + rust/pl011/src/definitions.rs | 95 ++++++ rust/pl011/src/device.rs | 531 ++++++++++++++++++++++++++++++ rust/pl011/src/device_class.rs | 95 ++++++ rust/pl011/src/generated.rs | 5 + rust/pl011/src/lib.rs | 581 +++++++++++++++++++++++++++++++++ rust/pl011/src/memory_ops.rs | 38 +++ rust/rustfmt.toml | 7 + 19 files changed, 1706 insertions(+) create mode 100644 rust/.cargo/config.toml create mode 100644 rust/pl011/.gitignore create mode 100644 rust/pl011/Cargo.lock create mode 100644 rust/pl011/Cargo.toml create mode 100644 rust/pl011/README.md create mode 100644 rust/pl011/build.rs create mode 100644 rust/pl011/deny.toml create mode 100644 rust/pl011/meson.build create mode 120000 rust/pl011/rustfmt.toml create mode 100644 rust/pl011/src/definitions.rs create mode 100644 rust/pl011/src/device.rs create mode 100644 rust/pl011/src/device_class.rs create mode 100644 rust/pl011/src/generated.rs create mode 100644 rust/pl011/src/lib.rs create mode 100644 rust/pl011/src/memory_ops.rs create mode 100644 rust/rustfmt.toml
MAINTAINERS +7 -0
@@ -1181,6 +1181,11 @@ F: include/hw/*/microbit*.h F: tests/qtest/microbit-test.c F: docs/system/arm/nrf.rst +ARM PL011 Rust device +M: Manos Pitsidianakis <[email protected]> +S: Maintained +F: rust/pl011/ + AVR Machines -------------
@@ -4230,6 +4235,8 @@ F: scripts/cargo_wrapper.py F: rust/meson.build F: rust/wrapper.h F: rust/.gitignore +F: rust/rustfmt.toml +F: rust/.cargo/config.toml Miscellaneous -------------
meson.build +4 -0
@@ -296,6 +296,10 @@ if get_option('with_rust').allowed() endif with_rust = cargo.found() +if with_rust + subdir('rust') +endif + # default flags for all hosts # We use -fwrapv to tell the compiler that we require a C dialect where # left shift of signed integers is well defined and has the expected
rust/.cargo/config.toml new file +2 -0
@@ -0,0 +1,2 @@ +[build] +rustflags = ["-Crelocation-model=pic", "-Ctarget-feature=+crt-static"]
rust/meson.build +2 -0
@@ -109,6 +109,8 @@ endif # bindgen dependency is declared. rust_hw_target_list = {} +subdir('pl011') + foreach rust_hw_target, rust_hws: rust_hw_target_list foreach rust_hw_dev: rust_hws output = meson.current_build_dir() / rust_target_triple / rs_build_type / rust_hw_dev['output']
rust/pl011/.gitignore new file +2 -0
@@ -0,0 +1,2 @@ +# Ignore generated bindings file overrides. +src/generated.rs.inc
rust/pl011/Cargo.lock new file +120 -0
120 lines not on this page · raw message
rust/pl011/Cargo.toml new file +66 -0
66 lines not on this page · raw message
rust/pl011/README.md new file +42 -0
42 lines not on this page · raw message
rust/pl011/build.rs new file +44 -0
44 lines not on this page · raw message
rust/pl011/deny.toml new file +57 -0
57 lines not on this page · raw message
rust/pl011/meson.build new file +7 -0
@@ -0,0 +1,7 @@ +rust_pl011 = { + 'name': 'rust_pl011_cargo', + 'dirname': 'pl011', + 'output': 'libpl011.a', + } + +rust_hw_target_list += {'aarch64-softmmu': [rust_pl011]}
rust/pl011/rustfmt.toml new file +1 -0
@@ -0,0 +1 @@ +../rustfmt.toml
rust/pl011/src/definitions.rs new file +95 -0
95 lines not on this page · raw message
rust/pl011/src/device.rs new file +531 -0
531 lines not on this page · raw message
rust/pl011/src/device_class.rs new file +95 -0
95 lines not on this page · raw message
rust/pl011/src/generated.rs new file +5 -0
@@ -0,0 +1,5 @@ +#[cfg( MESON_GENERATED_RS )] +include! (concat! (env! ("OUT_DIR" ),"/generated.rs" )); + +#[cfg(not( MESON_GENERATED_RS ))] +include! ("generated.rs.inc" );
rust/pl011/src/lib.rs new file +581 -0
581 lines not on this page · raw message
rust/pl011/src/memory_ops.rs new file +38 -0
@@ -0,0 +1,38 @@ +use core::{mem::MaybeUninit , ptr::NonNull }; + +use crate ::{device::PL011State , generated::*}; + +pub const PL011_OPS :MemoryRegionOps =MemoryRegionOps { + read:Some (pl011_read), + write:Some (pl011_write), + read_with_attrs:None , + write_with_attrs:None , + endianness: device_endian_DEVICE_NATIVE_ENDIAN, + valid:unsafe {MaybeUninit ::<MemoryRegionOps__bindgen_ty_1 >::zeroed ().assume_init () }, + impl_:MemoryRegionOps__bindgen_ty_2 { + min_access_size:4 , + max_access_size:4 , + ..unsafe {MaybeUninit ::<MemoryRegionOps__bindgen_ty_2 >::zeroed ().assume_init () } + }, +}; + +unsafe extern "C" fn pl011_read ( + opaque: *mut core::ffi::c_void , + addr:hwaddr , + size: core::ffi::c_uint , +) ->u64 { +assert! (!opaque.is_null ()); +let mut state =NonNull ::new_unchecked (opaque.cast ::<PL011State >()); + state.as_mut ().read (addr, size) +} + +unsafe extern "C" fn pl011_write ( + opaque: *mut core::ffi::c_void , + addr:hwaddr , + data:u64 , + _size: core::ffi::c_uint , +) { +assert! (!opaque.is_null ()); +let mut state =NonNull ::new_unchecked (opaque.cast ::<PL011State >()); + state.as_mut ().write (addr, data) +}
rust/rustfmt.toml new file +7 -0
@@ -0,0 +1,7 @@ +edition = "2021" +format_generated_files = false +format_code_in_doc_comments = true +format_strings = true +imports_granularity = "Crate" +group_imports = "StdExternalCrate" +wrap_comments = true
Signature · 1 line
γαῖα πυρί μιχθήτω
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Manos PitsidianakisQuoted 10 lines · Manos Pitsidianakis
Add mechanism to generate rust hw targets that depend on a custom bindgen target for rust bindings to C.
This way bindings will be created before the rust crate is compiled.
The bindings will end up in
BUILDDIR/{target}-generated.rsand have the same name as a target:ninja
aarch64-softmmu-generated.rs
<snip>
rust/meson.build line 42
Quoted 7 lines
rust/meson.build new file +7
-0+ +# FIXME: These are the latest stable versions, refine to actual minimum ones. +msrv = { + 'rustc': '1.79.0', + 'cargo': '1.79.0', + 'bindgen': '0.69.4', +}
So for Debian Bookworm this comes out as:
msrv = {
'rustc': '1.79.0',
'cargo': '1.79.0',
'bindgen': '0.69.4',
}I shall have to see how close Trixie is ;-)
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Manos PitsidianakisQuoted 10 lines · Manos Pitsidianakis
Add mechanism to generate rust hw targets that depend on a custom bindgen target for rust bindings to C.
This way bindings will be created before the rust crate is compiled.
The bindings will end up in
BUILDDIR/{target}-generated.rsand have the same name as a target:ninja
aarch64-softmmu-generated.rs
<snip>
rust/meson.build line 93
Quoted 11 lines
rust/meson.build new file +11
-0+ + +rust_targets = {} + +cargo_wrapper = [ + find_program(meson.global_source_root() / 'scripts/cargo_wrapper.py'), + '--config-headers', meson.project_build_root() / 'config-host.h', + '--meson-build-root', meson.project_build_root(), + '--meson-build-dir', meson.current_build_dir(), + '--meson-source-dir', meson.current_source_dir(), +]
I'm unclear what the difference between meson-build-root and meson-build-dir is?
We also end up defining crate-dir and outdir. Aren't these all derivable from whatever module we are building?
Quoted 109 lines
rust/meson.build new file +36
-0+ +if get_option('b_colorout') != 'never' + cargo_wrapper += ['--color', 'always'] +endif + +if get_option('optimization') in ['0', '1', 'g'] + rs_build_type = 'debug' +else + rs_build_type = 'release' +endif + +# Collect metadata for each (crate,qemu-target,compiler-target) combination. +# Rust meson targets cannot be defined a priori because they depend on bindgen +# generation that is created for each emulation target separately. Thus Rust +# meson targets will be defined for each target after the target-specific +# bindgen dependency is declared. +rust_hw_target_list = {} + +foreach rust_hw_target, rust_hws: rust_hw_target_list + foreach rust_hw_dev: rust_hws + output = meson.current_build_dir() / rust_target_triple / rs_build_type / rust_hw_dev['output'] + crate_metadata = { + 'name': rust_hw_dev['name'], + 'output': [rust_hw_dev['output']], + 'output-path': output, + 'command': [cargo_wrapper, + '--crate-dir', meson.current_source_dir() / rust_hw_dev['dirname'], + '--profile', rs_build_type, + '--target-triple', rust_target_triple, + '--outdir', '@OUTDIR@', + 'build-lib' + ] + } + rust_targets += { rust_hw_target: [crate_metadata] } + endforeach +endforeachrust/wrapper.h new file +39
-0@@ -0,0 +1,39 @@ +/* +* QEMU System Emulator +* +* Copyright (c) 2003-2020 Fabrice Bellard +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "qemu/module.h" +#include "qemu-io.h" +#include "sysemu/sysemu.h" +#include "hw/sysbus.h" +#include "exec/memory.h" +#include "chardev/char-fe.h" +#include "hw/clock.h" +#include "hw/qdev-clock.h" +#include "hw/qdev-properties.h" +#include "hw/qdev-properties-system.h" +#include "hw/irq.h" +#include "qapi/error.h" +#include "migration/vmstate.h" +#include "chardev/char-serial.h" scripts/cargo_wrapper.py +10
-0@@ -111,6 +111,8 @@ def get_cargo_rustc(args: argparse.Namespace) -> tuple[Dict[str, Any], List[str] env = os.environ env["CARGO_ENCODED_RUSTFLAGS"] = cfg + env["MESON_BUILD_DIR"] = str(target_dir) + env["MESON_BUILD_ROOT"] = str(args.meson_build_root) return (env, cargo_cmd)@@ -234,6 +236,14 @@ def main() -> None: required=True, ) parser.add_argument( + "--meson-build-root", + metavar="BUILD_ROOT", + help="meson.project_build_root()", + type=Path, + dest="meson_build_root", + required=True, + ) + parser.add_argument( "--meson-source-dir", metavar="SOURCE_DIR", help="meson.current_source_dir()",
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Alex BennéeQuoted 9 lines · Alex Bennée
rust/meson.build line 42
Quoted 6 lines
rust/meson.build new file +6
-0+# FIXME: These are the latest stable versions, refine to actual minimum ones. +msrv = { + 'rustc': '1.79.0', + 'cargo': '1.79.0', + 'bindgen': '0.69.4', +}So for Debian Bookworm this comes out as:
Quoted 5 lines
msrv = {
'rustc': '1.79.0',
'cargo': '1.79.0',
'bindgen': '0.69.4',
}I think it's 0.60.1 bindgen and 1.63.0 rustc/cargo? That means we don't have generic associated types (1.65), which are nice to have but not absolutely necessary.
The only other one with an old version is Ubuntu 22.04 (1.58.1), but it has 1.75.0 in updates
Paolo
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
In reply to Manos PitsidianakisQuoted 6 lines · Manos Pitsidianakis
Add options for Rust in meson_options.txt, meson.build, configure to prepare for adding Rust code in the followup commits.
rustis a reserved meson name, so we have to use an alternative.with_rustwas chosen.
Quoted 7 lines
A
cargo_wrapper.pyscript is added that is heavily based on the work of Marc-André Lureau from 2021.https://patchew.org/QEMU/[email protected]/
Signed-off-by Marc-André Lureau <[email protected]>Signed-off-by Manos Pitsidianakis <[email protected]>
The cargo_wrapper.py script is not used yet, so it should be delayed until it's used.
For the detection of the toolchain, I'd rather do everything in configure since that's where the cross file is built. Something like:
configure +44 -2
@@ -173,6 +173,8 @@ fi # default parameters container_engine="auto" +rust_target_triple="" +with_rust="no" cpu="" cross_compile="no" cross_prefix=""
@@ -201,6 +202,8 @@ for opt do --cross-prefix=*) cross_prefix="$optarg" cross_compile="yes" ;; + --cargo=*) CARGO="$optarg" + ;; --cc=*) CC="$optarg" ;; --cxx=*) CXX="$optarg"
@@ -317,6 +322,8 @@ windmc="${WINDMC-${cross_prefix}windmc}" pkg_config="${PKG_CONFIG-${cross_prefix}pkg-config}" sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}" +cargo="${CARGO-cargo}" + check_define() { cat > $TMPC <<EOF #if !defined($1)
@@ -628,6 +635,8 @@ for opt do ;; --cross-prefix=*) ;; + --cargo=*) + ;; --cc=*) ;; --host-cc=*) host_cc="$optarg"
@@ -755,8 +764,14 @@ for opt do ;; --container-engine=*) container_engine="$optarg" ;; + --rust-target-triple=*) rust_target_triple="$optarg" + ;; --gdb=*) gdb_bin="$optarg" ;; + --with-rust) with_rust=yes + ;; + --without-rust) with_rust=no + ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;;
@@ -854,6 +869,7 @@ $(echo Available targets: $default_target_list | \ Advanced options (experts only): -Dmesonoptname=val passthrough option to meson unmodified --cross-prefix=PREFIX use PREFIX for compile tools, PREFIX can be blank [$cross_prefix] + --cargo=CARGO use Cargo binary CARGO [$cargo] --cc=CC use C compiler CC [$cc] --host-cc=CC when cross compiling, use C compiler CC for code run at build time [$host_cc]
@@ -869,11 +885,13 @@ Advanced options (experts only): --python=PYTHON use specified python [$python] --ninja=NINJA use specified ninja [$ninja] --static enable static build [$static] - --without-default-features default all --enable-* options to "disabled" - --without-default-devices do not include any device that is not needed to + --rust-target-triple=TRIPLE target for Rust cross compilation + --without-default-features default all --enable-* options to "disabled" + --without-default-devices do not include any device that is not needed to start the emulator (only use if you are including desired devices in configs/devices/) --with-devices-ARCH=NAME override default configs/devices + --with-rust enable experimental Rust code --enable-debug enable common debug build options --cpu=CPU Build for host CPU [$cpu] --disable-containers don't use containers for cross-building
@@ -1138,6 +1159,20 @@ EOF fi fi +########################################## +# detect rust triples + +if test "$with_rust" = yes; then + $CARGO -vV > "${TMPDIR1}/${TMPB}.out" + if test $? != 0; then + error_exit "could not execute cargo binary \"$CARGO\"" + fi + rust_host_triple=$(sed -n 's/^host: //p' "${TMPDIR1}/${TMPB}.out") + if test "$rust_target_triple" = ""; then + rust_target_triple=$rust_host_triple + fi +fi + ########################################## # functions to probe cross compilers
@@ -1604,6 +1639,10 @@ if test "$container" != no; then echo "RUNC=$runc" >> $config_host_mak fi echo "SUBDIRS=$subdirs" >> $config_host_mak +if test "$with_rust" = yes; then + echo "RUST_HOST_TRIPLE=$rust_host_triple" >> $config_host_mak + echo "RUST_TARGET_TRIPLE=$rust_target_triple" >> $config_host_mak +fi echo "PYTHON=$python" >> $config_host_mak echo "MKVENV_ENSUREGROUP=$mkvenv ensuregroup $mkvenv_online_flag" >> $config_host_mak echo "GENISOIMAGE=$genisoimage" >> $config_host_mak
@@ -1731,6 +1770,13 @@ if test "$skip_meson" = no; then echo "c = [$(meson_quote $cc $CPU_CFLAGS)]" >> $cross test -n "$cxx" && echo "cpp = [$(meson_quote $cxx $CPU_CFLAGS)]" >> $cross test -n "$objcc" && echo "objc = [$(meson_quote $objcc $CPU_CFLAGS)]" >> $cross + if test "$with_rust" = yes; then + if test "$rust_host_triple" != "$rust_target_triple"; then + echo "cargo = [$(meson_quote $cargo --target "$rust_target_triple")]" >> $cross + else + echo "cargo = [$(meson_quote $cargo)]" >> $cross + fi + fi echo "ar = [$(meson_quote $ar)]" >> $cross echo "dlltool = [$(meson_quote $dlltool)]" >> $cross echo "nm = [$(meson_quote $nm)]" >> $cross
meson.build +9 -0
@@ -290,6 +290,11 @@ foreach lang : all_languages endif endforeach +cargo = not_found +if 'RUST_TARGET_TRIPLE' in config_host + cargo = find_program('cargo', required: true) +endif + # default flags for all hosts # We use -fwrapv to tell the compiler that we require a C dialect where # left shift of signed integers is well defined and has the expected
@@ -4239,6 +4244,10 @@ if 'objc' in all_languages else summary_info += {'Objective-C compiler': false} endif +summary_info += {'Rust support': cargo.found()} +if cargo.found() and config_host['RUST_TARGET_TRIPLE']) != config_host['RUST_HOST_TRIPLE'] + summary_info += {'Rust target': config_host['RUST_TARGET_TRIPLE']} +endif option_cflags = (get_option('debug') ? ['-g'] : []) if get_option('optimization') != 'plain' option_cflags += ['-O' + get_option('optimization')]
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
In reply to Manos PitsidianakisQuoted 13 lines · Manos Pitsidianakis
Add options for Rust in meson_options.txt, meson.build, configure to prepare for adding Rust code in the followup commits.
rustis a reserved meson name, so we have to use an alternative.with_rustwas chosen.A
cargo_wrapper.pyscript is added that is heavily based on the work of Marc-André Lureau from 2021.https://patchew.org/QEMU/[email protected]/
Signed-off-by Marc-André Lureau <[email protected]>Signed-off-by Manos Pitsidianakis <[email protected]>
<snip>
Quoted 29 lines
configure +11
-0+with_rust="auto" +with_rust_target_triple="" + ar="${AR-${cross_prefix}ar}" as="${AS-${cross_prefix}as}" ccas="${CCAS-$cc}"@@ -760,6 +763,12 @@ for opt do ;; --gdb=*) gdb_bin="$optarg" ;; + --enable-with-rust) with_rust=enabled + ;; + --disable-with-rust) with_rust=disabled + ;; + --with-rust-target-triple=*) with_rust_target_triple="$optarg" + ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;;@@ -1796,6 +1805,8 @@ if test "$skip_meson" = no; then test -n "${LIB_FUZZING_ENGINE+xxx}" && meson_option_add "-Dfuzzing_engine=$LIB_FUZZING_ENGINE" test "$plugins" = yes && meson_option_add "-Dplugins=true" test "$tcg" != enabled && meson_option_add "-Dtcg=$tcg" + test "$with_rust" != enabled && meson_option_add "-Dwith_rust=$with_rust" + test "$with_rust_target_triple" != "" && meson_option_add "-Dwith_rust_target_triple=$with_rust_target_triple" run_meson() { NINJA=$ninja $meson setup "$@" "$PWD" "$source_path" }
<snip>
meson.build if 'objc' in all_languages · line 4203
Quoted 4 lines
meson.build +4
-0+summary_info += {'Rust support': with_rust} +if with_rust and get_option('with_rust_target_triple') != '' + summary_info += {'Rust target': get_option('with_rust_target_triple')} +endif
<snip>
I wonder if we should display the auto-probed triple here as well, not just when its been overridden?
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Manos Pitsidianakisrust/meson.build line 42
Quoted 6 lines · Manos Pitsidianakis
rust/meson.build new file +6
-0+# FIXME: These are the latest stable versions, refine to actual minimum ones. +msrv = { + 'rustc': '1.79.0', + 'cargo': '1.79.0', + 'bindgen': '0.69.4', +}
A note for other rust newbies:
These versions are pretty darn close to actual minima. Ubuntu 24.04 packages rust 1.77, which does not support (but has a warning reserving syntax for)
Quoted 1 line
+ println!("cargo::rerun-if-env-changed=MESON_BUILD_DIR");
Since even the newest distros will not have current enough rust versions, we must rely on 'rustup'. This may be available even on older distros; for instance Ubuntu 22.04 has rustup via 'snap'.
I think this is good enough for rust development within qemu, but it may require that the configure switch be opt-in: default no rather than default auto.
r~
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
In reply to Paolo BonziniQuoted 10 lines · Paolo Bonzini
Quoted 9 lines · Manos Pitsidianakis
Add options for Rust in meson_options.txt, meson.build, configure to prepare for adding Rust code in the followup commits.
rustis a reserved meson name, so we have to use an alternative.with_rustwas chosen.A
cargo_wrapper.pyscript is added that is heavily based on the work of Marc-André Lureau from 2021.
Quoted 7 lines
Quoted 4 lines
https://patchew.org/QEMU/[email protected]/
Signed-off-by Marc-André Lureau <[email protected]>Signed-off-by Manos Pitsidianakis <[email protected]>The
cargo_wrapper.pyscript is not used yet, so it should be delayed until it's used.
That's true, I just wanted to make review easier by splitting it out. Can we squash them later or do you think I should I do it for the next series version?
Quoted 116 lines
For the detection of the toolchain, I'd rather do everything in configure since that's where the cross file is built. Something like:
configure +37
-2@@ -173,6 +173,8 @@ fi # default parameters container_engine="auto" +rust_target_triple="" +with_rust="no" cpu="" cross_compile="no" cross_prefix=""@@ -201,6 +202,8 @@ for opt do --cross-prefix=*) cross_prefix="$optarg" cross_compile="yes" ;; + --cargo=*) CARGO="$optarg" + ;; --cc=*) CC="$optarg" ;; --cxx=*) CXX="$optarg"@@ -317,6 +322,8 @@ windmc="${WINDMC-${cross_prefix}windmc}" pkg_config="${PKG_CONFIG-${cross_prefix}pkg-config}" sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}" +cargo="${CARGO-cargo}" + check_define() { cat > $TMPC <<EOF #if !defined($1)@@ -628,6 +635,8 @@ for opt do ;; --cross-prefix=*) ;; + --cargo=*) + ;; --cc=*) ;; --host-cc=*) host_cc="$optarg"@@ -755,8 +764,14 @@ for opt do ;; --container-engine=*) container_engine="$optarg" ;; + --rust-target-triple=*) rust_target_triple="$optarg" + ;; --gdb=*) gdb_bin="$optarg" ;; + --with-rust) with_rust=yes + ;; + --without-rust) with_rust=no + ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;;@@ -854,6 +869,7 @@ $(echo Available targets: $default_target_list | \ Advanced options (experts only): -Dmesonoptname=val passthrough option to meson unmodified --cross-prefix=PREFIX use PREFIX for compile tools, PREFIX can be blank [$cross_prefix] + --cargo=CARGO use Cargo binary CARGO [$cargo] --cc=CC use C compiler CC [$cc] --host-cc=CC when cross compiling, use C compiler CC for code run at build time [$host_cc]@@ -869,11 +885,13 @@ Advanced options (experts only): --python=PYTHON use specified python [$python] --ninja=NINJA use specified ninja [$ninja] --static enable static build [$static] - --without-default-features default all --enable-* options to "disabled" - --without-default-devices do not include any device that is not needed to + --rust-target-triple=TRIPLE target for Rust cross compilation + --without-default-features default all --enable-* options to "disabled" + --without-default-devices do not include any device that is not needed to start the emulator (only use if you are including desired devices in configs/devices/) --with-devices-ARCH=NAME override default configs/devices + --with-rust enable experimental Rust code --enable-debug enable common debug build options --cpu=CPU Build for host CPU [$cpu] --disable-containers don't use containers for cross-building@@ -1138,6 +1159,20 @@ EOF fi fi +########################################## +# detect rust triples + +if test "$with_rust" = yes; then + $CARGO -vV > "${TMPDIR1}/${TMPB}.out" + if test $? != 0; then + error_exit "could not execute cargo binary \"$CARGO\"" + fi + rust_host_triple=$(sed -n 's/^host: //p' "${TMPDIR1}/${TMPB}.out") + if test "$rust_target_triple" = ""; then + rust_target_triple=$rust_host_triple + fi +fi + ########################################## # functions to probe cross compilers@@ -1604,6 +1639,10 @@ if test "$container" != no; then echo "RUNC=$runc" >> $config_host_mak fi echo "SUBDIRS=$subdirs" >> $config_host_mak +if test "$with_rust" = yes; then + echo "RUST_HOST_TRIPLE=$rust_host_triple" >> $config_host_mak + echo "RUST_TARGET_TRIPLE=$rust_target_triple" >> $config_host_mak +fi echo "PYTHON=$python" >> $config_host_mak echo "MKVENV_ENSUREGROUP=$mkvenv ensuregroup $mkvenv_online_flag" >> $config_host_mak echo "GENISOIMAGE=$genisoimage" >> $config_host_mak
configure if test "$skip_meson" = no; then · line 1779
Quoted 10 lines
configure +7
-0@@ -1731,6 +1770,13 @@ if test "$skip_meson" = no; then echo "c = [$(meson_quote $cc $CPU_CFLAGS)]" >> $cross test -n "$cxx" && echo "cpp = [$(meson_quote $cxx $CPU_CFLAGS)]" >> $cross test -n "$objcc" && echo "objc = [$(meson_quote $objcc $CPU_CFLAGS)]" >> $cross + if test "$with_rust" = yes; then + if test "$rust_host_triple" != "$rust_target_triple"; then + echo "cargo = [$(meson_quote $cargo --target "$rust_target_triple")]" >> $cross + else + echo "cargo = [$(meson_quote $cargo)]" >> $cross + fi + fi
Hm that looks better indeed, thanks!
Quoted 32 lines
configure +0
-0echo "ar = [$(meson_quote $ar)]" >> $cross echo "dlltool = [$(meson_quote $dlltool)]" >> $cross echo "nm = [$(meson_quote $nm)]" >> $crossmeson.build +9
-0@@ -290,6 +290,11 @@ foreach lang : all_languages endif endforeach +cargo = not_found +if 'RUST_TARGET_TRIPLE' in config_host + cargo = find_program('cargo', required: true) +endif + # default flags for all hosts # We use -fwrapv to tell the compiler that we require a C dialect where # left shift of signed integers is well defined and has the expected@@ -4239,6 +4244,10 @@ if 'objc' in all_languages else summary_info += {'Objective-C compiler': false} endif +summary_info += {'Rust support': cargo.found()} +if cargo.found() and config_host['RUST_TARGET_TRIPLE']) != config_host['RUST_HOST_TRIPLE'] + summary_info += {'Rust target': config_host['RUST_TARGET_TRIPLE']} +endif option_cflags = (get_option('debug') ? ['-g'] : []) if get_option('optimization') != 'plain' option_cflags += ['-O' + get_option('optimization')]
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
In reply to Alex BennéeQuoted 8 lines · Alex Bennée
meson.build if 'objc' in all_languages · line 4203
Quoted 4 lines
meson.build +4
-0+summary_info += {'Rust support': with_rust} +if with_rust and get_option('with_rust_target_triple') != '' + summary_info += {'Rust target': get_option('with_rust_target_triple')} +endif<snip>
I wonder if we should display the auto-probed triple here as well, not just when its been overridden?
I agree, once we straighten out host target / cross target detection logic the target summary info print should be unconditional.
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Paolo BonziniQuoted 16 lines · Paolo Bonzini
Quoted 9 lines · Alex Bennée
rust/meson.build line 42
Quoted 6 lines
rust/meson.build new file +6
-0+# FIXME: These are the latest stable versions, refine to actual minimum ones. +msrv = { + 'rustc': '1.79.0', + 'cargo': '1.79.0', + 'bindgen': '0.69.4', +}So for Debian Bookworm this comes out as:
Quoted 5 lines
msrv = { 'rustc': '1.79.0', 'cargo': '1.79.0', 'bindgen': '0.69.4', }
Quoted 8 lines
I think it's 0.60.1 bindgen and 1.63.0 rustc/cargo? That means we don't have generic associated types (1.65), which are nice to have but not absolutely necessary.
The only other one with an old version is Ubuntu 22.04 (1.58.1), but it has 1.75.0 in updates
Paolo
1.63 is definitely old at this point but we might still be in luck, (Except for bindgen). I will try running cargo-msrv[0] which finds the actual minimum supported version of a codebase with a binary search and see if we can give up features if necessary.
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Alex BennéeQuoted 13 lines · Alex Bennée
Quoted 10 lines · Manos Pitsidianakis
Add mechanism to generate rust hw targets that depend on a custom bindgen target for rust bindings to C.
This way bindings will be created before the rust crate is compiled.
The bindings will end up in
BUILDDIR/{target}-generated.rsand have the same name as a target:ninja
aarch64-softmmu-generated.rs<snip>
Quoted 14 lines
rust/meson.build line 93
Quoted 11 lines
rust/meson.build new file +11
-0+ + +rust_targets = {} + +cargo_wrapper = [ + find_program(meson.global_source_root() / 'scripts/cargo_wrapper.py'), + '--config-headers', meson.project_build_root() / 'config-host.h', + '--meson-build-root', meson.project_build_root(), + '--meson-build-dir', meson.current_build_dir(), + '--meson-source-dir', meson.current_source_dir(), +]I'm unclear what the difference between meson-build-root and meson-build-dir is?
Build-dir is the subdir of the current subdir(...) meson.build file
So if we are building under qemu/build, meson_build_root is qemu/build and meson_build_dir is qemu/build/rust
Quoted 3 lines
We also end up defining crate-dir and outdir. Aren't these all derivable from whatever module we are building?
Crate dir is the source directory (i.e. qemu/rust/pl011) that contains the crate's manifest file Cargo.toml.
Outdir is where to put the final build artifact for meson to find. We could derive that from the build directories and package names somehow but I chose to be explicit instead of doing indirect logic to make the process less magic.
I know it's a lot so I'm open to simplifications. The only problem is that all of these directories, except the crate source code, are defined from meson and can change with any refactor we do from the meson side of things.
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Richard HendersonQuoted 10 lines · Richard Henderson
rust/meson.build line 42
Quoted 6 lines · Manos Pitsidianakis
rust/meson.build new file +6
-0+# FIXME: These are the latest stable versions, refine to actual minimum ones. +msrv = { + 'rustc': '1.79.0', + 'cargo': '1.79.0', + 'bindgen': '0.69.4', +}A note for other rust newbies:
Quoted 4 lines
These versions are pretty darn close to actual minima. Ubuntu 24.04 packages rust 1.77, which does not support (but has a warning reserving syntax for)
Quoted 1 line
+ println!("cargo::rerun-if-env-changed=MESON_BUILD_DIR");
rerun-if-env-changed is not new, I think, but the cargo:: instead of cargo: syntax is. Is this what the warning is saying?
Source <https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script>:
Note: The old invocation prefix cargo: (one colon only) is deprecated and won’t get any new features. To migrate, use two-colons prefix cargo::, which was added in Rust 1.77. If you were using cargo:KEY=VALUE for arbitrary links manifest key-value pairs, it is encouraged to switch to cargo::metadata=KEY=VALUE. Stick to cargo:
only if the support of Rust version older than 1.77 is required.
But this is not in any way necessary for us, we can ignore cargo's stale build detection and force it from meson.
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
Quoted 21 lines · Manos Pitsidianakis
Quoted 10 lines · Richard Henderson
rust/meson.build line 42
Quoted 6 lines · Manos Pitsidianakis
rust/meson.build new file +6
-0+# FIXME: These are the latest stable versions, refine to actual minimum ones. +msrv = { + 'rustc': '1.79.0', + 'cargo': '1.79.0', + 'bindgen': '0.69.4', +}A note for other rust newbies:
Quoted 4 lines
These versions are pretty darn close to actual minima. Ubuntu 24.04 packages rust 1.77, which does not support (but has a warning reserving syntax for)
Quoted 1 line
+ println!("cargo::rerun-if-env-changed=MESON_BUILD_DIR");
rerun-if-env-changed is not new, I think, but the
cargo::instead ofcargo:syntax is. Is this what the warning is saying?Source <https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script>:
Quoted 8 lines
Note: The old invocation prefix cargo: (one colon only) is deprecated and won’t get any new features. To migrate, use two-colons prefix cargo::, which was added in Rust 1.77. If you were using cargo:KEY=VALUE for arbitrary links manifest key-value pairs, it is encouraged to switch to cargo::metadata=KEY=VALUE. Stick to cargo:
only if the support of Rust version older than 1.77 is required.But this is not in any way necessary for us, we can ignore cargo's stale build detection and force it from meson.
Yes indeed. The exact error reported is
error: unsupported output in build script of pl011 v0.1.0 (/home/rth/qemu/src/rust/pl011): cargo::rerun-if-env-changed=MESON_BUILD_DIR Found a cargo::key=value build directive which is reserved for future use.
Either change the directive to cargo:key=value syntax (note the single :) or upgrade your version of Rust.
r~
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
In reply to Manos PitsidianakisIl gio 20 giu 2024, 20:13 Manos Pitsidianakis <
[email protected]> ha scritto:
Quoted 17 lines · Paolo Bonzini
Quoted 11 lines · Manos Pitsidianakis
Quoted 10 lines
Add options for Rust in meson_options.txt, meson.build, configure to prepare for adding Rust code in the followup commits.
rustis a reserved meson name, so we have to use an alternative.with_rustwas chosen.A
cargo_wrapper.pyscript is added that is heavily based on the work of Marc-André Lureau from 2021.https://patchew.org/QEMU/[email protected]/
Quoted 4 lines
Quoted 3 lines
Signed-off-by Marc-André Lureau <[email protected]>Signed-off-by Manos Pitsidianakis <[email protected]>
Quoted 7 lines
Quoted 2 lines
The
cargo_wrapper.pyscript is not used yet, so it should be delayed until it's used.That's true, I just wanted to make review easier by splitting it out. Can we squash them later or do you think I should I do it for the next series version?
I guess it depends on what the next version looks like. If you start working on the workspace/build tree/Kconfig parts, it might not have a lot of cargo_wrapper.py code surviving.
Feel free to take this patch and add
Paolo
Quoted 175 lines
For the detection of the toolchain, I'd rather do everything in
Quoted 58 lines
configure since that's where the cross file is built. Something like:
configure +14
-0@@ -173,6 +173,8 @@ fi # default parameters container_engine="auto" +rust_target_triple="" +with_rust="no" cpu="" cross_compile="no" cross_prefix=""@@ -201,6 +202,8 @@ for opt do --cross-prefix=*) cross_prefix="$optarg" cross_compile="yes" ;; + --cargo=*) CARGO="$optarg" + ;; --cc=*) CC="$optarg" ;; --cxx=*) CXX="$optarg"@@ -317,6 +322,8 @@ windmc="${WINDMC-${cross_prefix}windmc}" pkg_config="${PKG_CONFIG-${cross_prefix}pkg-config}" sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}" +cargo="${CARGO-cargo}" + check_define() { cat > $TMPC <<EOF #if !defined($1)@@ -628,6 +635,8 @@ for opt do ;; --cross-prefix=*) ;; + --cargo=*) + ;; --cc=*) ;; --host-cc=*) host_cc="$optarg"@@ -755,8 +764,14 @@ for opt do ;; --container-engine=*) container_engine="$optarg" ;; + --rust-target-triple=*) rust_target_triple="$optarg" + ;; --gdb=*) gdb_bin="$optarg" ;; + --with-rust) with_rust=yes + ;; + --without-rust) with_rust=no + ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;;configure $(echo Available targets: $default_target_list | \ · line 871
Quoted 3 lines
configure +0
-0@@ -854,6 +869,7 @@ $(echo Available targets: $default_target_list | \ Advanced options (experts only): -Dmesonoptname=val passthrough option to meson unmodified --cross-prefix=PREFIX use PREFIX for compile tools, PREFIX can beblank [$cross_prefix]
configure $(echo Available targets: $default_target_list | \
Quoted 3 lines
configure +1
-0+ --cargo=CARGO use Cargo binary CARGO [$cargo] --cc=CC use C compiler CC [$cc] --host-cc=CC when cross compiling, use C compiler CC forcode run
configure Advanced options (experts only): · line 873
Quoted 7 lines
configure +0
-2at build time [$host_cc]@@ -869,11 +885,13 @@ Advanced options (experts only): --python=PYTHON use specified python [$python] --ninja=NINJA use specified ninja [$ninja] --static enable static build [$static] - --without-default-features default all --enable-* options to "disabled" - --without-default-devices do not include any device that is notneeded to
configure Advanced options (experts only): · line 889
Quoted 2 lines
configure +2
-0+ --rust-target-triple=TRIPLE target for Rust cross compilation + --without-default-features default all --enable-* options to"disabled"
configure Advanced options (experts only): · line 890
Quoted 1 line
configure +1
-0+ --without-default-devices do not include any device that is notneeded to
configure Advanced options (experts only): · line 891
Quoted 1 line
configure +0
-0start the emulator (only use if you areincluding
Quoted 28 lines
configure +15
-0desired devices in configs/devices/) --with-devices-ARCH=NAME override default configs/devices + --with-rust enable experimental Rust code --enable-debug enable common debug build options --cpu=CPU Build for host CPU [$cpu] --disable-containers don't use containers for cross-building@@ -1138,6 +1159,20 @@ EOF fi fi +########################################## +# detect rust triples + +if test "$with_rust" = yes; then + $CARGO -vV > "${TMPDIR1}/${TMPB}.out" + if test $? != 0; then + error_exit "could not execute cargo binary \"$CARGO\"" + fi + rust_host_triple=$(sed -n 's/^host: //p' "${TMPDIR1}/${TMPB}.out") + if test "$rust_target_triple" = ""; then + rust_target_triple=$rust_host_triple + fi +fi + ########################################## # functions to probe cross compilersconfigure if test "$container" != no; then · line 1647
Quoted 9 lines
configure +4
-0@@ -1604,6 +1639,10 @@ if test "$container" != no; then echo "RUNC=$runc" >> $config_host_mak fi echo "SUBDIRS=$subdirs" >> $config_host_mak +if test "$with_rust" = yes; then + echo "RUST_HOST_TRIPLE=$rust_host_triple" >> $config_host_mak + echo "RUST_TARGET_TRIPLE=$rust_target_triple" >> $config_host_mak +fi echo "PYTHON=$python" >> $config_host_mak echo "MKVENV_ENSUREGROUP=$mkvenv ensuregroup $mkvenv_online_flag" >>$config_host_mak
configure if test "$skip_meson" = no; then · line 1771
Quoted 4 lines
configure +0
-0echo "GENISOIMAGE=$genisoimage" >> $config_host_mak@@ -1731,6 +1770,13 @@ if test "$skip_meson" = no; then echo "c = [$(meson_quote $cc $CPU_CFLAGS)]" >> $cross test -n "$cxx" && echo "cpp = [$(meson_quote $cxx $CPU_CFLAGS)]" >>$cross
configure if test "$skip_meson" = no; then
Quoted 5 lines
configure +0
-0test -n "$objcc" && echo "objc = [$(meson_quote $objcc $CPU_CFLAGS)]"Quoted 1 line
$cross
configure +3
-0+ if test "$with_rust" = yes; then + if test "$rust_host_triple" != "$rust_target_triple"; then + echo "cargo = [$(meson_quote $cargo --target"$rust_target_triple")]" >> $cross
configure if test "$skip_meson" = no; then · line 1779
Quoted 4 lines
configure +4
-0+ else + echo "cargo = [$(meson_quote $cargo)]" >> $cross + fi + fiHm that looks better indeed, thanks!
Quoted 20 lines
echo "ar = [$(meson_quote $ar)]" >> $cross echo "dlltool = [$(meson_quote $dlltool)]" >> $cross echo "nm = [$(meson_quote $nm)]" >> $crossmeson.build +5
-0@@ -290,6 +290,11 @@ foreach lang : all_languages endif endforeach +cargo = not_found +if 'RUST_TARGET_TRIPLE' in config_host + cargo = find_program('cargo', required: true) +endif + # default flags for all hosts # We use -fwrapv to tell the compiler that we require a C dialect where # left shift of signed integers is well defined and has the expectedmeson.build if 'objc' in all_languages · line 4248
Quoted 5 lines
meson.build +2
-0@@ -4239,6 +4244,10 @@ if 'objc' in all_languages else summary_info += {'Objective-C compiler': false} endif +summary_info += {'Rust support': cargo.found()} +if cargo.found() and config_host['RUST_TARGET_TRIPLE']) !=config_host['RUST_HOST_TRIPLE']
Quoted 7 lines
meson.build +2
-0+ summary_info += {'Rust target': config_host['RUST_TARGET_TRIPLE']} +endif option_cflags = (get_option('debug') ? ['-g'] : []) if get_option('optimization') != 'plain' option_cflags += ['-O' + get_option('optimization')]
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
In reply to Paolo Bonzini[snip]
meson.build foreach lang : all_languages · line 296
Quoted 12 lines
meson.build +5
-0@@ -290,6 +290,11 @@ foreach lang : all_languages endif endforeach +cargo = not_found +if 'RUST_TARGET_TRIPLE' in config_host + cargo = find_program('cargo', required: true) +endif +
As with the original Manos version, it looks like there's no need to check cargo here? Since patch 2 checks cargo and others in rust/meson.build.
Otherwise, cargo was checked twice.
Quoted 16 lines
meson.build +4
-0# default flags for all hosts # We use -fwrapv to tell the compiler that we require a C dialect where # left shift of signed integers is well defined and has the expected@@ -4239,6 +4244,10 @@ if 'objc' in all_languages else summary_info += {'Objective-C compiler': false} endif +summary_info += {'Rust support': cargo.found()} +if cargo.found() and config_host['RUST_TARGET_TRIPLE']) != config_host['RUST_HOST_TRIPLE'] + summary_info += {'Rust target': config_host['RUST_TARGET_TRIPLE']} +endif option_cflags = (get_option('debug') ? ['-g'] : []) if get_option('optimization') != 'plain' option_cflags += ['-O' + get_option('optimization')]
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Manos PitsidianakisHi Manos,
Thanks for your patch. I have a few comments below:
meson.build foreach target : target_dirs · line 3880
Quoted 10 lines
meson.build +2
-0@@ -3876,6 +3876,62 @@ foreach target : target_dirs lib_deps += dep.partial_dependency(compile_args: true, includes: true) endforeach + if with_rust and target_type == 'system' + # FIXME:
Just nit, FIXME looks a bit ambiguous, is it intended to fix the following warning? It could be a bit more specific.
Quoted 72 lines
meson.build +54
-0+ # > WARNING: Project specifies a minimum meson_version '>=0.63.0' but + # > uses features which were added in newer versions: + # > * 0.64.0: {'fs.copyfile'} + # > * 1.0.0: {'dependencies arg in rust.bindgen', 'module rust as stable module'} + rust_bindgen = import('rust') + + # We need one generated_rs target per target, so give them + # target-specific names. + copy = fs.copyfile('rust/wrapper.h', + target + '_wrapper.h') + generated_rs = rust_bindgen.bindgen( + input: copy, + dependencies: arch_deps + lib_deps, + output: target + '-generated.rs', + include_directories: include_directories('.', 'include'), + args: [ + '--ctypes-prefix', 'core::ffi', + '--formatter', 'rustfmt', + '--generate-block', + '--generate-cstr', + '--impl-debug', + '--merge-extern-blocks', + '--no-doc-comments', + '--no-include-path-detection', + '--use-core', + '--with-derive-default', + '--allowlist-file', meson.project_source_root() + '/include/.*', + '--allowlist-file', meson.project_source_root() + '/.*', + '--allowlist-file', meson.project_build_root() + '/.*' + ], + ) + + if target in rust_targets + rust_hw = ss.source_set() + foreach t: rust_targets[target] + rust_device_cargo = custom_target(t['name'], + output: t['output'], + depends: [generated_rs], + build_always_stale: true, + command: t['command']) + rust_dep = declare_dependency(link_args: [ + '-Wl,--whole-archive', + t['output-path'], + '-Wl,--no-whole-archive' + ], + sources: [rust_device_cargo]) + rust_hw.add(rust_dep) + endforeach + rust_hw_config = rust_hw.apply(config_target, strict: false) + arch_srcs += rust_hw_config.sources() + arch_deps += rust_hw_config.dependencies() + endif + endif + lib = static_library('qemu-' + target, sources: arch_srcs + genh, dependencies: lib_deps,rust/.gitignore new file +3
-0@@ -0,0 +1,3 @@ +# Ignore any cargo development build artifacts; for qemu-wide builds, all build +# artifacts will go to the meson build directory. +targetrust/meson.build new file +0
-0
rust/meson.build line 6
Quoted 6 lines
rust/meson.build new file +6
-0@@ -0,0 +1,129 @@ +# Supported hosts +rust_supported_oses = { + 'linux': '-unknown-linux-gnu', + 'darwin': '-apple-darwin', + 'windows': '-pc-windows-gnu' +}
Does the current test cover windows and apple? If not, I think we can add these two platforms later on.
rust/meson.build line 7
Quoted 1 line
rust/meson.build new file +1
-0+rust_supported_cpus = ['x86_64', 'aarch64']
Similarly, here I have another question, x-pl011-rust in arm virt machine I understand should only run on ARM platform, right? So compiling to x86_64 doesn't seem necessary at the moment?
Quoted 17 lines
rust/meson.build new file +17
-0+# Future-proof the above definitions against any change in the root meson.build file: +foreach rust_os: rust_supported_oses.keys() + if not supported_oses.contains(rust_os) + message() + warning('UNSUPPORTED OS VALUES IN ' + meson.current_source_dir() + '/meson.build') + message() + message('This meson.build file claims OS `+' + rust_os + '` is supported but') + message('it is not included in the global supported OSes list in') + message(meson.global_source_root() + '/meson.build.') + endif +endforeach +foreach rust_cpu: rust_supported_cpus + if not supported_cpus.contains(rust_cpu) + message() + warning('UNSUPPORTED CPU VALUES IN ' + meson.current_source_dir() + '/meson.build') + message() + message('This meson.build file claims CPU `+' + rust_cpu + '` is supported but')
rust/meson.build line 35
Quoted 10 lines
rust/meson.build new file +10
-0+ message('it is not included in the global supported CPUs list in') + message(meson.global_source_root() + '/meson.build.') + endif +endforeach + +# FIXME: retrieve target triple from meson and construct rust_target_triple +if meson.is_cross_build() + message() + error('QEMU does not support cross compiling with Rust enabled.') +endif
Is the check here to avoid inconsistencies between cross-build's target and rust_target_triple?
If target consistency is not guaranteed, then it seems custom rust_target_triple is useless, right? please educate me if I am wrong ;-)
rust/meson.build line 48
Quoted 12 lines
rust/meson.build new file +12
-0+# FIXME: These are the latest stable versions, refine to actual minimum ones. +msrv = { + 'rustc': '1.79.0', + 'cargo': '1.79.0', + 'bindgen': '0.69.4', +} + +# rustup = find_program('rustup', required: false) +foreach bin_dep: msrv.keys() + bin = find_program(bin_dep, required: true) + if bin.version() < msrv[bin_dep] + if bin_dep == 'bindgen' and get_option('wrap_mode') != 'nodownload'
What about adding a download info here?
It took a long time for my bindgen-cli to update quietly, and for a moment I thought it was stuck here...
e.g., message('Installing bindgen-cli...')
Or can we just report the error and let users install bindgen-cli themselves?
Quoted 23 lines
rust/meson.build new file +23
-0+ run_command(cargo, 'install', 'bindgen-cli', capture: true, check: true) + bin = find_program(bin_dep, required: true) + if bin.version() >= msrv[bin_dep] + continue + endif + endif + message() + error(bin_dep + ' version ' + bin.version() + ' is unsupported: Please upgrade to at least ' + msrv[bin_dep]) + endif +endforeach + +rust_target_triple = get_option('with_rust_target_triple') + +if rust_target_triple == '' + if not supported_oses.contains(host_os) + message() + error('QEMU does not support `' + host_os +'` as a Rust platform.') + elif not supported_cpus.contains(host_arch) + message() + error('QEMU does not support `' + host_arch +'` as a Rust architecture.') + endif + rust_target_triple = host_arch + rust_supported_oses[host_os] + if host_os == 'windows' and host_arch == 'aarch64'
rust/meson.build line 81
Quoted 10 lines
rust/meson.build new file +10
-0+ rust_target_triple += 'llvm' + endif +else + # verify rust_target_triple if given as an option + rustc = find_program('rustc', required: true) + rustc_targets = run_command(rustc, '--print', 'target-list', capture: true, check: true).stdout().strip().split() + if not rustc_targets.contains(rust_target_triple) + message() + error('Given rust_target_triple ' + rust_target_triple + ' is not listed in rustc --print target-list output') + endif
It seems rust_target_triple missed support check, we should use rust_supported_cpus
+ rust_supported_oses to check rust_target_triple, right? ;-)
rust/meson.build line 36
Quoted 3 lines
rust/meson.build new file +3
-0+endif + +
An extra blank line :-)
Quoted 9 lines
rust/meson.build new file +9
-0+rust_targets = {} + +cargo_wrapper = [ + find_program(meson.global_source_root() / 'scripts/cargo_wrapper.py'), + '--config-headers', meson.project_build_root() / 'config-host.h', + '--meson-build-root', meson.project_build_root(), + '--meson-build-dir', meson.current_build_dir(), + '--meson-source-dir', meson.current_source_dir(), +]
rust/meson.build line 103
Quoted 10 lines
rust/meson.build new file +10
-0+ +if get_option('b_colorout') != 'never' + cargo_wrapper += ['--color', 'always'] +endif + +if get_option('optimization') in ['0', '1', 'g'] + rs_build_type = 'debug' +else + rs_build_type = 'release' +endif
Could we decouple 'optimization' with cargo's opt-level (maybe in the future)?
Or we could add new option to specify custom rust opt-level, which will set the environment varialbe CARGO_PROFILE_<profile_name>_OPT_LEVEL and override the default opt-level in Cargo.toml.
Quoted 31 lines
rust/meson.build new file +25
-0+# Collect metadata for each (crate,qemu-target,compiler-target) combination. +# Rust meson targets cannot be defined a priori because they depend on bindgen +# generation that is created for each emulation target separately. Thus Rust +# meson targets will be defined for each target after the target-specific +# bindgen dependency is declared. +rust_hw_target_list = {} + +foreach rust_hw_target, rust_hws: rust_hw_target_list + foreach rust_hw_dev: rust_hws + output = meson.current_build_dir() / rust_target_triple / rs_build_type / rust_hw_dev['output'] + crate_metadata = { + 'name': rust_hw_dev['name'], + 'output': [rust_hw_dev['output']], + 'output-path': output, + 'command': [cargo_wrapper, + '--crate-dir', meson.current_source_dir() / rust_hw_dev['dirname'], + '--profile', rs_build_type, + '--target-triple', rust_target_triple, + '--outdir', '@OUTDIR@', + 'build-lib' + ] + } + rust_targets += { rust_hw_target: [crate_metadata] } + endforeach +endforeachrust/wrapper.h new file +0
-0
rust/wrapper.h line 4
Quoted 4 lines
rust/wrapper.h new file +4
-0@@ -0,0 +1,39 @@ +/* +* QEMU System Emulator +* +* Copyright (c) 2003-2020 Fabrice Bellard
Here should the author be yourself?
Quoted 25 lines
rust/wrapper.h new file +25
-0+* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "qemu/module.h" +#include "qemu-io.h" +#include "sysemu/sysemu.h" +#include "hw/sysbus.h"
rust/wrapper.h line 39
Quoted 10 lines
rust/wrapper.h new file +10
-0+#include "exec/memory.h" +#include "chardev/char-fe.h" +#include "hw/clock.h" +#include "hw/qdev-clock.h" +#include "hw/qdev-properties.h" +#include "hw/qdev-properties-system.h" +#include "hw/irq.h" +#include "qapi/error.h" +#include "migration/vmstate.h" +#include "chardev/char-serial.h"
Thanks,
Zhao
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
Hello Zhao!
Quoted 12 lines · Zhao Liu
Hi Manos,
Thanks for your patch. I have a few comments below:
Quoted 8 lines
meson.build +0
-0@@ -3876,6 +3876,62 @@ foreach target : target_dirs lib_deps += dep.partial_dependency(compile_args: true, includes: true) endforeach
Quoted 5 lines
meson.build foreach target : target_dirs · line 3880
Quoted 2 lines
meson.build +2
-0+ if with_rust and target_type == 'system' + # FIXME:Just nit, FIXME looks a bit ambiguous, is it intended to fix the following warning? It could be a bit more specific.
Yes it could, that's true. Basically meson prints this warning when it reconfigures the build targets. So to use these newer features in our minimum supported meson version the code should be fixed to be backwards compatible.
I will add a sentence explaining that this is meson's output.
Quoted 74 lines
Quoted 74 lines
meson.build +54
-0+ # > WARNING: Project specifies a minimum meson_version '>=0.63.0' but + # > uses features which were added in newer versions: + # > * 0.64.0: {'fs.copyfile'} + # > * 1.0.0: {'dependencies arg in rust.bindgen', 'module rust as stable module'} + rust_bindgen = import('rust') + + # We need one generated_rs target per target, so give them + # target-specific names. + copy = fs.copyfile('rust/wrapper.h', + target + '_wrapper.h') + generated_rs = rust_bindgen.bindgen( + input: copy, + dependencies: arch_deps + lib_deps, + output: target + '-generated.rs', + include_directories: include_directories('.', 'include'), + args: [ + '--ctypes-prefix', 'core::ffi', + '--formatter', 'rustfmt', + '--generate-block', + '--generate-cstr', + '--impl-debug', + '--merge-extern-blocks', + '--no-doc-comments', + '--no-include-path-detection', + '--use-core', + '--with-derive-default', + '--allowlist-file', meson.project_source_root() + '/include/.*', + '--allowlist-file', meson.project_source_root() + '/.*', + '--allowlist-file', meson.project_build_root() + '/.*' + ], + ) + + if target in rust_targets + rust_hw = ss.source_set() + foreach t: rust_targets[target] + rust_device_cargo = custom_target(t['name'], + output: t['output'], + depends: [generated_rs], + build_always_stale: true, + command: t['command']) + rust_dep = declare_dependency(link_args: [ + '-Wl,--whole-archive', + t['output-path'], + '-Wl,--no-whole-archive' + ], + sources: [rust_device_cargo]) + rust_hw.add(rust_dep) + endforeach + rust_hw_config = rust_hw.apply(config_target, strict: false) + arch_srcs += rust_hw_config.sources() + arch_deps += rust_hw_config.dependencies() + endif + endif + lib = static_library('qemu-' + target, sources: arch_srcs + genh, dependencies: lib_deps,rust/.gitignore new file +3
-0@@ -0,0 +1,3 @@ +# Ignore any cargo development build artifacts; for qemu-wide builds, all build +# artifacts will go to the meson build directory. +targetrust/meson.build new file +1
-0@@ -0,0 +1,129 @@ +# Supported hosts
Quoted 8 lines
rust/meson.build line 6
Quoted 5 lines
rust/meson.build new file +5
-0+rust_supported_oses = { + 'linux': '-unknown-linux-gnu', + 'darwin': '-apple-darwin', + 'windows': '-pc-windows-gnu' +}Does the current test cover windows and apple? If not, I think we can add these two platforms later on.
Do you mean the test for supported OSes?
This is for future-proofing the Rust integration in general. I haven't been able to compile under macos yet because bindgen cannot find the system clang header. I also don't have a windows pc to test it on. But it should work theoretically under all three.
Quoted 6 lines
rust/meson.build line 7
Quoted 2 lines
rust/meson.build new file +1
-0+rust_supported_cpus = ['x86_64', 'aarch64']Similarly, here I have another question, x-pl011-rust in arm virt machine I understand should only run on ARM platform, right? So compiling to
x86_64doesn't seem necessary at the moment?
This refers to the host cpu not the virtualized ones, you can run qemu-system-aarch64 under x86_64
Quoted 31 lines
Quoted 17 lines
rust/meson.build new file +17
-0+# Future-proof the above definitions against any change in the root meson.build file: +foreach rust_os: rust_supported_oses.keys() + if not supported_oses.contains(rust_os) + message() + warning('UNSUPPORTED OS VALUES IN ' + meson.current_source_dir() + '/meson.build') + message() + message('This meson.build file claims OS `+' + rust_os + '` is supported but') + message('it is not included in the global supported OSes list in') + message(meson.global_source_root() + '/meson.build.') + endif +endforeach +foreach rust_cpu: rust_supported_cpus + if not supported_cpus.contains(rust_cpu) + message() + warning('UNSUPPORTED CPU VALUES IN ' + meson.current_source_dir() + '/meson.build') + message() + message('This meson.build file claims CPU `+' + rust_cpu + '` is supported but')rust/meson.build line 35
Quoted 10 lines
rust/meson.build new file +10
-0+ message('it is not included in the global supported CPUs list in') + message(meson.global_source_root() + '/meson.build.') + endif +endforeach + +# FIXME: retrieve target triple from meson and construct rust_target_triple +if meson.is_cross_build() + message() + error('QEMU does not support cross compiling with Rust enabled.') +endifIs the check here to avoid inconsistencies between cross-build's target and
rust_target_triple?
Quoted 2 lines
If target consistency is not guaranteed, then it seems custom
rust_target_tripleis useless, right? please educate me if I am wrong ;-)
It is, correct. I was hoping someone on the list would have advice on how to best do this (see the FIXME comment right above)
Quoted 16 lines
rust/meson.build line 48
Quoted 13 lines
rust/meson.build new file +12
-0+# FIXME: These are the latest stable versions, refine to actual minimum ones. +msrv = { + 'rustc': '1.79.0', + 'cargo': '1.79.0', + 'bindgen': '0.69.4', +} + +# rustup = find_program('rustup', required: false) +foreach bin_dep: msrv.keys() + bin = find_program(bin_dep, required: true) + if bin.version() < msrv[bin_dep] + if bin_dep == 'bindgen' and get_option('wrap_mode') != 'nodownload'What about adding a download info here?
Quoted 7 lines
It took a long time for my bindgen-cli to update quietly, and for a moment I thought it was stuck here...
e.g., message('Installing bindgen-cli...')
Or can we just report the error and let users install bindgen-cli themselves?
Now that I think about it again I agree it's better to not mess with user/system installations without asking.
Quoted 65 lines
Quoted 23 lines
rust/meson.build new file +23
-0+ run_command(cargo, 'install', 'bindgen-cli', capture: true, check: true) + bin = find_program(bin_dep, required: true) + if bin.version() >= msrv[bin_dep] + continue + endif + endif + message() + error(bin_dep + ' version ' + bin.version() + ' is unsupported: Please upgrade to at least ' + msrv[bin_dep]) + endif +endforeach + +rust_target_triple = get_option('with_rust_target_triple') + +if rust_target_triple == '' + if not supported_oses.contains(host_os) + message() + error('QEMU does not support `' + host_os +'` as a Rust platform.') + elif not supported_cpus.contains(host_arch) + message() + error('QEMU does not support `' + host_arch +'` as a Rust architecture.') + endif + rust_target_triple = host_arch + rust_supported_oses[host_os] + if host_os == 'windows' and host_arch == 'aarch64'rust/meson.build line 81
Quoted 10 lines
rust/meson.build new file +10
-0+ rust_target_triple += 'llvm' + endif +else + # verify rust_target_triple if given as an option + rustc = find_program('rustc', required: true) + rustc_targets = run_command(rustc, '--print', 'target-list', capture: true, check: true).stdout().strip().split() + if not rustc_targets.contains(rust_target_triple) + message() + error('Given rust_target_triple ' + rust_target_triple + ' is not listed in rustc --print target-list output') + endifIt seems
rust_target_triplemissed support check, we should userust_supported_cpus
+rust_supported_osesto checkrust_target_triple, right? ;-)rust/meson.build line 36
Quoted 3 lines
rust/meson.build new file +3
-0+endif + +An extra blank line :-)
Quoted 9 lines
rust/meson.build new file +9
-0+rust_targets = {} + +cargo_wrapper = [ + find_program(meson.global_source_root() / 'scripts/cargo_wrapper.py'), + '--config-headers', meson.project_build_root() / 'config-host.h', + '--meson-build-root', meson.project_build_root(), + '--meson-build-dir', meson.current_build_dir(), + '--meson-source-dir', meson.current_source_dir(), +]rust/meson.build line 103
Quoted 10 lines
rust/meson.build new file +10
-0+ +if get_option('b_colorout') != 'never' + cargo_wrapper += ['--color', 'always'] +endif + +if get_option('optimization') in ['0', '1', 'g'] + rs_build_type = 'debug' +else + rs_build_type = 'release' +endifCould we decouple 'optimization' with cargo's opt-level (maybe in the future)?
Quoted 3 lines
Or we could add new option to specify custom rust opt-level, which will set the environment varialbe CARGO_PROFILE_<profile_name>_OPT_LEVEL and override the default opt-level in Cargo.toml.
optimization here refers to LLVM's opt flags directly, so I think it makes sense to follow the project-wide compiler settings?
Quoted 30 lines
Quoted 30 lines
rust/meson.build new file +25
-0+# Collect metadata for each (crate,qemu-target,compiler-target) combination. +# Rust meson targets cannot be defined a priori because they depend on bindgen +# generation that is created for each emulation target separately. Thus Rust +# meson targets will be defined for each target after the target-specific +# bindgen dependency is declared. +rust_hw_target_list = {} + +foreach rust_hw_target, rust_hws: rust_hw_target_list + foreach rust_hw_dev: rust_hws + output = meson.current_build_dir() / rust_target_triple / rs_build_type / rust_hw_dev['output'] + crate_metadata = { + 'name': rust_hw_dev['name'], + 'output': [rust_hw_dev['output']], + 'output-path': output, + 'command': [cargo_wrapper, + '--crate-dir', meson.current_source_dir() / rust_hw_dev['dirname'], + '--profile', rs_build_type, + '--target-triple', rust_target_triple, + '--outdir', '@OUTDIR@', + 'build-lib' + ] + } + rust_targets += { rust_hw_target: [crate_metadata] } + endforeach +endforeachrust/wrapper.h new file +0
-1--- /dev/null
Quoted 8 lines
rust/wrapper.h line 4
Quoted 6 lines
+++
b/rust/wrapper.hrust/wrapper.h new file +4
-0@@ -0,0 +1,39 @@ +/* +* QEMU System Emulator +* +* Copyright (c) 2003-2020 Fabrice Bellard Here should the author be yourself?
Technically I copy pasted this file from the main.c executable so it's a derived work :D
Quoted 40 lines
Quoted 26 lines
rust/wrapper.h new file +25
-0+* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "qemu/module.h" +#include "qemu-io.h" +#include "sysemu/sysemu.h" +#include "hw/sysbus.h" rust/wrapper.h line 39
Quoted 10 lines
rust/wrapper.h new file +10
-0+#include "exec/memory.h" +#include "chardev/char-fe.h" +#include "hw/clock.h" +#include "hw/qdev-clock.h" +#include "hw/qdev-properties.h" +#include "hw/qdev-properties-system.h" +#include "hw/irq.h" +#include "qapi/error.h" +#include "migration/vmstate.h" +#include "chardev/char-serial.h" Thanks,
Zhao
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
In reply to Zhao LiuIl lun 24 giu 2024, 10:36 Zhao Liu <[email protected]> ha scritto:
Quoted 19 lines
[snip]
meson.build foreach lang : all_languages · line 296
Quoted 12 lines
meson.build +5
-0@@ -290,6 +290,11 @@ foreach lang : all_languages endif endforeach +cargo = not_found +if 'RUST_TARGET_TRIPLE' in config_host + cargo = find_program('cargo', required: true) +endif +As with the original Manos version, it looks like there's no need to check cargo here? Since patch 2 checks cargo and others in
rust/meson.build.
Quoted 2 lines
Otherwise, cargo was checked twice.
Yes, I would check it here though because it's used in the summary, already in this patch.
Paolo
Quoted 19 lines
meson.build if 'objc' in all_languages · line 4248
Quoted 9 lines
meson.build +2
-0# default flags for all hosts # We use -fwrapv to tell the compiler that we require a C dialect where # left shift of signed integers is well defined and has the expected@@ -4239,6 +4244,10 @@ if 'objc' in all_languages else summary_info += {'Objective-C compiler': false} endif +summary_info += {'Rust support': cargo.found()} +if cargo.found() and config_host['RUST_TARGET_TRIPLE']) !=config_host['RUST_HOST_TRIPLE']
Quoted 7 lines
meson.build +2
-0+ summary_info += {'Rust target': config_host['RUST_TARGET_TRIPLE']} +endif option_cflags = (get_option('debug') ? ['-g'] : []) if get_option('optimization') != 'plain' option_cflags += ['-O' + get_option('optimization')]
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
In reply to Manos PitsidianakisQuoted 28 lines · Manos Pitsidianakis
Add options for Rust in meson_options.txt, meson.build, configure to prepare for adding Rust code in the followup commits.
rustis a reserved meson name, so we have to use an alternative.with_rustwas chosen.A
cargo_wrapper.pyscript is added that is heavily based on the work of Marc-André Lureau from 2021.https://patchew.org/QEMU/[email protected]/
Signed-off-by Marc-André Lureau <[email protected]>Signed-off-by Manos Pitsidianakis <[email protected]>--- MAINTAINERS | 5 + configure | 11 ++ meson.build | 11 ++ meson_options.txt | 4 + scripts/cargo_wrapper.py | 279 ++++++++++++++++++++++++++++++++++ scripts/meson-buildoptions.sh | 6 + 6 files changed, 316 insertions(+) create mode 100644 scripts/cargo_wrapper.pyconfigure +0
-0
configure else · line 305
Quoted 4 lines
configure +1
-0@@ -302,6 +302,9 @@ else objcc="${objcc-${cross_prefix}clang}" fi +with_rust="auto"
On last week's call one of the things we discussed is the expectations for consumers of QEMU around the usage of Rust & its optionality (or not) long term.
If we consider Rust to be an optional feature, then this largely precludes re-writing existing C code in Rust, as we would be forced to either remove existing features from users, or maintain parallel impls in both C & Rust. Neither of these are desirable situations.
I'm of the view that to be valuable for QEMU, we need to consider Rust to become a mandatory feature. There's a question of how quickly we move, however, before declaring it mandatory. The longer we take to declare it mandatory, the longer we are limiting the value we can take from Rust.
If we want to make Rust mandatory, then we should set the user expectations to reflect this from the start.
IOW, with_rust="enabled" should be the default, and require an explicit --disable-rust to opt-out on a very time limited basis. Any use of --disable-rust ought to print a warning at the end of configure, and solicit feedback:
WARNING: Building without Rust is deprecated. QEMU intends
WARNING: to make Rust a mandatory build dependency in the
WARNING: 10.0.0 release.
WARNING:
WARNING: If you have concerns about this requirement
WARNING: please contact [email protected]
I illustrated '10.0.0' on the assumption that we add Rust support in 9.1.0, and thus have 2 releases where it is optional to align with our deprecation policy. Even though we don't usually apply our deprecation policy to build dependencies, this is a significant unique situation so IMHO worth doing.
configure for opt do · line 769
Quoted 13 lines
configure +6
-0+with_rust_target_triple="" + ar="${AR-${cross_prefix}ar}" as="${AS-${cross_prefix}as}" ccas="${CCAS-$cc}"@@ -760,6 +763,12 @@ for opt do ;; --gdb=*) gdb_bin="$optarg" ;; + --enable-with-rust) with_rust=enabled + ;; + --disable-with-rust) with_rust=disabled + ;;
--enable-with-XXX / --disable-with-XXX is pretty unsual naming.
Normally you'd see either --enable-XXX or --with-XXX and their corresponding --disable-XXX or --without-XXX.
configure for opt do · line 770
Quoted 1 line
configure +1
-0+ --with-rust-target-triple=*) with_rust_target_triple="$optarg"
As with previous posting, IMHO, this should ideally not exist. We should honour the --cross-prefix= values, re-mapping to to Rust targets for all the combos we know about. --with-rust-target-triple should only be needed as a workaround for where we might have missed a mapping.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
Il lun 24 giu 2024, 18:52 Daniel P. Berrangé <[email protected]> ha scritto:
Quoted 39 lines · Manos Pitsidianakis
Quoted 10 lines
Add options for Rust in meson_options.txt, meson.build, configure to prepare for adding Rust code in the followup commits.
rustis a reserved meson name, so we have to use an alternative.with_rustwas chosen.A
cargo_wrapper.pyscript is added that is heavily based on the work of Marc-André Lureau from 2021.https://patchew.org/QEMU/[email protected]/
Quoted 18 lines
Signed-off-by Marc-André Lureau <[email protected]>Signed-off-by Manos Pitsidianakis <[email protected]>--- MAINTAINERS | 5 + configure | 11 ++ meson.build | 11 ++ meson_options.txt | 4 + scripts/cargo_wrapper.py | 279 ++++++++++++++++++++++++++++++++++ scripts/meson-buildoptions.sh | 6 + 6 files changed, 316 insertions(+) create mode 100644 scripts/cargo_wrapper.pyconfigure +0
-0configure else · line 305
Quoted 4 lines
configure +1
-0@@ -302,6 +302,9 @@ else objcc="${objcc-${cross_prefix}clang}" fi +with_rust="auto"On last week's call one of the things we discussed is the expectations for consumers of QEMU around the usage of Rust & its optionality (or not) long term.
Quoted 6 lines
I'm of the view that to be valuable for QEMU, we need to consider Rust to become a mandatory feature. There's a question of how quickly we move, however, before declaring it mandatory. The longer we take to declare it mandatory, the longer we are limiting the value we can take from Rust.
Yes, I agree. However, considering we haven't even checked the situation with what language features are required by any idiomatic bindings vs the 1.63 version that we need to support for Debian, I think it's a bit premature to make it mandatory.
As soon as the PL011 device is a reasonable example of how to write good Rust device models, however, we should switch to making it default-on, and --without-rust can go away within one or two releases.
Quoted 2 lines
+ --with-rust-target-triple=*) with_rust_target_triple="$optarg"
As with previous posting, IMHO, this should ideally not exist. We should
Quoted 2 lines
honour the --cross-prefix= values, re-mapping to to Rust targets for all the combos we know about
More precisely it should be based not on the cross prefix, but on the same OS and CPU values that are detected from cpp symbols and used for the meson cross file. I have already made a rough list of differences between these and the Rust target triples, but I haven't yet turned them to code.
It would however be the first patch I send after these are in a good shape for inclusion.
The priority right now should be to investigate the build tree workspace design that I sketched last week in my reply to Richard. Once that can be considered a stable build system integration, further improvements can be done in tree.
--with-rust-target-triple should only be needed
Quoted 2 lines
as a workaround for where we might have missed a mapping.
Agreed.
Paolo
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Manos PitsidianakisQuoted 47 lines · Manos Pitsidianakis
Quoted 13 lines · Alex Bennée
Quoted 10 lines · Manos Pitsidianakis
Add mechanism to generate rust hw targets that depend on a custom bindgen target for rust bindings to C.
This way bindings will be created before the rust crate is compiled.
The bindings will end up in
BUILDDIR/{target}-generated.rsand have the same name as a target:ninja
aarch64-softmmu-generated.rs<snip>
Quoted 14 lines
rust/meson.build line 93
Quoted 11 lines
rust/meson.build new file +11
-0+ + +rust_targets = {} + +cargo_wrapper = [ + find_program(meson.global_source_root() / 'scripts/cargo_wrapper.py'), + '--config-headers', meson.project_build_root() / 'config-host.h', + '--meson-build-root', meson.project_build_root(), + '--meson-build-dir', meson.current_build_dir(), + '--meson-source-dir', meson.current_source_dir(), +]I'm unclear what the difference between meson-build-root and meson-build-dir is?
Build-dir is the subdir of the current subdir(...) meson.build file
So if we are building under qemu/build,
meson_build_rootis qemu/build andmeson_build_diris qemu/build/rustQuoted 3 lines
We also end up defining crate-dir and outdir. Aren't these all derivable from whatever module we are building?
Crate dir is the source directory (i.e. qemu/rust/pl011) that contains the crate's manifest file Cargo.toml.
Outdir is where to put the final build artifact for meson to find. We could derive that from the build directories and package names somehow but I chose to be explicit instead of doing indirect logic to make the process less magic.
Quoted 4 lines
I know it's a lot so I'm open to simplifications. The only problem is that all of these directories, except the crate source code, are defined from meson and can change with any refactor we do from the meson side of things.
Expanding the help text for these command-line options would make it easier to understand. It would be great to include an example path too.
Stefan
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Manos PitsidianakisQuoted 82 lines
Quoted 73 lines
Quoted 73 lines
meson.build +54
-0+ # > WARNING: Project specifies a minimum meson_version '>=0.63.0' but + # > uses features which were added in newer versions: + # > * 0.64.0: {'fs.copyfile'} + # > * 1.0.0: {'dependencies arg in rust.bindgen', 'module rust as stable module'} + rust_bindgen = import('rust') + + # We need one generated_rs target per target, so give them + # target-specific names. + copy = fs.copyfile('rust/wrapper.h', + target + '_wrapper.h') + generated_rs = rust_bindgen.bindgen( + input: copy, + dependencies: arch_deps + lib_deps, + output: target + '-generated.rs', + include_directories: include_directories('.', 'include'), + args: [ + '--ctypes-prefix', 'core::ffi', + '--formatter', 'rustfmt', + '--generate-block', + '--generate-cstr', + '--impl-debug', + '--merge-extern-blocks', + '--no-doc-comments', + '--no-include-path-detection', + '--use-core', + '--with-derive-default', + '--allowlist-file', meson.project_source_root() + '/include/.*', + '--allowlist-file', meson.project_source_root() + '/.*', + '--allowlist-file', meson.project_build_root() + '/.*' + ], + ) + + if target in rust_targets + rust_hw = ss.source_set() + foreach t: rust_targets[target] + rust_device_cargo = custom_target(t['name'], + output: t['output'], + depends: [generated_rs], + build_always_stale: true, + command: t['command']) + rust_dep = declare_dependency(link_args: [ + '-Wl,--whole-archive', + t['output-path'], + '-Wl,--no-whole-archive' + ], + sources: [rust_device_cargo]) + rust_hw.add(rust_dep) + endforeach + rust_hw_config = rust_hw.apply(config_target, strict: false) + arch_srcs += rust_hw_config.sources() + arch_deps += rust_hw_config.dependencies() + endif + endif + lib = static_library('qemu-' + target, sources: arch_srcs + genh, dependencies: lib_deps,rust/.gitignore new file +3
-0@@ -0,0 +1,3 @@ +# Ignore any cargo development build artifacts; for qemu-wide builds, all build +# artifacts will go to the meson build directory. +targetrust/meson.build new file +1
-0@@ -0,0 +1,129 @@ +# Supported hostsQuoted 8 lines
rust/meson.build line 6
Quoted 5 lines
rust/meson.build new file +5
-0+rust_supported_oses = { + 'linux': '-unknown-linux-gnu', + 'darwin': '-apple-darwin', + 'windows': '-pc-windows-gnu' +}Does the current test cover windows and apple? If not, I think we can add these two platforms later on.
Quoted 1 line
Do you mean the test for supported OSes?
Yes.
Quoted 4 lines
This is for future-proofing the Rust integration in general. I haven't been able to compile under macos yet because bindgen cannot find the system clang header. I also don't have a windows pc to test it on. But it should work theoretically under all three.
Yes, they should work. EMM, but there is no particular need for them at the moment, so just to be safe, we can put these two platforms on hold for now, and they can be easily added when the tests are covered.
A TODO can remind support for them.
Quoted 9 lines
Quoted 6 lines
rust/meson.build line 7
Quoted 2 lines
rust/meson.build new file +1
-0+rust_supported_cpus = ['x86_64', 'aarch64']Similarly, here I have another question, x-pl011-rust in arm virt machine I understand should only run on ARM platform, right? So compiling to
x86_64doesn't seem necessary at the moment?This refers to the host cpu not the virtualized ones, you can run qemu-system-aarch64 under
x86_64
Thanks, I see.
Quoted 31 lines
Quoted 31 lines
Quoted 17 lines
rust/meson.build new file +17
-0+# Future-proof the above definitions against any change in the root meson.build file: +foreach rust_os: rust_supported_oses.keys() + if not supported_oses.contains(rust_os) + message() + warning('UNSUPPORTED OS VALUES IN ' + meson.current_source_dir() + '/meson.build') + message() + message('This meson.build file claims OS `+' + rust_os + '` is supported but') + message('it is not included in the global supported OSes list in') + message(meson.global_source_root() + '/meson.build.') + endif +endforeach +foreach rust_cpu: rust_supported_cpus + if not supported_cpus.contains(rust_cpu) + message() + warning('UNSUPPORTED CPU VALUES IN ' + meson.current_source_dir() + '/meson.build') + message() + message('This meson.build file claims CPU `+' + rust_cpu + '` is supported but')rust/meson.build line 35
Quoted 10 lines
rust/meson.build new file +10
-0+ message('it is not included in the global supported CPUs list in') + message(meson.global_source_root() + '/meson.build.') + endif +endforeach + +# FIXME: retrieve target triple from meson and construct rust_target_triple +if meson.is_cross_build() + message() + error('QEMU does not support cross compiling with Rust enabled.') +endifIs the check here to avoid inconsistencies between cross-build's target and
rust_target_triple?
Quoted 5 lines
Quoted 2 lines
If target consistency is not guaranteed, then it seems custom
rust_target_tripleis useless, right? please educate me if I am wrong ;-)It is, correct. I was hoping someone on the list would have advice on how to best do this (see the FIXME comment right above)
Yes, Daniel has said what I would say in his reply.
Quoted 19 lines
Quoted 19 lines
rust/meson.build line 48
Quoted 13 lines
rust/meson.build new file +12
-0+# FIXME: These are the latest stable versions, refine to actual minimum ones. +msrv = { + 'rustc': '1.79.0', + 'cargo': '1.79.0', + 'bindgen': '0.69.4', +} + +# rustup = find_program('rustup', required: false) +foreach bin_dep: msrv.keys() + bin = find_program(bin_dep, required: true) + if bin.version() < msrv[bin_dep] + if bin_dep == 'bindgen' and get_option('wrap_mode') != 'nodownload'What about adding a download info here?
It took a long time for my bindgen-cli to update quietly, and for a moment I thought it was stuck here...
Quoted 7 lines
Quoted 4 lines
e.g., message('Installing bindgen-cli...')
Or can we just report the error and let users install bindgen-cli themselves?
Now that I think about it again I agree it's better to not mess with user/system installations without asking.
I agree. Thanks!
Quoted 65 lines
Quoted 65 lines
Quoted 23 lines
rust/meson.build new file +23
-0+ run_command(cargo, 'install', 'bindgen-cli', capture: true, check: true) + bin = find_program(bin_dep, required: true) + if bin.version() >= msrv[bin_dep] + continue + endif + endif + message() + error(bin_dep + ' version ' + bin.version() + ' is unsupported: Please upgrade to at least ' + msrv[bin_dep]) + endif +endforeach + +rust_target_triple = get_option('with_rust_target_triple') + +if rust_target_triple == '' + if not supported_oses.contains(host_os) + message() + error('QEMU does not support `' + host_os +'` as a Rust platform.') + elif not supported_cpus.contains(host_arch) + message() + error('QEMU does not support `' + host_arch +'` as a Rust architecture.') + endif + rust_target_triple = host_arch + rust_supported_oses[host_os] + if host_os == 'windows' and host_arch == 'aarch64'rust/meson.build line 81
Quoted 10 lines
rust/meson.build new file +10
-0+ rust_target_triple += 'llvm' + endif +else + # verify rust_target_triple if given as an option + rustc = find_program('rustc', required: true) + rustc_targets = run_command(rustc, '--print', 'target-list', capture: true, check: true).stdout().strip().split() + if not rustc_targets.contains(rust_target_triple) + message() + error('Given rust_target_triple ' + rust_target_triple + ' is not listed in rustc --print target-list output') + endifIt seems
rust_target_triplemissed support check, we should userust_supported_cpus
+rust_supported_osesto checkrust_target_triple, right? ;-)rust/meson.build line 36
Quoted 3 lines
rust/meson.build new file +3
-0+endif + +An extra blank line :-)
Quoted 9 lines
rust/meson.build new file +9
-0+rust_targets = {} + +cargo_wrapper = [ + find_program(meson.global_source_root() / 'scripts/cargo_wrapper.py'), + '--config-headers', meson.project_build_root() / 'config-host.h', + '--meson-build-root', meson.project_build_root(), + '--meson-build-dir', meson.current_build_dir(), + '--meson-source-dir', meson.current_source_dir(), +]rust/meson.build line 103
Quoted 10 lines
rust/meson.build new file +10
-0+ +if get_option('b_colorout') != 'never' + cargo_wrapper += ['--color', 'always'] +endif + +if get_option('optimization') in ['0', '1', 'g'] + rs_build_type = 'debug' +else + rs_build_type = 'release' +endifCould we decouple 'optimization' with cargo's opt-level (maybe in the future)?
Quoted 6 lines
Quoted 3 lines
Or we could add new option to specify custom rust opt-level, which will set the environment varialbe CARGO_PROFILE_<profile_name>_OPT_LEVEL and override the default opt-level in Cargo.toml.
optimization here refers to LLVM's opt flags directly, so I think it makes sense to follow the project-wide compiler settings?
Oh, yes. But I think the choice of debug or release is best based on the debug info. What about this?
if get_option('debug')
rs_build_type = 'debug'
else
rs_build_type = 'release'
endif
get_option('debug') would apply -g flag, and that flag is mapped to debuginfo=2, which is the default debuginfo level for debug version.
Quoted 38 lines
Quoted 29 lines
Quoted 29 lines
rust/meson.build new file +25
-0+# Collect metadata for each (crate,qemu-target,compiler-target) combination. +# Rust meson targets cannot be defined a priori because they depend on bindgen +# generation that is created for each emulation target separately. Thus Rust +# meson targets will be defined for each target after the target-specific +# bindgen dependency is declared. +rust_hw_target_list = {} + +foreach rust_hw_target, rust_hws: rust_hw_target_list + foreach rust_hw_dev: rust_hws + output = meson.current_build_dir() / rust_target_triple / rs_build_type / rust_hw_dev['output'] + crate_metadata = { + 'name': rust_hw_dev['name'], + 'output': [rust_hw_dev['output']], + 'output-path': output, + 'command': [cargo_wrapper, + '--crate-dir', meson.current_source_dir() / rust_hw_dev['dirname'], + '--profile', rs_build_type, + '--target-triple', rust_target_triple, + '--outdir', '@OUTDIR@', + 'build-lib' + ] + } + rust_targets += { rust_hw_target: [crate_metadata] } + endforeach +endforeachrust/wrapper.h new file +0
-1--- /dev/nullQuoted 8 lines
rust/wrapper.h line 4
Quoted 6 lines
+++
b/rust/wrapper.hrust/wrapper.h new file +4
-0@@ -0,0 +1,39 @@ +/* +* QEMU System Emulator +* +* Copyright (c) 2003-2020 Fabrice Bellard Here should the author be yourself?
Quoted 3 lines
Technically I copy pasted this file from the
main.cexecutable so it's a derived work :D
It's a totally new file, and it is used to derive other files. These are all significant changes. At least for 2024, I think you can declare copyright here. :-)
Thanks,
Zhao
Re: [RFC PATCH v3 5/5] DO NOT MERGE: replace TYPE_PL011 with x-pl011-rust in arm virt machine
In reply to Manos PitsidianakisHi Manos,
Quoted 19 lines · Manos Pitsidianakis
Date: Wed, 19 Jun 2024 23:14:02 +0300
From: Manos Pitsidianakis <[email protected]>
Subject: [RFC PATCH v3 5/5] DO NOT MERGE: replaceTYPE_PL011with
x-pl011-rust in arm virt machine
X-Mailer: git-send-email 2.44.0Convenience patch for testing the rust device.
Signed-off-by Manos Pitsidianakis <[email protected]>--- hw/arm/virt.c | 4 ++++ 1 file changed, 4 insertions(+)hw/arm/virt.c +0
-0@@ -912,7 +912,11 @@ static void create_uart(const VirtMachineState *vms, int uart,int irq = vms->irqmap[uart];
hw/arm/virt.c static void create_uart(const VirtMachineState *vms, int uart, · line 922
Quoted 10 lines
hw/arm/virt.c +4
-0const char compat[] ="arm,pl011\0arm,primecell" ;const char clocknames[] ="uartclk\0apb_pclk" ; +#ifdef CONFIG_WITH_RUST +DeviceState *dev =qdev_new ("x-pl011-rust" ); +#else DeviceState *dev =qdev_new (TYPE_PL011 ); +#endif SysBusDevice *s =SYS_BUS_DEVICE (dev);MachineState *ms =MACHINE (vms);
I realized that if we want to merge the rust pl011 device, then this patch or similar enablement support is necessary, otherwise, the rust code is only used for compilation and cannot actually be run...
This is also an open for the devices that are rewrite in Rust.
I think there should be an option for the user to choose whether to enable pl011 in C or pl011 in Rust. What do you think?
Perhaps the easiest way to enable rust pl011 is to add an option for virt machine... But that's certainly not a long-term approach. I think the ideal way would be to allow rust pl011 to be specified in the command line via -device, but this approach would mean allowing the user to create pl011 and would require changes to the current buildin pl011's creation logic.
-Zhao
Re: [RFC PATCH v3 5/5] DO NOT MERGE: replace TYPE_PL011 with x-pl011-rust in arm virt machine
Quoted 42 lines · Zhao Liu
Hi Manos,
Quoted 19 lines · Manos Pitsidianakis
Date: Wed, 19 Jun 2024 23:14:02 +0300
From: Manos Pitsidianakis <[email protected]>
Subject: [RFC PATCH v3 5/5] DO NOT MERGE: replaceTYPE_PL011with
x-pl011-rust in arm virt machine
X-Mailer: git-send-email 2.44.0Convenience patch for testing the rust device.
Signed-off-by Manos Pitsidianakis <[email protected]>--- hw/arm/virt.c | 4 ++++ 1 file changed, 4 insertions(+)hw/arm/virt.c +0
-0@@ -912,7 +912,11 @@ static void create_uart(const VirtMachineState *vms, int uart,int irq = vms->irqmap[uart];hw/arm/virt.c static void create_uart(const VirtMachineState *vms, int uart, · line 922
Quoted 10 lines
hw/arm/virt.c +4
-0const char compat[] ="arm,pl011\0arm,primecell" ;const char clocknames[] ="uartclk\0apb_pclk" ; +#ifdef CONFIG_WITH_RUST +DeviceState *dev =qdev_new ("x-pl011-rust" ); +#else DeviceState *dev =qdev_new (TYPE_PL011 ); +#endif SysBusDevice *s =SYS_BUS_DEVICE (dev);MachineState *ms =MACHINE (vms);I realized that if we want to merge the rust pl011 device, then this patch or similar enablement support is necessary, otherwise, the rust code is only used for compilation and cannot actually be run...
This is also an open for the devices that are rewrite in Rust.
I think there should be an option for the user to choose whether to enable pl011 in C or pl011 in Rust. What do you think?
Quoted 9 lines
Perhaps the easiest way to enable rust pl011 is to add an option for virt machine... But that's certainly not a long-term approach. I think the ideal way would be to allow rust pl011 to be specified in the command line via -device, but this approach would mean allowing the user to create pl011 and would require changes to the current buildin pl011's creation logic.
-Zhao
We should definitely refer to ARM maintainers. The peculiarity of it being a chardev seems to make it not straightforward... I think it might have to become a machine property like discussed here:
And that'd be a bigger change not related just to Rust.
Re: [RFC PATCH v3 5/5] DO NOT MERGE: replace TYPE_PL011 with x-pl011-rust in arm virt machine
In reply to Zhao LiuQuoted 42 lines · Zhao Liu
Hi Manos,
Quoted 19 lines · Manos Pitsidianakis
Date: Wed, 19 Jun 2024 23:14:02 +0300
From: Manos Pitsidianakis <[email protected]>
Subject: [RFC PATCH v3 5/5] DO NOT MERGE: replaceTYPE_PL011with
x-pl011-rust in arm virt machine
X-Mailer: git-send-email 2.44.0Convenience patch for testing the rust device.
Signed-off-by Manos Pitsidianakis <[email protected]>--- hw/arm/virt.c | 4 ++++ 1 file changed, 4 insertions(+)hw/arm/virt.c +0
-0@@ -912,7 +912,11 @@ static void create_uart(const VirtMachineState *vms, int uart,int irq = vms->irqmap[uart];hw/arm/virt.c static void create_uart(const VirtMachineState *vms, int uart, · line 922
Quoted 10 lines
hw/arm/virt.c +4
-0const char compat[] ="arm,pl011\0arm,primecell" ;const char clocknames[] ="uartclk\0apb_pclk" ; +#ifdef CONFIG_WITH_RUST +DeviceState *dev =qdev_new ("x-pl011-rust" ); +#else DeviceState *dev =qdev_new (TYPE_PL011 ); +#endif SysBusDevice *s =SYS_BUS_DEVICE (dev);MachineState *ms =MACHINE (vms);I realized that if we want to merge the rust pl011 device, then this patch or similar enablement support is necessary, otherwise, the rust code is only used for compilation and cannot actually be run...
This is also an open for the devices that are rewrite in Rust.
I think there should be an option for the user to choose whether to enable pl011 in C or pl011 in Rust. What do you think?
Quoted 6 lines
Perhaps the easiest way to enable rust pl011 is to add an option for virt machine... But that's certainly not a long-term approach. I think the ideal way would be to allow rust pl011 to be specified in the command line via -device, but this approach would mean allowing the user to create pl011 and would require changes to the current buildin pl011's creation logic.
While having both impls present is reasonable for the RFC, IMHO, if we're to merge this, then the Rust impl of pl011 should fully replace the C impl. Maintaining 2 different impls of the same device in parallel is highly undesirable. This would of course impl that the Rust impls needs to be a feature match for the existing impl. If we do a full replacement, then the question of how the user chooses between the 2 impls is then entirely avoided.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Zhao LiuQuoted 11 lines · Zhao Liu
[snip]
Quoted 4 lines
This is for future-proofing the Rust integration in general. I haven't been able to compile under macos yet because bindgen cannot find the system clang header. I also don't have a windows pc to test it on. But it should work theoretically under all three.
Yes, they should work. EMM, but there is no particular need for them at the moment, so just to be safe, we can put these two platforms on hold for now, and they can be easily added when the tests are covered.
A TODO can remind support for them.
I'm still trying to figure out why bindgen doesn't find the /Library/*** include paths.. it's frustrating! I will remove them if I don't succeed and also no one volunteers to attempt a windows build. :)
Quoted 14 lines
[snip]
Quoted 9 lines
Quoted 6 lines
Could we decouple 'optimization' with cargo's opt-level (maybe in the future)?
Or we could add new option to specify custom rust opt-level, which will set the environment varialbe CARGO_PROFILE_<profile_name>_OPT_LEVEL and override the default opt-level in Cargo.toml.
optimization here refers to LLVM's opt flags directly, so I think it makes sense to follow the project-wide compiler settings?
Oh, yes. But I think the choice of debug or release is best based on the debug info. What about this?
Quoted 8 lines
if
get_option('debug')
rs_build_type= 'debug'
else
rs_build_type= 'release'
endif
get_option('debug')would apply -g flag, and that flag is mapped to debuginfo=2, which is the default debuginfo level for debug version.
But wait, I just noticed rs_build_type should be 'dev' not 'debug'. You can build with optimizations and keep debuginfo if you override the 'release' profile or use a different profile altogether.
I will correct the optimization and debuginfo mappings in the next version.
Quoted 3 lines
It's a totally new file, and it is used to derive other files. These are all significant changes. At least for 2024, I think you can declare copyright here. :-)
Heh yes :)
Quoted 4 lines
Thanks,
Zhao
Re: [RFC PATCH v3 5/5] DO NOT MERGE: replace TYPE_PL011 with x-pl011-rust in arm virt machine
In reply to Daniel P. BerrangéQuoted 51 lines · Daniel P. Berrangé
Quoted 42 lines · Zhao Liu
Hi Manos,
Quoted 19 lines · Manos Pitsidianakis
Date: Wed, 19 Jun 2024 23:14:02 +0300
From: Manos Pitsidianakis <[email protected]>
Subject: [RFC PATCH v3 5/5] DO NOT MERGE: replaceTYPE_PL011with
x-pl011-rust in arm virt machine
X-Mailer: git-send-email 2.44.0Convenience patch for testing the rust device.
Signed-off-by Manos Pitsidianakis <[email protected]>--- hw/arm/virt.c | 4 ++++ 1 file changed, 4 insertions(+)hw/arm/virt.c +0
-0@@ -912,7 +912,11 @@ static void create_uart(const VirtMachineState *vms, int uart,int irq = vms->irqmap[uart];hw/arm/virt.c static void create_uart(const VirtMachineState *vms, int uart, · line 922
Quoted 10 lines
hw/arm/virt.c +4
-0const char compat[] ="arm,pl011\0arm,primecell" ;const char clocknames[] ="uartclk\0apb_pclk" ; +#ifdef CONFIG_WITH_RUST +DeviceState *dev =qdev_new ("x-pl011-rust" ); +#else DeviceState *dev =qdev_new (TYPE_PL011 ); +#endif SysBusDevice *s =SYS_BUS_DEVICE (dev);MachineState *ms =MACHINE (vms);I realized that if we want to merge the rust pl011 device, then this patch or similar enablement support is necessary, otherwise, the rust code is only used for compilation and cannot actually be run...
This is also an open for the devices that are rewrite in Rust.
I think there should be an option for the user to choose whether to enable pl011 in C or pl011 in Rust. What do you think?
Quoted 6 lines
Perhaps the easiest way to enable rust pl011 is to add an option for virt machine... But that's certainly not a long-term approach. I think the ideal way would be to allow rust pl011 to be specified in the command line via -device, but this approach would mean allowing the user to create pl011 and would require changes to the current buildin pl011's creation logic.
Quoted 7 lines
While having both impls present is reasonable for the RFC, IMHO, if we're to merge this, then the Rust impl of pl011 should fully replace the C impl. Maintaining 2 different impls of the same device in parallel is highly undesirable. This would of course impl that the Rust impls needs to be a feature match for the existing impl. If we do a full replacement, then the question of how the user chooses between the 2 impls is then entirely avoided.
Yes, I agree this is where we want to go for the long term. For the short term of reaching consensus on all the earlier parts of the patch series (build system integration, how to do a rust device model, etc), I think this patch is fine as a "don't commit this" way to test the series. In the medium term I guess "feature-parity both in parallel for a (hopefully) short time and then drop the C version" ? I definitely feel we don't want a user-facing runtime option for selecting which one, at any rate.
thanks
-- PMM
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
In reply to Paolo BonziniQuoted 5 lines · Paolo Bonzini
Yes, I agree. However, considering we haven't even checked the situation with what language features are required by any idiomatic bindings vs the 1.63 version that we need to support for Debian, I think it's a bit premature to make it mandatory.
FWIW, I just ran
cargo msrv -- cargo check --target x86_64-unknown-linux-gnu
And the result was The MSRV is: 1.77.2
and the most important issue was that the mem::offset_of! macro was unstable till then.
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
Quoted 7 lines · Manos Pitsidianakis
Quoted 5 lines · Paolo Bonzini
Yes, I agree. However, considering we haven't even checked the situation with what language features are required by any idiomatic bindings vs the 1.63 version that we need to support for Debian, I think it's a bit premature to make it mandatory.
Quoted 7 lines
FWIW, I just ran
cargo msrv -- cargo check --target x86_64-unknown-linux-gnuAnd the result was
The MSRV is: 1.77.2and the most important issue was that the
mem::offset_of! macro was unstable till then.
I looked for a way to avoid it and the most promising is https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=10a22a9b8393abd7b541d8fc844bc0df
Basically, you replace
pub struct Foo {
foo: i32,
bar: i32
}with
with_offsets! {
#[repr(C)] // mandatory
pub struct Foo {
foo: i32,
bar: i32,
}
}The macro walks the struct twice, once to actually declare it and once to determine the offsets using mem::size_of and mem::align_of. The result is something like
#[repr(C)] // mandatory
pub struct Foo {
foo: i32,
bar: i32,
}
pub struct FooOffsets {
foo: usize,
bar: usize,
}
impl Foo {
pub const offset_to: FooOffsets = FooOffsets {
foo: 0,
bar: 4,
}
}(where 0 and 4 are actually the aforementioned computation based on size_of and align_of).
There are some limitations but the trick is really well done; the need for #[repr(C)] is not a problem for us (C<->Rust interoperability needs it anyway), and the implementation is fully "const". And though it only works for structs that use "with_offsets!", and with just one level of fields, the implementation of offset_of is trivial:
macro_rules! offset_of {
($Container:ty, $field:ident) => {
<$Container>::offset_to.$field
};
}Anyhow, this should _not_ be in the first version that is committed---which, as you remarked in the v2, should focus on working build system integration. As long as we know it is doable, it can be left for later.
Paolo
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Manos PitsidianakisQuoted 12 lines · Manos Pitsidianakis
Quoted 11 lines · Zhao Liu
[snip]
Quoted 5 lines
This is for future-proofing the Rust integration in general. I haven't been
able to compile under macos yet because bindgen cannot find the system clang header. I also don't have a windows pc to test it on. But it should work theoretically under all three.Yes, they should work. EMM, but there is no particular need for them at the moment, so just to be safe, we can put these two platforms on hold for now, and they can be easily added when the tests are covered.
Quoted 6 lines
Quoted 1 line
A TODO can remind support for them.
I'm still trying to figure out why bindgen doesn't find the /Library/*** include paths.. it's frustrating! I will remove them if I don't succeed and also no one volunteers to attempt a windows build. :)
I'm currently doing it, and managed to run until bindgen step. Same problem that you found on MacOS, it can't locate some headers (strings.h, included from osdep.h). I'll try to dig into this, but if you found a solution already, you're welcome to share it.
'gcc | grep' command you used should work, but should be adapted because windows paths start with C:/ instead of /.
Quoted 41 lines
Quoted 14 lines
[snip]
Quoted 9 lines
Quoted 6 lines
Could we decouple 'optimization' with cargo's opt-level (maybe in the future)?
Or we could add new option to specify custom rust opt-level, which will set the environment varialbe CARGO_PROFILE_<profile_name>_OPT_LEVEL and override the default opt-level in Cargo.toml.
optimization here refers to LLVM's opt flags directly, so I think it makes sense to follow the project-wide compiler settings?
Oh, yes. But I think the choice of debug or release is best based on the debug info. What about this?
Quoted 8 lines
if
get_option('debug')
rs_build_type= 'debug'
else
rs_build_type= 'release'
endif
get_option('debug')would apply -g flag, and that flag is mapped to debuginfo=2, which is the default debuginfo level for debug version.But wait, I just noticed
rs_build_typeshould be 'dev' not 'debug'. You can build with optimizations and keep debuginfo if you override the 'release' profile or use a different profile altogether.I will correct the optimization and debuginfo mappings in the next version.
Quoted 4 lines
It's a totally new file, and it is used to derive other files. These are
all significant changes. At least for 2024, I think you can declare copyright here. :-)Heh yes :)
Quoted 4 lines
Thanks,
Zhao
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
Quoted 20 lines · Pierrick Bouvier
Quoted 12 lines · Manos Pitsidianakis
Quoted 11 lines · Zhao Liu
[snip]
Quoted 5 lines
This is for future-proofing the Rust integration in general. I haven't been
able to compile under macos yet because bindgen cannot find the system clang header. I also don't have a windows pc to test it on. But it should work theoretically under all three.Yes, they should work. EMM, but there is no particular need for them at the moment, so just to be safe, we can put these two platforms on hold for now, and they can be easily added when the tests are covered.
Quoted 6 lines
Quoted 1 line
A TODO can remind support for them.
I'm still trying to figure out why bindgen doesn't find the /Library/*** include paths.. it's frustrating! I will remove them if I don't succeed and also no one volunteers to attempt a windows build. :)
Quoted 8 lines
I'm currently doing it, and managed to run until bindgen step. Same problem that you found on MacOS, it can't locate some headers (
strings.h, included fromosdep.h). I'll try to dig into this, but if you found a solution already, you're welcome to share it.'gcc | grep' command you used should work, but should be adapted because windows paths start with C:/ instead of /.
I've been able to build rust device on windows, with a few tweaks needed.
- specificy the target for libclang (used by bindgen), which targets MSVC by default (so different set of headers)
- additional headers (libclang searches its own header with a relative path instead of absolute)
- additional windows libs that must be linked in final executable
However, even tough I can build the executable, I get this error:
$ ./build/qemu-system-aarch64 -M virt C:\w\qemu\build\qemu-system-aarch64.exe: unknown type 'x-pl011-rust'
Any idea of what could be missing here?
By the way, I noticed configure --enable-with-rust does not trigger error when not finding cargo, it just deactivates rust support, which is probably not what is expected.
---
QEMU Build instructions for windows are here:
https://wiki.qemu.org/Hosts/W32#Native_builds_with_MSYS2
Additional steps needed:
$ cargo install bindgen-cli $ export PATH=/c/Users/user/.cargo/bin/:$PATH $ wget https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.6/LLVM-18.1.6-win64.exe # for libclang.dll $ pacman -S p7zip $ mkdir llvm && cd llvm && 7z x ../LLVM-18.1.6-win64.exe && cd .. $ export LIBCLANG_PATH=$(cygpath -m $(pwd)/llvm/bin/libclang.dll)
Additional libs to link can be found with:
$ touch empty.rs $ rustc empty.rs --print=native-static-libs --crate-type=staticlib note: Link against the following native artifacts when linking against this static library. The order and any duplication can be significant on so me platforms. note: native-static-libs: -lkernel32 -ladvapi32 -lkernel32 -lntdll -luserenv -lws2_32 -lkernel32 -lws2_32 -lkernel32
---
meson.build +12 -3
@@ -3896,7 +3896,8 @@ foreach target : target_dirs input: copy, dependencies: arch_deps + lib_deps, output: target + '-generated.rs', - include_directories: include_directories('.', 'include'), + include_directories: include_directories('.', 'include', + 'llvm/lib/clang/18/include/'), args: [ '--ctypes-prefix', 'core::ffi', '--formatter', 'rustfmt',
@@ -3910,7 +3911,10 @@ foreach target : target_dirs '--with-derive-default', '--allowlist-file', meson.project_source_root() + '/include/.*', '--allowlist-file', meson.project_source_root() + '/.*', - '--allowlist-file', meson.project_build_root() + '/.*' + '--allowlist-file', meson.project_build_root() + '/.*', + ], + c_args: [ + '--target=x86_64-pc-windows-gnu' ], )
@@ -3925,7 +3929,12 @@ foreach target : target_dirs rust_dep = declare_dependency(link_args: [ '-Wl,--whole-archive', t['output-path'], - '-Wl,--no-whole-archive' + '-Wl,--no-whole-archive', + '-lkernel32', + '-ladvapi32', + '-lntdll', + '-luserenv', + '-lws2_32', ], sources: [rust_device_cargo]) rust_hw.add(rust_dep)
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
Quoted 5 lines · Pierrick Bouvier
However, even tough I can build the executable, I get this error:
$ ./build/qemu-system-aarch64 -M virt C:\w\qemu\build\qemu-system-aarch64.exe: unknown type 'x-pl011-rust'Any idea of what could be missing here?
Maybe the underlying mechanism to invoke constructors is different?
Perhaps we could use https://crates.io/crates/ctor instead?
Paolo
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Pierrick BouvierQuoted 8 lines · Pierrick Bouvier
I've been able to build rust device on windows, with a few tweaks needed.
- specificy the target for libclang (used by bindgen), which targets MSVC by default (so different set of headers)
- additional headers (libclang searches its own header with a relative path instead of absolute)
- additional windows libs that must be linked in final executable
Quoted 5 lines
However, even tough I can build the executable, I get this error:
$ ./build/qemu-system-aarch64 -M virt C:\w\qemu\build\qemu-system-aarch64.exe: unknown type 'x-pl011-rust'Any idea of what could be missing here?
Sounds like either the rust lib is not linked to the final binary (which you can confirm if you look at the included symbols and don't see anything with rust origin) or the constructor decorated with #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU"] is not linked properly (you can add a puts() and see if it's invoked or add a breakpoint in gdb)
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Paolo BonziniSeems like the using cfg_attr was taken from this crate, so it should be equivalent to using it.
Quoted 14 lines · Paolo Bonzini
Quoted 5 lines · Pierrick Bouvier
However, even tough I can build the executable, I get this error:
$ ./build/qemu-system-aarch64 -M virt C:\w\qemu\build\qemu-system-aarch64.exe: unknown type 'x-pl011-rust'Any idea of what could be missing here?
Maybe the underlying mechanism to invoke constructors is different?
Perhaps we could use https://crates.io/crates/ctor instead?
Paolo
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Manos PitsidianakisThe ctor is not run, but, interesting point, there is the same problem on Linux :)
Can you please confirm with a clean build this work on your machine with this v3, and report exact configure and run commands you use?
Thanks,
Pierrick
Quoted 22 lines · Manos Pitsidianakis
Quoted 9 lines · Pierrick Bouvier
I've been able to build rust device on windows, with a few tweaks needed.
- specificy the target for libclang (used by bindgen), which targets MSVC by default (so different set of headers)
- additional headers (libclang searches its own header with a relative path instead of absolute)
- additional windows libs that must be linked in final executableQuoted 5 lines
However, even tough I can build the executable, I get this error:
$ ./build/qemu-system-aarch64 -M virt C:\w\qemu\build\qemu-system-aarch64.exe: unknown type 'x-pl011-rust'Any idea of what could be missing here?
Sounds like either the rust lib is not linked to the final binary (which you can confirm if you look at the included symbols and don't see anything with rust origin) or the constructor decorated with #[cfg_attr(target_os = "windows",
link_section= ".CRT$XCU"] is not linked properly (you can add aputs()and see if it's invoked or add a breakpoint in gdb)
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
Quoted 3 lines · Pierrick Bouvier
The ctor is not run, but, interesting point, there is the same problem on Linux :)
Can you please confirm with a clean build this work on your machine with this v3, and report exact configure and run commands you use?
On a clean checkout, I did exactly the following:
``` mkdir ./build && cd ./build ../configure --enable-system --enable-debug \ --target-list=aarch64-softmmu --enable-with-rust ninja qemu-system-aarch64 ```
which selected the default cc/gcc on debian,
gcc (Debian 12.2.0-14) 12.2.0
and ran QEMU with like this:
``` ./qemu-system-aarch64 -M virt \ -machine virtualization=true -machine virt,gic-version=3 \ -cpu max,pauth-impdef=on -smp 2 -m 4096 \ [.. drive and img arguments ..] \ -nographic ```
Quoted 27 lines
Thanks,
PierrickQuoted 22 lines · Manos Pitsidianakis
Quoted 9 lines · Pierrick Bouvier
I've been able to build rust device on windows, with a few tweaks needed.
- specificy the target for libclang (used by bindgen), which targets MSVC by default (so different set of headers)
- additional headers (libclang searches its own header with a relative path instead of absolute)
- additional windows libs that must be linked in final executableQuoted 5 lines
However, even tough I can build the executable, I get this error:
$ ./build/qemu-system-aarch64 -M virt C:\w\qemu\build\qemu-system-aarch64.exe: unknown type 'x-pl011-rust'Any idea of what could be missing here?
Sounds like either the rust lib is not linked to the final binary (which you can confirm if you look at the included symbols and don't see anything with rust origin) or the constructor decorated with #[cfg_attr(target_os = "windows",
link_section= ".CRT$XCU"] is not linked properly (you can add aputs()and see if it's invoked or add a breakpoint in gdb)
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
In reply to Daniel P. BerrangéHi Daniel, I missed one of your questions and I just re-read the thread today,
Quoted 49 lines · Daniel P. Berrangé
Quoted 38 lines · Manos Pitsidianakis
Add options for Rust in meson_options.txt, meson.build, configure to prepare for adding Rust code in the followup commits.
rustis a reserved meson name, so we have to use an alternative.with_rustwas chosen.A
cargo_wrapper.pyscript is added that is heavily based on the work of Marc-André Lureau from 2021.https://patchew.org/QEMU/[email protected]/
Signed-off-by Marc-André Lureau <[email protected]>Signed-off-by Manos Pitsidianakis <[email protected]>--- MAINTAINERS | 5 + configure | 11 ++ meson.build | 11 ++ meson_options.txt | 4 + scripts/cargo_wrapper.py | 279 ++++++++++++++++++++++++++++++++++ scripts/meson-buildoptions.sh | 6 + 6 files changed, 316 insertions(+) create mode 100644 scripts/cargo_wrapper.pyconfigure +2
-0@@ -302,6 +302,9 @@ else objcc="${objcc-${cross_prefix}clang}" fi +with_rust_target_triple="" + ar="${AR-${cross_prefix}ar}" as="${AS-${cross_prefix}as}" ccas="${CCAS-$cc}"configure for opt do · line 769
Quoted 7 lines
configure +4
-0@@ -760,6 +763,12 @@ for opt do ;; --gdb=*) gdb_bin="$optarg" ;; + --enable-with-rust) with_rust=enabled + ;; + --disable-with-rust) with_rust=disabled + ;;--enable-with-XXX / --disable-with-XXX is pretty unsual naming.
Quoted 2 lines
Normally you'd see either --enable-XXX or --with-XXX and their corresponding --disable-XXX or --without-XXX.
True. As the commit message says, rust is a reserved meson feature name, so the auto-generated scripts/meson-buildoptions.sh has the following args:
--enable-with-rust
--disable-with-rust
I used the same in configure even though it's not autogenerated in order to keep the two synced. If there's a way to get around this I'd prefer it.
Re: [RFC PATCH v3 1/5] build-sys: Add rust feature option
Quoted 7 lines · Manos Pitsidianakis
Quoted 2 lines
Normally you'd see either --enable-XXX or --with-XXX and their corresponding --disable-XXX or --without-XXX.
True. As the commit message says,
rustis a reserved meson feature name, so the auto-generatedscripts/meson-buildoptions.shhas the following args:
Quoted 6 lines
--enable-with-rust
--disable-with-rustI used the same in
configureeven though it's not autogenerated in order to keep the two synced. If there's a way to get around this I'd prefer it.
With the patch I posted, --with-rust/--without-rust is handled entirely in configure, Meson gleans the result from the presence of RUST_TARGET_TRIPLE in config-host.mak.
Paolo
Re: [RFC PATCH v3 2/5] rust: add bindgen step as a meson dependency
In reply to Manos PitsidianakisQuoted 7 lines · Manos Pitsidianakis
Quoted 5 lines · Pierrick Bouvier
The ctor is not run, but, interesting point, there is the same problem on Linux :)
Can you please confirm with a clean build this work on your machine with this v3, and report exact configure and run commands you use?
Quoted 9 lines
On a clean checkout, I did exactly the following:
``` mkdir ./build && cd ./build ../configure --enable-system --enable-debug \ --target-list=aarch64-softmmu --enable-with-rust ninja qemu-system-aarch64 ```
Debug flag was the difference.
After looking with Manos, we identified an issue with release builds (unused symbols gets dropped, including device ctor), that will be solved in v4.
Quoted 41 lines
which selected the default cc/gcc on debian,
gcc (Debian 12.2.0-14) 12.2.0and ran QEMU with like this:
``` ./qemu-system-aarch64 -M virt \ -machine virtualization=true -machine virt,gic-version=3 \ -cpu max,pauth-impdef=on -smp 2 -m 4096 \ [.. drive and img arguments ..] \ -nographic ```Quoted 27 lines
Thanks,
PierrickQuoted 22 lines · Manos Pitsidianakis
Quoted 9 lines · Pierrick Bouvier
I've been able to build rust device on windows, with a few tweaks needed.
- specificy the target for libclang (used by bindgen), which targets MSVC by default (so different set of headers)
- additional headers (libclang searches its own header with a relative path instead of absolute)
- additional windows libs that must be linked in final executableQuoted 5 lines
However, even tough I can build the executable, I get this error:
$ ./build/qemu-system-aarch64 -M virt C:\w\qemu\build\qemu-system-aarch64.exe: unknown type 'x-pl011-rust'Any idea of what could be missing here?
Sounds like either the rust lib is not linked to the final binary (which you can confirm if you look at the included symbols and don't see anything with rust origin) or the constructor decorated with #[cfg_attr(target_os = "windows",
link_section= ".CRT$XCU"] is not linked properly (you can add aputs()and see if it's invoked or add a breakpoint in gdb)