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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//!
//! 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;
pub use self::platforms::Platform;

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

///
/// Executes the LLVM repository cloning.
///
pub fn clone(lock: Lock, deep: bool) -> 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
        );
    }

    let mut clone_args = vec!["clone", "--branch", lock.branch.as_str()];
    if !deep {
        clone_args.push("--depth");
        clone_args.push("1");
    }

    utils::command(
        Command::new("git")
            .args(clone_args)
            .arg(lock.url.as_str())
            .arg(destination_path.to_string_lossy().as_ref()),
        "LLVM repository cloning",
    )?;

    if let Some(r#ref) = lock.r#ref {
        utils::command(
            Command::new("git")
                .args(["checkout", r#ref.as_str()])
                .current_dir(destination_path.to_string_lossy().as_ref()),
            "LLVM repository commit checking out",
        )?;
    }

    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 data pulling",
    )?;

    if let Some(r#ref) = lock.r#ref {
        let mut checkout_command = Command::new("git");
        checkout_command.current_dir(destination_path.as_path());
        checkout_command.arg("checkout");
        if force {
            checkout_command.arg("--force");
        }
        checkout_command.arg(r#ref);
        utils::command(&mut checkout_command, "LLVM repository checking out")?;
    }

    Ok(())
}

///
/// Executes the building of the LLVM framework for the platform determined by the cfg macro.
/// Since cfg is evaluated at compile time, overriding the platform with a command-line
/// argument is not possible. So for cross-platform testing, comment out all but the
/// line to be tested, and perhaps also checks in the platform-specific build method.
///
pub fn build(
    build_type: BuildType,
    targets: HashSet<Platform>,
    enable_tests: bool,
    enable_coverage: bool,
    extra_args: Vec<String>,
    use_ccache: bool,
    enable_assertions: bool,
) -> 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,
                    targets,
                    enable_tests,
                    enable_coverage,
                    extra_args,
                    use_ccache,
                    enable_assertions,
                )?;
            } else if cfg!(target_env = "musl") {
                platforms::x86_64_linux_musl::build(
                    build_type,
                    targets,
                    enable_tests,
                    enable_coverage,
                    extra_args,
                    use_ccache,
                    enable_assertions,
                )?;
            } else {
                anyhow::bail!("Unsupported target environment for x86_64 and Linux");
            }
        } else if cfg!(target_os = "macos") {
            platforms::x86_64_macos::build(
                build_type,
                targets,
                enable_tests,
                enable_coverage,
                extra_args,
                use_ccache,
                enable_assertions,
            )?;
        } else if cfg!(target_os = "windows") && cfg!(target_env = "gnu") {
            platforms::x86_64_windows_gnu::build(
                build_type,
                targets,
                enable_tests,
                enable_coverage,
                extra_args,
                use_ccache,
                enable_assertions,
            )?;
        } else {
            anyhow::bail!("Unsupported target OS for x86_64");
        }
    } else if cfg!(target_arch = "aarch64") {
        if cfg!(target_os = "linux") {
            if cfg!(target_env = "gnu") {
                platforms::aarch64_linux_gnu::build(
                    build_type,
                    targets,
                    enable_tests,
                    enable_coverage,
                    extra_args,
                    use_ccache,
                    enable_assertions,
                )?;
            } else if cfg!(target_env = "musl") {
                platforms::aarch64_linux_musl::build(
                    build_type,
                    targets,
                    enable_tests,
                    enable_coverage,
                    extra_args,
                    use_ccache,
                    enable_assertions,
                )?;
            } else {
                anyhow::bail!("Unsupported target environment for aarch64 and Linux");
            }
        } else if cfg!(target_os = "macos") {
            platforms::aarch64_macos::build(
                build_type,
                targets,
                enable_tests,
                enable_coverage,
                extra_args,
                use_ccache,
                enable_assertions,
            )?;
        } else {
            anyhow::bail!("Unsupported target OS for aarch64");
        }
    } else {
        anyhow::bail!("Unsupported target architecture");
    }

    Ok(())
}

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