This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.rs
43 lines (41 loc) · 1.67 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
use std::env;
use std::path::Path;
use std::process::Command;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let mut args = vec![
"rustyc".to_owned(),
"iec61131-st/*.st".to_owned(),
"-c".to_owned(),
"-o".to_owned(),
format!("{out_dir}/st.o"),
];
if let Ok(target) = env::var("TARGET") {
args.push("--target".to_owned());
args.push(target);
}
if let Ok(optimization) = env::var("PROFILE") {
args.push("-O".to_owned());
if optimization == "release" {
args.push("default".to_owned());
} else {
args.push("none".to_owned());
}
}
rusty::build_with_params(rusty::cli::CompileParameters::parse(&args).unwrap()).unwrap();
Command::new("ar")
.args(["crs", "libst.a", "st.o"])
.current_dir(Path::new(&out_dir))
.status()
.unwrap();
//link the object file
println!("cargo:rustc-link-search=native={out_dir}");
println!("cargo:rustc-link-lib=static=st");
println!("cargo:rerun-if-changed=iec61131-st/*")
//We can link against the st lib gernerated, but this will only be reflected in static libs.
// The shared lib still has to be generated later.
// There is a planned feature in rust to allow whole-archive linking, but i could not get it to
// work (should look something like this : `println!("cargo:rustc-flags=-l static:+whole-archive=st");`)
// The following clang command is equivalent: clang -o libiec.so --shared -Wl,--whole-archive -lst -L. -Wl,--no-whole-archive iec.o
// https://stackoverflow.com/questions/55886779/how-to-link-a-c-library-without-calling-one-of-its-functions
}