This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
forked from arkworks-rs/snark
-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.rs
107 lines (97 loc) · 3.42 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#[cfg(feature = "llvm_asm")]
use {
field_assembly::generate_macro_string,
std::{env, fs},
};
use std::path::{Path, PathBuf};
#[cfg(feature = "llvm_asm")]
const NUM_LIMBS: usize = 8;
fn replace_extension(file_name: &str, extension: &str) -> String {
let mut new_name = PathBuf::from(Path::new(file_name));
new_name.set_extension(extension);
new_name.as_os_str().to_str().unwrap().to_string()
}
fn preprocess_and_replace_semicolon(top_level_file: &str, file_names: &[&str], tag: &str) {
const EXPANDED_SUFFIX: &str = "SCE.S";
const WITHOUT_SEMICOLON_SUFFIX: &str = "SCR.S";
for file_name in file_names {
let expanded = cc::Build::new().file(file_name).expand();
let expanded_path = replace_extension(file_name, EXPANDED_SUFFIX);
std::fs::write(&expanded_path, expanded).expect("Should have expanded");
let replaced = std::fs::read_to_string(expanded_path)
.expect("Should have read expanded file")
.replace(";", "\n");
let replaced_path = replace_extension(file_name, WITHOUT_SEMICOLON_SUFFIX);
std::fs::write(&replaced_path, replaced).expect("Should have written replaced file");
}
cc::Build::new().file(top_level_file).compile(tag);
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
#[cfg(feature = "llvm_asm")]
{
use rustc_version::{version_meta, Channel};
let is_nightly = version_meta().expect("nightly check failed").channel == Channel::Nightly;
let should_use_asm = cfg!(all(
feature = "llvm_asm",
target_feature = "bmi2",
target_feature = "adx",
target_arch = "x86_64"
)) && is_nightly;
if should_use_asm {
let out_dir = env::var_os("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("field_assembly.rs");
fs::write(&dest_path, generate_macro_string(NUM_LIMBS)).unwrap();
println!("cargo:rustc-cfg=use_asm");
}
}
let should_use_bw6_asm = cfg!(any(
all(
feature = "bw6_asm",
any(
all(
target_arch = "x86_64",
target_feature = "bmi2",
target_feature = "adx"
),
target_arch = "aarch64"
)
),
feature = "force_bw6_asm"
));
if should_use_bw6_asm {
preprocess_and_replace_semicolon(
"bw6-assembly/modmul768.S",
&vec![
"bw6-assembly/modmul768-x86_64.S",
"bw6-assembly/modmul768-armv8.S",
],
"modmul768",
);
preprocess_and_replace_semicolon(
"bw6-assembly/modadd768.S",
&vec![
"bw6-assembly/modadd768-x86_64.S",
"bw6-assembly/modadd768-armv8.S",
],
"modadd768",
);
preprocess_and_replace_semicolon(
"bw6-assembly/modsub768.S",
&vec![
"bw6-assembly/modsub768-x86_64.S",
"bw6-assembly/modsub768-armv8.S",
],
"modsub768",
);
preprocess_and_replace_semicolon(
"bw6-assembly/modsqr768.S",
&vec![
"bw6-assembly/modsqr768-x86_64.S",
"bw6-assembly/modsqr768-armv8.S",
],
"modsqr768",
);
println!("cargo:rustc-cfg=use_bw6_asm");
}
}