From 2df4c33974769c39f618bd25a352588b51143959 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Sat, 16 Jul 2022 03:26:36 +0800 Subject: [PATCH] Try to use the "cargo metadata" command when automatically discovering the target dir --- Cargo.lock | 1 + crates/fj-host/Cargo.toml | 1 + crates/fj-host/src/lib.rs | 18 ++++++++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e33938e92..4cb7bf150 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -870,6 +870,7 @@ dependencies = [ name = "fj-host" version = "0.8.0" dependencies = [ + "cargo_metadata", "fj", "libloading", "notify", diff --git a/crates/fj-host/Cargo.toml b/crates/fj-host/Cargo.toml index 395486a0b..4fcd24d4b 100644 --- a/crates/fj-host/Cargo.toml +++ b/crates/fj-host/Cargo.toml @@ -16,6 +16,7 @@ categories = ["encoding", "mathematics", "rendering"] libloading = "0.7.2" notify = "5.0.0-pre.15" thiserror = "1.0.31" +cargo_metadata = "0.15.0" [dependencies.fj] version = "0.8.0" diff --git a/crates/fj-host/src/lib.rs b/crates/fj-host/src/lib.rs index c8ddc6463..f71ad9d42 100644 --- a/crates/fj-host/src/lib.rs +++ b/crates/fj-host/src/lib.rs @@ -21,12 +21,13 @@ use std::{ collections::{HashMap, HashSet}, ffi::OsStr, io, - path::PathBuf, + path::{Path, PathBuf}, process::Command, sync::mpsc, thread, }; +use cargo_metadata::Metadata; use notify::Watcher as _; use thiserror::Error; @@ -64,7 +65,9 @@ impl Model { let lib_path = { let file = HostPlatform::lib_file_name(&name); - let target_dir = target_dir.unwrap_or_else(|| path.join("target")); + let target_dir = target_dir + .or_else(|| cargo_metadata_target_dir(&path)) + .unwrap_or_else(|| path.join("target")); target_dir.join("debug").join(file) }; @@ -285,3 +288,14 @@ pub enum Error { } type ModelFn = unsafe extern "C" fn(args: &Parameters) -> fj::Shape; + +fn cargo_metadata_target_dir(model: &Path) -> Option { + let Metadata { + target_directory, .. + } = cargo_metadata::MetadataCommand::new() + .current_dir(model) + .exec() + .ok()?; + + Some(target_directory.into()) +}