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 authored and LucasSte committed Feb 1, 2024
1 parent 2a331a2 commit 81ef46f
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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 @@ -87,6 +90,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 @@ -40,6 +40,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 @@ -207,6 +208,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 @@ -575,6 +577,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 @@ -16,7 +16,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 @@ -47,6 +47,7 @@ pub mod int;
all(target_family = "wasm", target_os = "unknown"),
target_os = "uefi",
target_os = "none",
target_os = "solana",
target_os = "xous",
all(target_vendor = "fortanix", target_env = "sgx"),
target_os = "windows"
Expand Down Expand Up @@ -79,4 +80,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 @@ -21,6 +21,7 @@ macro_rules! no_mangle {
target_os = "unknown",
not(target_env = "wasi")
),
target_os = "solana",
target_os = "xous",
target_os = "uefi",
all(target_arch = "xtensa", target_os = "none"),
Expand Down Expand Up @@ -126,6 +127,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 @@ -155,3 +157,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 81ef46f

Please sign in to comment.