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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//!
//! Main entrypoint: [`bin::Compiler`]

use std::{
    io::BufReader,
    path::PathBuf,
    process::{Command, Stdio},
};

use camino::Utf8PathBuf;
use cargo_metadata::CompilerMessage;
use derivative::Derivative;
use tracing::instrument;

use crate::{
    handle_compiler_msg, BuildError, ExecutableArtifact, FeatureSpec, PackageSpec, MSG_FORMAT,
};

/// Compile a binary
///
/// ```
/// # use seacan::{bin::Compiler, FeatureSpec};
/// let artifact = Compiler::bin("hello_world")
///     .workspace("samples/hello_world")
///     .features(FeatureSpec::new(vec!["non_default_feature".into()]))
///     .release(true)
///     .compile()?;
/// # Ok::<_, seacan::BuildError>(())
#[derive(Derivative)]
#[derivative(Debug)]
pub struct Compiler {
    workspace: Option<PathBuf>,
    package: PackageSpec,
    name: String,
    is_example: bool,
    #[derivative(Debug = "ignore")]
    on_compiler_msg: Option<Box<dyn FnMut(CompilerMessage)>>,
    target_dir: Option<Utf8PathBuf>,
    features: Option<FeatureSpec>,
    is_release: bool,
}

impl Compiler {
    // TODO: fn default_bin
    //   See <https://github.com/rust-lang/cargo/issues/9491>

    /// Compile a binary.
    ///
    /// Note: By default the default binary has the name of the crate.
    #[must_use]
    pub fn bin(name: impl Into<String>) -> Self {
        Self::new(name, false)
    }

    /// Compile an example.
    #[must_use]
    pub fn example(name: impl Into<String>) -> Self {
        Self::new(name, true)
    }

    fn new(name: impl Into<String>, is_example: bool) -> Self {
        Self {
            workspace: None,
            package: PackageSpec::Any,
            name: name.into(),
            is_example,
            on_compiler_msg: None,
            target_dir: None,
            features: None,
            is_release: false,
        }
    }

    /// The directory to run cargo in.
    ///
    /// By default the current working directory.
    pub fn workspace(&mut self, path: impl Into<PathBuf>) -> &mut Self {
        self.workspace = Some(path.into());
        self
    }

    /// The package the binary is in.
    ///
    /// By default [`PackageSpec::Any`].
    pub fn package(&mut self, package: PackageSpec) -> &mut Self {
        self.package = package;
        self
    }

    /// Callback for compiler messages.
    ///
    /// Regardless of if you specify this compiler messages will be logged at
    /// debug level using [`tracing`].
    pub fn on_compiler_msg(&mut self, cb: impl FnMut(CompilerMessage) + 'static) -> &mut Self {
        self.on_compiler_msg = Some(Box::new(cb));
        self
    }

    /// Where to put the build artifacts.
    ///
    /// By default this is whatever cargo chooses by default.
    pub fn target_dir(&mut self, target_dir: impl Into<Utf8PathBuf>) -> &mut Self {
        self.target_dir = Some(target_dir.into());
        self
    }

    /// Enable or disable feature flags.
    ///
    /// By default this is whatever cargo chooses by default.
    pub fn features(&mut self, features: FeatureSpec) -> &mut Self {
        self.features = Some(features);
        self
    }

    /// If we should build in release mode.
    pub fn release(&mut self, is_release: bool) -> &mut Self {
        self.is_release = is_release;
        self
    }

    /// Compile the described executable
    #[instrument(err)]
    pub fn compile(&mut self) -> Result<ExecutableArtifact, BuildError> {
        let mut cmd = Command::new("cargo");

        cmd.arg("build")
            .arg(MSG_FORMAT)
            .args(&["--package", self.package.as_repr()])
            .stderr(Stdio::piped())
            .stdout(Stdio::piped())
            .stdin(Stdio::null());

        if let Some(features) = &self.features {
            cmd.args(features.to_args());
        }

        if let Some(ref workspace) = self.workspace {
            cmd.current_dir(workspace);
        }

        if self.is_release {
            cmd.arg("--release");
        }

        if let Some(ref target_dir) = self.target_dir {
            cmd.args(&["--target-dir", target_dir.as_str()]);
        }

        if self.is_example {
            cmd.args(&["--example", &self.name]);
        } else {
            cmd.args(&["--bin", &self.name]);
        }

        let mut cmd = cmd.spawn()?;

        let stdout = cmd.stdout.take().unwrap();
        let stderr = cmd.stderr.take().unwrap();

        let mut artifact = None;

        let messages = cargo_metadata::Message::parse_stream(BufReader::new(stdout));
        for msg in messages {
            match msg? {
                cargo_metadata::Message::CompilerMessage(msg) => {
                    handle_compiler_msg(msg, &mut self.on_compiler_msg)
                }
                cargo_metadata::Message::CompilerArtifact(art) => {
                    if art.executable.is_none() {
                        continue;
                    }
                    assert!(
                    artifact.is_none(),
                    "Expected cargo build with --bin or --example to only produce one executable"
                );
                    artifact = Some(art);
                }
                _ => {}
            }
        }

        if cmd.wait()?.success() {
            let artifact = artifact
                .expect("If cargo build exits with success should have built an executable");
            Ok(ExecutableArtifact::maybe_from(artifact).expect("Artifact has executable"))
        } else {
            Err(BuildError::from_stderr(stderr))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_common::{init, Result};
    use pretty_assertions::{assert_eq, assert_ne};

    // TODO: Use assert_matches! when stable

    #[test]
    fn test_features() -> Result {
        let artifact = Compiler::bin("hello_world")
            .workspace("samples/hello_world")
            .features(FeatureSpec::new(vec!["non_default_feature".into()]))
            .compile()?;
        assert_eq!(
            vec![
                "default".to_string(),
                "default_feature".to_string(),
                "non_default_feature".to_string()
            ],
            artifact.features
        );
        Ok(())
    }

    #[test]
    fn test_no_default_features() -> Result {
        let artifact = Compiler::bin("hello_world")
            .workspace("samples/hello_world")
            .features(FeatureSpec::none())
            .compile()?;
        assert!(artifact.features.is_empty());
        Ok(())
    }

    #[test]
    fn test_release_false() -> Result {
        init();
        let artifact = Compiler::bin("hello_world")
            .workspace("samples/hello_world")
            .release(false)
            .compile()?;
        assert_eq!("0", artifact.profile.opt_level);
        Ok(())
    }
    #[test]
    fn test_release_true() -> Result {
        let artifact = Compiler::bin("hello_world")
            .workspace("samples/hello_world")
            .release(true)
            .compile()?;
        assert_ne!("0", artifact.profile.opt_level);
        Ok(())
    }

    #[test]
    fn test_release_default() -> Result {
        init();
        let artifact = Compiler::bin("hello_world")
            .workspace("samples/hello_world")
            .compile()?;
        assert_eq!("0", artifact.profile.opt_level);
        Ok(())
    }

    #[test]
    fn test_cargo_error() {
        init();
        let result = Compiler::bin("hello_world").workspace("/").compile();
        assert!(matches!(
            result,
            Err(BuildError::Cargo(stderr)) if stderr == "error: could not find `Cargo.toml` in `/` or any parent directory\n"
        ));
    }

    #[test]
    fn test_bin_main() -> Result {
        init();
        let artifact = Compiler::bin("hello_world")
            .workspace("samples/hello_world")
            .compile()?;
        assert_eq!("hello_world", artifact.target.name);
        assert!(artifact.target.src_path.ends_with("src/main.rs"));
        Ok(())
    }

    #[test]
    fn test_bin_2() -> Result {
        init();
        let artifact = Compiler::bin("bin_2")
            .workspace("samples/hello_world")
            .compile()?;
        assert_eq!("bin_2", artifact.target.name);
        assert!(artifact.target.src_path.ends_with("src/bin/bin_2.rs"));
        Ok(())
    }

    #[test]
    fn test_bin_nonexistent() {
        let result = Compiler::bin("bin_that_doesnt_exist")
            .workspace("samples/hello_world")
            .compile();
        assert!(matches!(result, Err(BuildError::NotFound(_))));
    }

    #[test]
    fn test_bin_nonexistent_package() {
        init();
        let result = Compiler::bin("bin_that_doesnt_exist")
            .package(PackageSpec::name("package_that_doesnt_exist"))
            .workspace("samples/hello_world")
            .compile();
        assert!(matches!(result, Err(BuildError::PackageNotFound(_))));
    }

    #[test]
    fn test_example() -> Result {
        init();
        let artifact = Compiler::example("example_1")
            .workspace("samples/hello_world")
            .compile()?;
        assert_eq!("example_1", artifact.target.name);
        assert!(artifact.target.src_path.ends_with("examples/example_1.rs"));
        Ok(())
    }

    #[test]
    fn test_example_nonexistent() {
        init();
        let result = Compiler::example("example_does_not_exist")
            .workspace("samples/hello_world")
            .compile();
        assert!(matches!(result, Err(BuildError::NotFound(_))));
    }

    #[test]
    fn test_example_nonexistent_package() {
        init();
        let result = Compiler::example("example_1")
            .workspace("samples/hello_world")
            .package(PackageSpec::name("nonexistent_package"))
            .compile();
        assert!(matches!(result, Err(BuildError::PackageNotFound(_))));
    }

    #[test]
    fn test_ws_member_main() -> Result {
        init();
        init();
        let artifact = Compiler::bin("ws_member")
            .package(PackageSpec::name("ws_member"))
            .workspace("samples/hello_world")
            .compile()?;
        assert_eq!("ws_member", artifact.target.name);
        assert!(artifact.target.src_path.ends_with("ws_member/src/main.rs"));
        Ok(())
    }
}