-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
173 lines (152 loc) · 5.9 KB
/
build.rs
File metadata and controls
173 lines (152 loc) · 5.9 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::env;
use std::path::Path;
fn main() {
let target = env::var("TARGET").unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
// Make sure to rebuild when these files change
println!("cargo:rerun-if-changed=src/randomx.h");
println!("cargo:rerun-if-changed=src/randomx.cpp");
println!("cargo:rerun-if-changed=build.rs");
// Platform-specific flags
let mut cc_config = cc::Build::new();
cc_config
.cpp(true)
.include("src")
.warnings(false)
.extra_warnings(false);
// Configure C/C++ compiler flags based on platform
if target.contains("msvc") {
// MSVC-specific flags
cc_config.flag("/EHsc").flag("/O2").flag("/std:c++14");
} else {
// GCC/Clang flags
cc_config
.flag("-std=c++14")
.flag("-O3")
.flag("-fno-rtti")
.flag("-Wno-unused-parameter");
if target.contains("x86_64") {
cc_config.flag("-maes");
} else if target.contains("aarch64") {
cc_config.flag("-march=armv8-a+crypto");
}
}
// Compile all C/C++ files
let mut sources = vec![
"src/aes_hash.cpp",
"src/allocator.cpp",
"src/assembly_generator_x86.cpp",
"src/blake2_generator.cpp",
"src/blake2/blake2b.c",
"src/bytecode_machine.cpp",
"src/cpu.cpp",
"src/dataset.cpp",
"src/instruction.cpp",
"src/instructions_portable.cpp",
"src/randomx.cpp",
"src/reciprocal.c",
"src/soft_aes.cpp",
"src/superscalar.cpp",
"src/virtual_machine.cpp",
"src/virtual_memory.c",
"src/vm_compiled_light.cpp",
"src/vm_compiled.cpp",
"src/vm_interpreted_light.cpp",
"src/vm_interpreted.cpp",
"src/argon2_core.c",
"src/argon2_ref.c",
];
// Add platform-specific JIT compiler implementations
if target.contains("x86_64") {
sources.push("src/jit_compiler_x86.cpp");
if target.contains("linux") || target.contains("freebsd") || target.contains("dragonfly") {
cc_config.define("XMRIG_OS_UNIX", "1");
cc_config.flag("-fPIC");
// For x86-64 Linux, handle the assembly file separately
let mut asm_config = cc::Build::new();
asm_config
.file("src/jit_compiler_x86_static.S")
.flag("-fPIC")
.include("src");
asm_config.compile("randomx_asm");
} else if target.contains("windows") {
cc_config.define("XMRIG_OS_WIN", "1");
// For Windows, compile the assembly file separately
if target.contains("msvc") {
let mut asm_config = cc::Build::new();
asm_config.file("src/jit_compiler_x86_static.asm");
asm_config.compile("randomx_asm");
} else {
let mut asm_config = cc::Build::new();
asm_config
.file("src/jit_compiler_x86_static.S")
.include("src");
asm_config.compile("randomx_asm");
}
}
} else if target.contains("aarch64") {
sources.push("src/jit_compiler_a64.cpp");
if target.contains("linux") || target.contains("freebsd") || target.contains("dragonfly") {
cc_config.define("XMRIG_OS_UNIX", "1");
cc_config.flag("-fPIC");
// Handle AArch64 assembly separately
let mut asm_config = cc::Build::new();
asm_config
.file("src/jit_compiler_a64_static.S")
.flag("-fPIC")
.include("src");
asm_config.compile("randomx_asm");
}
} else if target.contains("riscv64") {
sources.push("src/jit_compiler_rv64.cpp");
if target.contains("linux") || target.contains("freebsd") || target.contains("dragonfly") {
cc_config.define("XMRIG_OS_UNIX", "1");
cc_config.flag("-fPIC");
// Handle RISC-V assembly separately
let mut asm_config = cc::Build::new();
asm_config
.file("src/jit_compiler_rv64_static.S")
.flag("-fPIC")
.include("src");
asm_config.compile("randomx_asm");
}
}
// Add optimized Argon2 implementations if supported
if target.contains("x86_64") || target.contains("i686") {
sources.push("src/argon2_ssse3.c");
if !target.contains("msvc") {
cc_config.flag("-mssse3");
}
sources.push("src/argon2_avx2.c");
if !target.contains("msvc") {
cc_config.flag("-mavx2");
}
}
// Compile all the sources
cc_config.files(&sources);
// Important - compile everything as a single unit to resolve linking issues
// This helps ensure all symbols are properly linked
cc_config.compile("randomx_all");
// Make sure to link against C++ standard library
println!("cargo:rustc-link-lib=stdc++");
// Link search paths
println!("cargo:rustc-link-search=native={}", out_dir);
println!("cargo:rustc-link-search=native=./src");
// Link against our compiled libraries
println!("cargo:rustc-link-lib=static=randomx_all");
println!("cargo:rustc-link-lib=static=randomx_asm");
// Generate Rust bindings to C/C++ code
let bindings = bindgen::Builder::default()
.header("src/randomx.h")
.clang_arg("-I./src")
.allowlist_function("randomx_.*")
.allowlist_type("randomx_.*")
.allowlist_var("RANDOMX_.*")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate bindings");
let out_path = Path::new(&out_dir);
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}