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
//! Library to retrieve information about a Cargo project
//!
//! Useful for building Cargo subcommands that require building the project.

#![deny(missing_docs)]
#![deny(warnings)]

#[macro_use]
extern crate failure;
extern crate serde;
extern crate toml;
#[macro_use]
extern crate serde_derive;
extern crate rustc_cfg;

mod config;
mod manifest;
mod workspace;

use std::{
    env,
    fs::File,
    io::Read,
    path::{Path, PathBuf},
};

use rustc_cfg::Cfg;
use serde::Deserialize;
use toml::de;

use config::Config;
use manifest::Manifest;

/// Information about a Cargo project
pub struct Project {
    name: String,

    target: Option<String>,

    target_dir: PathBuf,
}

/// Errors
#[derive(Debug, Fail)]
pub enum Error {
    /// error: not a Cargo project
    #[fail(display = "not a Cargo project")]
    NotACargoProject,
}

impl Project {
    /// Retrieves information about the Cargo project at the given `path`
    ///
    /// `path` doesn't need to be the directory that contains the `Cargo.toml` file; it can be any
    /// point within the Cargo project.
    pub fn query<P>(path: P) -> Result<Self, failure::Error>
    where
        P: AsRef<Path>,
    {
        let path = path.as_ref();
        let root = search(path, "Cargo.toml").ok_or(Error::NotACargoProject)?;

        // parse Cargo.toml
        let manifest = parse::<Manifest>(&root.join("Cargo.toml"))?;

        // parse .cargo/config
        let mut target = None;
        let mut target_dir = env::var_os("CARGO_TARGET_DIR").map(PathBuf::from);
        if let Some(path) = search(root, ".cargo/config") {
            let config: Config = parse(&path.join(".cargo/config"))?;

            if let Some(build) = config.build {
                target = build.target;
                target_dir = target_dir.or(build.target_dir.map(PathBuf::from));
            }
        }

        // is this project member of a workspace?
        let mut cwd = root.parent();
        let mut workspace = None;
        while let Some(path) = cwd {
            if let Some(outer_root) = search(path, "Cargo.toml") {
                if let Ok(manifest) = parse::<workspace::Manifest>(&outer_root.join("Cargo.toml")) {
                    // this is indeed a workspace
                    if manifest
                        .workspace
                        .members
                        .iter()
                        .any(|member| outer_root.join(member) == root)
                    {
                        // we are a member of this workspace
                        workspace = Some(outer_root);
                        break;
                    }
                }

                // this is not a workspace; keep looking
                cwd = outer_root.parent();
                continue;
            }

            break;
        }

        target_dir = target_dir.or_else(|| workspace.map(|path| path.join("target")));

        Ok(Project {
            name: manifest.package.name,
            target,
            target_dir: target_dir.unwrap_or(root.join("target")),
        })
    }

    /// Returns the path to a build artifact
    ///
    /// # Inputs
    ///
    /// - `artifact` is the kind of build artifact: `Bin` (`--bin`), `Example` (`--example`), `Lib`
    /// (`--lib`)
    /// - `profile` is the compilation profile: `Dev` or `Release` (`--release`)
    /// - `target` is the specified compilation target (`--target`)
    /// - `host` is the triple of host -- this is used as the compilation target when no `target` is
    /// specified and the project has no default build target
    pub fn path(
        &self,
        artifact: Artifact,
        profile: Profile,
        target: Option<&str>,
        host: &str,
    ) -> Result<PathBuf, failure::Error> {
        let mut path = self.target_dir().to_owned();

        if let Some(target) = target.or(self.target()) {
            path.push(target);
        }

        let cfg = Cfg::of(target.or(self.target()).unwrap_or(host))?;

        match profile {
            Profile::Dev => path.push("debug"),
            Profile::Release => path.push("release"),
            Profile::__HIDDEN__ => unreachable!(),
        }

        match artifact {
            Artifact::Bin(bin) => {
                path.push(bin);

                if cfg.target_arch == "wasm32" {
                    path.set_extension("wasm");
                } else if cfg
                    .target_family
                    .as_ref()
                    .map(|f| f == "windows")
                    .unwrap_or(false)
                {
                    path.set_extension("exe");
                }
            }
            Artifact::Example(example) => {
                path.push("examples");
                path.push(example);

                if cfg.target_arch == "wasm32" {
                    path.set_extension("wasm");
                } else if cfg
                    .target_family
                    .as_ref()
                    .map(|f| f == "windows")
                    .unwrap_or(false)
                {
                    path.set_extension("exe");
                }
            }
            Artifact::Lib => {
                path.push(format!("lib{}.rlib", self.name().replace("-", "_")));
            }
            Artifact::__HIDDEN__ => unreachable!(),
        }

        Ok(path)
    }

    /// Returns the name of the project (`package.name`)
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the default compilation target
    pub fn target(&self) -> Option<&str> {
        self.target.as_ref().map(|s| &**s)
    }

    /// Returns the target directory path
    ///
    /// This is where build artifacts are placed
    pub fn target_dir(&self) -> &Path {
        &self.target_dir
    }
}

/// Build artifact
#[derive(Clone, Copy)]
pub enum Artifact<'a> {
    /// Binary (`--bin`)
    Bin(&'a str),
    /// Example (`--example`)
    Example(&'a str),
    /// Library (`--lib`)
    Lib,
    #[doc(hidden)]
    __HIDDEN__,
}

/// Build profile
#[derive(Clone, Copy, PartialEq)]
pub enum Profile {
    /// Development profile
    Dev,
    /// Release profile (`--release`)
    Release,
    #[doc(hidden)]
    __HIDDEN__,
}

impl Profile {
    /// Is this the release profile?
    pub fn is_release(&self) -> bool {
        *self == Profile::Release
    }
}

/// Search for `file` in `path` and its parent directories
fn search<'p>(mut path: &'p Path, file: &str) -> Option<&'p Path> {
    loop {
        if path.join(file).exists() {
            return Some(path);
        }

        if let Some(p) = path.parent() {
            path = p;
        } else {
            return None;
        }
    }
}

fn parse<T>(path: &Path) -> Result<T, failure::Error>
where
    T: for<'de> Deserialize<'de>,
{
    let mut s = String::new();
    File::open(path)?.read_to_string(&mut s)?;
    Ok(de::from_str(&s)?)
}

#[cfg(test)]
mod tests {
    use std::env;

    use super::{Artifact, Profile, Project};

    #[test]
    fn path() {
        let project = Project::query(env::current_dir().unwrap()).unwrap();

        let thumb = "thumbv7m-none-eabi";
        let wasm = "wasm32-unknown-unknown";
        let windows = "x86_64-pc-windows-msvc";
        let linux = "x86_64-unknown-linux-gnu";

        let p = project
            .path(Artifact::Bin("foo"), Profile::Dev, None, windows)
            .unwrap();

        assert!(p.to_str().unwrap().ends_with("target/debug/foo.exe"));

        let p = project
            .path(Artifact::Example("bar"), Profile::Dev, None, windows)
            .unwrap();

        assert!(p
            .to_str()
            .unwrap()
            .ends_with("target/debug/examples/bar.exe"));

        let p = project
            .path(Artifact::Bin("foo"), Profile::Dev, Some(thumb), windows)
            .unwrap();

        assert!(p
            .to_str()
            .unwrap()
            .ends_with(&format!("target/{}/debug/foo", thumb)));

        let p = project
            .path(Artifact::Example("bar"), Profile::Dev, Some(thumb), windows)
            .unwrap();

        assert!(p
            .to_str()
            .unwrap()
            .ends_with(&format!("target/{}/debug/examples/bar", thumb)));

        let p = project
            .path(Artifact::Bin("foo"), Profile::Dev, Some(wasm), linux)
            .unwrap();

        assert!(p
            .to_str()
            .unwrap()
            .ends_with(&format!("target/{}/debug/foo.wasm", wasm)));

        let p = project
            .path(Artifact::Example("bar"), Profile::Dev, Some(wasm), linux)
            .unwrap();

        assert!(p
            .to_str()
            .unwrap()
            .ends_with(&format!("target/{}/debug/examples/bar.wasm", wasm)));
    }
}