Skip to content

Commit 93528ff

Browse files
committed
Improve libwg rerun-if-changed detection
1 parent 905d24c commit 93528ff

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

wireguard-go-rs/build.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,16 @@ fn build_android_dynamic_lib(daita: bool) -> anyhow::Result<()> {
168168
let target_triple = env::var("TARGET").context("Missing 'TARGET'")?;
169169
let target = AndroidTarget::from_str(&target_triple)?;
170170

171-
// TODO: Since `libwg.so` is always copied to `android_output_path`, this rerun-directive will
172-
// always trigger cargo to rebuild this crate. Some mechanism to detected changes to `android_output_path`
173-
// is needed, because some external program may clean it at any time (e.g. gradle).
171+
// This will either trigger a rebuild if any changes have been made to the libwg code
172+
// or if the libwg.so file has been changed. The latter is required since the
173+
// libwg.so file could be deleted. It however means that this build will need
174+
// to run two times before it is properly cached.
175+
// FIXME: Figure out a way to do this better. This is tracked in DROID-1697.
174176
println!(
175177
"cargo::rerun-if-changed={}",
176-
android_output_path(target)?.display()
178+
android_output_path(target)?.join("libwg.so").display()
177179
);
180+
println!("cargo::rerun-if-changed={}", libwg_path()?.display());
178181

179182
// Before calling `canonicalize`, the directory we're referring to actually has to exist.
180183
std::fs::create_dir_all("../build")?;
@@ -229,12 +232,15 @@ fn android_move_binary(binary: &Path, output: &Path) -> anyhow::Result<()> {
229232
))?;
230233
std::fs::create_dir_all(parent_of_output)?;
231234

232-
let mut move_command = Command::new("mv");
233-
move_command
235+
let mut copy_command = Command::new("cp");
236+
// -p command is required to preserve ownership and timestamp of the file to prevent a
237+
// rebuild of this module every time.
238+
copy_command
239+
.arg("-p")
234240
.arg(binary.to_str().unwrap())
235241
.arg(output.to_str().unwrap());
236242

237-
exec(&mut move_command)?;
243+
exec(&mut copy_command)?;
238244

239245
Ok(())
240246
}
@@ -273,12 +279,20 @@ fn android_arch_name(target: AndroidTarget) -> String {
273279

274280
// Returns the path where the Android project expects Rust binaries to be
275281
fn android_output_path(target: AndroidTarget) -> anyhow::Result<PathBuf> {
276-
let relative_output_path = Path::new("../android/app/build/extraJni").join(android_abi(target));
282+
let relative_output_path =
283+
Path::new("../android/app/build/rustJniLibs/android").join(android_abi(target));
277284
std::fs::create_dir_all(relative_output_path.clone())?;
278285
let output_path = relative_output_path.canonicalize()?;
279286
Ok(output_path)
280287
}
281288

289+
// Return the path of the libwg folder so that we can trigger rebuilds when any code is
290+
fn libwg_path() -> anyhow::Result<PathBuf> {
291+
let relative_output_path = Path::new("libwg");
292+
let output_path = relative_output_path.canonicalize()?;
293+
Ok(output_path)
294+
}
295+
282296
/// Execute a command, assert that it succeeds, and return stdout as a string.
283297
fn exec(mut command: impl BorrowMut<Command>) -> anyhow::Result<String> {
284298
let command = command.borrow_mut();

0 commit comments

Comments
 (0)