1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::path::PathBuf;
use anyhow::Context;
use clap::Parser;
use crate::cli::Result;
use crate::config::Config;
use crate::metadata::{
write_metadata, Cargo, CargoUpdateRequest, FeatureGenerator, Generator, MetadataGenerator,
};
use crate::splicing::{generate_lockfile, Splicer, SplicingManifest, WorkspaceMetadata};
#[derive(Parser, Debug)]
#[clap(about = "Command line options for the `splice` subcommand", version)]
pub struct SpliceOptions {
#[clap(long)]
pub splicing_manifest: PathBuf,
#[clap(long)]
pub cargo_lockfile: Option<PathBuf>,
#[clap(long, env = "CARGO_BAZEL_REPIN", num_args=0..=1, default_missing_value = "true")]
pub repin: Option<CargoUpdateRequest>,
#[clap(long)]
pub workspace_dir: Option<PathBuf>,
#[clap(long)]
pub output_dir: PathBuf,
#[clap(long)]
pub dry_run: bool,
#[clap(long)]
pub cargo_config: Option<PathBuf>,
#[clap(long)]
pub config: PathBuf,
#[clap(long, env = "CARGO")]
pub cargo: PathBuf,
#[clap(long, env = "RUSTC")]
pub rustc: PathBuf,
}
pub fn splice(opt: SpliceOptions) -> Result<()> {
let splicing_manifest = SplicingManifest::try_from_path(&opt.splicing_manifest)?;
let temp_dir;
let splicing_dir = match &opt.workspace_dir {
Some(dir) => dir.clone(),
None => {
temp_dir = tempfile::tempdir().context("Failed to generate temporary directory")?;
temp_dir.as_ref().to_path_buf()
}
};
let splicer = Splicer::new(splicing_dir, splicing_manifest)?;
let manifest_path = splicer.splice_workspace(&opt.cargo)?;
let cargo = Cargo::new(opt.cargo);
let cargo_lockfile = generate_lockfile(
&manifest_path,
&opt.cargo_lockfile,
cargo.clone(),
&opt.rustc,
&opt.repin,
)?;
let config = Config::try_from_path(&opt.config)?;
let feature_map = FeatureGenerator::new(cargo.clone(), opt.rustc.clone()).generate(
manifest_path.as_path_buf(),
&config.supported_platform_triples,
)?;
WorkspaceMetadata::write_registry_urls_and_feature_map(
&cargo,
&cargo_lockfile,
feature_map,
manifest_path.as_path_buf(),
manifest_path.as_path_buf(),
)?;
let output_dir = opt.output_dir.clone();
let (cargo_metadata, _) = Generator::new()
.with_cargo(cargo)
.with_rustc(opt.rustc)
.generate(manifest_path.as_path_buf())?;
let cargo_lockfile_path = manifest_path
.as_path_buf()
.parent()
.with_context(|| {
format!(
"The path {} is expected to have a parent directory",
manifest_path.as_path_buf().display()
)
})?
.join("Cargo.lock");
std::fs::create_dir_all(&output_dir)
.with_context(|| format!("Failed to create directories for {}", &output_dir.display()))?;
write_metadata(&opt.output_dir.join("metadata.json"), &cargo_metadata)?;
std::fs::copy(cargo_lockfile_path, output_dir.join("Cargo.lock"))
.context("Failed to copy lockfile")?;
Ok(())
}