Skip to content

Commit d2e926a

Browse files
Add optional --target flag
1 parent d607b95 commit d2e926a

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

src/main.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::path::Path;
3535
use std::path::PathBuf;
3636
use symbol_graph::SymGraph;
3737

38-
#[derive(Parser, Debug)]
38+
#[derive(Parser, Debug, Clone)]
3939
#[clap(version, about)]
4040
struct Args {
4141
/// Directory containing crate to analyze. Defaults to current working
@@ -82,6 +82,12 @@ struct Args {
8282
/// Don't print anything on success.
8383
#[clap(long)]
8484
quiet: bool,
85+
86+
/// Override the target used when compiling. e.g. specify "x86_64-apple-darwin" to compile for
87+
/// x86 Mac. Note that build scripts and procedural macros will still be compiled for the host
88+
/// target.
89+
#[clap(long)]
90+
target: Option<String>,
8591
}
8692

8793
fn main() -> Result<()> {
@@ -105,14 +111,14 @@ fn run(args: Args) -> Result<()> {
105111
.canonicalize()
106112
.with_context(|| format!("Failed to read directory `{}`", root_path.display()))?;
107113

108-
proxy::clean(&root_path, args.colour)?;
114+
proxy::clean(&root_path, &args)?;
109115

110116
let config_path = args
111117
.cackle_path
112118
.clone()
113119
.unwrap_or_else(|| root_path.join("cackle.toml"));
114120

115-
let mut cackle = Cackle::new(config_path, &root_path, args)?;
121+
let mut cackle = Cackle::new(config_path, &root_path, args.clone())?;
116122
cackle.load_config()?;
117123

118124
if !cackle.args.object_paths.is_empty() {
@@ -124,7 +130,7 @@ fn run(args: Args) -> Result<()> {
124130
let mut problems = cackle.unfixed_problems(None)?;
125131
let config_path = cackle.flattened_config_path();
126132
let build_result = if problems.is_empty() {
127-
proxy::invoke_cargo_build(&root_path, &config_path, cackle.args.colour, |request| {
133+
proxy::invoke_cargo_build(&root_path, &config_path, &args, |request| {
128134
problems.merge(cackle.unfixed_problems(Some(request))?);
129135
Ok(problems.can_continue())
130136
})

src/proxy.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//! * We can run them inside a sandbox if the config says to do so.
2121
//! * We can capture their output and check for any directives to cargo that haven't been permitted.
2222
23-
use crate::colour::Colour;
23+
use crate::Args;
2424
use anyhow::Context;
2525
use anyhow::Result;
2626
use std::fmt::Display;
@@ -45,10 +45,10 @@ pub(crate) struct CargoBuildFailure {
4545
output: std::process::Output,
4646
}
4747

48-
pub(crate) fn clean(dir: &Path, colour: Colour) -> Result<()> {
48+
pub(crate) fn clean(dir: &Path, args: &Args) -> Result<()> {
4949
// For now, we always clean before we build. It might be possible to not do this, but we'd need
5050
// to carefully track changes to things we care about, like cackle.toml.
51-
run_command(&mut cargo::command("clean", dir, colour))?;
51+
run_command(&mut cargo::command("clean", dir, args))?;
5252
Ok(())
5353
}
5454

@@ -57,7 +57,7 @@ pub(crate) fn clean(dir: &Path, colour: Colour) -> Result<()> {
5757
pub(crate) fn invoke_cargo_build(
5858
dir: &Path,
5959
config_path: &Path,
60-
colour: Colour,
60+
args: &Args,
6161
mut callback: impl FnMut(rpc::Request) -> Result<rpc::CanContinueResponse>,
6262
) -> Result<Option<CargoBuildFailure>> {
6363
if !std::env::var(SOCKET_ENV).unwrap_or_default().is_empty() {
@@ -72,7 +72,10 @@ pub(crate) fn invoke_cargo_build(
7272
let listener = UnixListener::bind(&ipc_path)
7373
.with_context(|| format!("Failed to create Unix socket `{}`", ipc_path.display()))?;
7474

75-
let mut command = cargo::command("build", dir, colour);
75+
let mut command = cargo::command("build", dir, args);
76+
if let Some(target) = &args.target {
77+
command.arg("--target").arg(target);
78+
}
7679
command
7780
.env(SOCKET_ENV, &ipc_path)
7881
.env(CONFIG_PATH_ENV, config_path)

src/proxy/cargo.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use crate::colour::Colour;
1+
use crate::Args;
22
use std::path::Path;
33
use std::process::Command;
44

55
/// The name of the cargo profile that we use.
66
pub(crate) const PROFILE_NAME: &str = "cackle";
77

8-
pub(crate) fn command(base_command: &str, dir: &Path, colour: Colour) -> Command {
8+
pub(crate) fn command(base_command: &str, dir: &Path, args: &Args) -> Command {
99
let mut command = Command::new("cargo");
1010
command.current_dir(dir);
11-
if colour.should_use_colour() {
11+
if args.colour.should_use_colour() {
1212
command.arg("--color=always");
1313
}
1414
command.arg(base_command);

test_crates/.cargo/config.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# For instructions on how to set up cross compiling for Mac from Linux, see
2+
# https://wapl.es/rust/2019/02/17/rust-cross-compile-linux-to-macos.html
3+
4+
[target.x86_64-apple-darwin]
5+
linker = "x86_64-apple-darwin14-clang"
6+
ar = "x86_64-apple-darwin14-ar"
7+

0 commit comments

Comments
 (0)