Skip to content

Commit

Permalink
Fix corelib rev for buildrs
Browse files Browse the repository at this point in the history
  • Loading branch information
maciektr committed Nov 20, 2023
1 parent 325b975 commit 4144f2e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 44 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ cargo_metadata = ">=0.18"
clap = { version = "4", features = ["derive", "env", "string"] }
clap-verbosity-flag = "2"
console = "0.15"
copy_dir = "0.1"
darling = "0.20"
data-encoding = "2"
deno_task_shell = ">=0.13"
Expand Down
2 changes: 2 additions & 0 deletions scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cairo-lang-utils.workspace = true
camino.workspace = true
clap-verbosity-flag.workspace = true
clap.workspace = true
copy_dir.workspace = true
create-output-dir = { path = "../utils/create-output-dir" }
data-encoding.workspace = true
deno_task_shell.workspace = true
Expand Down Expand Up @@ -102,3 +103,4 @@ test-for-each-example = { path = "../utils/test-for-each-example" }
scarb-build-metadata = { path = "../utils/scarb-build-metadata" }
toml.workspace = true
zip.workspace = true
copy_dir.workspace = true
93 changes: 50 additions & 43 deletions scarb/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use copy_dir::copy_dir;
use std::fs::File;
use std::path::PathBuf;
use std::process::Command;
use std::{env, fs, io};

use zip::ZipArchive;

use scarb_build_metadata::{CAIRO_COMMIT_REV, CAIRO_VERSION};
use scarb_build_metadata::{CAIRO_COMMIT_REV, CAIRO_VERSION, SCARB_CORELIB_LOCAL_SOURCE};

fn main() {
download_core(CAIRO_COMMIT_REV);
Expand All @@ -28,61 +29,67 @@ fn download_core(rev: &str) {
);
return;
}

let core_path = PathBuf::from_iter([&out_dir, &format!("core-{}", ident(rev))]);
if !core_path.is_dir() {
let cairo_zip = PathBuf::from_iter([&out_dir, "cairo.zip"]);

if let Ok(cairo_archive) = env::var("CAIRO_ARCHIVE") {
// Copy archive to `cairo_zip`, without keeping file attributes.
eprintln!("Copying Cairo archive from `CAIRO_ARCHIVE={cairo_archive}`.");
let mut src = File::open(&cairo_archive).unwrap();
let mut dst = File::create(&cairo_zip).unwrap();
io::copy(&mut src, &mut dst).unwrap();
if !SCARB_CORELIB_LOCAL_SOURCE.is_empty() {
copy_dir(PathBuf::from(SCARB_CORELIB_LOCAL_SOURCE), core_path.clone()).unwrap();
} else {
let url = format!("https://github.com/starkware-libs/cairo/archive/{rev}.zip");
let mut curl = Command::new("curl");
curl.args(["--proto", "=https", "--tlsv1.2", "-fL"]);
curl.arg("-o");
curl.arg(&cairo_zip);
curl.arg(&url);
eprintln!("{curl:?}");
let curl_exit = curl.status().expect("Failed to start curl");
if !curl_exit.success() {
panic!("Failed to download {url} with curl")
}
extract_corelib(rev, out_dir, core_path.clone());
}
}

fs::create_dir_all(&core_path).unwrap();
let cairo_file = File::open(cairo_zip).unwrap();
let mut cairo_archive = ZipArchive::new(cairo_file).unwrap();
for i in 0..cairo_archive.len() {
let mut input = cairo_archive.by_index(i).unwrap();
check_corelib_version(core_path.join("Scarb.toml"));
println!("cargo:rustc-env=SCARB_CORE_PATH={}", core_path.display());
}

if input.name().ends_with('/') {
continue;
}
fn extract_corelib(rev: &str, out_dir: String, core_path: PathBuf) {
let cairo_zip = PathBuf::from_iter([&out_dir, "cairo.zip"]);
if let Ok(cairo_archive) = env::var("CAIRO_ARCHIVE") {
// Copy archive to `cairo_zip`, without keeping file attributes.
eprintln!("Copying Cairo archive from `CAIRO_ARCHIVE={cairo_archive}`.");
let mut src = File::open(&cairo_archive).unwrap();
let mut dst = File::create(&cairo_zip).unwrap();
io::copy(&mut src, &mut dst).unwrap();
} else {
let url = format!("https://github.com/starkware-libs/cairo/archive/{rev}.zip");
let mut curl = Command::new("curl");
curl.args(["--proto", "=https", "--tlsv1.2", "-fL"]);
curl.arg("-o");
curl.arg(&cairo_zip);
curl.arg(&url);
eprintln!("{curl:?}");
let curl_exit = curl.status().expect("Failed to start curl");
if !curl_exit.success() {
panic!("Failed to download {url} with curl")
}
}

let path = input.enclosed_name().unwrap();
fs::create_dir_all(&core_path).unwrap();
let cairo_file = File::open(cairo_zip).unwrap();
let mut cairo_archive = ZipArchive::new(cairo_file).unwrap();
for i in 0..cairo_archive.len() {
let mut input = cairo_archive.by_index(i).unwrap();

let path = PathBuf::from_iter(path.components().skip(1));
let Ok(path) = path.strip_prefix("corelib") else {
continue;
};
if input.name().ends_with('/') {
continue;
}

let path = input.enclosed_name().unwrap();

let path = core_path.join(path);
let path = PathBuf::from_iter(path.components().skip(1));
let Ok(path) = path.strip_prefix("corelib") else {
continue;
};

if let Some(parent) = path.parent() {
fs::create_dir_all(parent).unwrap();
}
let path = core_path.join(path);

let mut output = File::create(path).unwrap();
io::copy(&mut input, &mut output).unwrap();
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).unwrap();
}
}

check_corelib_version(core_path.join("Scarb.toml"));
println!("cargo:rustc-env=SCARB_CORE_PATH={}", core_path.display());
let mut output = File::create(path).unwrap();
io::copy(&mut input, &mut output).unwrap();
}
}

fn ident(id: &str) -> String {
Expand Down
32 changes: 31 additions & 1 deletion utils/scarb-build-metadata/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,40 @@ fn cairo_version() {
.iter()
.find(|pkg| pkg.id == compiler_dep.pkg)
.unwrap();

let version = compiler_package.version.to_string();
println!("cargo:rustc-env=SCARB_CAIRO_VERSION={version}");

let corelib_local_path = compiler_package
.id
.clone()
.repr
.strip_prefix(&format!("cairo-lang-compiler {version} ("))
.map(ToString::to_string)
.and_then(|source| source.split_once('+').map(|(s, _)| s.to_string()))
.and_then(|source| {
if source == "registry" {
None
} else {
// Source is local or git - corelib already downloaded by Cargo
let path = compiler_package.manifest_path.clone();
let path = path
.parent()
.expect("cairo-lang-compiler Cargo.toml parent must exist");
let path = path
.parent()
.expect("cairo-lang-compiler crate root parent must exist");
let path = path
.parent()
.expect("cairo-lang-compiler crates dir parent must exist");
let path = path.join("corelib");
assert!(path.exists(), "cairo-lang-compiler corelib must exist");
Some(path)
}
})
.map(|p| p.to_string())
.unwrap_or("".into());
println!("cargo:rustc-env=SCARB_CORELIB_LOCAL_SOURCE={corelib_local_path}");

let mut rev = format!("refs/tags/v{version}");
if let Some(source) = &compiler_package.source {
let source = source.to_string();
Expand Down
1 change: 1 addition & 0 deletions utils/scarb-build-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub const CAIRO_COMMIT_HASH: Option<CommitHash> = match (
_ => panic!("Either SCARB_CAIRO_COMMIT_HASH or SCARB_CAIRO_SHORT_COMMIT_HASH is missing."),
};
pub const CAIRO_COMMIT_REV: &str = env!("SCARB_CAIRO_COMMIT_REV");
pub const SCARB_CORELIB_LOCAL_SOURCE: &str = env!("SCARB_CORELIB_LOCAL_SOURCE");

#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 4144f2e

Please sign in to comment.