Skip to content

Commit

Permalink
Support MSVC in hacl build
Browse files Browse the repository at this point in the history
  • Loading branch information
mamonet committed Aug 29, 2023
1 parent 2fdee8a commit 14c3d5f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 57 deletions.
2 changes: 1 addition & 1 deletion sys/hacl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"

[build-dependencies]
libc = { version = "0.2", default-features = false }
fs_extra = "1.2"
fs_extra = "1.3"
cc = { version = "1.0", features = ["parallel"] }
libcrux_platform = { path = "../platform" }

Expand Down
59 changes: 27 additions & 32 deletions sys/hacl/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#[cfg(not(windows))]
extern crate bindgen;

use std::{env, path::Path};

macro_rules! svec {
Expand All @@ -9,8 +6,12 @@ macro_rules! svec {

fn includes(home_dir: &Path, include_str: &str) -> Vec<String> {
let c_path = home_dir.join("c");
let mut include_path = c_path.join("include");
if cfg!(target_env = "msvc") {
include_path = include_path.join("msvc");
}
vec![
format!("{}{}", include_str, c_path.join("include").display()),
format!("{}{}", include_str, include_path.display()),
format!(
"{}{}",
include_str,
Expand All @@ -30,15 +31,17 @@ fn includes(home_dir: &Path, include_str: &str) -> Vec<String> {
}

fn append_simd128_flags(flags: &mut Vec<String>) {
if cfg!(any(target_arch = "x86", target_arch = "x86_64")) {
if cfg!(all(any(target_arch = "x86", target_arch = "x86_64"), not(target_env = "msvc"))) {
flags.push("-msse4.1".to_string());
flags.push("-msse4.2".to_string());
flags.push("-mavx".to_string());
}
}

fn append_simd256_flags(flags: &mut Vec<String>) {
flags.push("-mavx2".to_string());
if cfg!(not(target_env = "msvc")) {
flags.push("-mavx2".to_string());
}
}

#[cfg(not(windows))]
Expand Down Expand Up @@ -109,22 +112,19 @@ fn create_bindings(platform: Platform, home_dir: &Path) {
}

#[cfg(windows)]
fn create_bindings(_: &Path, _: &Path) {}

fn copy_files(home_path: &Path, out_path: &Path) {
let mut options = fs_extra::dir::CopyOptions::new();
options.overwrite = true;
fs_extra::dir::copy(home_path.join("c"), out_path, &options).unwrap();
}
fn create_bindings(_: Platform, _: &Path) {}

fn compile_files(
library_name: &str,
files: &[String],
out_path: &Path,
home_path: &Path,
args: &[String],
defines: &[(&str, &str)],
) {
let src_prefix = out_path.join("c").join("src");
let mut src_prefix = home_path.join("c").join("src");
if cfg!(target_env = "msvc") {
src_prefix = src_prefix.join("msvc");
}

let mut build = cc::Build::new();
build
Expand All @@ -135,10 +135,10 @@ fn compile_files(
.extra_warnings(true);
// .no_default_flags(true);

for include in includes(out_path, "") {
for include in includes(home_path, "") {
build.include(include);
}
build.flag("-O3").flag("-c");
build.opt_level(3);
for arg in args {
build.flag(arg);
}
Expand All @@ -149,7 +149,7 @@ fn compile_files(
build.compile(library_name);
}

fn build(platform: Platform, out_path: &Path) {
fn build(platform: Platform, home_path: &Path) {
let files = svec![
"Hacl_NaCl.c",
"Hacl_Salsa20.c",
Expand Down Expand Up @@ -214,7 +214,7 @@ fn build(platform: Platform, out_path: &Path) {
compile_files(
"libhacl_128.a",
&files128,
out_path,
home_path,
&simd128_flags,
&defines,
);
Expand All @@ -238,13 +238,13 @@ fn build(platform: Platform, out_path: &Path) {
compile_files(
"libhacl_256.a",
&files256,
out_path,
home_path,
&simd256_flags,
&defines,
);
}

compile_files("libhacl.a", &files, out_path, &[], &defines);
compile_files("libhacl.a", &files, home_path, &[], &defines);
}

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -281,29 +281,24 @@ fn main() {

// Set re-run trigger for all of c
println!("cargo:rerun-if-changed=c");
println!("cargo:rerun-if-changed=h");

// Moving C library to output to make build easier.
copy_files(home_path, out_path);
eprintln!(" >>> out {:?}", out_path);

// Build the C files
build(platform, out_path);
build(platform, home_path);

// Set library name to look up
let library_name = "hacl";
const LIB_NAME: &str = "hacl";

// Generate new bindings. This is a no-op on Windows.
create_bindings(platform, home_path);

// Link hacl library.
let mode = "static";
println!("cargo:rustc-link-lib={}={}", mode, library_name);
const MODE: &str = "static";
println!("cargo:rustc-link-lib={}={}", MODE, LIB_NAME);
if platform.simd128 {
println!("cargo:rustc-link-lib={}={}", mode, "hacl_128");
println!("cargo:rustc-link-lib={}={}", MODE, "hacl_128");
}
if platform.simd256 {
println!("cargo:rustc-link-lib={}={}", mode, "hacl_256");
println!("cargo:rustc-link-lib={}={}", MODE, "hacl_256");
}
println!("cargo:rustc-link-search=native={}", out_path.display());
println!("cargo:lib={}", out_path.display());
Expand Down
38 changes: 14 additions & 24 deletions sys/libjade/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ macro_rules! svec {
($($x:expr),*$(,)?) => (vec![$($x.to_string()),*]);
}

fn copy_files(home_path: &Path, out_path: &Path) {
let mut options = fs_extra::dir::CopyOptions::new();
options.overwrite = true;
fs_extra::dir::copy(home_path.join("jazz"), out_path, &options).unwrap();
}

fn append_simd128_flags(flags: &mut Vec<String>) {
flags.push("-DSIMD128".to_string());
flags.push("-mavx".to_string());
Expand Down Expand Up @@ -66,10 +60,10 @@ fn create_bindings(platform: Platform, home_dir: &Path) {
}

#[cfg(windows)]
fn create_bindings(platform: Platform, _: &Path) {}
fn create_bindings(_: Platform, _: &Path) {}

fn compile_files(library_name: &str, files: &[String], out_path: &Path, args: &[String]) {
let jazz_dir = out_path.join("jazz");
fn compile_files(library_name: &str, files: &[String], home_path: &Path, args: &[String]) {
let jazz_dir = home_path.join("jazz");

let mut build = cc::Build::new();
build
Expand All @@ -78,15 +72,15 @@ fn compile_files(library_name: &str, files: &[String], out_path: &Path, args: &[
.no_default_flags(true);

build.include(jazz_dir.join("include"));
build.flag("-O3").flag("-c");
build.opt_level(3);
for arg in args {
build.flag(arg);
}

build.compile(library_name);
}

fn build(platform: Platform, out_path: &Path, cross_target: Option<String>) {
fn build(platform: Platform, home_path: &Path, cross_target: Option<String>) {
let args = cross_target
.map(|s| match s.as_str() {
// We only support cross compilation here for now.
Expand All @@ -107,7 +101,7 @@ fn build(platform: Platform, out_path: &Path, cross_target: Option<String>) {
"poly1305_ref.s",
"kyber_kyber768_ref.s",
];
compile_files("libjade.a", &files, out_path, &args);
compile_files("libjade.a", &files, home_path, &args);

if platform.simd256 {
let files256 = svec![
Expand All @@ -121,15 +115,15 @@ fn build(platform: Platform, out_path: &Path, cross_target: Option<String>) {

let mut simd256_flags = args.clone();
append_simd256_flags(&mut simd256_flags);
compile_files("libjade_256.a", &files256, out_path, &simd256_flags);
compile_files("libjade_256.a", &files256, home_path, &simd256_flags);
}

if platform.simd128 {
let files128 = svec!["chacha20_avx.s", "poly1305_avx.s",];

let mut simd128_flags = args.clone();
append_simd128_flags(&mut simd128_flags);
compile_files("libjade_128.a", &files128, out_path, &simd128_flags);
compile_files("libjade_128.a", &files128, home_path, &simd128_flags);
}
}

Expand Down Expand Up @@ -173,15 +167,11 @@ pub fn main() -> Result<(), u8> {
}
};

// Moving C/ASM code to output to make build easier.
copy_files(home_path, out_path);
eprintln!(" >>> out {:?}", out_path);

// Build the C/ASM files
build(platform, out_path, cross_target);
build(platform, home_path, cross_target);

// Set library name to look up
let library_name = "jade";
const LIB_NAME: &str = "jade";

// Set re-run trigger for all of s
println!("cargo:rerun-if-changed=cs");
Expand All @@ -190,15 +180,15 @@ pub fn main() -> Result<(), u8> {
create_bindings(platform, home_path);

// Link hacl library.
let mode = "static";
println!("cargo:rustc-link-lib={}={}", mode, library_name);
const MODE: &str = "static";
println!("cargo:rustc-link-lib={}={}", MODE, LIB_NAME);
if platform.simd128 {
println!("cargo:rustc-cfg=simd128");
println!("cargo:rustc-link-lib={}={}", mode, "jade_128");
println!("cargo:rustc-link-lib={}={}", MODE, "jade_128");
}
if platform.simd256 {
println!("cargo:rustc-cfg=simd256");
println!("cargo:rustc-link-lib={}={}", mode, "jade_256");
println!("cargo:rustc-link-lib={}={}", MODE, "jade_256");
}
println!("cargo:rustc-link-search=native={}", out_path.display());
println!("cargo:lib={}", out_path.display());
Expand Down

0 comments on commit 14c3d5f

Please sign in to comment.