step.rs 12.9 KB
Newer Older
A
Alex Crichton 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright 2016 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.

use std::collections::HashSet;

use build::{Build, Compiler};

#[derive(Hash, Eq, PartialEq, Clone, Debug)]
pub struct Step<'a> {
    pub src: Source<'a>,
    pub target: &'a str,
}

macro_rules! targets {
    ($m:ident) => {
        $m! {
24 25
            // Step representing building the stageN compiler. This is just the
            // compiler executable itself, not any of the support libraries
A
Alex Crichton 已提交
26
            (rustc, Rustc { stage: u32 }),
27

28 29
            // Steps for the two main cargo builds. These are parameterized over
            // the compiler which is producing the artifact.
30
            (libstd, Libstd { compiler: Compiler<'a> }),
31
            (libtest, Libtest { compiler: Compiler<'a> }),
32
            (librustc, Librustc { compiler: Compiler<'a> }),
33

34 35
            // Links the target produced by the compiler provided into the
            // host's directory also provided.
36 37 38 39
            (libstd_link, LibstdLink {
                compiler: Compiler<'a>,
                host: &'a str
            }),
40 41 42 43
            (libtest_link, LibtestLink {
                compiler: Compiler<'a>,
                host: &'a str
            }),
44 45 46 47 48
            (librustc_link, LibrustcLink {
                compiler: Compiler<'a>,
                host: &'a str
            }),

49
            // Various tools that we can build as part of the build.
50
            (tool_linkchecker, ToolLinkchecker { stage: u32 }),
51
            (tool_rustbook, ToolRustbook { stage: u32 }),
52
            (tool_error_index, ToolErrorIndex { stage: u32 }),
53
            (tool_cargotest, ToolCargoTest { stage: u32 }),
54
            (tool_tidy, ToolTidy { stage: u32 }),
55

56 57 58 59 60 61
            // Steps for long-running native builds. Ideally these wouldn't
            // actually exist and would be part of build scripts, but for now
            // these are here.
            //
            // There aren't really any parameters to this, but empty structs
            // with braces are unstable so we just pick something that works.
A
Alex Crichton 已提交
62 63
            (llvm, Llvm { _dummy: () }),
            (compiler_rt, CompilerRt { _dummy: () }),
64 65 66 67

            // Steps for various pieces of documentation that we can generate,
            // the 'doc' step is just a pseudo target to depend on a bunch of
            // others.
68 69 70 71 72
            (doc, Doc { stage: u32 }),
            (doc_book, DocBook { stage: u32 }),
            (doc_nomicon, DocNomicon { stage: u32 }),
            (doc_style, DocStyle { stage: u32 }),
            (doc_standalone, DocStandalone { stage: u32 }),
73
            (doc_std, DocStd { stage: u32 }),
74
            (doc_test, DocTest { stage: u32 }),
75
            (doc_rustc, DocRustc { stage: u32 }),
76
            (doc_error_index, DocErrorIndex { stage: u32 }),
77 78 79 80

            // Steps for running tests. The 'check' target is just a pseudo
            // target to depend on a bunch of others.
            (check, Check { stage: u32, compiler: Compiler<'a> }),
81
            (check_linkcheck, CheckLinkcheck { stage: u32 }),
82
            (check_cargotest, CheckCargoTest { stage: u32 }),
83
            (check_tidy, CheckTidy { stage: u32 }),
A
Alex Crichton 已提交
84 85 86 87 88 89 90

            // Distribution targets, creating tarballs
            (dist, Dist { stage: u32 }),
            (dist_docs, DistDocs { stage: u32 }),
            (dist_mingw, DistMingw { _dummy: () }),
            (dist_rustc, DistRustc { stage: u32 }),
            (dist_std, DistStd { compiler: Compiler<'a> }),
A
Alex Crichton 已提交
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
        }
    }
}

macro_rules! item { ($a:item) => ($a) }

macro_rules! define_source {
    ($(($short:ident, $name:ident { $($args:tt)* }),)*) => {
        item! {
            #[derive(Hash, Eq, PartialEq, Clone, Debug)]
            pub enum Source<'a> {
                $($name { $($args)* }),*
            }
        }
    }
}

targets!(define_source);

pub fn all(build: &Build) -> Vec<Step> {
    let mut ret = Vec::new();
    let mut all = HashSet::new();
    for target in top_level(build) {
        fill(build, &target, &mut ret, &mut all);
    }
    return ret;

    fn fill<'a>(build: &'a Build,
                target: &Step<'a>,
                ret: &mut Vec<Step<'a>>,
                set: &mut HashSet<Step<'a>>) {
        if set.insert(target.clone()) {
            for dep in target.deps(build) {
                fill(build, &dep, ret, set);
            }
            ret.push(target.clone());
        }
    }
}

fn top_level(build: &Build) -> Vec<Step> {
    let mut targets = Vec::new();
    let stage = build.flags.stage.unwrap_or(2);

    let host = Step {
        src: Source::Llvm { _dummy: () },
        target: build.flags.host.iter().next()
                     .unwrap_or(&build.config.build),
    };
    let target = Step {
        src: Source::Llvm { _dummy: () },
        target: build.flags.target.iter().next().map(|x| &x[..])
                     .unwrap_or(host.target)
    };

    add_steps(build, stage, &host, &target, &mut targets);

    if targets.len() == 0 {
        let t = Step {
            src: Source::Llvm { _dummy: () },
            target: &build.config.build,
        };
153
        targets.push(t.doc(stage));
A
Alex Crichton 已提交
154 155 156 157 158
        for host in build.config.host.iter() {
            if !build.flags.host.contains(host) {
                continue
            }
            let host = t.target(host);
159
            if host.target == build.config.build {
160
                targets.push(host.librustc(host.compiler(stage)));
161
            } else {
162
                targets.push(host.librustc_link(t.compiler(stage), host.target));
163
            }
A
Alex Crichton 已提交
164 165 166 167
            for target in build.config.target.iter() {
                if !build.flags.target.contains(target) {
                    continue
                }
168 169 170

                if host.target == build.config.build {
                    targets.push(host.target(target)
171
                                     .libtest(host.compiler(stage)));
172 173
                } else {
                    targets.push(host.target(target)
174
                                     .libtest_link(t.compiler(stage), host.target));
175
                }
A
Alex Crichton 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188
            }
        }
    }

    return targets

}

fn add_steps<'a>(build: &'a Build,
                 stage: u32,
                 host: &Step<'a>,
                 target: &Step<'a>,
                 targets: &mut Vec<Step<'a>>) {
189 190 191 192 193 194
    struct Context<'a> {
        stage: u32,
        compiler: Compiler<'a>,
        _dummy: (),
        host: &'a str,
    }
A
Alex Crichton 已提交
195
    for step in build.flags.step.iter() {
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214

        // The macro below insists on hygienic access to all local variables, so
        // we shove them all in a struct and subvert hygiene by accessing struct
        // fields instead,
        let cx = Context {
            stage: stage,
            compiler: host.target(&build.config.build).compiler(stage),
            _dummy: (),
            host: host.target,
        };
        macro_rules! add_step {
            ($(($short:ident, $name:ident { $($arg:ident: $t:ty),* }),)*) => ({$(
                let name = stringify!($short).replace("_", "-");
                if &step[..] == &name[..] {
                    targets.push(target.$short($(cx.$arg),*));
                    continue
                }
                drop(name);
            )*})
A
Alex Crichton 已提交
215
        }
216 217

        targets!(add_step);
218 219

        panic!("unknown step: {}", step);
A
Alex Crichton 已提交
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
    }
}

macro_rules! constructors {
    ($(($short:ident, $name:ident { $($arg:ident: $t:ty),* }),)*) => {$(
        fn $short(&self, $($arg: $t),*) -> Step<'a> {
            Step {
                src: Source::$name { $($arg: $arg),* },
                target: self.target,
            }
        }
    )*}
}

impl<'a> Step<'a> {
    fn compiler(&self, stage: u32) -> Compiler<'a> {
        Compiler::new(stage, self.target)
    }

    fn target(&self, target: &'a str) -> Step<'a> {
        Step { target: target, src: self.src.clone() }
    }

    targets!(constructors);

    pub fn deps(&self, build: &'a Build) -> Vec<Step<'a>> {
        match self.src {
            Source::Rustc { stage: 0 } => {
248
                Vec::new()
A
Alex Crichton 已提交
249 250
            }
            Source::Rustc { stage } => {
251
                let compiler = Compiler::new(stage - 1, &build.config.build);
252
                vec![self.librustc(compiler)]
A
Alex Crichton 已提交
253
            }
254
            Source::Librustc { compiler } => {
255 256 257 258
                vec![self.libtest(compiler), self.llvm(())]
            }
            Source::Libtest { compiler } => {
                vec![self.libstd(compiler)]
A
Alex Crichton 已提交
259
            }
260
            Source::Libstd { compiler } => {
A
Alex Crichton 已提交
261 262 263
                vec![self.compiler_rt(()),
                     self.rustc(compiler.stage).target(compiler.host)]
            }
264 265
            Source::LibrustcLink { compiler, host } => {
                vec![self.librustc(compiler),
266 267 268 269
                     self.libtest_link(compiler, host)]
            }
            Source::LibtestLink { compiler, host } => {
                vec![self.libtest(compiler), self.libstd_link(compiler, host)]
270
            }
271 272 273
            Source::LibstdLink { compiler, host } => {
                vec![self.libstd(compiler),
                     self.target(host).rustc(compiler.stage)]
274
            }
A
Alex Crichton 已提交
275 276 277 278
            Source::CompilerRt { _dummy } => {
                vec![self.llvm(()).target(&build.config.build)]
            }
            Source::Llvm { _dummy } => Vec::new(),
279 280 281 282

            // Note that all doc targets depend on artifacts from the build
            // architecture, not the target (which is where we're generating
            // docs into).
283
            Source::DocStd { stage } => {
284 285
                let compiler = self.target(&build.config.build).compiler(stage);
                vec![self.libstd(compiler)]
286
            }
287
            Source::DocTest { stage } => {
288 289
                let compiler = self.target(&build.config.build).compiler(stage);
                vec![self.libtest(compiler)]
290
            }
291 292
            Source::DocBook { stage } |
            Source::DocNomicon { stage } |
293
            Source::DocStyle { stage } => {
294
                vec![self.target(&build.config.build).tool_rustbook(stage)]
295
            }
296
            Source::DocErrorIndex { stage } => {
297
                vec![self.target(&build.config.build).tool_error_index(stage)]
298
            }
299
            Source::DocStandalone { stage } => {
300
                vec![self.target(&build.config.build).rustc(stage)]
301
            }
302
            Source::DocRustc { stage } => {
303
                vec![self.doc_test(stage)]
304
            }
305 306
            Source::Doc { stage } => {
                vec![self.doc_book(stage), self.doc_nomicon(stage),
307
                     self.doc_style(stage), self.doc_standalone(stage),
308 309
                     self.doc_std(stage),
                     self.doc_error_index(stage)]
310
            }
311
            Source::Check { stage, compiler: _ } => {
A
Alex Crichton 已提交
312 313
                vec![self.check_linkcheck(stage),
                     self.dist(stage)]
314 315 316 317
            }
            Source::CheckLinkcheck { stage } => {
                vec![self.tool_linkchecker(stage), self.doc(stage)]
            }
318 319 320
            Source::CheckCargoTest { stage } => {
                vec![self.tool_cargotest(stage)]
            }
321 322 323
            Source::CheckTidy { stage } => {
                vec![self.tool_tidy(stage)]
            }
324

325 326 327
            Source::ToolLinkchecker { stage } |
            Source::ToolTidy { stage } |
            Source::ToolCargoTest { stage } => {
328
                vec![self.libstd(self.compiler(stage))]
329
            }
330
            Source::ToolErrorIndex { stage } |
331
            Source::ToolRustbook { stage } => {
332
                vec![self.librustc(self.compiler(stage))]
333
            }
334
            Source::ToolCargoTest { stage } => {
B
Brian Anderson 已提交
335
                vec![self.librustc(self.compiler(stage))]
336
            }
A
Alex Crichton 已提交
337 338 339 340 341 342 343

            Source::DistDocs { stage } => vec![self.doc(stage)],
            Source::DistMingw { _dummy: _ } => Vec::new(),
            Source::DistRustc { stage } => {
                vec![self.rustc(stage)]
            }
            Source::DistStd { compiler } => {
344
                vec![self.libtest(compiler)]
A
Alex Crichton 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358
            }

            Source::Dist { stage } => {
                let mut base = Vec::new();

                for host in build.config.host.iter() {
                    let host = self.target(host);
                    base.push(host.dist_rustc(stage));
                    if host.target.contains("windows-gnu") {
                        base.push(host.dist_mingw(()));
                    }

                    let compiler = self.compiler(stage);
                    for target in build.config.target.iter() {
359 360 361
                        let target = self.target(target);
                        base.push(target.dist_docs(stage));
                        base.push(target.dist_std(compiler));
A
Alex Crichton 已提交
362 363 364 365
                    }
                }
                return base
            }
A
Alex Crichton 已提交
366 367 368
        }
    }
}