-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.rs
452 lines (389 loc) · 14.8 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use std::collections::HashMap;
use std::path::Path;
use std::process::Command;
use std::thread::available_parallelism;
use std::{env, fs};
use cmake::Config;
fn main() {
build();
}
const TON_MONOREPO_REVISION: &str = "v2024.10";
const TON_MONOREPO_DIR: &str = "./ton";
#[cfg(not(feature = "shared-tonlib"))]
fn build() {
#[cfg(feature = "with_debug_info")]
let cmake_build_type = "Debug";
#[cfg(not(feature = "with_debug_info"))]
let cmake_build_type = "Release";
#[cfg(feature = "no_avx512")]
disable_avx512_for_rustc();
env::set_var("TON_MONOREPO_REVISION", TON_MONOREPO_REVISION);
println!("cargo:rerun-if-env-changed=TON_MONOREPO_REVISION");
println!("cargo:rerun-if-changed=build.rs");
// cleanup tonlib after previous build
if Path::new(TON_MONOREPO_DIR).exists() {
let _ = fs::remove_dir_all(TON_MONOREPO_DIR);
}
let clone_status = Command::new("git")
.args([
"clone",
"--branch",
TON_MONOREPO_REVISION,
"--depth",
"1", // get only the latest commit
"--recurse-submodules", // clone submodules as well
"--shallow-submodules", // get only the latest commit of submodules
"https://github.com/ston-fi/ton",
TON_MONOREPO_DIR,
])
.status()
.unwrap();
if !clone_status.success() {
// fallback to clone entire repo and then checkout desired commit
let full_clone_status = Command::new("git")
.args([
"clone",
"--recurse-submodules", // clone submodules as well
"--shallow-submodules", // get only the latest commit of submodules
"https://github.com/ston-fi/ton",
TON_MONOREPO_DIR,
])
.status()
.unwrap();
if full_clone_status.success() {
println!("Cloned repository successfully!");
} else {
panic!("Failed to clone repository!");
}
};
let checkout_status = Command::new("git")
.current_dir(TON_MONOREPO_DIR)
.args(["checkout", TON_MONOREPO_REVISION])
.status()
.unwrap();
if checkout_status.success() {
println!("Cloned and checked out specific commit successfully!");
} else {
panic!("Failed to checkout specific commit!");
}
let update_submodules_status = Command::new("git")
.current_dir(TON_MONOREPO_DIR)
.args(["submodule", "update", "--init", "--recursive"])
.status()
.unwrap();
if !update_submodules_status.success() {
panic!("Git update submodules for TON repo fail");
}
if cfg!(target_os = "macos") {
if Command::new("brew").args(["-h"]).output().is_err() {
panic!("brew is not available. Please install it to proceed");
}
let mut dep_paths = HashMap::new();
for dep in &["openssl@3", "lz4", "pkgconfig", "libsodium", "secp256k1"] {
let dep_installed = Command::new("brew")
.args(["--prefix", dep])
.output()
.unwrap();
if !dep_installed.status.success() {
panic!(
"{} is not installed. To install: `brew install {}`",
dep, dep
);
} else {
dep_paths.insert(
dep.to_string(),
std::str::from_utf8(dep_installed.stdout.as_slice())
.unwrap()
.trim()
.to_string(),
);
}
}
let openssl = &dep_paths["openssl@3"];
let libsodium = &dep_paths["libsodium"];
let secp256k1 = &dep_paths["secp256k1"];
env::set_var("OPENSSL_ROOT_DIR", openssl);
env::set_var("OPENSSL_INCLUDE_DIR", format!("{openssl}/include"));
env::set_var("OPENSSL_CRYPTO_LIBRARY", format!("{openssl}/lib"));
env::set_var("CXX", "clang++");
println!("cargo:rustc-link-search=native={openssl}/lib");
println!("cargo:rustc-link-search=native={libsodium}/lib");
println!("cargo:rustc-link-search=native={secp256k1}/lib");
}
env::set_var("LD_LIBRARY_PATH", "lib/x86_64-linux-gnu");
build_tonlibjson(cmake_build_type);
build_emulator(cmake_build_type);
}
fn build_tonlibjson(cmake_build_type: &str) {
let mut cfg = Config::new(TON_MONOREPO_DIR);
let mut dst = cfg
.configure_arg("-DTON_ONLY_TONLIB=true")
.configure_arg("-DBUILD_SHARED_LIBS=false")
.define("TON_ONLY_TONLIB", "OFF")
.define("BUILD_SHARED_LIBS", "OFF")
.define("PORTABLE", "1")
.define("CMAKE_BUILD_TYPE", cmake_build_type)
.define("CMAKE_C_FLAGS", "-w")
.define("CMAKE_CXX_FLAGS", "-w")
// multi-thread build used to fail compilation. Please try comment out next 2 lines if you have build errors
.build_arg("-j")
.build_arg(available_parallelism().unwrap().get().to_string())
.configure_arg("-Wno-dev")
.build_target("tonlibjson")
.always_configure(true)
.very_verbose(false);
#[cfg(feature = "no_avx512")]
disable_avx512_for_gcc(dst);
if cfg!(target_os = "macos") {
let brew_prefix_output = Command::new("brew").arg("--prefix").output().unwrap();
let brew_prefix = String::from_utf8(brew_prefix_output.stdout).unwrap();
let lib_arg = format!("-DCMAKE_EXE_LINKER_FLAGS=-L{}/lib", brew_prefix.trim());
dst = dst.configure_arg(lib_arg)
}
let dst = dst.build();
println!("cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu");
println!("cargo:rustc-link-search=native=/usr/include");
println!("cargo:rustc-link-search=native=/lib/x86_64-linux-gnu");
if cfg!(target_os = "macos") {
println!("cargo:rustc-link-lib=dylib=c++");
println!("cargo:rustc-link-arg=-lc++");
} else if cfg!(target_os = "linux") {
println!("cargo:rustc-link-lib=dylib=stdc++");
println!("cargo:rustc-link-arg=-lstdc++");
}
println!(
"cargo:rustc-link-search=native={}/build/keys",
dst.display()
);
println!("cargo:rustc-link-lib=static=keys");
println!(
"cargo:rustc-link-search=native={}/build/lite-client",
dst.display()
);
println!("cargo:rustc-link-lib=static=lite-client-common");
println!(
"cargo:rustc-link-search=native={}/build/adnl",
dst.display()
);
println!("cargo:rustc-link-lib=static=adnllite");
println!(
"cargo:rustc-link-search=native={}/build/tdactor",
dst.display()
);
println!("cargo:rustc-link-lib=static=tdactor");
println!(
"cargo:rustc-link-search=native={}/build/tdutils",
dst.display()
);
println!("cargo:rustc-link-lib=static=tdutils");
println!("cargo:rustc-link-lib=dylib=sodium");
println!("cargo:rustc-link-lib=dylib=secp256k1");
println!("cargo:rustc-link-lib=static=tl_lite_api");
println!("cargo:rustc-link-lib=static=tl_api");
println!("cargo:rustc-link-lib=static=tl_tonlib_api_json");
println!("cargo:rustc-link-lib=static=tl_tonlib_api");
println!(
"cargo:rustc-link-search=native={}/build/tdnet",
dst.display()
);
println!("cargo:rustc-link-lib=static=tdnet");
println!(
"cargo:rustc-link-search=native={}/build/tl-utils",
dst.display()
);
println!("cargo:rustc-link-lib=static=tl-utils");
println!("cargo:rustc-link-lib=static=tl-lite-utils");
println!(
"cargo:rustc-link-search=native={}/build/crypto",
dst.display()
);
println!("cargo:rustc-link-lib=static=smc-envelope");
println!("cargo:rustc-link-lib=static=ton_block");
println!("cargo:rustc-link-lib=static=ton_crypto");
println!("cargo:rustc-link-lib=static=ton_crypto_core");
println!(
"cargo:rustc-link-search=native={}/build/tddb",
dst.display()
);
println!("cargo:rustc-link-lib=static=tddb_utils");
println!(
"cargo:rustc-link-search=native={}/build/third-party/crc32c",
dst.display()
);
println!("cargo:rustc-link-lib=static=crc32c");
println!(
"cargo:rustc-link-search=native={}/build/third-party/blst",
dst.display()
);
println!("cargo:rustc-link-lib=static=blst");
println!(
"cargo:rustc-link-search=native={}/build/emulator",
dst.display()
);
println!("cargo:rustc-link-lib=static=emulator_static");
println!(
"cargo:rustc-link-search=native={}/build/tonlib",
dst.display()
);
println!("cargo:rustc-link-lib=static=tonlibjson");
println!("cargo:rustc-link-lib=static=tonlib");
println!("cargo:rustc-link-lib=static=tonlibjson_private");
}
fn build_emulator(cmake_build_type: &str) {
let mut cfg = Config::new(TON_MONOREPO_DIR);
let mut dst = cfg
.configure_arg("-DTON_ONLY_TONLIB=true")
.configure_arg("-DBUILD_SHARED_LIBS=false")
.define("TON_ONLY_TONLIB", "ON")
.define("BUILD_SHARED_LIBS", "OFF")
.define("PORTABLE", "1")
.define("CMAKE_BUILD_TYPE", cmake_build_type)
.define("CMAKE_C_FLAGS", "-w")
.define("CMAKE_CXX_FLAGS", "-w")
// multi-thread build used to fail compilation. Please try comment out next 2 lines if you have build errors
.build_arg("-j")
.build_arg(available_parallelism().unwrap().get().to_string())
.configure_arg("-Wno-dev")
.build_target("emulator")
.always_configure(true)
.very_verbose(false);
#[cfg(feature = "no_avx512")]
disable_avx512_for_gcc(dst);
if cfg!(target_os = "macos") {
let brew_prefix_output = Command::new("brew").arg("--prefix").output().unwrap();
let brew_prefix = String::from_utf8(brew_prefix_output.stdout).unwrap();
let lib_arg = format!("-DCMAKE_EXE_LINKER_FLAGS=-L{}/lib", brew_prefix.trim());
dst = dst.configure_arg(lib_arg)
}
let dst = dst.build();
if cfg!(target_os = "macos") {
println!("cargo:rustc-link-lib=dylib=c++");
println!("cargo:rustc-link-arg=-lc++");
} else if cfg!(target_os = "linux") {
println!("cargo:rustc-link-lib=dylib=stdc++");
println!("cargo:rustc-link-arg=-lstdc++");
}
println!(
"cargo:rustc-link-search=native={}/build/tdutils",
dst.display()
);
println!("cargo:rustc-link-lib=static=tdutils");
println!(
"cargo:rustc-link-search=native={}/build/keys",
dst.display()
);
println!("cargo:rustc-link-lib=static=keys");
println!(
"cargo:rustc-link-search=native={}/build/lite-client",
dst.display()
);
println!("cargo:rustc-link-lib=static=lite-client-common");
println!(
"cargo:rustc-link-search=native={}/build/adnl",
dst.display()
);
println!("cargo:rustc-link-lib=static=adnllite");
println!(
"cargo:rustc-link-search=native={}/build/tdactor",
dst.display()
);
println!("cargo:rustc-link-lib=static=tdactor");
println!(
"cargo:rustc-link-search=native={}/build/tdutils",
dst.display()
);
println!("cargo:rustc-link-lib=static=tdutils");
println!(
"cargo:rustc-link-search=native={}/build/third-party/abseil-cpp/absl",
dst.display()
);
println!("cargo:rustc-link-search=native={}/build/tl", dst.display());
println!("cargo:rustc-link-lib=static=tl_lite_api");
println!("cargo:rustc-link-lib=static=tl_api");
println!("cargo:rustc-link-lib=static=tl_tonlib_api_json");
println!("cargo:rustc-link-lib=static=tl_tonlib_api");
println!(
"cargo:rustc-link-search=native={}/build/keys",
dst.display()
);
println!("cargo:rustc-link-lib=static=keys");
println!(
"cargo:rustc-link-search=native={}/build/crypto",
dst.display()
);
println!("cargo:rustc-link-lib=static=smc-envelope");
println!("cargo:rustc-link-lib=static=ton_block");
println!("cargo:rustc-link-lib=static=ton_crypto");
println!("cargo:rustc-link-lib=static=ton_crypto_core");
println!("cargo:rustc-link-lib=crypto");
println!(
"cargo:rustc-link-search=native={}/build/lite-client",
dst.display()
);
println!("cargo:rustc-link-lib=static=lite-client-common");
println!(
"cargo:rustc-link-search=native={}/build/tl-utils",
dst.display()
);
println!("cargo:rustc-link-lib=static=tl-utils");
println!("cargo:rustc-link-lib=static=tl-lite-utils");
println!(
"cargo:rustc-link-search=native={}/build/third-party/crc32c",
dst.display()
);
println!("cargo:rustc-link-lib=static=crc32c");
println!(
"cargo:rustc-link-search=native={}/build/third-party/blst",
dst.display()
);
println!("cargo:rustc-link-lib=static=blst");
println!("cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu");
println!("cargo:rustc-link-search=native=/usr/include");
println!("cargo:rustc-link-search=native=/lib/x86_64-linux-gnu");
println!(
"cargo:rustc-link-search=native={}/build/tdutils",
dst.display()
);
println!("cargo:rustc-link-lib=static=tdutils");
println!("cargo:rustc-link-lib=dylib=secp256k1");
println!("cargo:rustc-link-lib=dylib=sodium");
println!("cargo:rustc-link-search=native={}/build/tl", dst.display());
println!("cargo:rustc-link-lib=static=tl_lite_api");
println!("cargo:rustc-link-lib=static=tl_api");
println!("cargo:rustc-link-lib=static=tl_tonlib_api_json");
println!("cargo:rustc-link-lib=static=tl_tonlib_api");
println!(
"cargo:rustc-link-search=native={}/build/third-party/crc32c",
dst.display()
);
println!("cargo:rustc-link-lib=static=crc32c");
println!(
"cargo:rustc-link-search=native={}/build/emulator",
dst.display()
);
println!("cargo:rustc-link-lib=static=emulator_static");
println!("cargo:rustc-link-lib=static=emulator");
}
#[cfg(feature = "shared-tonlib")]
fn build() {
println!("cargo:rustc-link-lib=tonlibjson.0.5");
}
// for clang we just ignore unknown instructions
#[cfg(feature = "no_avx512")]
fn disable_avx512_for_gcc(dst: &mut Config) -> &mut Config {
let disable_avx512 = "-mno-avx512f -mno-avx512dq -mno-avx512cd -mno-avx512bw -mno-avx512vl -mno-avx512ifma -mno-avx512vbmi -mno-vpclmulqdq";
let compiler_flags = format!("-Wno-unused-command-line-argument {}", disable_avx512);
for flag in &[
"CMAKE_C_FLAGS",
"CMAKE_CXX_FLAGS",
"CMAKE_C_FLAGS_RELEASE",
"CMAKE_CXX_FLAGS_RELEASE",
] {
dst.define(flag, compiler_flags.as_str());
}
dst.asmflag(disable_avx512)
}
#[cfg(feature = "no_avx512")]
fn disable_avx512_for_rustc() {
println!("cargo:rustc-env=RUSTFLAGS=-C target-feature=-avx512f,-avx512dq,-avx512cd,-avx512bw,-avx512vl,-avx512ifma,-avx512vbmi,-vpclmulqdq");
}