Skip to content

Commit

Permalink
[WIP]Tweak cli parse and xcframework.toml load
Browse files Browse the repository at this point in the history
  • Loading branch information
Binlogo committed Jul 1, 2024
1 parent 287383c commit e07e1f6
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 99 deletions.
11 changes: 9 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ categories = ["development-tools::cargo-plugins", "development-tools::ffi"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.0", features = ["derive"] }
anyhow = "1.0"
cargo_metadata = { version = "0.18", features = ["builder"] }
cargo_metadata = { version = "0.17", features = ["builder"] }
camino = "1.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand All @@ -30,6 +29,14 @@ glob = "0.3.1"
toml = "0.8.14"
serde-aux = "4.5.0"

clap = { version = "4.0", features = ["derive"], optional = true }
clap-cargo = { version = "0.14", optional = true }
clap-cargo-extra = { version = "0.3", optional = true }

[dev-dependencies]
tempfile = "3.3"
markdown-includes = "0.1"

[features]
default = ["cli"]
cli = ["clap", "clap-cargo", "clap-cargo-extra"]
14 changes: 4 additions & 10 deletions src/conf/configuration.rs → src/cli/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ pub struct Configuration {

impl Configuration {
pub fn load(mut cli: XcCli) -> Result<Self> {
let manifest_path = cli
.manifest_path
.clone()
.unwrap_or_else(|| Utf8PathBuf::from("Cargo.toml"));
let mut dir = manifest_path.clone();
let manifest_path = cli.clap_cargo.manifest_path()?;
let mut dir =
Utf8PathBuf::from_path_buf(manifest_path.clone()).expect("manifest_path is valid");
dir.pop();

let target_dir = cli.target_dir.clone().unwrap_or_else(|| dir.join("target"));
Expand Down Expand Up @@ -108,10 +106,6 @@ impl Configuration {
}

pub fn profile(&self) -> &str {
if self.cli.release {
"release"
} else {
self.cli.profile.as_deref().unwrap_or("debug")
}
self.cli.clap_cargo.cargo_build.profile()
}
}
2 changes: 1 addition & 1 deletion src/conf/mod.rs → src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ mod configuration;
mod xc_cli;
mod xcframework;

pub use crate::conf::xcframework::{LibType, XCFrameworkConfiguration};
pub use crate::cli::xcframework::{LibType, XCFrameworkConfiguration};
pub use configuration::Configuration;
pub use xc_cli::XcCli;
32 changes: 4 additions & 28 deletions src/conf/xc_cli.rs → src/cli/xc_cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use camino::Utf8PathBuf;
use clap::Parser;
use clap_cargo_extra::ClapCargo;

use crate::config::Config;

Expand All @@ -9,6 +10,9 @@ use super::LibType;
#[derive(Debug, Parser)]
#[clap(version)]
pub struct XcCli {
#[clap(flatten)]
pub clap_cargo: ClapCargo,

/// Chose library type to build when Cargo.toml `crate-type` has both.
#[arg(long)]
pub lib_type: Option<LibType>,
Expand All @@ -17,10 +21,6 @@ pub struct XcCli {
#[arg(short, long)]
pub quiet: bool,

/// Package to build (see `cargo help pkgid`)
#[arg(short, long)]
pub package: Option<String>,

/// Use verbose output (-vv very verbose/build.rs output)
#[arg(short, long, action = clap::ArgAction::Count)]
pub verbose: u8,
Expand All @@ -29,33 +29,9 @@ pub struct XcCli {
#[arg(short = 'Z', value_name = "FLAG")]
pub unstable_flags: Option<String>,

/// Build artifacts in release mode, with optimizations
#[arg(short, long)]
pub release: bool,

/// Build artifacts with the specified profile
#[arg(long, value_name = "PROFILE-NAME")]
pub profile: Option<String>,

/// Space or comma separated list of features to activate
#[arg(short, long)]
pub features: Vec<String>,

/// Activate all available features
#[arg(long)]
pub all_features: bool,

/// Do not activate the `default` feature
#[arg(long)]
pub no_default_features: bool,

/// Directory for all generated artifacts
#[arg(long, value_name = "DIRECTORY")]
pub target_dir: Option<Utf8PathBuf>,

/// Path to Cargo.toml.
#[arg(long, value_name = "PATH")]
pub manifest_path: Option<Utf8PathBuf>,
}

impl XcCli {
Expand Down
File renamed without changes.
52 changes: 7 additions & 45 deletions src/cmd/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,19 @@
use anyhow::Result;

use crate::conf::Configuration;
use crate::cli::Configuration;

pub fn build(conf: &Configuration) -> Result<()> {
let mut args: Vec<String> = vec![];

args.push("build".into());
let mut cmd = conf.cli.clap_cargo.build_cmd();

if conf.target_dir != "target" {
args.push(format!("--target-dir={}", conf.target_dir));
}

if let Some(manifest_path) = &conf.cli.manifest_path {
args.push(format!("--manifest-path={manifest_path}"));
}
if conf.cli.quiet {
args.push("--quiet".into());
}

if let Some(package) = &conf.cli.package {
args.push(format!("--package={package}"));
}

for _ in 0..conf.cli.verbose {
args.push("-v".into());
}

if let Some(flags) = &conf.cli.unstable_flags {
args.push(format!("-Z={flags}"));
cmd.args(["--target-dir", conf.target_dir.as_str()]);
}

if conf.cli.release {
args.push("--release".into());
}

if let Some(profile) = &conf.cli.profile {
args.push(format!("--profile={profile}"));
}

if !conf.cli.features.is_empty() {
args.push(format!("--features={}", conf.cli.features.join(",")));
}

if conf.cli.all_features {
args.push("--all-features".into());
for target in conf.cargo_section.chosen_targets() {
cmd.args(["--target", target]);
}

if conf.cli.no_default_features {
args.push("--no-default-features".into());
}
cmd.status()?;

for target in conf.cargo_section.chosen_targets() {
args.push(format!("--target={}", target));
}
super::run_cargo(&args, conf.cli.quiet)
Ok(())
}
2 changes: 1 addition & 1 deletion src/cmd/modulemap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io;

use crate::conf::Configuration;
use crate::cli::Configuration;
use anyhow::{bail, Context, Result};
use fs_err::File;

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/rustup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::process::exit;

use crate::conf::Configuration;
use crate::cli::Configuration;
use anyhow::{bail, Result};
use dialoguer::Confirm;

Expand Down
23 changes: 18 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,23 @@
//! `MACOSX_DEPLOYMENT_TARGET` and `IPHONEOS_DEPLOYMENT_TARGET`. See [apple_base.rs](https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/spec/apple_base.rs) for the default values.
//!
mod cmd;
mod conf; // TODO: deprecate or migrate to focus on parsing cli arguments to config;

pub mod config;
pub mod core;
pub mod ext;
pub mod ops;

#[cfg(feature = "cli")]
mod cli; // TODO: deprecate or migrate to focus on parsing cli arguments to config;

use core::platform::{ApplePlatform, Environment};
use std::collections::HashMap;

use crate::conf::Configuration;
use crate::cli::Configuration;
use anyhow::{Context, Result};
use camino::Utf8PathBuf;
pub use cli::{XCFrameworkConfiguration, XcCli};
use cmd::{cargo, rustup};
pub use conf::{XCFrameworkConfiguration, XcCli};
use ext::PathBufExt;
use fs_err as fs;
use rustup_configurator::target::Triple;
Expand All @@ -146,6 +149,16 @@ pub struct Produced {
}

pub fn build(cli: XcCli) -> Result<Produced> {
let package = cli
.clap_cargo
.metadata()?
.root_package()
.context("no package")?;
if let Ok(config) = config::load_package_config(package) {
let res = ops::cargo::build(config, cli.clap_cargo.metadata().expect("metadata"))?;
return Ok(res);
}

let conf = Configuration::load(cli).context("loading configuration")?;

conf.build_dir
Expand Down Expand Up @@ -188,8 +201,8 @@ pub fn build(cli: XcCli) -> Result<Produced> {

let bundle_name = conf.module_name()?;
let crate_type = match conf.lib_type {
conf::LibType::StaticLib => &config::LibType::Staticlib,
conf::LibType::CDyLib => &config::LibType::Cdylib,
cli::LibType::StaticLib => &config::LibType::Staticlib,
cli::LibType::CDyLib => &config::LibType::Cdylib,
};
let framework_paths = libs
.into_iter()
Expand Down
2 changes: 1 addition & 1 deletion src/ops.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mod cargo;
pub mod cargo;
68 changes: 63 additions & 5 deletions src/ops/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,63 @@
// use crate::config::Config;
//
// pub fn build(config: Config) -> Result<()> {
// crate::core::build::build_targets(pkg, profile, lib_type, targets, sequentially)
// }
use anyhow::{Context, Result};
use cargo_metadata::Metadata;

use crate::{config::Config, Produced};

// TODO: fix all testing
pub fn build(config: Config, metadata: &Metadata) -> Result<Produced> {
let lib_name = "mymath";
let pkg = &metadata.root_package().context("Missing package")?.name;
let profile = "release";

let bundle_name = config.name.as_ref().unwrap_or(pkg);

let targets = config.targets();
let lib_type = &config.lib_type;
let sequentially = false;

let libs_dir = metadata.target_directory.join("libs");
std::fs::create_dir_all(&libs_dir)?;

let platform_lib_paths =
crate::core::build::build_targets(pkg, lib_name, profile, lib_type, targets, sequentially)?;

let libs =
crate::core::lipo_create_platform_libraries(&platform_lib_paths, bundle_name, &libs_dir)?;

let header_paths = config.header_paths;
let module_paths = config.module_paths;
let frameworks_dir = metadata.target_directory.join("frameworks");
std::fs::create_dir_all(&frameworks_dir)?;

let framework_paths = libs
.into_iter()
.map(|(platform, lib_path)| {
std::fs::create_dir_all(&frameworks_dir)?;

crate::core::wrap_as_framework(
platform,
lib_type,
&lib_path,
&header_paths,
&module_paths,
bundle_name,
&frameworks_dir,
)
})
.collect::<anyhow::Result<Vec<_>>>()?;

let output_dir = metadata.target_directory.join("xcframeworks");
let xcframework_path =
crate::core::create_xcframework(framework_paths, bundle_name, &output_dir)?;

println!("✅ Created XCFramework at {:?}", xcframework_path);

let is_zipped = false;
let path = xcframework_path;

Ok(Produced {
module_name: bundle_name.to_string(),
path,
is_zipped,
})
}

0 comments on commit e07e1f6

Please sign in to comment.