diff --git a/optee-utee/Cargo.toml b/optee-utee/Cargo.toml index 56bcfffe..6c510ddc 100644 --- a/optee-utee/Cargo.toml +++ b/optee-utee/Cargo.toml @@ -42,6 +42,9 @@ serde_json = { version = "1.0.133" } optee-utee-sys = { version = "0.7.0", path = "optee-utee-sys", features = ["no_link"] } optee-utee-mock = { version = "0.7.0", path = "optee-utee-mock" } +[build-dependencies] +version_check = "0.9.5" + [features] default = [] std = ["optee-utee-sys/std", "optee-utee-macros/std"] diff --git a/optee-utee/build.rs b/optee-utee/build.rs new file mode 100644 index 00000000..f3ddae86 --- /dev/null +++ b/optee-utee/build.rs @@ -0,0 +1,40 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use std::fs; + +/// Outputs unstable #[feature = "foo"] iff the rustc version is older than $version +macro_rules! maybe_feat { + ($out:expr, $feat:literal, $version:literal) => {{ + let filename = $out.join(concat!($feat, ".rs")); + + let s = if version_check::is_min_version($version).unwrap_or(false) { + String::new() + } else { + format!("#![feature({})]\n", $feat) + }; + fs::write(filename, s).expect("failed to write to {filename:?}"); + }}; +} + +fn main() { + let out = std::path::PathBuf::from(std::env::var("OUT_DIR").expect("infallible")); + + // The custom patched std version is currently on 1.80. When we upgrade, we should + // bump the MSRV accordingly and remove any of these features that are stablized. + maybe_feat!(out, "error_in_core", "1.81.0"); +} diff --git a/optee-utee/src/lib.rs b/optee-utee/src/lib.rs index 1622a9ae..2ca2bfb5 100644 --- a/optee-utee/src/lib.rs +++ b/optee-utee/src/lib.rs @@ -16,7 +16,20 @@ // under the License. #![cfg_attr(not(feature = "std"), no_std)] -#![cfg_attr(not(feature = "std"), feature(error_in_core))] + +#[allow(unused_macros)] +/// Cargo will complain if your source file contains `#![features = ..]` even +/// whe the current cfg wouldn't enable the attribute. To get around this, we +/// `include!()` a source file with the offending attr *only* present when the compiler +/// is too old. +macro_rules! enable_feat { + ($feature:literal) => { + include!(concat!(env!("OUT_DIR"), "/", $feature, ".rs")); + } +} + +#[cfg(not(feature = "std"))] +enable_feat!("error_in_core"); // Requires `alloc`. #[macro_use]