Skip to content

Commit

Permalink
[SOL] Make SBF target specific adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
dmakarov committed Sep 26, 2023
1 parent 82feed3 commit 44fe136
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 18 deletions.
28 changes: 16 additions & 12 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ jobs:
- target: i686-unknown-linux-gnu
os: ubuntu-latest
rust: nightly
- target: mips-unknown-linux-gnu
os: ubuntu-latest
rust: nightly
- target: mips64-unknown-linux-gnuabi64
os: ubuntu-latest
rust: nightly
- target: mips64el-unknown-linux-gnuabi64
os: ubuntu-latest
rust: nightly
- target: mipsel-unknown-linux-gnu
os: ubuntu-latest
rust: nightly
# - target: mips-unknown-linux-gnu
# os: ubuntu-latest
# rust: nightly
# - target: mips64-unknown-linux-gnuabi64
# os: ubuntu-latest
# rust: nightly
# - target: mips64el-unknown-linux-gnuabi64
# os: ubuntu-latest
# rust: nightly
# - target: mipsel-unknown-linux-gnu
# os: ubuntu-latest
# rust: nightly
- target: powerpc-unknown-linux-gnu
os: ubuntu-latest
rust: nightly
Expand All @@ -44,6 +44,9 @@ jobs:
- target: powerpc64le-unknown-linux-gnu
os: ubuntu-latest
rust: nightly
- target: sbf-solana-solana
os: ubuntu-latest
rust: nightly
- target: thumbv6m-none-eabi
os: ubuntu-latest
rust: nightly
Expand Down Expand Up @@ -85,6 +88,7 @@ jobs:
run: rustup update ${{ matrix.rust }} --no-self-update && rustup default ${{ matrix.rust }}
shell: bash
- run: rustup target add ${{ matrix.target }}
if: matrix.target != 'sbf-solana-solana'
- run: rustup component add llvm-tools-preview
- name: Download compiler-rt reference sources
run: |
Expand Down
19 changes: 19 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn main() {
|| target.contains("i686")
|| target.contains("aarch64")
|| target.contains("bpf")
|| target.contains("sbf")
{
println!("cargo:rustc-cfg=feature=\"mem-unaligned\"");
}
Expand Down Expand Up @@ -206,6 +207,7 @@ mod c {
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap();
let target_feature = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or_default();
let mut consider_float_intrinsics = true;
let cfg = &mut cc::Build::new();

Expand Down Expand Up @@ -572,6 +574,23 @@ mod c {
sources.extend(&[("__emutls_get_address", "emutls.c")]);
}

if target_os == "solana" {
cfg.define("__ELF__", None);
// Use the static-syscall target feature to detect if we're
// compiling for sbfv2, in which case set the corresponding clang
// cpu flag.
if target_feature.contains("static-syscalls") {
cfg.flag("-mcpu=sbfv2");
}
// Remove the implementations that fail to build.
// This list should shrink to zero
sources.remove(&[
"__int_util", // Unsupported architecture error
"__mulvdi3", // Unsupported signed division
"__mulvsi3", // Unsupported signed division
]);
}

// When compiling the C code we require the user to tell us where the
// source code is, and this is largely done so when we're compiling as
// part of rust-lang/rust we can use the same llvm-project repository as
Expand Down
23 changes: 23 additions & 0 deletions ci/docker/sbf-solana-solana/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM ubuntu:20.04
RUN apt-get update && \
apt-get install -y --no-install-recommends \
curl \
gcc libc6-dev ca-certificates

ENV RUSTUP_INIT_SKIP_PATH_CHECK="yes"
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y -v --no-modify-path
RUN cp ${HOME}/.cargo/bin/* /usr/local/bin/

RUN cargo install --git https://github.com/solana-labs/cargo-run-solana-tests.git \
--rev df2f642924aee7bbd2566017b3d71cb0c389b015 \
--bin cargo-run-solana-tests --root /usr/local

RUN mkdir -p /tmp/.cache/solana/v1.38/platform-tools
RUN curl -L -o platform-tools-linux-x86_64.tar.bz2 https://github.com/solana-labs/platform-tools/releases/download/v1.38/platform-tools-linux-x86_64.tar.bz2
RUN tar -xjf platform-tools-linux-x86_64.tar.bz2 --strip-components 1 -C /tmp/.cache/solana/v1.38/platform-tools
RUN rustup toolchain link solana /tmp/.cache/solana/v1.38/platform-tools/rust
RUN cp -R ${HOME}/.rustup /tmp/

ENV CARGO_TARGET_SBF_SOLANA_SOLANA_RUNNER="cargo-run-solana-tests --heap-size 104857600"
ENV CC="/tmp/.cache/solana/v1.38/platform-tools/llvm/bin/clang"
ENV RUSTUP_TOOLCHAIN="solana"
7 changes: 6 additions & 1 deletion examples/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

extern crate panic_handler;

#[cfg(all(not(thumb), not(windows), not(target_arch = "wasm32")))]
#[cfg(all(
not(thumb),
not(windows),
not(target_arch = "wasm32"),
not(target_os = "solana")
))]
#[link(name = "c")]
extern "C" {}

Expand Down
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![cfg_attr(not(feature = "no-asm"), feature(global_asm))]
#![feature(cfg_target_has_atomic)]
#![feature(compiler_builtins)]
#![feature(core_ffi_c)]
#![cfg_attr(not(target_os = "solana"), feature(core_ffi_c))]
#![feature(core_intrinsics)]
#![feature(inline_const)]
#![feature(lang_items)]
Expand Down Expand Up @@ -49,6 +49,7 @@ pub mod int;
all(target_arch = "arm", target_os = "none"),
all(target_arch = "xtensa", target_os = "none"),
all(target_arch = "mips", target_os = "none"),
target_os = "solana",
target_os = "xous",
all(target_vendor = "fortanix", target_env = "sgx")
))]
Expand Down Expand Up @@ -77,4 +78,12 @@ pub mod x86;
#[cfg(target_arch = "x86_64")]
pub mod x86_64;

#[cfg(all(target_os = "solana", target_feature = "static-syscalls"))]
#[cfg_attr(not(feature = "mangled-names"), no_mangle)]
#[linkage = "weak"]
pub unsafe extern "C" fn abort() -> ! {
let syscall: extern "C" fn() -> ! = core::mem::transmute(3069975057u64); // murmur32 hash of "abort"
syscall()
}

pub mod probestack;
8 changes: 8 additions & 0 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ macro_rules! no_mangle {
target_os = "unknown",
not(target_env = "wasi")
),
target_os = "solana",
target_os = "xous",
all(target_arch = "x86_64", target_os = "uefi"),
all(target_arch = "xtensa", target_os = "none"),
Expand Down Expand Up @@ -124,6 +125,7 @@ no_mangle! {
#[cfg(any(
all(target_vendor = "fortanix", target_env = "sgx"),
all(target_arch = "xtensa", target_os = "none"),
target_os = "solana",
target_os = "xous",
target_os = "uefi"
))]
Expand Down Expand Up @@ -153,3 +155,9 @@ no_mangle! {
// `f32 % f32`
fn fmodf(x: f32, y: f32) -> f32;
}

#[cfg(target_os = "solana")]
no_mangle! {
fn sqrt(x: f64) -> f64;
fn sqrtf(x: f32) -> f32;
}
Loading

0 comments on commit 44fe136

Please sign in to comment.