refactor(toml): Move target build.rs logic next to use

This commit is contained in:
Ed Page 2023-11-08 15:19:05 -06:00
parent 807d7ed2f0
commit 778dc4d4b3
2 changed files with 21 additions and 25 deletions

View File

@ -1310,30 +1310,6 @@ impl schema::TomlManifest {
}
Ok(patch)
}
/// Returns the path to the build script if one exists for this crate.
fn maybe_custom_build(
&self,
build: &Option<schema::StringOrBool>,
package_root: &Path,
) -> Option<PathBuf> {
let build_rs = package_root.join("build.rs");
match *build {
// Explicitly no build script.
Some(schema::StringOrBool::Bool(false)) => None,
Some(schema::StringOrBool::Bool(true)) => Some(build_rs),
Some(schema::StringOrBool::String(ref s)) => Some(PathBuf::from(s)),
None => {
// If there is a `build.rs` file next to the `Cargo.toml`, assume it is
// a build script.
if build_rs.is_file() {
Some(build_rs)
} else {
None
}
}
}
}
}
struct Context<'a, 'b> {

View File

@ -105,7 +105,7 @@ pub(super) fn targets(
)?);
// processing the custom build script
if let Some(custom_build) = manifest.maybe_custom_build(custom_build, package_root) {
if let Some(custom_build) = maybe_custom_build(custom_build, package_root) {
if metabuild.is_some() {
anyhow::bail!("cannot specify both `metabuild` and `build`");
}
@ -964,3 +964,23 @@ Cargo doesn't know which to use because multiple target files found at `{}` and
(None, Some(_)) => unreachable!(),
}
}
/// Returns the path to the build script if one exists for this crate.
fn maybe_custom_build(build: &Option<StringOrBool>, package_root: &Path) -> Option<PathBuf> {
let build_rs = package_root.join("build.rs");
match *build {
// Explicitly no build script.
Some(StringOrBool::Bool(false)) => None,
Some(StringOrBool::Bool(true)) => Some(build_rs),
Some(StringOrBool::String(ref s)) => Some(PathBuf::from(s)),
None => {
// If there is a `build.rs` file next to the `Cargo.toml`, assume it is
// a build script.
if build_rs.is_file() {
Some(build_rs)
} else {
None
}
}
}
}