rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Miguel Ojeda <ojeda@kernel.org>
To: Miguel Ojeda <ojeda@kernel.org>,
	Wedson Almeida Filho <wedsonaf@gmail.com>,
	Alex Gaynor <alex.gaynor@gmail.com>
Cc: "Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@samsung.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	patches@lists.linux.dev, "Finn Behrens" <me@kloenk.dev>,
	"Masahiro Yamada" <masahiroy@kernel.org>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Nicolas Schier" <nicolas@fjasle.eu>,
	linux-kbuild@vger.kernel.org
Subject: [PATCH v2 11/13] kbuild: rust: add `rustc-version` support
Date: Tue,  9 Jul 2024 18:06:06 +0200	[thread overview]
Message-ID: <20240709160615.998336-12-ojeda@kernel.org> (raw)
In-Reply-To: <20240709160615.998336-1-ojeda@kernel.org>

Now that we are starting to support several Rust versions, introduce
`rustc-version` support, mimicking the C side:

  - `scripts/rustc-version.sh`, that mimics the other version scripts
     (with one more digit, e.g. Rust 1.79.0 is 107900).

  - `rustc-{info,name,version}` Kbuild macros.

  - `CONFIG_RUSTC_VERSION` Kconfig symbol that calls `rustc-version`.

  - `rustc-min-version` Kbuild macro that uses `CONFIG_RUSTC_VERSION`.

With these, we can easily support flags conditionally depending on
`rustc`'s version -- a user comes in the next patch.

Another user will be the `-Ctarget-feature=+reserve-x18`/`-Zfixed-x18`
arm64 flags [1].

Link: https://lore.kernel.org/rust-for-linux/20240305-shadow-call-stack-v2-1-c7b4a3f4d616@google.com/ [1]
Reviewed-by: Finn Behrens <me@kloenk.dev>
Tested-by: Benno Lossin <benno.lossin@proton.me>
Tested-by: Andreas Hindborg <a.hindborg@samsung.com>
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
v2:
  - Fix comment from "5 or 6-digit" to "6 to 7-digit".

 init/Kconfig              |  6 +++++
 scripts/Kconfig.include   |  6 +++++
 scripts/Makefile.compiler |  4 +++
 scripts/rustc-version.sh  | 52 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 68 insertions(+)
 create mode 100755 scripts/rustc-version.sh

diff --git a/init/Kconfig b/init/Kconfig
index 94e20d3b99d4..7d344f248785 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1920,6 +1920,12 @@ config RUST

 	  If unsure, say N.

+config RUSTC_VERSION
+	int
+	depends on RUST
+	default $(rustc-version)
+	default 0
+
 config RUSTC_VERSION_TEXT
 	string
 	depends on RUST
diff --git a/scripts/Kconfig.include b/scripts/Kconfig.include
index 3ee8ecfb8c04..82ab889725db 100644
--- a/scripts/Kconfig.include
+++ b/scripts/Kconfig.include
@@ -45,6 +45,12 @@ $(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this C compiler is not
 cc-name := $(shell,set -- $(cc-info) && echo $1)
 cc-version := $(shell,set -- $(cc-info) && echo $2)

+# Get the Rust compiler name, version, and error out if it is not supported.
+rustc-info := $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC))
+$(error-if,$(success,test -z "$(rustc-info)"),Sorry$(comma) this Rust compiler is not supported.)
+rustc-name := $(shell,set -- $(rustc-info) && echo $1)
+rustc-version := $(shell,set -- $(rustc-info) && echo $2)
+
 # Get the assembler name, version, and error out if it is not supported.
 as-info := $(shell,$(srctree)/scripts/as-version.sh $(CC) $(CLANG_FLAGS))
 $(error-if,$(success,test -z "$(as-info)"),Sorry$(comma) this assembler is not supported.)
diff --git a/scripts/Makefile.compiler b/scripts/Makefile.compiler
index 92be0c9a13ee..17eaa085b59c 100644
--- a/scripts/Makefile.compiler
+++ b/scripts/Makefile.compiler
@@ -69,6 +69,10 @@ gcc-min-version = $(call test-ge, $(CONFIG_GCC_VERSION), $1)
 # Usage: cflags-$(call clang-min-version, 110000) += -foo
 clang-min-version = $(call test-ge, $(CONFIG_CLANG_VERSION), $1)

+# rustc-min-version
+# Usage: rustflags-$(call rustc-min-version, 107900) += -foo
+rustc-min-version = $(call test-ge, $(CONFIG_RUSTC_VERSION), $1)
+
 # ld-option
 # Usage: KBUILD_LDFLAGS += $(call ld-option, -X, -Y)
 ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3))
diff --git a/scripts/rustc-version.sh b/scripts/rustc-version.sh
new file mode 100755
index 000000000000..7b27ab5d4ecd
--- /dev/null
+++ b/scripts/rustc-version.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+#
+# Print the Rust compiler name and its version in a 6 or 7-digit form.
+# Also, perform the minimum version check.
+
+set -e
+
+# Convert the version string x.y.z to a canonical up-to-7-digits form.
+#
+# Note that this function uses one more digit (compared to other
+# instances in other version scripts) to give a bit more space to
+# `rustc` since it will reach 1.100.0 in late 2026.
+get_canonical_version()
+{
+	IFS=.
+	set -- $1
+	echo $((100000 * $1 + 100 * $2 + $3))
+}
+
+orig_args="$@"
+
+set -- $("$@" --version)
+
+name=$1
+
+min_tool_version=$(dirname $0)/min-tool-version.sh
+
+case "$name" in
+rustc)
+	version=$2
+	min_version=$($min_tool_version rustc)
+	;;
+*)
+	echo "$orig_args: unknown Rust compiler" >&2
+	exit 1
+	;;
+esac
+
+rustcversion=$(get_canonical_version $version)
+min_rustcversion=$(get_canonical_version $min_version)
+
+if [ "$rustcversion" -lt "$min_rustcversion" ]; then
+	echo >&2 "***"
+	echo >&2 "*** Rust compiler is too old."
+	echo >&2 "***   Your $name version:    $version"
+	echo >&2 "***   Minimum $name version: $min_version"
+	echo >&2 "***"
+	exit 1
+fi
+
+echo $name $rustcversion
--
2.45.2

  parent reply	other threads:[~2024-07-09 16:07 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-09 16:05 [PATCH v2 00/13] Support several Rust toolchain versions Miguel Ojeda
2024-07-09 16:05 ` [PATCH v2 01/13] rust: macros: indent list item in `paste!`'s docs Miguel Ojeda
2024-07-09 16:05 ` [PATCH v2 02/13] rust: init: simplify from `map_err` to `inspect_err` Miguel Ojeda
2024-07-09 16:05 ` [PATCH v2 03/13] rust: allow `dead_code` for never constructed bindings Miguel Ojeda
2024-07-09 16:05 ` [PATCH v2 04/13] rust: relax most deny-level lints to warnings Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 05/13] rust: simplify Clippy warning flags set Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 06/13] rust: start supporting several compiler versions Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 07/13] rust: avoid assuming a particular `bindgen` build Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 08/13] rust: work around `bindgen` 0.69.0 issue Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 09/13] rust: start supporting several `bindgen` versions Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 10/13] rust: warn about `bindgen` versions 0.66.0 and 0.66.1 Miguel Ojeda
2024-07-09 16:06 ` Miguel Ojeda [this message]
2024-07-09 17:26   ` [PATCH v2 11/13] kbuild: rust: add `rustc-version` support Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 12/13] rust: support the new `-Zub-checks` flag Miguel Ojeda
2024-07-09 16:06 ` [PATCH v2 13/13] docs: rust: quick-start: add section on Linux distributions Miguel Ojeda
2024-07-10  9:04 ` [PATCH v2 00/13] Support several Rust toolchain versions Miguel Ojeda

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=20240709160615.998336-12-ojeda@kernel.org \
    --to=ojeda@kernel.org \
    --cc=a.hindborg@samsung.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=me@kloenk.dev \
    --cc=nathan@kernel.org \
    --cc=nicolas@fjasle.eu \
    --cc=patches@lists.linux.dev \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.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).