-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.rs
More file actions
255 lines (227 loc) · 9.27 KB
/
Copy pathbuild.rs
File metadata and controls
255 lines (227 loc) · 9.27 KB
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
use ament_rs::{search_paths::get_search_paths, AMENT_PREFIX_PATH_ENV_VAR};
use cargo_toml::Manifest;
use std::collections::{HashMap, HashSet, VecDeque};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs};
fn is_marked_for_inclusion(path: &PathBuf) -> bool {
Manifest::from_path(path)
.map(|manifest| {
manifest
.package
.as_ref()
.and_then(|pkg| pkg.metadata.as_ref())
.and_then(|metadata| metadata.get("ros-env"))
.and_then(|ros_env| ros_env.get("include"))
.and_then(|include| include.as_bool())
.unwrap_or(false)
})
.unwrap_or(false)
}
fn star_dep_names(manifest: &Manifest) -> Vec<String> {
// Find all dependencies for this crate that have a `*` version requirement.
// We will assume that these are other exported dependencies that need symbols
// exposed in their module.
manifest
.dependencies
.iter()
.filter(|(_, version)| version.req() == "*")
.map(|(name, _)| name.to_owned())
.collect()
}
fn star_deps_to_use(manifest: &Manifest) -> String {
star_dep_names(manifest)
.into_iter()
.map(|name| format!("use crate::{name};\n"))
.collect()
}
fn crate_name_from_ament_package_dir(package_dir: &Path) -> &str {
package_dir
.parent()
.and_then(|parent| parent.file_name())
.and_then(|name| name.to_str())
.expect("AMENT package directory should be <prefix>/share/<package>/rust")
}
fn try_rustfmt(path: &Path) {
match Command::new("rustfmt")
.arg("--edition")
.arg("2021")
.arg(path)
.status()
{
Ok(status) if status.success() => {}
Ok(status) => {
println!("cargo:warning=rustfmt exited with status: {status}");
}
Err(err) => {
println!(
"cargo:warning=failed to run rustfmt for {}: {err}",
path.display()
);
}
}
}
fn main() {
println!("cargo:rerun-if-env-changed={AMENT_PREFIX_PATH_ENV_VAR}");
let ament_prefix_paths = get_search_paths().unwrap_or_default();
// Re-export any generated interface crates that we find. AMENT_PREFIX_PATH
// can contain overlays and underlays that provide the same package, so keep
// the first provider according to the search path order.
let mut discovered_packages = HashSet::new();
let export_candidates: Vec<PathBuf> = ament_prefix_paths
.iter()
.map(PathBuf::from)
.flat_map(|base_path| {
// 1. Try to read share/ directory
fs::read_dir(base_path.join("share")).into_iter().flatten()
})
.filter_map(|entry| entry.ok())
.filter(|entry| entry.path().is_dir())
.flat_map(|package_dir| {
// 2. Try to read <package>/rust/ directory
fs::read_dir(package_dir.path().join("rust"))
.into_iter()
.flatten()
})
.filter_map(|entry| entry.ok())
.map(|entry| entry.path())
.filter(|path| path.file_name() == Some(std::ffi::OsStr::new("Cargo.toml")))
.filter(|path| {
path.parent()
.map(crate_name_from_ament_package_dir)
.map(|package| discovered_packages.insert(package.to_owned()))
.unwrap_or(false)
})
.collect();
let candidate_by_package: HashMap<String, PathBuf> = export_candidates
.iter()
.filter_map(|path| {
path.parent()
.map(crate_name_from_ament_package_dir)
.map(|package| (package.to_owned(), path.to_owned()))
})
.collect();
let dependencies_by_package: HashMap<String, Vec<String>> = candidate_by_package
.iter()
.filter_map(|(package, cargo_toml)| {
Manifest::from_path(cargo_toml)
.ok()
.map(|manifest| (package.to_owned(), star_dep_names(&manifest)))
})
.collect();
// Include dependencies of exported packages too. Some distro packages export
// generated crates whose metadata is incomplete, but their generated Rust code
// still imports dependency packages through the ros-env crate root.
let mut included_packages: HashSet<String> = export_candidates
.iter()
.filter(|path| is_marked_for_inclusion(path))
.filter_map(|path| {
path.parent()
.map(crate_name_from_ament_package_dir)
.map(str::to_owned)
})
.collect();
let mut pending_packages: VecDeque<String> = included_packages.iter().cloned().collect();
while let Some(package) = pending_packages.pop_front() {
let Some(dependencies) = dependencies_by_package.get(&package) else {
continue;
};
for dependency in dependencies {
if candidate_by_package.contains_key(dependency)
&& included_packages.insert(dependency.clone())
{
pending_packages.push_back(dependency.clone());
}
}
}
loop {
let invalid_packages: Vec<String> = included_packages
.iter()
.filter(|package| {
dependencies_by_package
.get(*package)
.map(|dependencies| {
dependencies
.iter()
.any(|dependency| !included_packages.contains(dependency))
})
.unwrap_or(false)
})
.cloned()
.collect();
if invalid_packages.is_empty() {
break;
}
for package in invalid_packages {
included_packages.remove(&package);
}
}
let export_crate_tomls: Vec<PathBuf> = export_candidates
.into_iter()
.filter(|path| {
path.parent()
.map(crate_name_from_ament_package_dir)
.map(|package| included_packages.contains(package))
.unwrap_or(false)
})
.collect();
// Make sure the script re-runs if any of the sources we want to include change.
for cargo_toml in &export_crate_tomls {
println!("cargo:rerun-if-changed={}", cargo_toml.display());
if let Some(package_dir) = cargo_toml.parent() {
let src_dir = package_dir.join("src");
println!("cargo:rerun-if-changed={}", src_dir.display());
}
}
let content: String = export_crate_tomls
.iter()
.filter_map(|path| path.parent().map(|p| p.to_path_buf()))
.map(|package_dir| {
let package = crate_name_from_ament_package_dir(&package_dir);
// Find all dependencies for this crate that have a `*` version requirement.
// We will assume that these are other exported dependencies that need symbols
// exposed in their module.
let dependencies: String = Manifest::from_path(package_dir.clone().join("Cargo.toml"))
.iter()
.map(star_deps_to_use)
.collect();
let internal_mods: String = fs::read_dir(package_dir.join("src"))
.into_iter()
.flatten()
.filter_map(|entry| entry.ok())
.filter(|entry| entry.path().is_file())
// Ignore lib.rs and any rmw.rs. lib.rs is only used if the crate is consumed
// independently, and rmw.rs files need their top-level module
// (i.e., msg, srv, action) to exist to be re-exported.
.filter(|entry| {
let name = entry.file_name();
name != "lib.rs" && name != "rmw.rs"
})
// Wrap the inclusion of each file in a module matching the file stem
// so that the generated code can be imported like `ros_env::std_msgs::msgs::Bool`
.filter_map(|e| {
let path = std::path::absolute(e.path()).expect("Failed to get absolute path for idiomatic module");
path.file_stem()
.and_then(|stem| stem.to_str())
.map(|stem| {
let idiomatic_path = path.to_string_lossy().replace('\\', "/");
let parent = path
.parent()
.expect("Failed to create rmw path");
let rmw_path = parent
.join(stem)
.join("rmw.rs")
.to_string_lossy()
.replace('\\', "/");
format!("pub mod {stem} {{ {dependencies} include!(\"{idiomatic_path}\"); pub mod rmw {{ {dependencies} include!(\"{rmw_path}\"); }} }}")
})
})
.collect();
format!("#[allow(unused_imports, missing_docs)]\npub mod {package} {{ {internal_mods} }}")
})
.collect();
let out_path =
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR not set")).join("interfaces.rs");
fs::write(&out_path, content).expect("Failed to write interfaces.rs");
try_rustfmt(&out_path);
}