1
0
Fork 0

Parse target dir location from cargo metadata

This commit is contained in:
Robbert van der Helm 2024-03-23 11:51:11 +01:00
parent 245add6530
commit bb274976ca
4 changed files with 112 additions and 52 deletions

View file

@ -10,6 +10,13 @@ Since there is no stable release yet, the changes are organized per day in
reverse chronological order. The main purpose of this document in its current
state is to list breaking changes.
## [2024-03-23]
### Added
- `nih_plug_xtask` now detects and uses non-standard `target` directory
locations if overridden through Cargo's settings.
## [2024-03-18]
### Changed

94
Cargo.lock generated
View file

@ -459,7 +459,7 @@ checksum = "5fd55a5ba1179988837d24ab4c7cc8ed6efdeff578ede0416b4225a5fca35bd0"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -521,7 +521,7 @@ checksum = "fdf6721fb0140e4f897002dd086c06f6c27775df19cfe1fccb21181a48fd2c98"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -654,7 +654,7 @@ dependencies = [
"regex",
"rustc-hash",
"shlex",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -749,7 +749,7 @@ checksum = "965ab7eb5f8f97d2a083c799f3a1b994fc397b2fe2da5d1da1626ce15a39f2b1"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -770,6 +770,15 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "981520c98f422fcc584dc1a95c334e6953900b9106bc47a9839b81790009eb21"
[[package]]
name = "camino"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c"
dependencies = [
"serde",
]
[[package]]
name = "cargo-nih-plug"
version = "0.1.0"
@ -777,6 +786,29 @@ dependencies = [
"nih_plug_xtask",
]
[[package]]
name = "cargo-platform"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24b1f0365a6c6bb4020cd05806fd0d33c44d38046b8bd7f0e40814b9763cabfc"
dependencies = [
"serde",
]
[[package]]
name = "cargo_metadata"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d886547e41f740c616ae73108f6eb70afe6d940c7bc697cb30f13daec073037"
dependencies = [
"camino",
"cargo-platform",
"semver",
"serde",
"serde_json",
"thiserror",
]
[[package]]
name = "cc"
version = "1.0.83"
@ -891,7 +923,7 @@ dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -1305,7 +1337,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13b588ba4ac1a99f7f2964d24b3d896ddc6bf847ee3855dbd4366f058cfcd331"
dependencies = [
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -1411,7 +1443,7 @@ checksum = "487585f4d0c6655fe74905e2504d8ad6908e4db67f744eb140876906c2f3175d"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -1538,7 +1570,7 @@ checksum = "f95e2801cd355d4a1a3e3953ce6ee5ae9603a5c833455343a8bfe3f44d418246"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -1899,7 +1931,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -3025,6 +3057,7 @@ name = "nih_plug_xtask"
version = "0.1.0"
dependencies = [
"anyhow",
"cargo_metadata",
"goblin",
"reflink",
"serde",
@ -3183,7 +3216,7 @@ dependencies = [
"proc-macro-crate",
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -3670,9 +3703,9 @@ checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"
[[package]]
name = "proc-macro2"
version = "1.0.71"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75cb1540fadbd5b8fbccc4dddad2734eba435053f725621c070711a14bb5f4b8"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [
"unicode-ident",
]
@ -3705,9 +3738,9 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.33"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
@ -4077,7 +4110,7 @@ checksum = "1db149f81d46d2deba7cd3c50772474707729550221e69588478ebf9ada425ae"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -4116,25 +4149,28 @@ name = "semver"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
dependencies = [
"serde",
]
[[package]]
name = "serde"
version = "1.0.193"
version = "1.0.197"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.193"
version = "1.0.197"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -4156,7 +4192,7 @@ checksum = "3081f5ffbb02284dda55132aa26daecedd7372a42417bbbab6f14ab7d6bb9145"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -4393,9 +4429,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.43"
version = "2.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53"
checksum = "7383cd0e49fff4b6b90ca5670bfd3e9d6a733b3f90c686605aa7eec8c4996032"
dependencies = [
"proc-macro2",
"quote",
@ -4467,7 +4503,7 @@ checksum = "e7fbe9b594d6568a6a1443250a7e67d80b74e1e96f6d1715e1e21cc1888291d3"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -4589,7 +4625,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]
@ -4714,7 +4750,7 @@ checksum = "fea2a4c80deb4fb3ca51f66b5e2dd91e3642bbce52234bcf22e41668281208e4"
dependencies = [
"proc-macro-hack",
"quote",
"syn 2.0.43",
"syn 2.0.53",
"unic-langid-impl",
]
@ -5015,7 +5051,7 @@ dependencies = [
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
"wasm-bindgen-shared",
]
@ -5049,7 +5085,7 @@ checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
@ -5794,7 +5830,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.43",
"syn 2.0.53",
]
[[package]]

View file

@ -8,6 +8,7 @@ license = "ISC"
[dependencies]
anyhow = "1.0"
cargo_metadata = "0.18.1"
goblin = "0.6.1"
# Version 0.1.3 from crates.io assumes a 64-bit toolchain
reflink = { git = "https://github.com/nicokoch/reflink.git", rev = "e8d93b465f5d9ad340cd052b64bbc77b8ee107e2" }

View file

@ -14,9 +14,6 @@ mod util;
/// Re-export for the main function.
pub use anyhow::Result;
/// The base directory for the bundler's output.
const BUNDLE_HOME: &str = "target/bundled";
fn build_usage_string(command_name: &str) -> String {
format!(
"Usage:
@ -79,6 +76,11 @@ pub fn main() -> Result<()> {
/// `std::env::args()` before passing it to this function.
pub fn main_with_args(command_name: &str, args: impl IntoIterator<Item = String>) -> Result<()> {
chdir_workspace_root()?;
let cargo_metadata = cargo_metadata::MetadataCommand::new()
.manifest_path("./Cargo.toml")
.exec()
.context("Could not parse `cargo-metadata`")?;
let target_dir = cargo_metadata.target_directory.as_std_path();
let mut args = args.into_iter();
let usage_string = build_usage_string(command_name);
@ -96,9 +98,9 @@ pub fn main_with_args(command_name: &str, args: impl IntoIterator<Item = String>
// As explained above, for efficiency's sake this is a two step process
build(&packages, &other_args)?;
bundle(&packages[0], &other_args, false)?;
bundle(target_dir, &packages[0], &other_args, false)?;
for package in packages.into_iter().skip(1) {
bundle(&package, &other_args, false)?;
bundle(target_dir, &package, &other_args, false)?;
}
Ok(())
@ -134,9 +136,9 @@ pub fn main_with_args(command_name: &str, args: impl IntoIterator<Item = String>
// This `true` indicates a universal build. This will cause the two sets of built
// binaries to beq lipo'd together into universal binaries before bundling
bundle(&packages[0], &other_args, true)?;
bundle(target_dir, &packages[0], &other_args, true)?;
for package in packages.into_iter().skip(1) {
bundle(&package, &other_args, true)?;
bundle(target_dir, &package, &other_args, true)?;
}
Ok(())
@ -214,7 +216,7 @@ pub fn build(packages: &[String], args: &[String]) -> Result<()> {
/// Normally this respects the `--target` option for cross compilation. If the `universal` option is
/// specified instead, then this will assume both `x86_64-apple-darwin` and `aarch64-apple-darwin`
/// have been built and it will try to lipo those together instead.
pub fn bundle(package: &str, args: &[String], universal: bool) -> Result<()> {
pub fn bundle(target_dir: &Path, package: &str, args: &[String], universal: bool) -> Result<()> {
let mut build_type_dir = "debug";
let mut cross_compile_target: Option<String> = None;
for arg_idx in (0..args.len()).rev() {
@ -252,7 +254,8 @@ pub fn bundle(package: &str, args: &[String], universal: bool) -> Result<()> {
// We can bundle both library targets (for plugins) and binary targets (for standalone
// applications)
if universal {
let x86_64_target_base = target_base(Some("x86_64-apple-darwin"))?.join(build_type_dir);
let x86_64_target_base =
target_base(target_dir, Some("x86_64-apple-darwin"))?.join(build_type_dir);
let x86_64_bin_path = x86_64_target_base.join(binary_basename(
package,
CompilationTarget::MacOS(Architecture::X86_64),
@ -262,7 +265,8 @@ pub fn bundle(package: &str, args: &[String], universal: bool) -> Result<()> {
CompilationTarget::MacOS(Architecture::X86_64),
));
let aarch64_target_base = target_base(Some("aarch64-apple-darwin"))?.join(build_type_dir);
let aarch64_target_base =
target_base(target_dir, Some("aarch64-apple-darwin"))?.join(build_type_dir);
let aarch64_bin_path = aarch64_target_base.join(binary_basename(
package,
CompilationTarget::MacOS(Architecture::AArch64),
@ -281,6 +285,7 @@ pub fn bundle(package: &str, args: &[String], universal: bool) -> Result<()> {
eprintln!();
if build_bin {
bundle_binary(
target_dir,
package,
&[&x86_64_bin_path, &aarch64_bin_path],
CompilationTarget::MacOSUniversal,
@ -288,6 +293,7 @@ pub fn bundle(package: &str, args: &[String], universal: bool) -> Result<()> {
}
if build_lib {
bundle_plugin(
target_dir,
package,
&[&x86_64_lib_path, &aarch64_lib_path],
CompilationTarget::MacOSUniversal,
@ -295,7 +301,8 @@ pub fn bundle(package: &str, args: &[String], universal: bool) -> Result<()> {
}
} else {
let compilation_target = compilation_target(cross_compile_target.as_deref())?;
let target_base = target_base(cross_compile_target.as_deref())?.join(build_type_dir);
let target_base =
target_base(target_dir, cross_compile_target.as_deref())?.join(build_type_dir);
let bin_path = target_base.join(binary_basename(package, compilation_target));
let lib_path = target_base.join(library_basename(package, compilation_target));
if !bin_path.exists() && !lib_path.exists() {
@ -314,10 +321,10 @@ to your Cargo.toml file?"#,
eprintln!();
if bin_path.exists() {
bundle_binary(package, &[&bin_path], compilation_target)?;
bundle_binary(target_dir, package, &[&bin_path], compilation_target)?;
}
if lib_path.exists() {
bundle_plugin(package, &[&lib_path], compilation_target)?;
bundle_plugin(target_dir, package, &[&lib_path], compilation_target)?;
}
}
@ -328,10 +335,12 @@ to your Cargo.toml file?"#,
/// combined into a single binary using a method that depends on the compilation target. For
/// universal macOS builds this uses lipo.
fn bundle_binary(
target_dir: &Path,
package: &str,
bin_paths: &[&Path],
compilation_target: CompilationTarget,
) -> Result<()> {
let bundle_home_dir = bundle_home(target_dir);
let bundle_name = match load_bundler_config()?.and_then(|c| c.get(package).cloned()) {
Some(PackageConfig { name: Some(name) }) => name,
_ => package.to_string(),
@ -340,7 +349,7 @@ fn bundle_binary(
// On MacOS the standalone target needs to be in a bundle
let standalone_bundle_binary_name =
standalone_bundle_binary_name(&bundle_name, compilation_target);
let standalone_binary_path = Path::new(BUNDLE_HOME).join(&standalone_bundle_binary_name);
let standalone_binary_path = bundle_home_dir.join(&standalone_bundle_binary_name);
fs::create_dir_all(standalone_binary_path.parent().unwrap())
.context("Could not create standalone bundle directory")?;
@ -363,7 +372,7 @@ fn bundle_binary(
})?;
}
let standalone_bundle_home = Path::new(BUNDLE_HOME).join(
let standalone_bundle_home = bundle_home_dir.join(
Path::new(&standalone_bundle_binary_name)
.components()
.next()
@ -390,10 +399,12 @@ fn bundle_binary(
/// the libraries will be combined into a single library using a method that depends on the
/// compilation target. For universal macOS builds this uses lipo.
fn bundle_plugin(
target_dir: &Path,
package: &str,
lib_paths: &[&Path],
compilation_target: CompilationTarget,
) -> Result<()> {
let bundle_home_dir = bundle_home(target_dir);
let bundle_name = match load_bundler_config()?.and_then(|c| c.get(package).cloned()) {
Some(PackageConfig { name: Some(name) }) => name,
_ => package.to_string(),
@ -419,7 +430,7 @@ fn bundle_plugin(
if bundle_clap {
let clap_bundle_library_name = clap_bundle_library_name(&bundle_name, compilation_target);
let clap_lib_path = Path::new(BUNDLE_HOME).join(&clap_bundle_library_name);
let clap_lib_path = bundle_home_dir.join(&clap_bundle_library_name);
fs::create_dir_all(clap_lib_path.parent().unwrap())
.context("Could not create CLAP bundle directory")?;
@ -428,7 +439,7 @@ fn bundle_plugin(
// In contrast to VST3, CLAP only uses bundles on macOS, so we'll just take the first
// component of the library name instead
let clap_bundle_home = Path::new(BUNDLE_HOME).join(
let clap_bundle_home = bundle_home_dir.join(
Path::new(&clap_bundle_library_name)
.components()
.next()
@ -447,7 +458,7 @@ fn bundle_plugin(
}
if bundle_vst2 {
let vst2_bundle_library_name = vst2_bundle_library_name(&bundle_name, compilation_target);
let vst2_lib_path = Path::new(BUNDLE_HOME).join(&vst2_bundle_library_name);
let vst2_lib_path = bundle_home_dir.join(&vst2_bundle_library_name);
fs::create_dir_all(vst2_lib_path.parent().unwrap())
.context("Could not create VST2 bundle directory")?;
@ -456,7 +467,7 @@ fn bundle_plugin(
// VST2 only uses bundles on macOS, so we'll just take the first component of the library
// name instead
let vst2_bundle_home = Path::new(BUNDLE_HOME).join(
let vst2_bundle_home = bundle_home_dir.join(
Path::new(&vst2_bundle_library_name)
.components()
.next()
@ -475,7 +486,7 @@ fn bundle_plugin(
}
if bundle_vst3 {
let vst3_lib_path =
Path::new(BUNDLE_HOME).join(vst3_bundle_library_name(&bundle_name, compilation_target));
bundle_home_dir.join(vst3_bundle_library_name(&bundle_name, compilation_target));
fs::create_dir_all(vst3_lib_path.parent().unwrap())
.context("Could not create VST3 bundle directory")?;
@ -604,13 +615,18 @@ fn compilation_target(cross_compile_target: Option<&str>) -> Result<CompilationT
}
}
/// The directory bundled plugins should be written to.
fn bundle_home(target_directory: &Path) -> PathBuf {
target_directory.join("bundled")
}
/// The base directory for the compiled binaries. This does not use [`CompilationTarget`] as we need
/// to be able to differentiate between native and cross-compilation.
fn target_base(cross_compile_target: Option<&str>) -> Result<PathBuf> {
fn target_base(target_directory: &Path, cross_compile_target: Option<&str>) -> Result<PathBuf> {
match cross_compile_target {
// Unhandled targets will already be handled in `compilation_target`
Some(target) => Ok(Path::new("target").join(target)),
None => Ok(PathBuf::from("target")),
Some(target) => Ok(target_directory.join(target)),
None => Ok(target_directory.to_owned()),
}
}