-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.rs
242 lines (206 loc) · 9.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
fn main() {
println!("cargo:rerun-if-env-changed=PYLON_VERSION");
println!("cargo:rerun-if-env-changed=PYLON_ROOT");
println!("cargo:rerun-if-env-changed=PYLON_DEV_DIR");
println!("cargo:rerun-if-env-changed=PYLONFRAMEWORKDIR");
let pylon_major_version: Option<u8> =
std::env::var_os("PYLON_VERSION").map(|s| s.into_string().unwrap().parse::<u8>().unwrap());
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/lib.rs");
println!("cargo:rerun-if-changed=include/catcher.h");
println!("cargo:rerun-if-changed=include/pylon-cxx-rs.h");
println!("cargo:rerun-if-changed=src/pylon-cxx-rs.cc");
let mut build = cxx_build::bridge("src/lib.rs");
build
.file("src/pylon-cxx-rs.cc")
.warnings(false)
.cpp(true)
.include("include");
#[cfg(feature = "stream")]
build.define("FEATURE_STREAM", None);
#[cfg(target_os = "linux")]
{
let pylon_root = match std::env::var("PYLON_ROOT") {
Ok(val) => val,
Err(_) => match pylon_major_version {
Some(5) => "/opt/pylon5",
Some(6) | None => "/opt/pylon",
Some(version) => panic!("unsupported pylon version: {}", version),
}
.into(),
};
let expected_major_version = match pylon_root.as_str() {
"/opt/pylon5" => Some(5),
"/opt/pylon" => Some(6),
_ => None,
};
let pylon_major_version = match (expected_major_version, pylon_major_version) {
(Some(expected_major_version), Some(actual_major_version)) => {
assert_eq!(expected_major_version, actual_major_version);
actual_major_version
}
(Some(v), None) | (None, Some(v)) => v,
(None, None) => 6,
};
let pylon_root = std::path::PathBuf::from(pylon_root);
let include1 = pylon_root.join("include");
build.flag("-std=c++14").include(&include1);
let mut lib_dir = pylon_root;
if pylon_major_version == 5 {
lib_dir.push("lib64");
} else {
lib_dir.push("lib");
}
let dir_str = lib_dir.to_str().unwrap();
println!("cargo:rustc-link-search=native={}", dir_str);
println!("cargo:rustc-link-lib=pylonc");
// The Basler docs want the rest of these libraries to be automatically
// found using rpath linker args, but sending options to the linker in rust
// requires the unstable link_args feature. So we specify them manually.
// See https://github.com/rust-lang/cargo/issues/5077
println!("cargo:rustc-link-lib=pylonbase");
println!("cargo:rustc-link-lib=pylonutility");
println!("cargo:rustc-link-lib=gxapi");
if pylon_major_version == 5 {
enum PylonVersion {
V5_0,
V5_1,
V5_2,
Unknown,
}
let mut so_file_for_5_2 = lib_dir.clone();
so_file_for_5_2.push("libpylon_TL_usb-5.2.0.so");
let mut so_file_for_5_1 = lib_dir.clone();
so_file_for_5_1.push("libGenApi_gcc_v3_1_Basler_pylon_v5_1");
so_file_for_5_1.set_extension("so");
eprint!(
"# pylon build: checking for file {}...",
so_file_for_5_2.display()
);
let version = if so_file_for_5_2.exists() {
eprintln!("found");
PylonVersion::V5_2
} else {
eprintln!("not found");
eprint!(
"# pylon build: checking for file {}...",
so_file_for_5_1.display()
);
if so_file_for_5_1.exists() {
eprintln!("found");
PylonVersion::V5_1
} else {
eprintln!("not found");
let mut so_file_for_5_0 = lib_dir.clone();
so_file_for_5_0.push("libGenApi_gcc_v3_0_Basler_pylon_v5_0");
so_file_for_5_0.set_extension("so");
eprint!(
"# pylon build: checking for file {}...",
so_file_for_5_0.display()
);
if so_file_for_5_0.exists() {
eprintln!("found");
PylonVersion::V5_0
} else {
eprintln!("not found");
PylonVersion::Unknown
}
}
};
match version {
PylonVersion::V5_0 => {
println!("cargo:rustc-link-lib=GenApi_gcc_v3_0_Basler_pylon_v5_0");
println!("cargo:rustc-link-lib=GCBase_gcc_v3_0_Basler_pylon_v5_0");
println!("cargo:rustc-link-lib=Log_gcc_v3_0_Basler_pylon_v5_0");
println!("cargo:rustc-link-lib=MathParser_gcc_v3_0_Basler_pylon_v5_0");
println!("cargo:rustc-link-lib=XmlParser_gcc_v3_0_Basler_pylon_v5_0");
println!("cargo:rustc-link-lib=NodeMapData_gcc_v3_0_Basler_pylon_v5_0");
}
PylonVersion::V5_1 => {
println!("cargo:rustc-link-lib=GenApi_gcc_v3_1_Basler_pylon_v5_1");
println!("cargo:rustc-link-lib=GCBase_gcc_v3_1_Basler_pylon_v5_1");
println!("cargo:rustc-link-lib=Log_gcc_v3_1_Basler_pylon_v5_1");
println!("cargo:rustc-link-lib=MathParser_gcc_v3_1_Basler_pylon_v5_1");
println!("cargo:rustc-link-lib=XmlParser_gcc_v3_1_Basler_pylon_v5_1");
println!("cargo:rustc-link-lib=NodeMapData_gcc_v3_1_Basler_pylon_v5_1");
}
PylonVersion::V5_2 => {
println!("cargo:rustc-link-lib=GenApi_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=GCBase_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=Log_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=MathParser_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=XmlParser_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=NodeMapData_gcc_v3_1_Basler_pylon");
}
PylonVersion::Unknown => {
panic!("could not detect pylon library version");
}
}
} else {
assert_eq!(pylon_major_version, 6);
// The following are for Pylon 6.1 and may need to be updated for other versions.
println!("cargo:rustc-link-lib=GenApi_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=GCBase_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=Log_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=MathParser_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=XmlParser_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=NodeMapData_gcc_v3_1_Basler_pylon");
}
}
#[cfg(target_os = "macos")]
{
match pylon_major_version {
Some(6) | Some(7) | None => {
// directory with `pylon.framework`
let pylon_framework_dir = match std::env::var_os("PYLONFRAMEWORKDIR") {
Some(val) => std::path::PathBuf::from(val),
None => "/Library/Frameworks".into(),
};
assert!(
pylon_major_version == Some(6)
|| pylon_major_version == Some(7)
|| pylon_major_version.is_none()
);
let flag = format!("-F{}", pylon_framework_dir.display());
println!("cargo:rustc-link-arg=-Wl,-ld_classic");
let lib_dir = pylon_framework_dir.join("pylon.framework/Libraries");
println!("cargo:rustc-link-search={}", lib_dir.display());
println!("cargo:rustc-link-lib=pylonbase");
println!("cargo:rustc-link-lib=pylonutility");
println!("cargo:rustc-link-lib=GenApi_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=GCBase_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=Log_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=MathParser_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=XmlParser_gcc_v3_1_Basler_pylon");
println!("cargo:rustc-link-lib=NodeMapData_gcc_v3_1_Basler_pylon");
build
.flag("-std=c++14")
.include(pylon_framework_dir.join("pylon.framework/Versions/A/Headers/GenICam"))
.flag(&flag);
}
Some(version) => panic!("unsupported pylon version: {}", version),
}
}
#[cfg(target_os = "windows")]
{
use std::path::PathBuf;
let pylon_dev_dir = match std::env::var_os("PYLON_DEV_DIR") {
Some(val) => PathBuf::from(val),
None => match pylon_major_version {
Some(5) => PathBuf::from(r#"C:\Program Files\Basler\pylon 5\Development"#),
Some(6) | None => PathBuf::from(r#"C:\Program Files\Basler\pylon 6\Development"#),
Some(version) => panic!("unsupported pylon version: {}", version),
},
};
let mut include_dir = pylon_dev_dir.clone();
include_dir.push("include");
let mut pylon_include_dir = include_dir.clone();
pylon_include_dir.push("pylon");
let mut lib_dir = pylon_dev_dir;
lib_dir.push("lib");
lib_dir.push("x64");
println!("cargo:rustc-link-search={}", lib_dir.display());
build.include(include_dir);
}
build.compile("pyloncxxrs");
}