Skip to content

Commit

Permalink
Merge 6146e40 into cb2a781
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer committed Jul 12, 2023
2 parents cb2a781 + 6146e40 commit 9fedd34
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 69 deletions.
43 changes: 39 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ jobs:
run: brew install ninja

- if: matrix.os == 'ubuntu-latest'
run: sudo apt-get install ninja-build
run: |
sudo apt-get update
sudo apt-get install ninja-build
- if: matrix.os == 'windows-latest'
uses: ilammy/msvc-dev-cmd@7315a94840631165970262a99c72cfb48a65d25d
Expand Down Expand Up @@ -90,10 +92,14 @@ jobs:
strategy:
fail-fast: false
matrix:
bits: [32, 64]
os:
- macos-latest
- ubuntu-latest
- windows-latest
exclude:
- bits: 32
os: "macos-latest"

runs-on: ${{ matrix.os }}

Expand All @@ -114,26 +120,55 @@ jobs:
run: brew install ninja

- if: matrix.os == 'ubuntu-latest'
run: sudo apt-get install ninja-build
run: |
sudo apt-get update
sudo apt-get install ninja-build gcc-multilib g++-multilib
rustup target add i686-unknown-linux-gnu
- if: matrix.os == 'windows-latest'
- name: Setup | Developer Command Prompt (x86)
if: ${{ matrix.bits == 32 && matrix.os == 'windows-latest' }}
uses: ilammy/msvc-dev-cmd@7315a94840631165970262a99c72cfb48a65d25d
with:
arch: x86

- name: Setup | Developer Command Prompt (x64)
if: ${{ matrix.bits == 64 && matrix.os == 'windows-latest' }}
uses: ilammy/msvc-dev-cmd@7315a94840631165970262a99c72cfb48a65d25d
with:
arch: x64

- if: matrix.os == 'windows-latest'
run: choco install ninja
run: |
choco install ninja
rustup target add i686-pc-windows-msvc
- name: Cargo update
working-directory: rust
run: cargo update

- name: Debug Build
if: matrix.bits == 64
working-directory: rust
run: cargo build -v

- name: Release Build
if: matrix.bits == 64
working-directory: rust
run: cargo build --release -v

- name: Test Debug
if: matrix.bits == 64
working-directory: rust
run: cargo test -v

- name: Test Debug 32-bit
if: ${{ matrix.bits == 32 && matrix.os == 'ubuntu-latest' }}
working-directory: rust
run: cargo test -v --target i686-unknown-linux-gnu

- name: Test Debug 32-bit
if: ${{ matrix.bits == 32 && matrix.os == 'windows-latest' }}
working-directory: rust
run: |
../tools/vcbuild.cmd 32
cargo test -v --target i686-pc-windows-msvc
2 changes: 1 addition & 1 deletion mach
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def build(args):
print("! Cross-compilation is not supported on Windows.")
exit(1)
if m32:
print("! Cross-compilation is not supported when --m32 is set.")
print("! Cross-compilation is not supported when -m32 is set.")
exit(1)
if args.target == "x86_64-apple-darwin":
cmake_args.extend(
Expand Down
8 changes: 6 additions & 2 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hacl"
version = "0.0.2"
version = "0.0.3-pre.1"
authors = ["Franziskus Kiefer <franziskuskiefer@gmail.com>"]
edition = "2021"
license = "Apache-2.0"
Expand All @@ -19,7 +19,7 @@ serialization = ["serde", "serde_json"]
hazmat = [] # This feature exposes raw, but safe HACL APIs for use in other libraries. Use with care.

[dependencies]
hacl-sys = { version = "=0.0.2", path = "hacl-sys" }
hacl-sys = { version = "=0.0.3-pre.1", path = "hacl-sys" }
rand = { version = "0.8", optional = true }
rand_core = { version = "0.6", optional = true }
serde_json = { version = "1.0", optional = true }
Expand All @@ -39,3 +39,7 @@ harness = false
[[bench]]
name = "aead"
harness = false

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "nobindgen"]
rustc-args = ["--cfg", "nobindgen"]
62 changes: 46 additions & 16 deletions rust/build.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,80 @@
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn simd128_support() -> bool {
fn simd128_support(target: &str) -> bool {
if target != "x86_64" {
// We don't want any SIMD for x86 for now when cross compiling.
return false;
}

std::arch::is_x86_feature_detected!("sse2")
&& std::arch::is_x86_feature_detected!("sse3")
&& std::arch::is_x86_feature_detected!("sse4.1")
&& std::arch::is_x86_feature_detected!("avx")
}

#[cfg(target_arch = "aarch64")]
fn simd128_support() -> bool {
fn simd128_support(_target: &str) -> bool {
true
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")))]
fn simd128_support() -> bool {
fn simd128_support(_target: &str) -> bool {
// XXX: Check for z14 or z15
false
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn simd256_support() -> bool {
fn simd256_support(target: &str) -> bool {
if target != "x86_64" {
// We don't want any SIMD for x86 for now when cross compiling.
return false;
}

std::arch::is_x86_feature_detected!("avx2")
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn simd256_support() -> bool {
fn simd256_support(_target: &str) -> bool {
// XXX: Check for z14 or z15
false
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn bmi2_adx_support() -> bool {
fn bmi2_adx_support(target: &str) -> bool {
if target != "x86_64" {
// We don't want any SIMD for x86 for now when cross compiling.
return false;
}

std::arch::is_x86_feature_detected!("bmi2") && std::arch::is_x86_feature_detected!("adx")
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn bmi2_adx_support() -> bool {
fn bmi2_adx_support(_target: &str) -> bool {
false
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn sha_ni_support() -> bool {
fn sha_ni_support(target: &str) -> bool {
if target != "x86_64" {
// We don't want any SIMD for x86 for now when cross compiling.
return false;
}

std::arch::is_x86_feature_detected!("sha")
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn sha_ni_support() -> bool {
fn sha_ni_support(_target: &str) -> bool {
false
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn aes_ni_support() -> bool {
fn aes_ni_support(target: &str) -> bool {
if target != "x86_64" {
// We don't want any SIMD for x86 for now when cross compiling.
return false;
}

// Note that we don't check for "movbe" here.
// This will be checked only on runtime.
// This is good enough here for the build.
Expand All @@ -60,25 +85,30 @@ fn aes_ni_support() -> bool {
}

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
fn aes_ni_support() -> bool {
fn aes_ni_support(_target: &str) -> bool {
false
}

fn main() {
if simd128_support() {
// We can't use cfg for cross compilation because the build script is compiled
// for the host.
let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
eprintln!("Building for {target_arch}");

if simd128_support(&target_arch) {
println!("cargo:rustc-cfg=simd128");
}
if simd256_support() {
if simd256_support(&target_arch) {
println!("cargo:rustc-cfg=simd256");
}
if bmi2_adx_support() {
if bmi2_adx_support(&target_arch) {
println!("cargo:rustc-cfg=bmi2");
println!("cargo:rustc-cfg=adx");
}
if sha_ni_support() {
if sha_ni_support(&target_arch) {
println!("cargo:rustc-cfg=sha_ni");
}
if aes_ni_support() {
if aes_ni_support(&target_arch) {
println!("cargo:rustc-cfg=aes_ni");
}
}
2 changes: 1 addition & 1 deletion rust/hacl-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hacl-sys"
version = "0.0.2"
version = "0.0.3-pre.1"
authors = ["Franziskus Kiefer <franziskuskiefer@gmail.com>"]
edition = "2021"
build = "build.rs"
Expand Down
60 changes: 35 additions & 25 deletions rust/hacl-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,45 @@ fn create_bindings(include_path: &Path, home_dir: &Path) {
fn create_bindings(_: &Path, _: &Path) {}

fn build_hacl_c(path: &Path, cross_target: Option<String>) {
println!(" >>> Building HACL C in {}", path.display());
eprintln!(" >>> Building HACL C in {}", path.display());
// cmake
let mut cmake_cmd = Command::new("cmake");

// Map cross compile targets to cmake toolchain files
let toolchain_file = cross_target
.clone()
.map(|s| match s.as_str() {
"x86_64-apple-darwin" => "-DCMAKE_TOOLCHAIN_FILE=config/x64-darwin.cmake",
"aarch64-apple-darwin" => "-DCMAKE_TOOLCHAIN_FILE=config/aarch64-darwin.cmake",
_ => "",
"x86_64-apple-darwin" => vec!["-D", "CMAKE_TOOLCHAIN_FILE=config/x64-darwin.cmake"],
"aarch64-apple-darwin" => {
vec!["-D", "CMAKE_TOOLCHAIN_FILE=config/aarch64-darwin.cmake"]
}
_ => vec![],
})
.unwrap_or_default();
let mut cmake_args = cross_target
.map(|s| match s.as_str() {
"i686-unknown-linux-gnu" => vec!["-DCMAKE_C_FLAGS=-m32", "-D", "CMAKE_CXX_FLAGS=-m32"],
"i686-pc-windows-msvc" => vec!["-DCMAKE_C_FLAGS=-m32", "-D", "CMAKE_CXX_FLAGS=-m32"],
_ => vec![],
})
.unwrap_or_default();
if !toolchain_file.is_empty() {
cmake_args.extend_from_slice(&toolchain_file);
}
cmake_args.extend_from_slice(&[
"-B",
"build",
"-G",
"Ninja",
"-D",
"CMAKE_BUILD_TYPE=Release",
]);

// We always build the release version here.
// TODO: For debugging don't use this.
let cmake_status = cmake_cmd
.current_dir(path)
.args(&[
"-B",
"build",
"-G",
"Ninja",
"-D",
"CMAKE_BUILD_TYPE=Release",
toolchain_file,
])
.status()
.expect("Failed to run cmake.");
let cmake_cmd = cmake_cmd.current_dir(path).args(&cmake_args);
eprintln!(" >>> CMAKE: {cmake_cmd:?}");
let cmake_status = cmake_cmd.status().expect("Failed to run cmake.");
if !cmake_status.success() {
panic!("Failed to run cmake.")
}
Expand All @@ -114,7 +125,7 @@ fn build_hacl_c(path: &Path, cross_target: Option<String>) {

// install
let install_path = path.join("build").join("installed");
println!(" >>> Installing HACL C into {}", install_path.display());
eprintln!(" >>> Installing HACL C into {}", install_path.display());
let mut cmake_cmd = Command::new("cmake");
let cmake_status = cmake_cmd
.current_dir(path)
Expand Down Expand Up @@ -173,9 +184,9 @@ fn main() {
let host = env::var("HOST").unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir);
println!("mach_build: {}", mach_build);
eprintln!("mach_build: {}", mach_build);

let cross_target = if target != host { Some(target) } else { None };
let cross_target = if target != host { Some(target.clone()) } else { None };

// Get the C library and build it first.
// This is the default behaviour. It can be disabled when working on this
Expand All @@ -184,9 +195,9 @@ fn main() {
// Copy all of the code into out to prepare build
let c_out_dir = out_dir.join("c");
if !c_out_dir.join("build").join("installed").exists() {
println!(" >>> Copying HACL C file");
println!(" from {}", home_dir.join(".c").display());
println!(" to {}", c_out_dir.display());
eprintln!(" >>> Copying HACL C file");
eprintln!(" from {}", home_dir.join(".c").display());
eprintln!(" to {}", c_out_dir.display());
copy_hacl_to_out(&c_out_dir);
}
build_hacl_c(&c_out_dir, cross_target);
Expand Down Expand Up @@ -222,8 +233,7 @@ fn main() {
create_bindings(&hacl_include_path, home_dir);

// Link hacl library.
let mode = "static";
println!("cargo:rustc-link-lib={}={}", mode, library_name);
println!("cargo:rustc-link-search=native={}", hacl_lib_path.display());
println!("cargo:lib={}", hacl_lib_path.display());
println!("cargo:rustc-link-lib=static={library_name}");
}
Loading

0 comments on commit 9fedd34

Please sign in to comment.