about summary refs log tree commit diff
path: root/src/librustpkg/rustpkg.rc
blob: 54713a66c0c95085e513d434bae19b17314d6a1c (plain)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// rustpkg - a package manager and build system for Rust

#[link(name = "rustpkg",
       vers = "0.7-pre",
       uuid = "25de5e6e-279e-4a20-845c-4cabae92daaf",
       url = "https://github.com/mozilla/rust/tree/master/src/librustpkg")];

#[license = "MIT/ASL2"];
#[crate_type = "lib"];

extern mod std(vers = "0.7-pre");
extern mod rustc(vers = "0.7-pre");
extern mod syntax(vers = "0.7-pre");

use core::*;
pub use core::path::Path;
use core::hashmap::HashMap;
use rustc::driver::{driver, session};
use rustc::metadata::filesearch;
use std::{getopts};
use syntax::{ast, diagnostic};
use util::*;
use path_util::normalize;
use path_util::{build_pkg_id_in_workspace, pkgid_src_in_workspace};
use path_util::{built_executable_in_workspace, built_library_in_workspace};
use path_util::{target_executable_in_workspace, target_library_in_workspace};
use workspace::pkg_parent_workspaces;
use rustc::driver::session::{lib_crate, bin_crate, crate_type};
use context::Ctx;

mod conditions;
mod context;
mod path_util;
mod tests;
mod util;
mod workspace;

pub mod usage;

/// A PkgScript represents user-supplied custom logic for
/// special build hooks. This only exists for packages with
/// an explicit package script.
struct PkgScript<'self> {
    /// Uniquely identifies this package
    id: &'self PkgId,
    // Used to have this field:    deps: ~[(~str, Option<~str>)]
    // but I think it shouldn't be stored here
    /// The contents of the package script: either a file path,
    /// or a string containing the text of the input
    input: driver::input,
    /// The session to use *only* for compiling the custom
    /// build script
    sess: session::Session,
    /// The config for compiling the custom build script
    cfg: ast::crate_cfg,
    /// The crate for the custom build script
    crate: @ast::crate,
    /// Directory in which to store build output
    build_dir: Path
}

impl<'self> PkgScript<'self> {
    /// Given the path name for a package script
    /// and a package ID, parse the package script into
    /// a PkgScript that we can then execute
    fn parse<'a>(script: Path, workspace: &Path, id: &'a PkgId) -> PkgScript<'a> {
        // Get the executable name that was invoked
        let binary = @copy os::args()[0];
        // Build the rustc session data structures to pass
        // to the compiler
        let options = @session::options {
            binary: binary,
            crate_type: session::bin_crate,
            .. copy *session::basic_options()
        };
        let input = driver::file_input(script);
        let sess = driver::build_session(options, diagnostic::emit);
        let cfg = driver::build_configuration(sess, binary, &input);
        let (crate, _) = driver::compile_upto(sess, copy cfg, &input,
                                              driver::cu_parse, None);
        let work_dir = build_pkg_id_in_workspace(id, workspace);

        debug!("Returning package script with id %?", id);

        PkgScript {
            id: id,
            input: input,
            sess: sess,
            cfg: cfg,
            crate: crate,
            build_dir: work_dir
        }
    }

    /// Run the contents of this package script, where <what>
    /// is the command to pass to it (e.g., "build", "clean", "install")
    /// Returns a pair of an exit code and list of configs (obtained by
    /// calling the package script's configs() function if it exists
    // FIXME (#4432): Use workcache to only compile the script when changed
    fn run_custom(&self, what: ~str) -> (~[~str], ExitCode) {
        debug!("run_custom: %s", what);
        let sess = self.sess;

        debug!("Working directory = %s", self.build_dir.to_str());
        // Collect together any user-defined commands in the package script
        let crate = util::ready_crate(sess, self.crate);
        debug!("Building output filenames with script name %s",
               driver::source_name(&self.input));
        match filesearch::get_rustpkg_sysroot() {
            Ok(r) => {
                let root = r.pop().pop().pop().pop(); // :-\
                debug!("Root is %s, calling compile_rest", root.to_str());
                let exe = self.build_dir.push(~"pkg" + util::exe_suffix());
                util::compile_crate_from_input(&self.input, self.id,
                                               Some(copy self.build_dir),
                                               sess, Some(crate),
                                               &exe, @copy os::args()[0],
                                               driver::cu_everything);
                debug!("Running program: %s %s %s", exe.to_str(), root.to_str(), what);
                let status = run::run_program(exe.to_str(), ~[root.to_str(), what]);
                if status != 0 {
                    return (~[], status);
                }
                else {
                    debug!("Running program (configs): %s %s %s",
                           exe.to_str(), root.to_str(), ~"configs");
                    let output = run::program_output(exe.to_str(), ~[root.to_str(), ~"configs"]);
                    // Run the configs() function to get the configs
                    let mut cfgs = ~[];
                    for str::each_word(output.out) |w| {
                        cfgs.push(w.to_owned());
                    }
                    (cfgs, output.status)
                }
            }
            Err(e) => {
                fail!("Running package script, couldn't find rustpkg sysroot (%s)", e)
            }
        }
    }

    fn hash(&self) -> ~str {
        self.id.hash()
    }

}

impl Ctx {

    fn run(&self, cmd: ~str, args: ~[~str]) {
        let root = util::root();

        util::need_dir(&root);
        util::need_dir(&root.push(~"work"));
        util::need_dir(&root.push(~"lib"));
        util::need_dir(&root.push(~"bin"));
        util::need_dir(&root.push(~"tmp"));

        fn sep_name_vers(in: ~str) -> (Option<~str>, Option<~str>) {
            let mut name = None;
            let mut vers = None;

            for str::each_split_char(in, '@') |s| {
                if      name.is_none() { name = Some(s.to_owned()); }
                else if vers.is_none() { vers = Some(s.to_owned()); }
                else                   { break;               }
            }

            (name, vers)
        }

        match cmd {
            ~"build" => {
                if args.len() < 1 {
                    return usage::build();
                }
                // The package id is presumed to be the first command-line
                // argument
                let pkgid = PkgId::new(copy args[0]);
                for pkg_parent_workspaces(&pkgid) |workspace| {
                    self.build(workspace, &pkgid);
                }
            }
            ~"clean" => {
                if args.len() < 1 {
                    return usage::build();
                }
                // The package id is presumed to be the first command-line
                // argument
                let pkgid = PkgId::new(copy args[0]);
                let cwd = os::getcwd();
                self.clean(&cwd, &pkgid); // tjc: should use workspace, not cwd
            }
            ~"do" => {
                if args.len() < 2 {
                    return usage::do_cmd();
                }

                self.do_cmd(copy args[0], copy args[1]);
            }
            ~"info" => {
                self.info();
            }
            ~"install" => {
                if args.len() < 1 {
                    return usage::install();
                }

                // The package id is presumed to be the first command-line
                // argument
                let pkgid = PkgId::new(args[0]);
                for pkg_parent_workspaces(&pkgid) |workspace| {
                    self.install(workspace, &pkgid);
                }
            }
            ~"prefer" => {
                if args.len() < 1 {
                    return usage::uninstall();
                }

                let (name, vers) = sep_name_vers(copy args[0]);

                self.prefer(name.get(), vers);
            }
            ~"test" => {
                self.test();
            }
            ~"uninstall" => {
                if args.len() < 1 {
                    return usage::uninstall();
                }

                let (name, vers) = sep_name_vers(copy args[0]);

                self.uninstall(name.get(), vers);
            }
            ~"unprefer" => {
                if args.len() < 1 {
                    return usage::uninstall();
                }

                let (name, vers) = sep_name_vers(copy args[0]);

                self.unprefer(name.get(), vers);
            }
            _ => fail!("reached an unhandled command")
        }
    }

    fn do_cmd(&self, _cmd: &str, _pkgname: &str)  {
        // stub
        fail!("`do` not yet implemented");
    }

    fn build(&self, workspace: &Path, pkgid: &PkgId) {
        let src_dir   = pkgid_src_in_workspace(pkgid, workspace);
        let build_dir = build_pkg_id_in_workspace(pkgid, workspace);
        debug!("Destination dir = %s", build_dir.to_str());

        // Create the package source
        let mut src = PkgSrc::new(&workspace.push("src"), &build_dir, pkgid);
        debug!("Package src = %?", src);

        // Is there custom build logic? If so, use it
        let pkg_src_dir = src_dir;
        let mut custom = false;
        debug!("Package source directory = %s", pkg_src_dir.to_str());
        let cfgs = match src.package_script_option(&pkg_src_dir) {
            Some(package_script_path) => {
                let pscript = PkgScript::parse(package_script_path,
                                               workspace,
                                               pkgid);
                // Limited right now -- we're only running the post_build
                // hook and probably fail otherwise
                // also post_build should be called pre_build
                let (cfgs, hook_result) = pscript.run_custom(~"post_build");
                debug!("Command return code = %?", hook_result);
                if hook_result != 0 {
                    fail!("Error running custom build command")
                }
                custom = true;
                // otherwise, the package script succeeded
                cfgs
            }
            None => {
                debug!("No package script, continuing");
                ~[]
            }
        };

        // If there was a package script, it should have finished
        // the build already. Otherwise...
        if !custom {
            // Find crates inside the workspace
            src.find_crates();
            // Build it!
            src.build(&build_dir, cfgs, self.sysroot_opt);
        }

    }

    fn clean(&self, workspace: &Path, id: &PkgId)  {
        // Could also support a custom build hook in the pkg
        // script for cleaning files rustpkg doesn't know about.
        // Do something reasonable for now

        let dir = build_pkg_id_in_workspace(id, workspace);
        util::note(fmt!("Cleaning package %s (removing directory %s)",
                        id.to_str(), dir.to_str()));
        if os::path_exists(&dir) {
            util::remove_dir_r(&dir);
            util::note(fmt!("Removed directory %s", dir.to_str()));
        }

        util::note(fmt!("Cleaned package %s", id.to_str()));
    }

    fn info(&self) {
        // stub
        fail!("info not yet implemented");
    }

    fn install(&self, workspace: &Path, id: &PkgId)  {
        use conditions::copy_failed::cond;

        // Should use RUST_PATH in the future.
        // Also should use workcache to not build if not necessary.
        self.build(workspace, id);

        // Now copy stuff into the install dirs
        let maybe_executable = built_executable_in_workspace(id, workspace);
        let maybe_library = built_library_in_workspace(id, workspace);
        let target_exec = target_executable_in_workspace(id, workspace);
        let target_lib = target_library_in_workspace(id, workspace);

        for maybe_executable.each |exec| {
            debug!("Copying: %s -> %s", exec.to_str(), target_exec.to_str());
            if !os::copy_file(exec, &target_exec) {
                cond.raise((copy *exec, copy target_exec));
            }
        }
        for maybe_library.each |lib| {
            debug!("Copying: %s -> %s", lib.to_str(), target_lib.to_str());
            if !os::copy_file(lib, &target_lib) {
                cond.raise((copy *lib, copy target_lib));
            }
        }
    }

    fn fetch(&self, _dir: &Path, _url: ~str, _target: Option<~str>)  {
        // stub
        fail!("fetch not yet implemented");
    }

    fn fetch_curl(&self, dir: &Path, url: ~str)  {
        util::note(fmt!("fetching from %s using curl", url));

        let tar = dir.dir_path().push(&dir.file_path().to_str() + ~".tar");

        if run::program_output(~"curl", ~[~"-f", ~"-s",
                                          ~"-o", tar.to_str(),
                                          url]).status != 0 {
            util::error(~"fetching failed: downloading using curl failed");

            fail!();
        }

        if run::program_output(~"tar", ~[~"-x", ~"--strip-components=1",
                                         ~"-C", dir.to_str(), ~"-f",
                                         tar.to_str()]).status != 0 {
            util::error(~"fetching failed: extracting using tar failed" +
                        ~"(is it a valid tar archive?)");

           fail!();
        }
    }

    fn fetch_git(&self, dir: &Path, url: ~str, mut target: Option<~str>)  {
        util::note(fmt!("fetching from %s using git", url));

        // Git can't clone into a non-empty directory
        util::remove_dir_r(dir);

        if run::program_output(~"git", ~[~"clone", url,
                                         dir.to_str()]).status != 0 {
            util::error(~"fetching failed: can't clone repository");
            fail!();
        }

        if !target.is_none() {
            let mut success = true;

            do util::temp_change_dir(dir) {
                success = run::program_output(~"git",
                                              ~[~"checkout",
                                                target.swap_unwrap()]).status != 0
            }

            if !success {
                util::error(~"fetching failed: can't checkout target");
                fail!();
            }
        }
    }

    fn prefer(&self, id: ~str, vers: Option<~str>)  {
        let package = match util::get_pkg(id, vers) {
            result::Ok(package) => package,
            result::Err(err) => {
                util::error(err);
                fail!(); // Condition?
            }
        };
        let name = package.id.path.to_str(); // ???

        util::note(fmt!("preferring %s v%s", name, package.id.version.to_str()));

        let bin_dir = util::root().push(~"bin");

        for package.bins.each |&bin| {
            let path = Path(bin);
            let mut name = None;
            for str::each_split_char(path.file_path().to_str(), '-') |s| {
                name = Some(s.to_owned());
                break;
            }
            let out = bin_dir.push(name.unwrap());

            util::link_exe(&path, &out);
            util::note(fmt!("linked %s", out.to_str()));
        }

        util::note(fmt!("preferred %s v%s", name, package.id.version.to_str()));
    }

    fn test(&self)  {
        // stub
        fail!("test not yet implemented");
    }

    fn uninstall(&self, _id: ~str, _vers: Option<~str>)  {
        fail!("uninstall not yet implemented");
    }

    fn unprefer(&self, _id: ~str, _vers: Option<~str>)  {
        fail!("unprefer not yet implemented");
    }
}

pub fn main() {
    io::println("WARNING: The Rust package manager is experimental and may be unstable");

    let args = os::args();
    let opts = ~[getopts::optflag(~"h"), getopts::optflag(~"help"),
                 getopts::optflag(~"j"), getopts::optflag(~"json"),
                 getopts::optmulti(~"c"), getopts::optmulti(~"cfg")];
    let matches = &match getopts::getopts(args, opts) {
        result::Ok(m) => m,
        result::Err(f) => {
            util::error(fmt!("%s", getopts::fail_str(f)));

            return;
        }
    };
    let help = getopts::opt_present(matches, ~"h") ||
               getopts::opt_present(matches, ~"help");
    let json = getopts::opt_present(matches, ~"j") ||
               getopts::opt_present(matches, ~"json");
    let mut args = copy matches.free;

    args.shift();

    if (args.len() < 1) {
        return usage::general();
    }

    let cmd = args.shift();

    if !util::is_cmd(cmd) {
        return usage::general();
    } else if help {
        return match cmd {
            ~"build" => usage::build(),
            ~"clean" => usage::clean(),
            ~"do" => usage::do_cmd(),
            ~"info" => usage::info(),
            ~"install" => usage::install(),
            ~"prefer" => usage::prefer(),
            ~"test" => usage::test(),
            ~"uninstall" => usage::uninstall(),
            ~"unprefer" => usage::unprefer(),
            _ => usage::general()
        };
    }

    Ctx {
        sysroot_opt: None, // Currently, only tests override this
        json: json,
        dep_cache: @mut HashMap::new()
    }.run(cmd, args);
}

/// A crate is a unit of Rust code to be compiled into a binary or library
pub struct Crate {
    file: Path,
    flags: ~[~str],
    cfgs: ~[~str]
}

pub struct Listener {
    cmds: ~[~str],
    cb: ~fn()
}

pub fn run(listeners: ~[Listener]) {
    let rcmd = copy os::args()[2];
    let mut found = false;

    for listeners.each |listener| {
        for listener.cmds.each |&cmd| {
            if cmd == rcmd {
                (listener.cb)();

                found = true;

                break;
            }
        }
    }

    if !found {
        os::set_exit_status(42);
    }
}

pub impl Crate {

    fn new(p: &Path) -> Crate {
        Crate {
            file: copy *p,
            flags: ~[],
            cfgs: ~[]
        }
    }

    fn flag(&self, flag: ~str) -> Crate {
        Crate {
            flags: vec::append(copy self.flags, ~[flag]),
            .. copy *self
        }
    }

    fn flags(&self, flags: ~[~str]) -> Crate {
        Crate {
            flags: vec::append(copy self.flags, flags),
            .. copy *self
        }
    }

    fn cfg(&self, cfg: ~str) -> Crate {
        Crate {
            cfgs: vec::append(copy self.cfgs, ~[cfg]),
            .. copy *self
        }
    }

    fn cfgs(&self, cfgs: ~[~str]) -> Crate {
        Crate {
            cfgs: vec::append(copy self.cfgs, cfgs),
            .. copy *self
        }
    }
}

/**
 * Get the working directory of the package script.
 * Assumes that the package script has been compiled
 * in is the working directory.
 */
pub fn work_dir() -> Path {
    os::self_exe_path().get()
}

/**
 * Get the source directory of the package (i.e.
 * where the crates are located). Assumes
 * that the cwd is changed to it before
 * running this executable.
 */
pub fn src_dir() -> Path {
    os::getcwd()
}

condition! {
    bad_pkg_id: (super::Path, ~str) -> ::util::PkgId;
}

// An enumeration of the unpacked source of a package workspace.
// This contains a list of files found in the source workspace.
pub struct PkgSrc {
    root: Path, // root of where the package source code lives
    dst_dir: Path, // directory where we will put the compiled output
    id: PkgId,
    libs: ~[Crate],
    mains: ~[Crate],
    tests: ~[Crate],
    benchs: ~[Crate],
}

condition! {
    build_err: (~str) -> ();
}

impl PkgSrc {

    fn new(src_dir: &Path, dst_dir: &Path,
                  id: &PkgId) -> PkgSrc {
        PkgSrc {
            root: copy *src_dir,
            dst_dir: copy *dst_dir,
            id: copy *id,
            libs: ~[],
            mains: ~[],
            tests: ~[],
            benchs: ~[]
        }
    }


    fn check_dir(&self) -> Path {
        use conditions::nonexistent_package::cond;

        debug!("Pushing onto root: %s | %s", self.id.path.to_str(),
               self.root.to_str());

        let dir = self.root.push_rel(&self.id.path).normalize();

        debug!("Checking dir: %s", dir.to_str());

        // tjc: Rather than erroring out, need to try downloading the
        // contents of the path to a local directory (#5679)
        if !os::path_exists(&dir) {
            cond.raise((copy self.id, ~"missing package dir"));
        }

        if !os::path_is_dir(&dir) {
            cond.raise((copy self.id, ~"supplied path for package dir is a \
                                        non-directory"));
        }

        dir
    }

    // If a file named "pkg.rs" in the current directory exists,
    // return the path for it. Otherwise, None
    fn package_script_option(&self, cwd: &Path) -> Option<Path> {
        let maybe_path = cwd.push("pkg.rs");
        if os::path_exists(&maybe_path) {
            Some(maybe_path)
        }
        else {
            None
        }
    }

    /// True if the given path's stem is self's pkg ID's stem
    /// or if the pkg ID's stem is <rust-foo> and the given path's
    /// stem is foo
    /// Requires that dashes in p have already been normalized to
    /// underscores
    fn stem_matches(&self, p: &Path) -> bool {
        let self_id = normalize(~copy self.id.path).filestem();
        if self_id == p.filestem() {
            return true;
        }
        else {
            for self_id.each |pth| {
                if pth.starts_with("rust_") // because p is already normalized
                    && match p.filestem() {
                           Some(s) => str::eq_slice(s, pth.slice(5, pth.len())),
                           None => false
                       } { return true; }
            }
        }
        false
    }

    fn push_crate(cs: &mut ~[Crate], prefix: uint, p: &Path) {
        assert!(p.components.len() > prefix);
        let mut sub = Path("");
        for vec::slice(p.components, prefix,
                       p.components.len()).each |c| {
            sub = sub.push(*c);
        }
        debug!("found crate %s", sub.to_str());
        cs.push(Crate::new(&sub));
    }

    /// Infers crates to build. Called only in the case where there
    /// is no custom build logic
    fn find_crates(&mut self) {
        use PkgSrc::push_crate;
        use conditions::missing_pkg_files::cond;

        let dir = self.check_dir();
        let prefix = dir.components.len();
        debug!("Matching against %?", self.id.path.filestem());
        for os::walk_dir(&dir) |pth| {
            match pth.filename() {
                Some(~"lib.rs") => push_crate(&mut self.libs,
                                              prefix, pth),
                Some(~"main.rs") => push_crate(&mut self.mains,
                                               prefix, pth),
                Some(~"test.rs") => push_crate(&mut self.tests,
                                               prefix, pth),
                Some(~"bench.rs") => push_crate(&mut self.benchs,
                                                prefix, pth),
                _ => ()
            }
        }

        if self.libs.is_empty() && self.mains.is_empty()
            && self.tests.is_empty() && self.benchs.is_empty() {

            util::note(~"Couldn't infer any crates to build.\n\
                         Try naming a crate `main.rs`, `lib.rs`, \
                         `test.rs`, or `bench.rs`.");
            cond.raise(copy self.id);
        }

        debug!("found %u libs, %u mains, %u tests, %u benchs",
               self.libs.len(),
               self.mains.len(),
               self.tests.len(),
               self.benchs.len())
    }

    fn build_crates(&self,
                    maybe_sysroot: Option<@Path>,
                    dst_dir: &Path,
                    src_dir: &Path,
                    crates: &[Crate],
                    cfgs: &[~str],
                    test: bool, crate_type: crate_type) {

        for crates.each |&crate| {
            let path = &src_dir.push_rel(&crate.file).normalize();
            util::note(fmt!("build_crates: compiling %s", path.to_str()));
            util::note(fmt!("build_crates: destination dir is %s", dst_dir.to_str()));

            let result = util::compile_crate(maybe_sysroot, &self.id, path,
                                     dst_dir,
                                     crate.flags,
                                     crate.cfgs + cfgs,
                                     false, test, crate_type);
            if !result {
                build_err::cond.raise(fmt!("build failure on %s",
                                           path.to_str()));
            }
            debug!("Result of compiling %s was %?",
                   path.to_str(), result);
        }
    }

    fn build(&self, dst_dir: &Path, cfgs: ~[~str], maybe_sysroot: Option<@Path>) {
        let dir = self.check_dir();
        debug!("Building libs");
        self.build_crates(maybe_sysroot, dst_dir, &dir, self.libs, cfgs, false, lib_crate);
        debug!("Building mains");
        self.build_crates(maybe_sysroot, dst_dir, &dir, self.mains, cfgs, false, bin_crate);
        debug!("Building tests");
        self.build_crates(maybe_sysroot, dst_dir, &dir, self.tests, cfgs, true, bin_crate);
        debug!("Building benches");
        self.build_crates(maybe_sysroot, dst_dir, &dir, self.benchs, cfgs, true, bin_crate);
    }
}