eval_stack/
compile.rs

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
use std::{
    env,
    path::{Path, PathBuf},
    process::Stdio,
};

use anyhow::Result;
use tokio::process::Command;

#[derive(Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum Language {
    C,
    CPP,
    Rust,
    Python,
    NodeJs,
}

pub async fn compile<B: Into<PathBuf>, E: AsRef<str>, O: AsRef<str>>(
    language: Language,
    base: B,
    source_file: E,
    output_file: O,
) -> Result<()> {
    let base_path = Into::<PathBuf>::into(base);
    let source_path = base_path.join(source_file.as_ref());
    let source_path_str = source_path.to_string_lossy();
    let output_path = base_path.join(output_file.as_ref());
    let output_path_str = output_path.to_string_lossy();

    let command = match language {
        Language::C => {
            let mut command = Command::new("gcc");
            command.args([
                "-O2",
                "-w",
                "-fmax-errors=3",
                "-std=c17",
                source_path_str.as_ref(),
                "-lm",
                "-o",
                output_path_str.as_ref(),
            ]);
            Some(command)
        }
        Language::CPP => {
            let mut command = Command::new("g++");
            command.args([
                "-O2",
                "-w",
                "-fmax-errors=3",
                "-std=c++20",
                source_path_str.as_ref(),
                "-lm",
                "-o",
                output_path_str.as_ref(),
            ]);
            Some(command)
        }
        Language::Rust => {
            let rustc_path = Path::new(&env::var("HOME")?)
                .join(".rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc");
            if !rustc_path.exists() {
                panic!("Failed to find rustc from {}", rustc_path.display());
            }
            let mut command = Command::new(rustc_path);
            command.args([
                "--edition=2021",
                source_path_str.as_ref(),
                "-C",
                "embed-bitcode=no",
                "-C",
                "opt-level=2",
                "-o",
                output_file.as_ref(),
            ]);
            Some(command)
        }
        Language::Python => {
            let mut command = Command::new("python3");
            command.args(["-m", "py_compile", source_path_str.as_ref()]);
            Some(command)
        }
        Language::NodeJs => None,
    };

    if let Some(mut command) = command {
        command.kill_on_drop(true).stdout(Stdio::piped()).spawn()?;

        let output = command.output().await?;

        if !output.status.success() {
            let error_message = String::from_utf8_lossy(&output.stderr).to_string();
            return Err(anyhow::anyhow!(
                "Failed to compile source code: {}",
                error_message
            ));
        }
    }
    Ok(())
}