forked from ZettaScaleLabs/cyclors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
595 lines (520 loc) · 19.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
extern crate bindgen;
use std::collections::HashSet;
use std::io::{BufRead, BufReader, LineWriter, Write};
use std::path::{Path, PathBuf};
use std::{env, fs};
use std::{ffi::OsStr, fs::metadata, fs::File};
fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut dir_builder = std::fs::DirBuilder::new();
dir_builder.recursive(true);
// Check features
let iceoryx_enabled = is_iceoryx_enabled();
let prefix_symbols_enabled = is_prefix_symbols_enabled();
if iceoryx_enabled && prefix_symbols_enabled {
print!("cargo:warning=iceoryx and prefix_symbols features cannot both be enabled!");
std::process::exit(1);
}
// Determine symbol prefix
let prefix = match prefix_symbols_enabled {
true => {
let mut prefix = env::var("CARGO_PKG_VERSION").unwrap().replace('.', "_");
prefix.insert_str(0, "cyclors_");
prefix.push('_');
prefix
}
false => String::new(),
};
// Build Iceoryx (if enabled)
let mut iceoryx = PathBuf::new();
if iceoryx_enabled {
let iceoryx_src_dir = Path::new("iceoryx/iceoryx_meta");
let iceoryx_out_dir = out_dir.join("iceoryx-build");
dir_builder.create(&iceoryx_out_dir).unwrap();
iceoryx = build_iceoryx(iceoryx_src_dir, &iceoryx_out_dir);
}
// Build Cyclone DDS
let cyclonedds_src_dir = prepare_cyclonedds_src("cyclonedds", &out_dir, &prefix);
let cyclonedds_out_dir = out_dir.join("cyclonedds-build");
dir_builder.create(&cyclonedds_out_dir).unwrap();
let cyclonedds = build_cyclonedds(
&cyclonedds_src_dir,
&cyclonedds_out_dir,
iceoryx.as_os_str(),
);
// Prefix Cyclone DDS library symbols if enabled
let mut symbols = HashSet::new();
if prefix_symbols_enabled {
let cyclonedds_lib = cyclonedds.join("lib");
let ddsc_lib_name = get_library_name("ddsc").unwrap();
let cyclone_symbols = get_defined_symbols(&cyclonedds_lib, &ddsc_lib_name)
.expect("Failed to get symbols from ddsc library!");
symbols.extend(cyclone_symbols);
prefix_symbols(&cyclonedds_lib, &ddsc_lib_name, &prefix, &symbols).unwrap();
}
// Build cyclocut
let cyclocut_src_dir = Path::new("cyclocut");
let cyclocut_out_dir = out_dir.join("cyclocut-build");
dir_builder.create(&cyclocut_out_dir).unwrap();
let cyclocut = build_cyclocut(cyclocut_src_dir, &cyclocut_out_dir, &cyclonedds);
// Prefix Cyclocut library symbols if enabled
if prefix_symbols_enabled {
let cyclocut_lib = cyclocut.join("lib");
let cyclocut_lib_name = get_library_name("cdds-util").unwrap();
let cyclocut_symbols = get_defined_symbols(&cyclocut_lib, &cyclocut_lib_name)
.expect("Failed to get symbols from cdds-util library!");
symbols.extend(cyclocut_symbols);
prefix_symbols(&cyclocut_lib, &cyclocut_lib_name, &prefix, &symbols).unwrap();
}
// Configure bindings build
let cyclonedds_include = cyclonedds.join("include");
let cyclocut_include = cyclocut.join("include");
// C++ library added here to avoid link line ordering issue on some platforms
if iceoryx_enabled {
#[cfg(target_os = "linux")]
println!("cargo:rustc-link-lib=stdc++");
#[cfg(target_os = "macos")]
println!("cargo:rustc-link-lib=c++");
}
let mut bindings = bindgen::Builder::default();
bindings = bindings
.header("wrapper.h")
.clang_arg(format!("-I{}", cyclonedds_include.to_str().unwrap()))
.clang_arg(format!("-I{}", cyclocut_include.to_str().unwrap()))
.generate_comments(false);
// Set link name if prefix enabled
if prefix_symbols_enabled {
#[derive(Debug)]
struct PrefixLinkNameCallback {
prefix: String,
symbols: HashSet<String>,
}
impl bindgen::callbacks::ParseCallbacks for PrefixLinkNameCallback {
fn generated_link_name_override(
&self,
item_info: bindgen::callbacks::ItemInfo<'_>,
) -> Option<String> {
let mut item = String::from("");
#[cfg(target_os = "macos")]
item.push('_');
item.push_str(item_info.name);
match self.symbols.contains(&item) {
true => {
let mut prefix = String::from("");
#[cfg(target_os = "macos")]
prefix.push('_');
prefix.push_str(&self.prefix);
prefix.push_str(item_info.name);
Some(prefix)
}
false => None,
}
}
}
bindings = bindings.parse_callbacks(Box::new(PrefixLinkNameCallback {
prefix: prefix.clone(),
symbols: symbols.clone(),
}));
}
// Add *IMAGE_TLS_DIRECTORY* to blocklist on Windows due to
// https://github.com/rust-lang/rust-bindgen/issues/2179
#[cfg(target_os = "windows")]
let bindings = bindings
.clang_arg("-Wno-invalid-token-paste")
.blocklist_type("^(.*IMAGE_TLS_DIRECTORY.*)$");
// Set link name prefix on additional wrapper functions
generate_template_src(&prefix, &out_dir).unwrap();
// Generate bindings
let bindings = bindings.generate().expect("Unable to generate bindings");
bindings
.write_to_file(out_dir.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
fn is_iceoryx_enabled() -> bool {
#[cfg(feature = "iceoryx")]
{
#[cfg(target_os = "windows")]
{
print!("cargo:warning=Cyclone DDS Iceoryx PSMX plugin is not supported on Windows!");
std::process::exit(1);
}
true
}
#[cfg(not(feature = "iceoryx"))]
{
false
}
}
fn is_prefix_symbols_enabled() -> bool {
#[cfg(feature = "prefix_symbols")]
{
true
}
#[cfg(not(feature = "prefix_symbols"))]
{
false
}
}
fn build_iceoryx(src_dir: &Path, out_dir: &Path) -> PathBuf {
let mut iceoryx = cmake::Config::new(src_dir);
// Force compilation of Iceoryx in release mode on Windows due to
// https://github.com/rust-lang/rust/issues/39016
#[cfg(all(debug_assertions, target_os = "windows"))]
let iceoryx = iceoryx.profile("Release");
let iceoryx_path = iceoryx
.define("BUILD_SHARED_LIBS", "OFF")
.out_dir(out_dir)
.build();
// Add iceoryx lib to link
let iceoryx_lib = iceoryx_path.join("lib");
println!("cargo:rustc-link-search=native={}", iceoryx_lib.display());
println!("cargo:rustc-link-lib=static=iceoryx_hoofs");
println!("cargo:rustc-link-lib=static=iceoryx_posh");
println!("cargo:rustc-link-lib=static=iceoryx_platform");
#[cfg(target_os = "linux")]
println!("cargo:rustc-link-lib=acl");
iceoryx_path
}
fn build_cyclonedds(src_dir: &Path, out_dir: &Path, iceoryx_path: &OsStr) -> PathBuf {
// Create Cyclone DDS build initial config
let mut cyclonedds = cmake::Config::new(src_dir);
let mut cyclonedds = cyclonedds.out_dir(out_dir);
// Configure cyclonedds build
cyclonedds = cyclonedds
.define("BUILD_SHARED_LIBS", "OFF")
.define("BUILD_IDLC", "OFF")
.define("BUILD_DDSPERF", "OFF")
.define("ENABLE_LTO", "NO")
.define("ENABLE_SSL", "NO")
.define("ENABLE_SECURITY", "NO")
.define("CMAKE_INSTALL_LIBDIR", "lib");
if !iceoryx_path.is_empty() {
cyclonedds = cyclonedds
.env("iceoryx_hoofs_DIR", iceoryx_path)
.env("iceoryx_posh_DIR", iceoryx_path)
.define("ENABLE_ICEORYX", "YES");
} else {
cyclonedds = cyclonedds.define("ENABLE_ICEORYX", "NO");
}
// Force compilation of Cyclone DDS in release mode on Windows due to
// https://github.com/rust-lang/rust/issues/39016
#[cfg(all(debug_assertions, target_os = "windows"))]
let cyclonedds = cyclonedds.profile("Release");
// Build cyclonedds
let cyclonedds_path = cyclonedds.build();
// Add cyclonedds lib to link
let cyclonedds_lib = cyclonedds_path.join("lib");
println!(
"cargo:rustc-link-search=native={}",
cyclonedds_lib.display()
);
println!("cargo:rustc-link-lib=static=ddsc");
// Add Windows libraries required by Cyclone to link
#[cfg(target_os = "windows")]
{
println!("cargo:rustc-link-lib=Iphlpapi");
println!("cargo:rustc-link-lib=DbgHelp");
println!("cargo:rustc-link-lib=Bcrypt");
}
cyclonedds_path
}
fn build_cyclocut(src_dir: &Path, out_dir: &Path, cyclonedds_dir: &Path) -> PathBuf {
let mut cyclocut = cmake::Config::new(src_dir);
// Force compilation of Cyclocut in release mode on Windows due to
// https://github.com/rust-lang/rust/issues/39016
#[cfg(all(debug_assertions, target_os = "windows"))]
let cyclocut = cyclocut.profile("Release");
let cyclonedds_include = cyclonedds_dir.join("include");
let cyclonedds_lib = cyclonedds_dir.join("lib");
let cyclocut_path = cyclocut
.env("CYCLONE_INCLUDE", &cyclonedds_include)
.env("CYCLONE_LIB", &cyclonedds_lib)
.define("CYCLONE_INCLUDE", cyclonedds_include.clone())
.define("CYCLONE_LIB", cyclonedds_lib.clone())
.define("BUILD_SHARED_LIBS", "OFF")
.define("CMAKE_INSTALL_LIBDIR", "lib")
.out_dir(out_dir)
.build();
// Add cyclocut lib to link
let cyclocut_lib = cyclocut_path.join("lib");
println!("cargo:rustc-link-search=native={}", cyclocut_lib.display());
println!("cargo:rustc-link-lib=static=cdds-util");
cyclocut_path
}
#[allow(unused_variables)]
fn prepare_cyclonedds_src(src_dir: &str, out_dir: &Path, prefix: &str) -> PathBuf {
#[cfg(target_os = "windows")]
if !prefix.is_empty() {
let mut dst_dir = src_dir.to_string();
dst_dir.push_str("-src");
let dst_dir = out_dir.join(dst_dir);
// Delete copied source directory if it already exists
if dst_dir.exists() {
fs::remove_dir_all(dst_dir.clone()).unwrap();
}
copy_dir_recursive(&PathBuf::from(src_dir), &dst_dir).unwrap();
// Prefix tls_callback_func in cyclonedds-src/src/ddsrt/src/cdtors.c
let mut prefixed_func = prefix.to_string();
prefixed_func.push_str("tls_callback_func");
let cdtors = dst_dir
.join("src")
.join("ddsrt")
.join("src")
.join("cdtors.c");
replace_in_file(&cdtors, "tls_callback_func", &prefixed_func).unwrap();
return dst_dir;
}
PathBuf::from(src_dir)
}
#[allow(unused)]
fn copy_dir_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
println!(
"src = {}, dir = {}",
src.to_str().unwrap(),
dst.to_str().unwrap()
);
if !dst.exists() {
fs::create_dir_all(dst)?;
}
for entry in fs::read_dir(src)? {
let entry = entry?;
let path = entry.path();
let dest_path = dst.join(entry.file_name());
if path.is_dir() {
copy_dir_recursive(&path, &dest_path)?;
} else {
fs::copy(&path, &dest_path)?;
}
}
Ok(())
}
#[allow(unused)]
fn replace_in_file(file_path: &Path, from: &str, to: &str) -> std::io::Result<()> {
// Read the file content into a string
let content = fs::read_to_string(file_path)?;
// Replace all occurrences of `from` with `to`
let new_content = content.replace(from, to);
// Write the modified content back to the file
let mut file = fs::OpenOptions::new()
.write(true)
.truncate(true) // Clear the file before writing
.open(file_path)?;
file.write_all(new_content.as_bytes())?;
Ok(())
}
#[allow(unused_variables)]
fn get_library_name(lib_name: &str) -> Option<String> {
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let mut file_name = String::from("lib");
file_name.push_str(lib_name);
file_name.push_str(".a");
Some(file_name)
}
#[cfg(target_os = "windows")]
{
let mut file_name = String::from(lib_name);
file_name.push_str(".lib");
Some(file_name)
}
#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
None
}
fn get_defined_symbols(lib_dir: &Path, lib_name: &str) -> Result<HashSet<String>, String> {
let lib_path = lib_dir.to_path_buf().join(lib_name);
let mut nm_file_name = lib_name.to_owned();
nm_file_name.push_str(".nm");
let symbol_file_path = lib_dir.to_path_buf().join(nm_file_name);
let mut nm = cmake::Config::new("nm");
nm.build_target("read_symbols")
.define("LIB_PATH", lib_path.clone())
.build();
// Check for unexpected errors in stderr.txt
let mut stderr_file_name = lib_name.to_owned();
stderr_file_name.push_str(".nm.stderr");
let stderr_file_path = lib_dir.to_path_buf().join(stderr_file_name);
check_nm_stderr(&stderr_file_path).unwrap();
match File::open(symbol_file_path.clone()) {
Ok(symbol_file) => {
let reader = BufReader::new(symbol_file);
let mut result: HashSet<String> = HashSet::new();
for line in reader.lines() {
match line {
Ok(line) => {
let tokens: Vec<&str> = line.split_whitespace().collect();
let symbol = *tokens.last().unwrap();
#[cfg(target_os = "windows")]
if !symbol.ends_with("tls_callback_func") {
result.insert(String::from(symbol));
}
#[cfg(not(target_os = "windows"))]
result.insert(String::from(symbol));
}
Err(_) => return Err(format!("Failed to run nm on library {}", lib_name)),
}
}
Ok(result)
}
Err(_) => {
println!(
"nm file open problem: {}",
symbol_file_path.to_str().unwrap()
);
Err(format!("Failed to run nm on library {}", lib_name))
}
}
}
fn check_nm_stderr(stderr: &Path) -> Result<(), String> {
match File::open(stderr) {
Ok(file) => {
let reader = BufReader::new(file);
for line in reader.lines() {
match line {
Ok(line) => {
// Some object files within the library may report no symbols - this is okay
if !line.ends_with(": no symbols") {
return Err(format!(
"nm completed with errors - see {} for details",
stderr.to_str().unwrap()
));
}
}
Err(_) => {
return Err(format!(
"Failed to read nm stderr file: {}",
stderr.to_str().unwrap()
))
}
}
}
}
Err(_) => {
return Err(format!(
"Failed to open nm stderr file: {}",
stderr.to_str().unwrap()
));
}
}
Ok(())
}
fn prefix_symbols(
lib_dir: &Path,
lib_name: &str,
prefix: &str,
symbols: &HashSet<String>,
) -> Result<(), String> {
let mut objcopy_file_name = lib_name.to_owned();
objcopy_file_name.push_str(".objcopy");
let lib_file_path = lib_dir.to_path_buf().join(lib_name);
let symbol_file_path = lib_dir.to_path_buf().join(objcopy_file_name);
match File::create(symbol_file_path.clone()) {
Ok(symbol_file) => {
let mut symbol_file = LineWriter::new(symbol_file);
for symbol in symbols {
let mut symbol_arg = symbol.clone();
#[cfg(target_os = "macos")]
{
let mut symbol_stripped = symbol.clone();
symbol_stripped.remove(0);
symbol_arg.push(' ');
symbol_arg.push('_');
symbol_arg.push_str(prefix);
symbol_arg.push_str(&symbol_stripped);
}
#[cfg(not(target_os = "macos"))]
{
symbol_arg.push(' ');
symbol_arg.push_str(prefix);
symbol_arg.push_str(symbol);
}
symbol_arg.push('\n');
if symbol_file.write_all(symbol_arg.as_bytes()).is_err() {
return Err(format!(
"Failed to write symbol file for library {}",
lib_name
));
}
}
if symbol_file.flush().is_err() {
return Err(format!(
"Failed to write symbol file for library {}",
lib_name
));
}
let mut objcopy = cmake::Config::new("objcopy");
objcopy
.build_target("mangle_library")
.define("LIB_PATH", lib_file_path.clone())
.define("SYMBOL_FILE_PATH", symbol_file_path.clone())
.build();
// Check for unexpected errors in stderr.txt
let mut stderr_file_name = lib_name.to_owned();
stderr_file_name.push_str(".objcopy.stderr");
let stderr_file_path = lib_dir.to_path_buf().join(stderr_file_name);
check_objcopy_stderr(&stderr_file_path).unwrap();
Ok(())
}
Err(_) => Err(format!(
"Failed to create symbol file for library {}",
lib_name
)),
}
}
fn check_objcopy_stderr(stderr: &Path) -> Result<(), String> {
if let Ok(metadata) = metadata(stderr) {
if metadata.is_file() {
if metadata.len() > 0 {
println!("File exists and has a size greater than 0.");
Err(format!(
"Objcopy command failed with errors - see {} for details",
stderr.to_str().unwrap()
))
} else {
Ok(())
}
} else {
Err(format!(
"Objcopy stderr file is not a file: {}",
stderr.to_str().unwrap()
))
}
} else {
Err(format!(
"Failed to read objcopy stderr file metadata: {}",
stderr.to_str().unwrap()
))
}
}
fn generate_template_src(prefix: &str, out_dir: &Path) -> Result<(), String> {
let src_path = Path::new("src/functions.template");
let dst_path = out_dir.join("functions.rs");
match fs::read_to_string(src_path) {
Ok(mut contents) => {
contents = contents.replace("<prefix>", prefix);
match File::create(&dst_path) {
Ok(mut file) => {
if file.write_all(contents.as_bytes()).is_err() {
let path = dst_path.to_str().unwrap();
return Err(format!(
"Failed to write the modified content to the destination file {}",
path
));
}
println!("cargo:rerun-if-changed=src/lib.rs");
println!("cargo:rerun-if-changed=src/functions.template");
Ok(())
}
Err(_) => {
let path = dst_path.to_str().unwrap();
Err(format!(
"Failed to open the destination file ({}) for writing",
path
))
}
}
}
Err(_) => Err(String::from(
"Failed to read the source file src/functions.template",
)),
}
}