Skip to content

Commit 98ea99f

Browse files
committed
make git optional to support offline and packaged builds
Previously the build script unconditionally required `git` to update submodules and apply patches. This breaks offline builds for vendored workspaces and environments without git installed where the files are already present but `.git` is absent. The new build flow is as follows: - Conditionally run `git submodule` only if the `mavlink` directory is a submodule. - Add an explicit existence check for the `message_definitions/v1.0` directory to provide a clear error message if submodule was omitted. Signed-off-by: Onur Özkan <work@onurozkan.dev>
1 parent 76ef372 commit 98ea99f

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

mavlink/build/main.rs

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,30 @@ use mavlink_bindgen::XmlDefinitions;
99

1010
fn main() -> ExitCode {
1111
let src_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
12+
let mavlink_dir = src_dir.join("mavlink");
1213

13-
// Check if git is installed
14-
if Command::new("git").arg("--version").status().is_err() {
15-
eprintln!("error: Git is not installed or could not be found.");
16-
return ExitCode::FAILURE;
17-
}
18-
19-
// Update and init submodule
20-
if let Err(error) = Command::new("git")
21-
.arg("submodule")
22-
.arg("update")
23-
.arg("--init")
24-
.current_dir(src_dir)
25-
.status()
26-
{
27-
eprintln!("Failed to update MAVLink definitions submodule: {error}");
28-
return ExitCode::FAILURE;
14+
// It is a submodule if it contains `.git` or if it's completely empty (uninitialized)
15+
let is_mavlink_empty = read_dir(&mavlink_dir)
16+
.map(|mut d| d.next().is_none())
17+
.unwrap_or(true);
18+
let is_submodule = mavlink_dir.join(".git").exists() || is_mavlink_empty;
19+
20+
if is_submodule {
21+
if let Err(error) = Command::new("git")
22+
.arg("submodule")
23+
.arg("update")
24+
.arg("--init")
25+
.current_dir(src_dir)
26+
.status()
27+
{
28+
eprintln!("Failed to update MAVLink definitions submodule: {error}");
29+
return ExitCode::FAILURE;
30+
}
2931
}
3032

3133
// find & apply patches to XML definitions to avoid crashes
3234
let patch_dir = src_dir.join("build/patches");
33-
let mavlink_dir = src_dir.join("mavlink");
34-
35-
if let Ok(dir) = read_dir(patch_dir) {
35+
if let Ok(dir) = read_dir(&patch_dir) {
3636
for entry in dir.flatten() {
3737
if let Err(error) = Command::new("git")
3838
.arg("apply")
@@ -47,8 +47,17 @@ fn main() -> ExitCode {
4747
}
4848

4949
let out_dir = env::var("OUT_DIR").unwrap();
50-
51-
let source_definitions_dir = src_dir.join("mavlink/message_definitions/v1.0");
50+
let source_definitions_dir = mavlink_dir.join("message_definitions/v1.0");
51+
52+
// Check if the source definitions directory exists
53+
if !source_definitions_dir.is_dir() {
54+
eprintln!(
55+
"MAVLink message definitions directory not found at: {}\n\
56+
Ensure submodules are included.",
57+
source_definitions_dir.display()
58+
);
59+
return ExitCode::FAILURE;
60+
}
5261

5362
let enabled_dialects: Vec<String> = env::vars()
5463
.filter_map(|(key, _)| {
@@ -59,19 +68,19 @@ fn main() -> ExitCode {
5968

6069
let mut definitions_to_bind = vec![];
6170

62-
if let Ok(dir) = read_dir(&source_definitions_dir) {
63-
for entry in dir.flatten() {
64-
let filename = entry
65-
.path()
66-
.file_stem()
67-
.unwrap()
68-
.to_string_lossy()
69-
.to_lowercase();
70-
71-
if enabled_dialects.contains(&filename) {
72-
definitions_to_bind.push(entry.path());
73-
}
71+
// Check if the expected dialects requested by Cargo features are missing
72+
for dialect in &enabled_dialects {
73+
let dialect_file = source_definitions_dir.join(format!("{dialect}.xml"));
74+
75+
if !dialect_file.is_file() {
76+
eprintln!(
77+
"Expected MAVLink dialect definition not found at: {}",
78+
dialect_file.display()
79+
);
80+
return ExitCode::FAILURE;
7481
}
82+
83+
definitions_to_bind.push(dialect_file);
7584
}
7685

7786
let xml_definitions = if definitions_to_bind.is_empty() {

0 commit comments

Comments
 (0)