|
20 | 20 | // Cargo.toml section.
|
21 | 21 |
|
22 | 22 | use std::{
|
23 |
| - env, fmt, |
| 23 | + env, fmt, fs, |
24 | 24 | io::{self, Read as _},
|
25 | 25 | process::{self, Command, Output},
|
26 | 26 | };
|
27 | 27 |
|
28 |
| -use serde_json::{Map, Value}; |
| 28 | +use toml::{map::Map, Value}; |
29 | 29 |
|
30 | 30 | #[derive(Debug)]
|
31 | 31 | enum Error {
|
@@ -110,52 +110,20 @@ impl Versions {
|
110 | 110 | }
|
111 | 111 |
|
112 | 112 | fn get_toolchain_versions() -> Versions {
|
113 |
| - // NOTE(#547): We set `CARGO_TARGET_DIR` here because `cargo metadata` |
114 |
| - // sometimes causes the `cargo-metadata` crate to be rebuilt from source |
115 |
| - // using the default toolchain. This has the effect of clobbering any |
116 |
| - // existing build artifacts from whatever toolchain the user has specified |
117 |
| - // (e.g., `+nightly`), causing the subsequent `cargo` invocation to rebuild |
118 |
| - // unnecessarily. By specifying a separate build directory here, we ensure |
119 |
| - // that this never clobbers the build artifacts used by the later `cargo` |
120 |
| - // invocation. |
121 |
| - // |
122 |
| - // In CI, make sure to use the default stable toolchain. If we're testing on |
123 |
| - // our MSRV, then we also have our MSRV toolchain installed. As of this |
124 |
| - // writing, our MSRV is low enough that the correspoding Rust toolchain's |
125 |
| - // Cargo doesn't know about the `rust-version` field, and so if we were to |
126 |
| - // use Cargo with that toolchain, `pkg-meta` would return `null` when asked |
127 |
| - // to retrieve the `rust-version` field. This also requires `RUSTFLAGS=''` |
128 |
| - // to override any unstable `RUSTFLAGS` set by the caller. |
129 |
| - let output = rustup( |
130 |
| - ["run", "stable", "cargo", "metadata", "--format-version", "1"], |
131 |
| - Some(("CARGO_TARGET_DIR", "target/cargo-zerocopy")), |
132 |
| - ) |
133 |
| - .env_remove("RUSTFLAGS") |
134 |
| - .output_or_exit(); |
135 |
| - |
136 |
| - let json = serde_json::from_slice::<Value>(&output.stdout).unwrap(); |
137 |
| - let packages = json.as_object().unwrap().get("packages").unwrap(); |
138 |
| - packages |
139 |
| - .as_array() |
140 |
| - .unwrap() |
141 |
| - .iter() |
142 |
| - .filter_map(|p| { |
143 |
| - let package = p.as_object().unwrap(); |
144 |
| - if package.get("name").unwrap().as_str() == Some("zerocopy") { |
145 |
| - let metadata = package.get("metadata").unwrap().as_object().unwrap(); |
146 |
| - let ci = metadata.get("ci").unwrap().as_object().unwrap(); |
147 |
| - Some(Versions { |
148 |
| - msrv: package.get("rust_version").unwrap().as_str().unwrap().to_string(), |
149 |
| - stable: ci.get("pinned-stable").unwrap().as_str().unwrap().to_string(), |
150 |
| - nightly: ci.get("pinned-nightly").unwrap().as_str().unwrap().to_string(), |
151 |
| - build_rs: metadata.get("build-rs").unwrap().as_object().unwrap().clone(), |
152 |
| - }) |
153 |
| - } else { |
154 |
| - None |
155 |
| - } |
156 |
| - }) |
157 |
| - .next() |
158 |
| - .unwrap() |
| 113 | + let manifest_text = fs::read_to_string("Cargo.toml").unwrap(); |
| 114 | + let manifest = toml::from_str::<Value>(&manifest_text).unwrap(); |
| 115 | + |
| 116 | + let package = manifest.as_table().unwrap()["package"].as_table().unwrap(); |
| 117 | + let metadata = package["metadata"].as_table().unwrap(); |
| 118 | + let build_rs = metadata["build-rs"].as_table().unwrap(); |
| 119 | + let ci = metadata["ci"].as_table().unwrap(); |
| 120 | + |
| 121 | + Versions { |
| 122 | + msrv: package["rust-version"].as_str().unwrap().to_string(), |
| 123 | + stable: ci["pinned-stable"].as_str().unwrap().to_string(), |
| 124 | + nightly: ci["pinned-nightly"].as_str().unwrap().to_string(), |
| 125 | + build_rs: build_rs.clone(), |
| 126 | + } |
159 | 127 | }
|
160 | 128 |
|
161 | 129 | fn is_toolchain_installed(versions: &Versions, name: &str) -> Result<bool, Error> {
|
@@ -190,7 +158,7 @@ fn install_toolchain_or_exit(versions: &Versions, name: &str) -> Result<(), Erro
|
190 | 158 | }
|
191 | 159 |
|
192 | 160 | let version = versions.get(name)?;
|
193 |
| - rustup(["toolchain", "install", &version, "-c", "rust-src"], None).execute(); |
| 161 | + rustup(["toolchain", "install", version, "-c", "rust-src"], None).execute(); |
194 | 162 |
|
195 | 163 | Ok(())
|
196 | 164 | }
|
@@ -263,7 +231,7 @@ fn delegate_cargo() -> Result<(), Error> {
|
263 | 231 | get_toolchain_rustflags(name),
|
264 | 232 | env_rustflags,
|
265 | 233 | );
|
266 |
| - rustup(["run", &version, "cargo"], Some(("RUSTFLAGS", &rustflags))) |
| 234 | + rustup(["run", version, "cargo"], Some(("RUSTFLAGS", &rustflags))) |
267 | 235 | .args(args)
|
268 | 236 | .execute();
|
269 | 237 |
|
|
0 commit comments