From nobody Fri Oct 25 19:23:34 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1718102099; cv=none; d=zohomail.com; s=zohoarc; b=grgKls7HOW7n8BbYs/nbiuLNVW5nsOY+3233biyMk4xqGVkWjgp843XeGsn4fKATtBEfhVWAAhYsK8YYis1vV0nkzJhACMnqbT2dI1U7oTH47392c/rdSZGFgpF/RQlMcE8A2cBCrN2ILHqWPToDMTUZXQ78QHbUBiRPt49wHDw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1718102099; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=MXMzufTDFqExgudi0feoRnQwFcdrUAhlrudwhggWGTg=; b=U0YDxo8ZURqfJ9ZojYMysR047ZHsmZPgO8cJGumUc+/xbzH5Wt5MTXRx1LHAYLB8BP+TppI5hVl+6x+dtGdWrp6lV9rKjeS1I9rPHub4G8FoEHhCPq6wJ0ZKZFIKAeAWQhvd4Ro2ZsX4HZ6HG3HwAqQN+gj7lHUa1Q7fUuO0re8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1718102099128961.8843668804107; Tue, 11 Jun 2024 03:34:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sGypW-0003M2-5H; Tue, 11 Jun 2024 06:34:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sGypS-0003LC-BL for qemu-devel@nongnu.org; Tue, 11 Jun 2024 06:34:14 -0400 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sGypP-0002l1-Ls for qemu-devel@nongnu.org; Tue, 11 Jun 2024 06:34:13 -0400 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-42189d3c7efso20948155e9.2 for ; Tue, 11 Jun 2024 03:34:11 -0700 (PDT) Received: from localhost.localdomain (adsl-33.37.6.162.tellas.gr. [37.6.162.33]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42232e71865sm30878795e9.46.2024.06.11.03.34.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jun 2024 03:34:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718102049; x=1718706849; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MXMzufTDFqExgudi0feoRnQwFcdrUAhlrudwhggWGTg=; b=v6xHMlCvquO4RRm4Bpzuhkfao6XmPDF37dKlm4dL8PQy6TfWRn0pz4j2h/zSvHDza7 vdcoRpWpFNKWbbY2hmym2FfseadjbNKybHnRraeujpcCQo7tKEPYGvVN7hLIRSow2ZJt +8qkD1zqxJ39dssafy1MpA8cwj193ptBlK3JI/U8YHLQwoXkqaaC6A0D9bo4xi0zCCtP LwdPmveFx5JuF7GyHSlzt7IdpYqVwyo6uTUqFK8FBsb3R48msiad7aa1XcYu++wAQsPz w24fVe6Y3rdYeSO2APu8ZrIiljaRvEeL+xYVq1YAMp43Djiqnn+2XH/g2d9I/CoOmym4 PKAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718102049; x=1718706849; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MXMzufTDFqExgudi0feoRnQwFcdrUAhlrudwhggWGTg=; b=o+ImFMC66ChUxpK8SfB/f4uxa1QJM2PysRJOCKvMUlIuyMwoKvqSNy1JgNzQ+tEEwr bqle/WJC0GjZIz9qLAKT1JrUbhqdMNWIEnK04MjTL1L0twGXxVAYEOV+8AmZQb/cTNg4 9w/b6tzKWPAKl40D6x3q3r4BOWogDAn3eg5n8unIr28R9VzINh/FGJNfFw2RYIBrOdoD oCc0x/i1p3o2vb1UmBedzoTPBupuO7fUIldhs1/4cn1QBtXKtlS85cd06J9CdgiYkMhE luZjm4SekOnH1Gu+VFn1+jO/L3xOhGpKRktPY/j8LH81Y9Bw8wpV/pcLlBFtaBiW6WI4 cSdQ== X-Gm-Message-State: AOJu0YwHGGoVgdgIvhF45iLki+KD7RavQtrrm3XKZ3jyMSJwnMW6sULw Jx/T4dSgTmC2TBI8m2AVA37E3vzCuZXgcUZ+k7BLnkTcEYOzhH/+m0hbmSesdJRd7r6bhWhzskU sIWE= X-Google-Smtp-Source: AGHT+IHv0uiaQs7XiGq4cOXuqPiOMFxb9+M553N+iXAXbylwPF6NHvVn9odv+ZedSDnVsKcteCEJVw== X-Received: by 2002:a05:600c:450c:b0:421:671b:586c with SMTP id 5b1f17b1804b1-421671b5c03mr114346815e9.9.1718102049418; Tue, 11 Jun 2024 03:34:09 -0700 (PDT) From: Manos Pitsidianakis To: qemu-devel@nongnu.org Cc: Stefan Hajnoczi , Mads Ynddal , Paolo Bonzini , Peter Maydell , =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Thomas Huth , Markus Armbruster , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Zhao Liu , Gustavo Romero , Pierrick Bouvier , John Snow , Cleber Rosa Subject: [RFC PATCH v2 1/5] build-sys: Add rust feature option Date: Tue, 11 Jun 2024 13:33:30 +0300 Message-ID: <89b7d4ae6c03cf16ce9fcc95d0406c20627fd444.1718101832.git.manos.pitsidianakis@linaro.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::330; envelope-from=manos.pitsidianakis@linaro.org; helo=mail-wm1-x330.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1718102101179100003 Add 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=C3=A9 Lureau from 2021. https://patchew.org/QEMU/20210907121943.3498701-1-marcandre.lureau@redhat.c= om/ Signed-off-by: Marc-Andr=C3=A9 Lureau Signed-off-by: Manos Pitsidianakis --- .gitignore | 2 + MAINTAINERS | 5 + configure | 12 ++ meson.build | 11 ++ meson_options.txt | 4 + scripts/cargo_wrapper.py | 211 ++++++++++++++++++++++++++++++++++ scripts/meson-buildoptions.sh | 6 + 7 files changed, 251 insertions(+) create mode 100644 scripts/cargo_wrapper.py diff --git a/.gitignore b/.gitignore index 61fa39967b..f42b0d937e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ /build/ /.cache/ /.vscode/ +/target/ +rust/**/target *.pyc .sdk .stgit-* diff --git a/MAINTAINERS b/MAINTAINERS index 1b79767d61..431010ddbf 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4223,6 +4223,11 @@ F: docs/sphinx/ F: docs/_templates/ F: docs/devel/docs.rst =20 +Rust build system integration +M: Manos Pitsidianakis +S: Maintained +F: scripts/cargo_wrapper.py + Miscellaneous ------------- Performance Tools and Tests diff --git a/configure b/configure index 38ee257701..6baa63b443 100755 --- a/configure +++ b/configure @@ -302,6 +302,9 @@ else objcc=3D"${objcc-${cross_prefix}clang}" fi =20 +with_rust=3D"auto" +with_rust_target_triple=3D"" + ar=3D"${AR-${cross_prefix}ar}" as=3D"${AS-${cross_prefix}as}" ccas=3D"${CCAS-$cc}" @@ -760,6 +763,12 @@ for opt do ;; --gdb=3D*) gdb_bin=3D"$optarg" ;; + --enable-with-rust) with_rust=3Denabled + ;; + --disable-with-rust) with_rust=3Ddisabled + ;; + --with-rust-target-triple=3D*) with_rust_target_triple=3D"$optarg" + ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;; @@ -1796,6 +1805,9 @@ if test "$skip_meson" =3D no; then test -n "${LIB_FUZZING_ENGINE+xxx}" && meson_option_add "-Dfuzzing_engin= e=3D$LIB_FUZZING_ENGINE" test "$plugins" =3D yes && meson_option_add "-Dplugins=3Dtrue" test "$tcg" !=3D enabled && meson_option_add "-Dtcg=3D$tcg" + test "$with_rust" !=3D enabled && meson_option_add "-Dwith_rust=3D$with_= rust" + test "$with_rust" !=3D enabled && meson_option_add "-Dwith_rust=3D$with_= rust" + test "$with_rust_target_triple" !=3D "" && meson_option_add "-Dwith_rust= _target_triple=3D$with_rust_target_triple" run_meson() { NINJA=3D$ninja $meson setup "$@" "$PWD" "$source_path" } diff --git a/meson.build b/meson.build index a9de71d450..3533889852 100644 --- a/meson.build +++ b/meson.build @@ -290,6 +290,12 @@ foreach lang : all_languages endif endforeach =20 +cargo =3D not_found +if get_option('with_rust').allowed() + cargo =3D find_program('cargo', required: get_option('with_rust')) +endif +with_rust =3D 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 =20 config_host_data =3D configuration_data() =20 +config_host_data.set('CONFIG_WITH_RUST', with_rust) audio_drivers_selected =3D [] if have_system audio_drivers_available =3D { @@ -4190,6 +4197,10 @@ if 'objc' in all_languages else summary_info +=3D {'Objective-C compiler': false} endif +summary_info +=3D {'Rust support': with_rust} +if with_rust and get_option('with_rust_target_triple') !=3D '' + summary_info +=3D {'Rust target': get_option('with_rust_target_tripl= e')} +endif option_cflags =3D (get_option('debug') ? ['-g'] : []) if get_option('optimization') !=3D 'plain' option_cflags +=3D ['-O' + get_option('optimization')] diff --git a/meson_options.txt b/meson_options.txt index 4c1583eb40..223491b731 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -366,3 +366,7 @@ option('qemu_ga_version', type: 'string', value: '', =20 option('hexagon_idef_parser', type : 'boolean', value : true, description: 'use idef-parser to automatically generate TCG code fo= r 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') diff --git a/scripts/cargo_wrapper.py b/scripts/cargo_wrapper.py new file mode 100644 index 0000000000..d338effdaa --- /dev/null +++ b/scripts/cargo_wrapper.py @@ -0,0 +1,211 @@ +#!/usr/bin/env python3 +# Copyright (c) 2020 Red Hat, Inc. +# Copyright (c) 2023 Linaro Ltd. +# +# Authors: +# Manos Pitsidianakis +# Marc-Andr=C3=A9 Lureau +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. + +import argparse +import configparser +import distutils.file_util +import json +import logging +import os +import os.path +import re +import subprocess +import sys +import pathlib +import shutil +import tomllib + +from pathlib import Path +from typing import Any, Dict, List, Tuple + +RUST_TARGET_TRIPLES =3D ( + "aarch64-unknown-linux-gnu", + "x86_64-unknown-linux-gnu", + "x86_64-apple-darwin", + "aarch64-apple-darwin", +) + + +def cfg_name(name: str) -> str: + if ( + name.startswith("CONFIG_") + or name.startswith("TARGET_") + or name.startswith("HAVE_") + ): + return name + return "" + + +def generate_cfg_flags(header: str) -> List[str]: + with open(header, encoding=3D"utf-8") as cfg: + config =3D [l.split()[1:] for l in cfg if l.startswith("#define")] + + cfg_list =3D [] + for cfg in config: + name =3D cfg_name(cfg[0]) + if not name: + continue + if len(cfg) >=3D 2 and cfg[1] !=3D "1": + continue + cfg_list.append("--cfg") + cfg_list.append(name) + return cfg_list + + +def cargo_target_dir(args: argparse.Namespace) -> pathlib.Path: + return args.meson_build_dir + + +def manifest_path(args: argparse.Namespace) -> pathlib.Path: + return args.crate_dir / "Cargo.toml" + + +def get_cargo_rustc(args: argparse.Namespace) -> tuple[Dict[str, Any], Lis= t[str]]: + # See https://doc.rust-lang.org/cargo/reference/environment-variables.= html + # Item `CARGO_ENCODED_RUSTFLAGS =E2=80=94 A list of custom flags separ= ated by + # 0x1f (ASCII Unit Separator) to pass to all compiler invocations that= Cargo + # performs` + cfg =3D chr(0x1F).join( + [c for h in args.config_headers for c in generate_cfg_flags(h)] + ) + target_dir =3D cargo_target_dir(args) + cargo_path =3D manifest_path(args) + + cargo_cmd =3D [ + "cargo", + "build", + "--target-dir", + str(target_dir), + "--manifest-path", + str(cargo_path), + ] + if args.target_triple: + cargo_cmd +=3D ["--target", args.target_triple] + if args.profile =3D=3D "release": + cargo_cmd +=3D ["--release"] + + env =3D os.environ + env["CARGO_ENCODED_RUSTFLAGS"] =3D cfg + + return (env, cargo_cmd) + + +def run_cargo(env: Dict[str, Any], cargo_cmd: List[str]) -> str: + envlog =3D " ".join(["{}=3D{}".format(k, v) for k, v in env.items()]) + cmdlog =3D " ".join(cargo_cmd) + logging.debug("Running %s %s", envlog, cmdlog) + try: + out =3D subprocess.check_output( + cargo_cmd, + env=3Ddict(os.environ, **env), + stderr=3Dsubprocess.STDOUT, + universal_newlines=3DTrue, + ) + except subprocess.CalledProcessError as err: + print("Environment: " + envlog) + print("Command: " + cmdlog) + print(err.output) + sys.exit(1) + + return out + + +def build_lib(args: argparse.Namespace) -> None: + logging.debug("build-lib") + target_dir =3D cargo_target_dir(args) + cargo_toml_path =3D manifest_path(args) + + with open(cargo_toml_path, "rb") as f: + config =3D tomllib.load(f) + + package_name =3D config["package"]["name"].strip('"').replace("-", "_") + + liba_filename =3D "lib" + package_name + ".a" + liba =3D target_dir / args.target_triple / args.profile / liba_filename + + env, cargo_cmd =3D get_cargo_rustc(args) + out =3D run_cargo(env, cargo_cmd) + logging.debug("cp %s %s", liba, args.outdir) + shutil.copy2(liba, args.outdir) + + +def main() -> None: + parser =3D argparse.ArgumentParser() + parser.add_argument("-v", "--verbose", action=3D"store_true") + parser.add_argument( + "--color", + metavar=3D"WHEN", + choices=3D["auto", "always", "never"], + default=3D"auto", + help=3D"Coloring: auto, always, never", + ) + parser.add_argument( + "--config-headers", + metavar=3D"CONFIG_HEADER", + action=3D"append", + dest=3D"config_headers", + required=3DFalse, + default=3D[], + ) + parser.add_argument( + "--meson-build-dir", + metavar=3D"BUILD_DIR", + help=3D"meson.current_build_dir()", + type=3Dpathlib.Path, + dest=3D"meson_build_dir", + required=3DTrue, + ) + parser.add_argument( + "--meson-source-dir", + metavar=3D"SOURCE_DIR", + help=3D"meson.current_source_dir()", + type=3Dpathlib.Path, + dest=3D"meson_source_dir", + required=3DTrue, + ) + parser.add_argument( + "--crate-dir", + metavar=3D"CRATE_DIR", + type=3Dpathlib.Path, + dest=3D"crate_dir", + help=3D"Absolute path that contains the manifest file of the crate= to compile", + required=3DTrue, + ) + parser.add_argument( + "--outdir", + metavar=3D"OUTDIR", + type=3Dpathlib.Path, + dest=3D"outdir", + help=3D"Path to copy compiled artifacts to for Meson to use.", + required=3DTrue, + ) + parser.add_argument( + "--profile", type=3Dstr, choices=3D["release", "debug"], required= =3DTrue + ) + parser.add_argument( + "--target-triple", type=3Dstr, choices=3DRUST_TARGET_TRIPLES, requ= ired=3DTrue + ) + + subparsers =3D parser.add_subparsers() + + buildlib =3D subparsers.add_parser("build-lib") + buildlib.set_defaults(func=3Dbuild_lib) + + args =3D parser.parse_args() + if args.verbose: + logging.basicConfig(level=3Dlogging.DEBUG) + logging.debug("args: %s", args) + + args.func(args) + + +if __name__ =3D=3D "__main__": + main() diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh index 6ce5a8b72a..fdcf17129e 100644 --- a/scripts/meson-buildoptions.sh +++ b/scripts/meson-buildoptions.sh @@ -79,6 +79,8 @@ meson_options_help() { printf "%s\n" ' auto/sigaltstack/ucontext/wind= ows)' printf "%s\n" ' --with-pkgversion=3DVALUE use specified string as sub-= version of the' printf "%s\n" ' package' + printf "%s\n" ' --with-rust-target-triple=3DVALUE' + printf "%s\n" ' Specify Rust host target tripl= e' printf "%s\n" ' --with-suffix=3DVALUE Suffix for QEMU data/modules= /config directories' printf "%s\n" ' (can be empty) [qemu]' printf "%s\n" ' --with-trace-file=3DVALUE 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=3Dfalse ;; --enable-whpx) printf "%s" -Dwhpx=3Denabled ;; --disable-whpx) printf "%s" -Dwhpx=3Ddisabled ;; + --enable-with-rust) printf "%s" -Dwith_rust=3Denabled ;; + --disable-with-rust) printf "%s" -Dwith_rust=3Ddisabled ;; + --with-rust-target-triple=3D*) quote_sh "-Dwith_rust_target_triple=3D$= 2" ;; --enable-xen) printf "%s" -Dxen=3Denabled ;; --disable-xen) printf "%s" -Dxen=3Ddisabled ;; --enable-xen-pci-passthrough) printf "%s" -Dxen_pci_passthrough=3Denab= led ;; --=20 =CE=B3=CE=B1=E1=BF=96=CE=B1 =CF=80=CF=85=CF=81=CE=AF =CE=BC=CE=B9=CF=87=CE= =B8=CE=AE=CF=84=CF=89