|
| 1 | +//! This crate contains the build script for the grevm project. |
| 2 | +
|
| 3 | +use std::{path::Path, process::Command}; |
| 4 | + |
| 5 | +struct TestSubmodule { |
| 6 | + path: &'static str, |
| 7 | + url: &'static str, |
| 8 | + branch: &'static str, |
| 9 | + commit: Option<&'static str>, |
| 10 | +} |
| 11 | + |
| 12 | +fn git_clone(submodule: &TestSubmodule) -> Command { |
| 13 | + let mut command = Command::new("git"); |
| 14 | + command.args(&["clone", "-b", submodule.branch, submodule.url, submodule.path]); |
| 15 | + command |
| 16 | +} |
| 17 | + |
| 18 | +fn git_fetch(submodule: &TestSubmodule) -> Command { |
| 19 | + let mut command = Command::new("git"); |
| 20 | + command.args(&["-C", submodule.path, "fetch", "origin", submodule.branch]); |
| 21 | + command |
| 22 | +} |
| 23 | + |
| 24 | +fn git_checkout(submodule: &TestSubmodule) -> Command { |
| 25 | + let mut command = Command::new("git"); |
| 26 | + if let Some(commit) = submodule.commit { |
| 27 | + command.args(&["-C", submodule.path, "checkout", commit]); |
| 28 | + } else { |
| 29 | + command.args(&["-C", submodule.path, "checkout", &format!("origin/{}", submodule.branch)]); |
| 30 | + } |
| 31 | + command |
| 32 | +} |
| 33 | + |
| 34 | +fn update_submodule(submodule: &TestSubmodule) { |
| 35 | + let test_data = Path::new(submodule.path); |
| 36 | + if !test_data.exists() && !git_clone(submodule).status().unwrap().success() { |
| 37 | + panic!("Failed to clone submodule {}", submodule.path); |
| 38 | + } |
| 39 | + |
| 40 | + if submodule.commit.is_some() { |
| 41 | + if git_checkout(submodule).status().unwrap().success() { |
| 42 | + return; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if !git_fetch(submodule).status().unwrap().success() { |
| 47 | + panic!("Failed to fetch branch {} in submodule {}", submodule.branch, submodule.path); |
| 48 | + } |
| 49 | + |
| 50 | + if !git_checkout(submodule).status().unwrap().success() { |
| 51 | + panic!("Failed to checkout {} in submodule {}", submodule.branch, submodule.path); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +fn update_submodules(submodules: &[TestSubmodule]) { |
| 56 | + std::thread::scope(|s| { |
| 57 | + for submodule in submodules { |
| 58 | + s.spawn(|| { |
| 59 | + println!("Updating submodule {}", submodule.path); |
| 60 | + update_submodule(submodule); |
| 61 | + println!("Updating submodule {} done", submodule.path); |
| 62 | + }); |
| 63 | + } |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +fn main() { |
| 68 | + #[allow(unused_mut)] |
| 69 | + let mut submodules = vec![]; |
| 70 | + |
| 71 | + #[cfg(feature = "update-submodule-test-data")] |
| 72 | + submodules.push(TestSubmodule { |
| 73 | + path: "test_data", |
| 74 | + url: "https://github.com/Galxe/grevm-test-data", |
| 75 | + branch: "main", |
| 76 | + commit: Some("4264bdf"), |
| 77 | + }); |
| 78 | + |
| 79 | + #[cfg(feature = "update-submodule-ethereum-tests")] |
| 80 | + submodules.push(TestSubmodule { |
| 81 | + path: "tests/ethereum/tests", |
| 82 | + url: "https://github.com/ethereum/tests", |
| 83 | + branch: "develop", |
| 84 | + commit: Some("4f65a0a"), |
| 85 | + }); |
| 86 | + |
| 87 | + update_submodules(&submodules); |
| 88 | +} |
0 commit comments