[PATCH RESEND v9 3/9] configure, meson: detect Rust toolchain

Manos Pitsidianakis posted 9 patches 2 months, 4 weeks ago
There is a newer version of this series
[PATCH RESEND v9 3/9] configure, meson: detect Rust toolchain
Posted by Manos Pitsidianakis 2 months, 4 weeks ago
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).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 configure   | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 meson.build |  8 +++-----
 2 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/configure b/configure
index 019fcbd0ef7b07e7b0280b358099cae72c73aa98..9ef6005c557fc627c7c6c732b4c92ed1b934f474 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,9 @@ python=
 download="enabled"
 skip_meson=no
 use_containers="yes"
+# do not enable by default because cross compilation requires --rust-target-triple
+rust="disabled"
+rust_target_triple=""
 gdb_bin=$(command -v "gdb-multiarch" || command -v "gdb")
 gdb_arches=""
 
@@ -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)
@@ -636,6 +643,8 @@ for opt do
   ;;
   --objcc=*)
   ;;
+  --rustc=*)
+  ;;
   --make=*)
   ;;
   --install=*)
@@ -755,8 +764,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 +874,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 +885,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  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
@@ -1139,6 +1156,21 @@ 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
+  rust_target_triple=$rust_host_triple
+fi
+
+##########################################
 # functions to probe cross compilers
 
 container="no"
@@ -1604,6 +1636,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,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 "$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
@@ -1770,6 +1812,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 +1833,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 67eb4eda649d5f0566de2b75466b5a9d9ca87ab4..065739ccb7300f4f0d487602485802f9f68fb095 100644
--- a/meson.build
+++ b/meson.build
@@ -70,9 +70,6 @@ if host_os == 'darwin' and \
   all_languages += ['objc']
   objc = meson.get_compiler('objc')
 endif
-if get_option('rust').enabled() and meson.version().version_compare('<1.0.0')
-  error('Rust support requires Meson version >=1.0.0')
-endif
 have_rust = false
 if not get_option('rust').disabled() and add_languages('rust', required: get_option('rust'), native: false)
   rustc = meson.get_compiler('rust')
@@ -4307,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
Re: [PATCH RESEND v9 3/9] configure, meson: detect Rust toolchain
Posted by Daniel P. Berrangé 2 months, 3 weeks ago
On Wed, Aug 28, 2024 at 07:11:44AM +0300, Manos Pitsidianakis wrote:
> 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).
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  configure   | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
>  meson.build |  8 +++-----
>  2 files changed, 51 insertions(+), 7 deletions(-)
> 
> diff --git a/configure b/configure
> index 019fcbd0ef7b07e7b0280b358099cae72c73aa98..9ef6005c557fc627c7c6c732b4c92ed1b934f474 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,9 @@ python=
>  download="enabled"
>  skip_meson=no
>  use_containers="yes"
> +# do not enable by default because cross compilation requires --rust-target-triple
> +rust="disabled"
> +rust_target_triple=""
>  gdb_bin=$(command -v "gdb-multiarch" || command -v "gdb")
>  gdb_arches=""
>  
> @@ -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)
> @@ -636,6 +643,8 @@ for opt do
>    ;;
>    --objcc=*)
>    ;;
> +  --rustc=*)
> +  ;;
>    --make=*)
>    ;;
>    --install=*)
> @@ -755,8 +764,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 +874,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 +885,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  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
> @@ -1139,6 +1156,21 @@ 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
> +  rust_target_triple=$rust_host_triple
> +fi

Defaulting to the $rust_host_triple is incorrect when QEMU has been
told to build for a non-host target.

Either we need todo the right thing and auto-set rust target based
on QEMU target (preferred), or we need to make it a fatal error
when rust target is omitted & QEMU is building a non-host target.


With regards,
Daniel
-- 
|: 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: [PATCH RESEND v9 3/9] configure, meson: detect Rust toolchain
Posted by Paolo Bonzini 2 months, 2 weeks ago
On Wed, Aug 28, 2024 at 2:28 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
> > +if test "$rust" != disabled && test -z "$rust_target_triple"; then
> > +  rust_target_triple=$rust_host_triple
> > +fi
>
> Defaulting to the $rust_host_triple is incorrect when QEMU has been
> told to build for a non-host target.
>
> Either we need todo the right thing and auto-set rust target based
> on QEMU target (preferred), or we need to make it a fatal error
> when rust target is omitted & QEMU is building a non-host target.

I already have a patch to autodetect the right triple, but I didn't
have time to finish it (and send it to Manos) before disappearing on
vacation. I think it's okay to initially require --rust-target-triple=
for cross compilation.

Paolo
Re: [PATCH RESEND v9 3/9] configure, meson: detect Rust toolchain
Posted by Paolo Bonzini 2 months, 2 weeks ago
On Wed, Sep 4, 2024 at 1:03 PM Paolo Bonzini <pbonzini@redhat.com> wrote:
> On Wed, Aug 28, 2024 at 2:28 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
> > Defaulting to the $rust_host_triple is incorrect when QEMU has been
> > told to build for a non-host target.

BTW, this is why as of this patch rust is set to "disabled" by default.

> > Either we need todo the right thing and auto-set rust target based
> > on QEMU target (preferred), or we need to make it a fatal error
> > when rust target is omitted & QEMU is building a non-host target.
>
> I already have a patch to autodetect the right triple, but I didn't
> have time to finish it (and send it to Manos) before disappearing on
> vacation. I think it's okay to initially require --rust-target-triple=
> for cross compilation.

Done: https://paste.debian.net/1328408/

Paolo
Re: [PATCH RESEND v9 3/9] configure, meson: detect Rust toolchain
Posted by Alex Bennée 2 months, 3 weeks ago
Manos Pitsidianakis <manos.pitsidianakis@linaro.org> writes:

> 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).
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  configure   | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
>  meson.build |  8 +++-----
>  2 files changed, 51 insertions(+), 7 deletions(-)
>
> diff --git a/configure b/configure
> index 019fcbd0ef7b07e7b0280b358099cae72c73aa98..9ef6005c557fc627c7c6c732b4c92ed1b934f474 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,9 @@ python=
>  download="enabled"
>  skip_meson=no
>  use_containers="yes"
> +# do not enable by default because cross compilation requires --rust-target-triple
> +rust="disabled"
> +rust_target_triple=""
>  gdb_bin=$(command -v "gdb-multiarch" || command -v "gdb")
>  gdb_arches=""
>  
> @@ -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)
> @@ -636,6 +643,8 @@ for opt do
>    ;;
>    --objcc=*)
>    ;;
> +  --rustc=*)
> +  ;;
>    --make=*)
>    ;;
>    --install=*)
> @@ -755,8 +764,14 @@ for opt do
>    ;;
>    --container-engine=*) container_engine="$optarg"
>    ;;
> +  --rust-target-triple=*) rust_target_triple="$optarg"
> +  ;;

Could we not map from --cross-prefix to the target triple? Having to
specify both seems prone to failure.

>    --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 +874,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 +885,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  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
> @@ -1139,6 +1156,21 @@ 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
> +  rust_target_triple=$rust_host_triple
> +fi
> +
> +##########################################
>  # functions to probe cross compilers
>  
>  container="no"
> @@ -1604,6 +1636,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,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 "$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
> @@ -1770,6 +1812,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 +1833,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 67eb4eda649d5f0566de2b75466b5a9d9ca87ab4..065739ccb7300f4f0d487602485802f9f68fb095 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -70,9 +70,6 @@ if host_os == 'darwin' and \
>    all_languages += ['objc']
>    objc = meson.get_compiler('objc')
>  endif
> -if get_option('rust').enabled() and meson.version().version_compare('<1.0.0')
> -  error('Rust support requires Meson version >=1.0.0')
> -endif

Ahh this is probably needs merging into the patch 2.

>  have_rust = false
>  if not get_option('rust').disabled() and add_languages('rust', required: get_option('rust'), native: false)
>    rustc = meson.get_compiler('rust')
> @@ -4307,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'

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro