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
#![feature(macro_reexport)]
extern crate gcc;
#[macro_reexport(init)]
extern crate init_codegen;
use std::path::Path;
use std::fs::File;
use std::io::Write;
use std::env;
pub fn build() {
let out_dir = env::var("OUT_DIR").unwrap();
let package_name = env::var("CARGO_PKG_NAME").unwrap().replace('-', "_");
let package_version = env::var("CARGO_PKG_VERSION").unwrap().replace('.', "_");
let spec = format!("{}_{}", package_name, package_version);
let c_abi_name = format!("_c_init_{}", spec);
let rust_abi_name = format!("_rust_init_{}", spec);
let c_lib_name = format!("lib_init_{}.a", spec);
let c_src_name = format!("init_{}.c", spec);
let source = format!(r#"
extern void {}();
extern void _rust_init_example_lib_0_1_0();
__attribute__ ((constructor, externally_visible))
void {}() {{
{}();
}}
"#, rust_abi_name, c_abi_name, rust_abi_name);
let dest_path = Path::new(&out_dir).join(c_src_name);
{
let mut f = File::create(&dest_path).unwrap();
f.write_all(source.as_bytes()).unwrap();
}
gcc::compile_library(&c_lib_name, &[&dest_path.to_string_lossy()]);
}