Skip to content

Commit

Permalink
Get target dir from invoking cargo metadata instead of forcing a targ…
Browse files Browse the repository at this point in the history
…et directory

Closes #27
  • Loading branch information
antoniusnaumann committed Oct 14, 2023
1 parent eb6e9a6 commit 55ba90e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ uniffi_bindgen = { version = "0.24.3" }
# Error Handling
anyhow = "1.0.75"
thiserror = "1.0.49"
lazy_static = "1.4.0"

[features]
29 changes: 24 additions & 5 deletions src/targets.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::{fmt::Display, process::Command};
use camino::{Utf8Path, Utf8PathBuf};
use cargo_metadata::MetadataCommand;

use execute::command;
use nonempty::{nonempty, NonEmpty};
use lazy_static::lazy_static;

use crate::lib_type::LibType;

Expand Down Expand Up @@ -48,11 +51,9 @@ impl Target {

self.architectures()
.into_iter()
// TODO: Setting `--target-dir` is a quick fix to make cargo swift package work for crates that
// are contained in a workspace as described in issue #24, remove this once #7 is implemented
.map(|arch| {
command(format!(
"cargo build --target {arch} {flag} --target-dir ./target"
"cargo build --target {arch} {flag}"
))
})
.collect()
Expand Down Expand Up @@ -161,8 +162,26 @@ pub fn library_file_name(lib_name: &str, lib_type: LibType) -> String {
format!("lib{}.{}", lib_name, lib_type.file_extension())
}

pub fn target_dir() -> &'static str {
"./target"
pub fn target_dir() -> &'static Utf8Path {
lazy_static! {
static ref TARGET_DIR: Utf8PathBuf = MetadataCommand::new()
.no_deps()
.other_options(["--offline".to_string()])
.exec()
// TODO: Error handling
.unwrap()
.target_directory;
}

let relative = TARGET_DIR
// TODO: Error handling
.strip_prefix(std::env::current_dir().unwrap())
.ok();

match relative {
Some(dir) => dir,
None => &TARGET_DIR,
}
}

#[derive(Clone, Copy, Debug)]
Expand Down

0 comments on commit 55ba90e

Please sign in to comment.