Skip to content

Commit

Permalink
Detect nightly in icu_capi_freertos, bump to 1.2.1 (#3394)
Browse files Browse the repository at this point in the history
* Fix freertos on modern nightlies

* Bump to 1.2.1

* fix
  • Loading branch information
Manishearth authored Apr 26, 2023
1 parent a3f79c5 commit 7017c3e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion ffi/freertos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[package]
name = "icu_freertos"
description = "C interface to ICU4X"
version = "1.2.0"
version = "1.2.1"
authors = ["The ICU4X Project Developers"]
edition = "2021"
resolver = "2"
Expand Down Expand Up @@ -42,3 +42,6 @@ freertos-rust = "0.1.2"
# Currently we don't enable anything here as WearOS loads data manually, but the feature remains for future use
wearos = []
default = ["icu_capi/default_components"]

[build-dependencies]
rustc_version = "0.4"
9 changes: 9 additions & 0 deletions ffi/freertos/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions ffi/freertos/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use std::env;

/// Returns whether the Rust compiler needs an `#[alloc_error_handler]`
/// set. Returns None for cases where we cannot determine the nightly version of the
/// compiler.
fn needs_alloc_error_handler() -> Option<bool> {
use rustc_version::Channel;
let version = rustc_version::version_meta().ok()?;
if version.channel != Channel::Nightly {
// Ignore custom/dev toolchains, commit date
// may not be meaningful
return None;
}
let commit_date = version.commit_date?;

let year = commit_date.split('-').next()?.parse::<u32>().ok()?;

// alloc_error_handler became defaulted to the panic handler
// in December 2022. Since it still works until April 2023,
// we can be fuzzy with our dates and just set the boundary at
// 2022/2023
Some(year <= 2022)
}

fn main() {
match env::var("CARGO_CFG_TARGET_OS") {
Ok(v) if v == "none" => (),
// Only on target_os = none
_ => return,
};

if let Some(true) = needs_alloc_error_handler() {
println!("cargo:rustc-cfg=needs_alloc_error_handler");
}

// For unknown compilers, assume that the nightly is recent.
}
24 changes: 18 additions & 6 deletions ffi/freertos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! This crate is a shim that enables one to use icu4x on Cortex-M + FreeRTOS by setting up the
//! relevant Rust runtime hooks.
//!
//! Note that compiling to this platform needs Rust nightly, and this crate attempts to
//! build across multiple nightly versions.
//!
//! This crate has a build script that will attempt to detect the nightly version and configure
//! things appropriately, where possible. Older nightlies will end up setting the
//! `--cfg needs_alloc_error_handler` flag: if using a custom build system and a nightly from
//! 2022 or earlier, please set this flag.

// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
#![no_std]
#![cfg_attr(
Expand All @@ -17,7 +28,10 @@
)
)]
#![allow(clippy::upper_case_acronyms)]
#![cfg_attr(target_os = "none", feature(alloc_error_handler))]
#![cfg_attr(
all(target_os = "none", needs_alloc_error_handler),
feature(alloc_error_handler)
)]

// Necessary to for symbols to be linked in
extern crate icu_capi;
Expand All @@ -27,21 +41,19 @@ extern crate icu_capi;
mod stuff {
extern crate alloc;

use alloc::alloc::Layout;
use core::panic::PanicInfo;
use cortex_m::asm;
use freertos_rust::FreeRtosAllocator;

#[global_allocator]
static GLOBAL: FreeRtosAllocator = FreeRtosAllocator;

#[cfg(needs_alloc_error_handler)] // this defaults to the panic handler on newer nightlies
#[alloc_error_handler]
fn alloc_error(_layout: Layout) -> ! {
asm::bkpt();
fn alloc_error(_layout: alloc::alloc::Layout) -> ! {
cortex_m::asm::bkpt();
loop {}
}

#[cfg(target_os = "none")]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
Expand Down

0 comments on commit 7017c3e

Please sign in to comment.