From: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
"Richard Henderson" <richard.henderson@linaro.org>,
"Gustavo Romero" <gustavo.romero@linaro.org>,
"Alex Bennée" <alex.bennee@linaro.org>,
"Peter Maydell" <peter.maydell@linaro.org>,
"Junjie Mao" <junjie.mao@intel.com>,
"Zhao Liu" <zhao1.liu@intel.com>, "John Snow" <jsnow@redhat.com>,
"Cleber Rosa" <crosa@redhat.com>,
"Beraldo Leal" <bleal@redhat.com>,
"Wainer dos Santos Moschetta" <wainersm@redhat.com>,
qemu-arm@nongnu.org
Subject: [PATCH v10 3/9] configure, meson: detect Rust toolchain
Date: Tue, 10 Sep 2024 15:35:09 +0300 [thread overview]
Message-ID: <20240910-rust-pl011-v10-3-85a89ee33c40@linaro.org> (raw)
In-Reply-To: <20240910-rust-pl011-v10-0-85a89ee33c40@linaro.org>
From: Paolo Bonzini <pbonzini@redhat.com>
Include the correct path and arguments to rustc in the native
and cross files (native compilation is needed for procedural
macros).
Based on the host architecture and OS, the compiler and optionally the argument
to --cpu, the Rust target triple can be detected automatically for either a
native or a cross compiler.
In general, it is only a matter of translating the architecture and OS, and
adding a machine to form the triple, but there are some special cases (e.g.
detecting soft vs. hard floating point on ARM) and some inconsistencies.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
configure | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
meson.build | 5 +-
2 files changed, 164 insertions(+), 4 deletions(-)
diff --git a/configure b/configure
index 019fcbd0ef7b07e7b0280b358099cae72c73aa98..4a011ff60bb8f351b3ee8d038a35a249dc297831 100755
--- a/configure
+++ b/configure
@@ -207,6 +207,8 @@ for opt do
;;
--objcc=*) objcc="$optarg"
;;
+ --rustc=*) RUSTC="$optarg"
+ ;;
--cpu=*) cpu="$optarg"
;;
--extra-cflags=*)
@@ -252,6 +254,8 @@ python=
download="enabled"
skip_meson=no
use_containers="yes"
+rust="auto"
+rust_target_triple=""
gdb_bin=$(command -v "gdb-multiarch" || command -v "gdb")
gdb_arches=""
@@ -310,6 +314,7 @@ objcopy="${OBJCOPY-${cross_prefix}objcopy}"
ld="${LD-${cross_prefix}ld}"
ranlib="${RANLIB-${cross_prefix}ranlib}"
nm="${NM-${cross_prefix}nm}"
+readelf="${READELF-${cross_prefix}readelf}"
strip="${STRIP-${cross_prefix}strip}"
widl="${WIDL-${cross_prefix}widl}"
windres="${WINDRES-${cross_prefix}windres}"
@@ -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}"
+rustc="${RUSTC-rustc}"
+
check_define() {
cat > $TMPC <<EOF
#if !defined($1)
@@ -425,6 +432,7 @@ fi
# Please keep it sorted and synchronized with meson.build's host_arch.
host_arch=
linux_arch=
+raw_cpu=$cpu
case "$cpu" in
aarch64)
host_arch=aarch64
@@ -636,6 +644,8 @@ for opt do
;;
--objcc=*)
;;
+ --rustc=*)
+ ;;
--make=*)
;;
--install=*)
@@ -755,8 +765,14 @@ for opt do
;;
--container-engine=*) container_engine="$optarg"
;;
+ --rust-target-triple=*) rust_target_triple="$optarg"
+ ;;
--gdb=*) gdb_bin="$optarg"
;;
+ --enable-rust) rust=enabled
+ ;;
+ --disable-rust) rust=disabled
+ ;;
# everything else has the same name in configure and meson
--*) meson_option_parse "$opt" "$optarg"
;;
@@ -859,6 +875,7 @@ Advanced options (experts only):
at build time [$host_cc]
--cxx=CXX use C++ compiler CXX [$cxx]
--objcc=OBJCC use Objective-C compiler OBJCC [$objcc]
+ --rustc=RUSTC use Rust compiler RUSTC [$rustc]
--extra-cflags=CFLAGS append extra C compiler flags CFLAGS
--extra-cxxflags=CXXFLAGS append extra C++ compiler flags CXXFLAGS
--extra-objcflags=OBJCFLAGS append extra Objective C compiler flags OBJCFLAGS
@@ -869,8 +886,9 @@ 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 compilation target for Rust code [autodetect]
+ --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
@@ -1139,6 +1157,132 @@ EOF
fi
##########################################
+# detect rust triple
+
+if test "$rust" != disabled && has "$rustc" && $rustc -vV > "${TMPDIR1}/${TMPB}.out"; then
+ rust_host_triple=$(sed -n 's/^host: //p' "${TMPDIR1}/${TMPB}.out")
+else
+ if test "$rust" = enabled; then
+ error_exit "could not execute rustc binary \"$rustc\""
+ fi
+ rust=disabled
+fi
+if test "$rust" != disabled && test -z "$rust_target_triple"; then
+ # arch and os generally matches between meson and rust
+ rust_arch=$host_arch
+ rust_os=$host_os
+ rust_machine=unknown
+ rust_osvariant=
+
+ # tweak rust_os if needed; also, machine and variant depend on the OS
+ android=no
+ case "$host_os" in
+ darwin)
+ # e.g. aarch64-apple-darwin
+ rust_machine=apple
+ ;;
+
+ linux)
+ # detect android/glibc/musl
+ if check_define __ANDROID__; then
+ rust_osvariant=android
+ android=yes
+ else
+ cat > $TMPC << EOF
+#define _GNU_SOURCE
+#include <features.h>
+#ifndef __USE_GNU
+error using musl
+#endif
+EOF
+ if compile_object; then
+ rust_osvariant=gnu
+ else
+ rust_osvariant=musl
+ fi
+ fi
+
+ case "$host_arch" in
+ arm)
+ # e.g. arm-unknown-linux-gnueabi, arm-unknown-linux-gnueabihf
+ write_c_skeleton
+ compile_object
+ if $READELF -A $TMPO | grep Tag_API_VFP_args: > /dev/null; then
+ rust_osvariant=${rust_osvariant}eabihf
+ else
+ rust_osvariant=${rust_osvariant}eabi
+ fi
+ ;;
+
+ mips64)
+ # e.g. mips64-unknown-linux-gnuabi64
+ rust_osvariant=${rust_osvariant}abi64
+ ;;
+ esac
+ ;;
+
+ netbsd)
+ # e.g. arm-unknown-netbsd-eabihf
+ test "$host_arch" = arm && rust_osvariant=eabihf
+ ;;
+
+ sunos)
+ rust_machine=pc
+ rust_os=solaris
+ ;;
+
+ windows)
+ # e.g. aarch64-pc-windows-gnullvm, x86_64-pc-windows-gnu (MSVC not supported)
+ rust_machine=pc
+ if test "$host_arch" = aarch64; then
+ rust_osvariant=gnullvm
+ else
+ rust_osvariant=gnu
+ fi
+ ;;
+ esac
+
+ # now tweak the architecture part, possibly based on pre-canonicalization --cpu
+ case "$host_arch" in
+ arm)
+ # preserve ISA version (armv7 etc.) from $raw_cpu if passed via --cpu
+ rust_arch=$raw_cpu
+ test "$rust_arch" = arm && test "$rust_os" != linux && rust_arch=armv7
+ ;;
+
+ mips|mips64)
+ # preserve ISA version (mipsisa64r6 etc.) and include endianness
+ rust_arch=${raw_cpu%el}
+ test "$bigendian" = no && rust_arch=${rust_arch}el
+ ;;
+
+ riscv32|riscv64)
+ # e.g. riscv64gc-unknown-linux-gnu, but riscv64-linux-android
+ test "$android" = no && rust_arch=${rust_arch}gc
+ ;;
+
+ sparc64)
+ if test "$rust_os" = solaris; then
+ rust_arch=sparcv9
+ rust_machine=sun
+ fi
+ ;;
+
+ x86_64)
+ # e.g. x86_64-unknown-linux-gnux32
+ test "$raw_cpu" = x32 && rust_osvariant=${rust_osvariant}x32
+ ;;
+ esac
+
+ if test "$android" = yes; then
+ # e.g. aarch64-linux-android
+ rust_target_triple=$rust_arch-$rust_os-$rust_osvariant
+ else
+ rust_target_triple=$rust_arch-$rust_machine-$rust_os${rust_osvariant+-$rust_osvariant}
+ fi
+fi
+
+##########################################
# functions to probe cross compilers
container="no"
@@ -1604,6 +1748,9 @@ if test "$container" != no; then
echo "RUNC=$runc" >> $config_host_mak
fi
echo "SUBDIRS=$subdirs" >> $config_host_mak
+if test "$rust" != disabled; then
+ 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
@@ -1735,12 +1882,20 @@ 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 "$rust" != disabled; then
+ if test "$rust_host_triple" != "$rust_target_triple"; then
+ echo "rust = [$(meson_quote $rustc --target "$rust_target_triple")]" >> $cross
+ else
+ echo "rust = [$(meson_quote $rustc)]" >> $cross
+ fi
+ fi
echo "ar = [$(meson_quote $ar)]" >> $cross
echo "dlltool = [$(meson_quote $dlltool)]" >> $cross
echo "nm = [$(meson_quote $nm)]" >> $cross
echo "pkgconfig = [$(meson_quote $pkg_config)]" >> $cross
echo "pkg-config = [$(meson_quote $pkg_config)]" >> $cross
echo "ranlib = [$(meson_quote $ranlib)]" >> $cross
+ echo "readelf = [$(meson_quote $readelf)]" >> $cross
if has $sdl2_config; then
echo "sdl2-config = [$(meson_quote $sdl2_config)]" >> $cross
fi
@@ -1770,6 +1925,9 @@ if test "$skip_meson" = no; then
echo "# Automatically generated by configure - do not modify" > $native
echo "[binaries]" >> $native
echo "c = [$(meson_quote $host_cc)]" >> $native
+ if test "$rust" != disabled; then
+ echo "rust = [$(meson_quote $rustc)]" >> $cross
+ fi
mv $native config-meson.native
meson_option_add --native-file
meson_option_add config-meson.native
@@ -1788,6 +1946,7 @@ if test "$skip_meson" = no; then
test "$pie" = no && meson_option_add -Db_pie=false
# QEMU options
+ test "$rust" != "auto" && meson_option_add "-Drust=$rust"
test "$cfi" != false && meson_option_add "-Dcfi=$cfi" "-Db_lto=$cfi"
test "$docs" != auto && meson_option_add "-Ddocs=$docs"
test -n "${LIB_FUZZING_ENGINE+xxx}" && meson_option_add "-Dfuzzing_engine=$LIB_FUZZING_ENGINE"
diff --git a/meson.build b/meson.build
index 47a9c04e322a2171daf188eb62b7b4ad941c9ea8..065739ccb7300f4f0d487602485802f9f68fb095 100644
--- a/meson.build
+++ b/meson.build
@@ -4304,8 +4304,9 @@ else
endif
summary_info += {'Rust support': have_rust}
if have_rust
- summary_info += {'rustc version': rustc.version()}
- summary_info += {'rustc': ' '.join(rustc.cmd_array())}
+ summary_info += {'rustc version': rustc.version()}
+ summary_info += {'rustc': ' '.join(rustc.cmd_array())}
+ summary_info += {'Rust target': config_host['RUST_TARGET_TRIPLE']}
endif
option_cflags = (get_option('debug') ? ['-g'] : [])
if get_option('optimization') != 'plain'
--
2.45.2
next prev parent reply other threads:[~2024-09-10 12:36 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-10 12:35 [PATCH v10 0/9] Add Rust build support, ARM PL011 device impl Manos Pitsidianakis
2024-09-10 12:35 ` [PATCH v10 1/9] Require meson version 1.5.0 Manos Pitsidianakis
2024-09-10 12:35 ` [PATCH v10 2/9] build-sys: Add rust feature option Manos Pitsidianakis
2024-09-10 12:35 ` Manos Pitsidianakis [this message]
2024-09-10 12:35 ` [PATCH v10 4/9] rust: add bindgen step as a meson dependency Manos Pitsidianakis
2024-09-10 12:35 ` [PATCH v10 5/9] .gitattributes: add Rust diff and merge attributes Manos Pitsidianakis
2024-09-10 12:35 ` [PATCH v10 6/9] meson.build: add HAVE_GLIB_WITH_ALIGNED_ALLOC flag Manos Pitsidianakis
2024-09-10 12:35 ` [PATCH v10 7/9] rust: add crate to expose bindings and interfaces Manos Pitsidianakis
2024-09-14 5:41 ` Junjie Mao
2024-09-30 9:24 ` Paolo Bonzini
2024-09-10 12:35 ` [PATCH v10 8/9] rust: add utility procedural macro crate Manos Pitsidianakis
2024-09-14 8:12 ` Junjie Mao
2024-09-10 12:35 ` [PATCH v10 9/9] rust: add PL011 device model Manos Pitsidianakis
2024-09-14 8:03 ` Junjie Mao
2024-09-14 11:36 ` Junjie Mao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240910-rust-pl011-v10-3-85a89ee33c40@linaro.org \
--to=manos.pitsidianakis@linaro.org \
--cc=alex.bennee@linaro.org \
--cc=berrange@redhat.com \
--cc=bleal@redhat.com \
--cc=crosa@redhat.com \
--cc=gustavo.romero@linaro.org \
--cc=jsnow@redhat.com \
--cc=junjie.mao@intel.com \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@linaro.org \
--cc=pierrick.bouvier@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=thuth@redhat.com \
--cc=wainersm@redhat.com \
--cc=zhao1.liu@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).