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
use crate::{builders::builder_trait::BuilderImpl, file_manager::PATH_SEP, log, shell::Shell};
use pyo3::prelude::PyAnyMethods;
use pyo3::{Bound, PyAny};
use std::{fs, path, path::Path};

#[derive(Debug, Clone)]
pub enum CMakeBuildType {
    Debug,
    Release,
    RelWithDebInfo,
    MinSizeRel,
}

#[derive(Debug, Clone)]
pub struct CMake {
    pub build_type: CMakeBuildType,
    pub jobs: usize,
    pub configure_flags: Option<Vec<String>>,
    pub cmake_root: Option<String>,
}

impl CMake {
    fn configure<P0: AsRef<Path> + std::fmt::Debug, P1: AsRef<Path> + std::fmt::Debug>(
        &self,
        source_path: &P0,
        output_path: &P1,
        dependencies: &[String],
    ) -> Result<(), String> {
        let source_path = path::absolute(source_path).map_err(|err| err.to_string())?;
        let output_path = path::absolute(output_path).map_err(|err| err.to_string())?;

        // Attempt to create the output directory if necessary
        fs::create_dir_all(&output_path).map_err(|e| e.to_string())?;

        let mut shell = Shell::default();
        shell.set_current_dir(output_path.to_str().unwrap());

        for dep in dependencies {
            shell.add_command(&format!("module load {dep}"));
        }

        let mut cmake_cmd = format!("cmake {source_path:?}");

        if let Some(flags) = &self.configure_flags {
            for flag in flags {
                // cmake.arg(flag);
                cmake_cmd.push_str(&format!(" {flag}"));
            }
        }

        cmake_cmd.push_str(&format!(" -DCMAKE_BUILD_TYPE={:?}", self.build_type));
        shell.add_command(&cmake_cmd);

        let (result, stdout, stderr) = shell.exec();

        if result.is_err() {
            return Err("Failed to run CMake command".to_string());
        }
        let result = result.unwrap();

        if !result.success() {
            return Err(format!(
                "Failed to execute command. Output:\n{}\n{}",
                stdout.join("\n"),
                stderr.join("\n")
            ));
        }

        Ok(())
    }

    fn compile<P: AsRef<Path> + std::fmt::Debug>(
        &self,
        path: &P,
        dependencies: &[String],
    ) -> Result<(), String> {
        let mut shell = Shell::default();
        shell.set_current_dir(path.as_ref().to_str().unwrap());
        for dep in dependencies {
            shell.add_command(&format!("module load {dep}"));
        }

        shell.add_command(&format!(
            "cmake --build . --config {:?} --parallel {:?}",
            self.build_type, self.jobs
        ));

        let (result, stdout, stderr) = shell.exec();

        let result = result.map_err(|_| "Failed to run CMake command")?;

        if !result.success() {
            return Err(format!(
                "Failed to execute command. Output:\n{}\n{}",
                stdout.join("\n"),
                stderr.join("\n")
            ));
        }

        Ok(())
    }
}

impl BuilderImpl for CMake {
    fn from_py(object: &Bound<PyAny>) -> Result<Self, String> {
        let build_type = match object
            .getattr("build_type")
            .map_err(|_| "Failed to read attribute 'build_type' of Builder object")?
            .extract::<String>()
            .map_err(|_| "Failed to convert attribute 'build_type' to Rust String")?
            .to_lowercase()
            .as_str()
        {
            "debug" => CMakeBuildType::Debug,
            "release" => CMakeBuildType::Release,
            "relwithdebinfo" => CMakeBuildType::RelWithDebInfo,
            "minsizerel" => CMakeBuildType::MinSizeRel,
            other => log::error(&format!("Unknown CMake build type {other}")),
        };

        let jobs: usize = object
            .getattr("jobs")
            .map_err(|_| "Failed to read attribute 'jobs' of Builder object")?
            .extract()
            .map_err(|_| "Failed to convert attribute 'jobs' to Rust usize")?;

        let configure_flags: Option<Vec<String>> = object
            .getattr("configure_flags")
            .map_err(|_| "Failed to read attribute 'configure_flags' of Builder object")?
            .extract()
            .map_err(|_| "Failed to convert attribute 'configure_flags' to Rust Vec<String>")?;

        let cmake_root: Option<String> = object
            .getattr("cmake_root")
            .map_err(|_| "Failed to read attribute 'cmake_root' of Builder object")?
            .extract()
            .map_err(|_| "Failed to convert attribute 'cmake_root' to Rust String")?;

        Ok(Self {
            build_type,
            jobs,
            configure_flags,
            cmake_root,
        })
    }

    fn build<
        P0: AsRef<Path> + std::fmt::Debug,
        P1: AsRef<Path> + std::fmt::Debug,
        P2: AsRef<Path>,
    >(
        &self,
        source_path: &P0,
        build_path: &P1,
        _: &P2,
        dependencies: &[String],
    ) -> Result<(), String> {
        let cmake_source_path = if let Some(root) = &self.cmake_root {
            source_path.as_ref().to_str().unwrap().to_owned() + PATH_SEP.to_string().as_ref() + root
        } else {
            source_path.as_ref().to_str().unwrap().to_owned()
        };

        self.configure(&cmake_source_path, build_path, dependencies)?;
        self.compile(build_path, dependencies)?;
        Ok(())
    }

    fn install<P0: AsRef<Path>, P1: AsRef<Path>, P2: AsRef<Path>>(
        &self,
        _: &P0, // Source path is not necessary for installation
        build_path: &P1,
        install_path: &P2,
        dependencies: &[String],
    ) -> Result<(), String> {
        let build_path = path::absolute(build_path).map_err(|err| err.to_string())?;
        let install_path = path::absolute(install_path).map_err(|err| err.to_string())?;

        fs::create_dir_all(&install_path).map_err(|e| e.to_string())?;

        if !build_path.exists() {
            return Err(format!("Build directory {build_path:?} does not exist"));
        }

        let mut shell = Shell::default();
        shell.set_current_dir(build_path.to_str().unwrap());

        for dep in dependencies {
            shell.add_command(&format!("module load {dep}"));
        }

        shell.add_command(&format!("cmake --install . --prefix {install_path:?}"));

        let (result, stdout, stderr) = shell.exec();

        let result = result.map_err(|_| "Failed to run CMake command")?;

        if !result.success() {
            return Err(format!(
                "Failed to execute command. Output:\n{}\n{}",
                stdout.join("\n"),
                stderr.join("\n")
            ));
        }

        Ok(())
    }
}