Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly test wasm32-unknown-unknown on CI #206

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ members = [
"crates/libm-bench",
]

[build-dependencies]
rand = { version = "0.6.5", optional = true }

[dev-dependencies]
no-panic = "0.1.8"

[build-dependencies]
rand = { version = "0.6.5", optional = true }
[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.2.47"

17 changes: 3 additions & 14 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,9 @@ jobs:
TARGET: powerpc64le-unknown-linux-gnu
x86_64:
TARGET: x86_64-unknown-linux-gnu

- job: wasm
pool:
vmImage: ubuntu-16.04
steps:
- template: ci/azure-install-rust.yml
- script: rustup target add wasm32-unknown-unknown
displayName: "Install rust wasm target"
- script: cargo build --target wasm32-unknown-unknown
displayName: "Build for wasm"
- script: cargo build --target wasm32-unknown-unknown --no-default-features
displayName: "Build for wasm (no default features)"
variables:
TOOLCHAIN: nightly
wasm32:
TARGET: wasm32-unknown-unknown
TOOLCHAIN: nightly

- job: rustfmt
pool:
Expand Down
8 changes: 5 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::env;
fn main() {
println!("cargo:rerun-if-changed=build.rs");

#[cfg(feature = "musl-reference-tests")]
musl_reference_tests::generate();
#[cfg(all(feature = "musl-reference-tests", target_os = "linux"))]
{
musl_reference_tests::generate();
}

if !cfg!(feature = "checked") {
let lvl = env::var("OPT_LEVEL").unwrap();
Expand All @@ -14,7 +16,7 @@ fn main() {
}
}

#[cfg(feature = "musl-reference-tests")]
#[cfg(all(feature = "musl-reference-tests", target_os = "linux"))]
mod musl_reference_tests {
use rand::seq::SliceRandom;
use rand::Rng;
Expand Down
24 changes: 24 additions & 0 deletions ci/docker/wasm32-unknown-unknown/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM ubuntu:18.04

RUN apt-get update -y && apt-get install -y --no-install-recommends \
ca-certificates \
clang \
cmake \
curl \
git \
libc6-dev \
make \
python \
xz-utils

# Install `wasm2wat`
RUN git clone --recursive https://github.com/WebAssembly/wabt
RUN make -C wabt -j$(nproc)
ENV PATH=$PATH:/wabt/bin

# Install `node`
RUN curl https://nodejs.org/dist/v12.0.0/node-v12.0.0-linux-x64.tar.xz | tar xJf -
ENV PATH=$PATH:/node-v12.0.0-linux-x64/bin

COPY docker/wasm32-unknown-unknown/wasm-entrypoint.sh /wasm-entrypoint.sh
ENTRYPOINT ["/wasm-entrypoint.sh"]
15 changes: 15 additions & 0 deletions ci/docker/wasm32-unknown-unknown/wasm-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

set -ex

# Download an appropriate version of wasm-bindgen based off of what's being used
# in the lock file. Ideally we'd use `wasm-pack` at some point for this!
version=$(grep -A 1 'name = "wasm-bindgen"' Cargo.lock | grep version)
version=$(echo $version | awk '{print $3}' | sed 's/"//g')
curl -L https://github.com/rustwasm/wasm-bindgen/releases/download/$version/wasm-bindgen-$version-x86_64-unknown-linux-musl.tar.gz \
| tar xzf - -C target
export PATH=$PATH:`pwd`/target/wasm-bindgen-$version-x86_64-unknown-linux-musl
export CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasm-bindgen-test-runner
export NODE_ARGS=--experimental-wasm-simd

exec "$@"
35 changes: 17 additions & 18 deletions ci/run-docker.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
#!/usr/bin/env sh

# Small script to run tests for a target (or all targets) inside all the
# respective docker images.

set -ex

run() {
local target=$1

echo $target

# This directory needs to exist before calling docker, otherwise docker will create it but it
# will be owned by root
mkdir -p target

docker build -t $target ci/docker/$target
docker build -t "${1}" -f "ci/docker/${1}/Dockerfile" ci/
docker run \
--rm \
--user $(id -u):$(id -g) \
-e CARGO_HOME=/cargo \
-e CARGO_TARGET_DIR=/target \
-v $(dirname $(dirname `which cargo`)):/cargo \
-v `pwd`/target:/target \
-v `pwd`:/checkout:ro \
-v `rustc --print sysroot`:/rust:ro \
--user "$(id -u)":"$(id -g)" \
--env CARGO_HOME=/cargo \
--env CARGO_TARGET_DIR=/checkout/target \
--volume "$(dirname "$(dirname "$(command -v cargo)")")":/cargo \
--volume "$(rustc --print sysroot)":/rust:ro \
--volume "$(pwd)":/checkout:ro \
--volume "$(pwd)"/target:/checkout/target \
--init \
-w /checkout \
$target \
sh -c "HOME=/tmp PATH=\$PATH:/rust/bin exec ci/run.sh $target"
--workdir /checkout \
--privileged \
"${1}" \
sh -c "HOME=/tmp PATH=\$PATH:/rust/bin exec ci/run.sh ${1}"
}

if [ -z "$1" ]; then
for d in `ls ci/docker/`; do
run $d
for d in ci/docker/*; do
run "${d}"
done
else
run $1
run "${1}"
fi
4 changes: 3 additions & 1 deletion crates/compiler-builtins-smoke-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ name = "cb"
version = "0.1.0"
authors = ["Jorge Aparicio <jorge@japaric.io>"]

[dependencies]
[features]
default = []
stable = []
6 changes: 5 additions & 1 deletion crates/compiler-builtins-smoke-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//! Fake compiler-builtins crate
//!
//! This is used to test that we can source import `libm` into the compiler-builtins crate.

#![cfg(not(test))]
#![allow(dead_code)]
#![no_std]
#![cfg_attr(
all(target_arch = "wasm32", not(feature = "stable")),
feature(core_intrinsics)
)]

#[path = "../../../src/math/mod.rs"]
mod libm;
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
feature(core_intrinsics)
)]

#[cfg(all(test, target_arch = "wasm32"))]
extern crate wasm_bindgen_test;
#[cfg(all(test, target_arch = "wasm32"))]
use wasm_bindgen_test::wasm_bindgen_test;

mod math;

use core::{f32, f64};
Expand Down
17 changes: 11 additions & 6 deletions src/math/atan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ pub fn atan(x: f64) -> f64 {

#[cfg(test)]
mod tests {
use super::atan;
use crate::*;
use core::f64;

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn sanity_check() {
for (input, answer) in [
(3.0_f64.sqrt() / 3.0, f64::consts::FRAC_PI_6),
Expand All @@ -163,22 +164,26 @@ mod tests {
}
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn zero() {
assert_eq!(atan(0.0), 0.0);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn infinity() {
assert_eq!(atan(f64::INFINITY), f64::consts::FRAC_PI_2);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn minus_infinity() {
assert_eq!(atan(f64::NEG_INFINITY), -f64::consts::FRAC_PI_2);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn nan() {
assert!(atan(f64::NAN).is_nan());
}
Expand Down
23 changes: 15 additions & 8 deletions src/math/atan2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,19 @@ pub fn atan2(y: f64, x: f64) -> f64 {
}
}

#[test]
fn sanity_check() {
assert_eq!(atan2(0.0, 1.0), 0.0);
assert_eq!(atan2(0.0, -1.0), PI);
assert_eq!(atan2(-0.0, -1.0), -PI);
assert_eq!(atan2(3.0, 2.0), atan(3.0 / 2.0));
assert_eq!(atan2(2.0, -1.0), atan(2.0 / -1.0) + PI);
assert_eq!(atan2(-2.0, -1.0), atan(-2.0 / -1.0) - PI);
#[cfg(test)]
mod tests {
use super::*;
use crate::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn sanity_check() {
assert_eq!(atan2(0.0, 1.0), 0.0);
assert_eq!(atan2(0.0, -1.0), PI);
assert_eq!(atan2(-0.0, -1.0), -PI);
assert_eq!(atan2(3.0, 2.0), atan(3.0 / 2.0));
assert_eq!(atan2(2.0, -1.0), atan(2.0 / -1.0) + PI);
assert_eq!(atan2(-2.0, -1.0), atan(-2.0 / -1.0) - PI);
}
}
10 changes: 6 additions & 4 deletions src/math/ceil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn ceil(x: f64) -> f64 {
// `f64.ceil` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "stable")))] {
return unsafe { ::core::intrinsics::ceilf64(x) }
}
}
Expand Down Expand Up @@ -43,9 +43,11 @@ pub fn ceil(x: f64) -> f64 {

#[cfg(test)]
mod tests {
#[test]
use crate::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn sanity_check() {
assert_eq!(super::ceil(1.1), 2.0);
assert_eq!(super::ceil(2.9), 3.0);
assert_eq!(ceil(1.1), 2.0);
assert_eq!(ceil(2.9), 3.0);
}
}
2 changes: 1 addition & 1 deletion src/math/ceilf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn ceilf(x: f32) -> f32 {
// `f32.ceil` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "stable")))] {
return unsafe { ::core::intrinsics::ceilf32(x) }
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/math/exp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,13 @@ pub fn exp2(mut x: f64) -> f64 {
scalbn(r, ki)
}

#[test]
fn i0_wrap_test() {
let x = -3.0 / 256.0;
assert_eq!(exp2(x), f64::from_bits(0x3fefbdba3692d514));
#[cfg(test)]
mod tests {
use crate::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn i0_wrap_test() {
let x = -3.0 / 256.0;
assert_eq!(exp2(x), f64::from_bits(0x3fefbdba3692d514));
}
}
6 changes: 4 additions & 2 deletions src/math/expm1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ pub fn expm1(mut x: f64) -> f64 {

#[cfg(test)]
mod tests {
#[test]
use crate::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn sanity_check() {
assert_eq!(super::expm1(1.1), 2.0041660239464334);
assert_eq!(expm1(1.1), 2.0041660239464334);
}
}
2 changes: 1 addition & 1 deletion src/math/fabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn fabs(x: f64) -> f64 {
// `f64.abs` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "stable")))] {
return unsafe { ::core::intrinsics::fabsf64(x) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/fabsf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn fabsf(x: f32) -> f32 {
// `f32.abs` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "stable")))] {
return unsafe { ::core::intrinsics::fabsf32(x) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/floor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn floor(x: f64) -> f64 {
// `f64.floor` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "stable")))] {
return unsafe { ::core::intrinsics::floorf64(x) }
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/math/floorf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn floorf(x: f32) -> f32 {
// `f32.floor` native instruction, so we can leverage this for both code size
// and speed.
llvm_intrinsically_optimized! {
#[cfg(target_arch = "wasm32")] {
#[cfg(all(target_arch = "wasm32", not(feature = "stable")))] {
return unsafe { ::core::intrinsics::floorf32(x) }
}
}
Expand Down Expand Up @@ -43,8 +43,10 @@ pub fn floorf(x: f32) -> f32 {

#[cfg(test)]
mod tests {
#[test]
use crate::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn no_overflow() {
assert_eq!(super::floorf(0.5), 0.0);
assert_eq!(floorf(0.5), 0.0);
}
}
8 changes: 5 additions & 3 deletions src/math/j1f.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,15 @@ fn qonef(x: f32) -> f32 {

#[cfg(test)]
mod tests {
use super::{j1f, y1f};
#[test]
use crate::*;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn test_j1f_2488() {
// 0x401F3E49
assert_eq!(j1f(2.4881766_f32), 0.49999475_f32);
}
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn test_y1f_2002() {
assert_eq!(y1f(2.0000002_f32), -0.10703229_f32);
}
Expand Down
Loading