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
//!
//! The zkEVM LLVM builder library.
//!

pub mod build_type;
pub mod llvm_path;
pub mod lock;
pub mod platforms;
pub mod utils;

pub use self::build_type::BuildType;
pub use self::llvm_path::LLVMPath;
pub use self::lock::Lock;

use std::path::PathBuf;
use std::process::Command;

///
/// Executes the LLVM repository cloning.
///
pub fn clone(lock: Lock) -> anyhow::Result<()> {
    utils::check_presence("git")?;

    let destination_path = PathBuf::from(LLVMPath::DIRECTORY_LLVM_SOURCE);
    if destination_path.exists() {
        anyhow::bail!(
            "The repository is already cloned at {:?}. Use `checkout` instead",
            destination_path
        );
    }

    utils::command(
        Command::new("git").args([
            "clone",
            "--branch",
            lock.branch.as_str(),
            lock.url.as_str(),
            destination_path.to_string_lossy().as_ref(),
        ]),
        "LLVM repository cloning",
    )?;

    Ok(())
}

///
/// Executes the checkout of the specified branch.
///
pub fn checkout(lock: Lock, force: bool) -> anyhow::Result<()> {
    let destination_path = PathBuf::from(LLVMPath::DIRECTORY_LLVM_SOURCE);

    utils::command(
        Command::new("git")
            .current_dir(destination_path.as_path())
            .args(["fetch", "--all", "--tags"]),
        "LLVM repository data fetching",
    )?;
    if force {
        utils::command(
            Command::new("git")
                .current_dir(destination_path.as_path())
                .args(["clean", "-d", "-x", "--force"]),
            "LLVM repository cleaning",
        )?;
        utils::command(
            Command::new("git")
                .current_dir(destination_path.as_path())
                .args(["checkout", "--force", lock.branch.as_str()]),
            "LLVM repository checking out",
        )?;
    } else {
        utils::command(
            Command::new("git")
                .current_dir(destination_path.as_path())
                .args(["checkout", lock.branch.as_str()]),
            "LLVM repository checking out",
        )?;
    }

    Ok(())
}

///
/// Executes the LLVM framework building.
///
pub fn build(build_type: BuildType) -> anyhow::Result<()> {
    std::fs::create_dir_all(LLVMPath::DIRECTORY_LLVM_TARGET)?;

    if cfg!(target_arch = "x86_64") {
        if cfg!(target_os = "linux") {
            if cfg!(target_env = "gnu") {
                platforms::x86_64_linux_gnu::build(build_type)?;
            } else if cfg!(target_env = "musl") {
                platforms::x86_64_linux_musl::build(build_type)?;
            }
        } else if cfg!(target_os = "macos") {
            platforms::x86_64_macos::build(build_type)?;
        } else if cfg!(target_os = "windows") && cfg!(target_env = "gnu") {
            platforms::x86_64_windows_gnu::build(build_type)?;
        }
    } else if cfg!(target_arch = "aarch64") {
        if cfg!(target_os = "macos") {
            platforms::aarch64_macos::build(build_type)?;
        }
    } else {
        anyhow::bail!("Unsupported on your machine");
    }

    Ok(())
}

///
/// Executes the build artifacts cleaning.
///
pub fn clean() -> anyhow::Result<()> {
    Ok(std::fs::remove_dir_all(PathBuf::from(
        LLVMPath::DIRECTORY_LLVM_TARGET,
    ))?)
}