From nobody Tue Feb 10 10:03:31 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1602448759; cv=none; d=zohomail.com; s=zohoarc; b=G+kjn36dTKsVXCbTlHBUQlUVnaAFcqrM9wUirMM69z6gaNBMtotKBrQjkrGkZ/gfl4sjvTtzbdNpv+x+tsBdkLGN4DHWO+BPck2LdxNJd6VCuYwXnPjfaUT14C29lpBLhELOXspEniMc3vqABNFI4dRz1a9XpGGuAteQ2To7wi4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1602448759; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=95axpi0IvDfLnJZoXZHGJcKk2SIGdhXysMCYnQ2+U+Y=; b=V6wACoZCihp/U1WR7GAvUqWK1B1Upqyqj6JtzrFYN+9e8tHU1p1X8vRVtCYHqTlK6gNgVBy8YcmmhEzFtgMpKcreYWsPCooslEWn/Mruuzjbe4u9IEVHJrHIWWFaKbNIrzM7Ze8yJf+XMLiXlsl24g8i5Uafh1SYTTcuSkQUcKA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 160244875993023.292892855500327; Sun, 11 Oct 2020 13:39:19 -0700 (PDT) Received: from localhost ([::1]:60208 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kRi7i-0005U3-RN for importer@patchew.org; Sun, 11 Oct 2020 16:39:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57376) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kRi4g-0000rU-4C for qemu-devel@nongnu.org; Sun, 11 Oct 2020 16:36:10 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:48770) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kRi4e-0005SJ-1D for qemu-devel@nongnu.org; Sun, 11 Oct 2020 16:36:09 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-394-fYwgFLRaMW6Hr-fLflQp-g-1; Sun, 11 Oct 2020 16:36:01 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E02F32FD02 for ; Sun, 11 Oct 2020 20:36:00 +0000 (UTC) Received: from localhost (unknown [10.36.110.19]) by smtp.corp.redhat.com (Postfix) with ESMTP id 88D955DA30; Sun, 11 Oct 2020 20:35:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1602448567; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=95axpi0IvDfLnJZoXZHGJcKk2SIGdhXysMCYnQ2+U+Y=; b=J11BBcz6UKDT3DR/6gOyc9gu26LlelKHKaPTEMTyOx0a4sPTL/1/HEPzMHGaq3SZdvC9Vg F7DvV/7/AsmZpE1+61+CqWjEJFSk3It2hwn+TeJOJ1x9Rus8HD8qSovBGl8889lTBIEQyf kWr6yvBKl9F6DUDmPr1MJ5aWzP8NRGU= X-MC-Unique: fYwgFLRaMW6Hr-fLflQp-g-1 From: marcandre.lureau@redhat.com To: qemu-devel@nongnu.org Subject: [PoCv2 04/15] build-sys: add a cargo-wrapper script Date: Mon, 12 Oct 2020 00:35:02 +0400 Message-Id: <20201011203513.1621355-5-marcandre.lureau@redhat.com> In-Reply-To: <20201011203513.1621355-1-marcandre.lureau@redhat.com> References: <20201011203513.1621355-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=marcandre.lureau@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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=63.128.21.124; envelope-from=marcandre.lureau@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/11 16:23:51 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -30 X-Spam_score: -3.1 X-Spam_bar: --- X-Spam_report: (-3.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=-1, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, berrange@redhat.com, armbru@redhat.com, stefanha@redhat.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) From: Marc-Andr=C3=A9 Lureau Introduce a script to help calling cargo from Rust. Cargo is the most convenient way to build Rust code, with various crates, and has many features that meson lacks in general for Rust. Trying to convert projects to meson automatically is an option I considered (see for ex https://github.com/badboy/bygge for ninja conversion), but the complexity of the task and the need of build.rs mechanism in various crates makes this endeavour out of scope at this point. This script will help to invoke cargo in different ways. For now, it supports building static libraries. It sets up a common environment, run the compiler and grep its output for the linker flags. Finally it copies the built library to the expected meson output directory, and create a file with the linker arguments. Signed-off-by: Marc-Andr=C3=A9 Lureau --- meson.build | 1 + scripts/cargo_wrapper.py | 101 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 scripts/cargo_wrapper.py diff --git a/meson.build b/meson.build index d8526dc999..c30bb290c5 100644 --- a/meson.build +++ b/meson.build @@ -75,6 +75,7 @@ endif =20 with_rust =3D 'CONFIG_WITH_RUST' in config_host cargo =3D find_program('cargo', required: with_rust) +cargo_wrapper =3D find_program('scripts/cargo_wrapper.py') =20 if with_rust rs_target_triple =3D config_host['CONFIG_WITH_RUST_TARGET'] diff --git a/scripts/cargo_wrapper.py b/scripts/cargo_wrapper.py new file mode 100644 index 0000000000..164fad5123 --- /dev/null +++ b/scripts/cargo_wrapper.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 +# Copyright (c) 2020 Red Hat, Inc. +# +# Author: +# 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 glob +import os +import os.path +import re +import subprocess +import sys +from typing import List + + +def get_cargo_target_dir(args: argparse.Namespace) -> str: + # avoid conflict with qemu "target" directory + return os.path.join(args.build_dir, "rs-target") + + +def get_manifest_path(args: argparse.Namespace) -> str: + return os.path.join(args.src_dir, "Cargo.toml") + + +def build_lib(args: argparse.Namespace) -> None: + target_dir =3D get_cargo_target_dir(args) + manifest_path =3D get_manifest_path(args) + # let's pretend it's an INI file to avoid extra dependency + config =3D configparser.ConfigParser() + config.read(manifest_path) + package_name =3D config["package"]["name"].strip('"') + liba =3D os.path.join( + target_dir, args.target_triple, args.build_type, "lib" + package_n= ame + ".a" + ) + libargs =3D os.path.join(args.build_dir, "lib" + package_name + ".args= ") + + env =3D {} + env["MESON_CURRENT_BUILD_DIR"] =3D args.build_dir + env["MESON_BUILD_ROOT"] =3D args.build_root + env["WINAPI_NO_BUNDLED_LIBRARIES"] =3D "1" + cargo_cmd =3D [ + "cargo", + "rustc", + "--target-dir", + target_dir, + "--manifest-path", + manifest_path, + ] + if args.target_triple: + cargo_cmd +=3D ["--target", args.target_triple] + if args.build_type =3D=3D "release": + cargo_cmd +=3D ["--release"] + cargo_cmd +=3D ["--", "--print", "native-static-libs"] + cargo_cmd +=3D args.EXTRA + try: + out =3D subprocess.check_output( + cargo_cmd, + env=3Ddict(os.environ, **env), + stderr=3Dsubprocess.STDOUT, + universal_newlines=3DTrue, + ) + native_static_libs =3D re.search(r"native-static-libs:(.*)", out) + link_args =3D native_static_libs.group(1) + with open(libargs, "w") as f: + print(link_args, file=3Df) + + distutils.file_util.copy_file(liba, args.build_dir, update=3DTrue) + except subprocess.CalledProcessError as e: + print( + "Environment: " + " ".join(["{}=3D{}".format(k, v) for k, v in= env.items()]) + ) + print("Command: " + " ".join(cargo_cmd)) + print(e.output) + sys.exit(1) + + +def main() -> None: + parser =3D argparse.ArgumentParser() + parser.add_argument("command") + parser.add_argument("build_dir") + parser.add_argument("src_dir") + parser.add_argument("build_root") + parser.add_argument("build_type") + parser.add_argument("target_triple") + parser.add_argument("EXTRA", nargs=3D"*") + args =3D parser.parse_args() + + if args.command =3D=3D "build-lib": + build_lib(args) + else: + raise argparse.ArgumentTypeError("Unknown command: %s" % args.comm= and) + + +if __name__ =3D=3D "__main__": + main() --=20 2.28.0