1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::{os, result};
12 use std::c_str::ToCStr;
13 use rustc::driver::{driver, session};
14 use rustc::metadata::filesearch;
15 use extra::getopts::groups::getopts;
16 use syntax::ast_util::*;
17 use syntax::codemap::{dummy_sp, spanned};
18 use syntax::ext::base::ExtCtxt;
19 use syntax::{ast, attr, codemap, diagnostic, fold};
20 use syntax::attr::AttrMetaMethods;
21 use rustc::back::link::output_type_exe;
22 use rustc::driver::session::{lib_crate, bin_crate};
24 use package_id::PkgId;
25 use search::find_library_in_search_path;
26 use path_util::target_library_in_workspace;
27 pub use target::{OutputType, Main, Lib, Bench, Test};
29 // It would be nice to have the list of commands in just one place -- for example,
30 // you could update the match in rustpkg.rc but forget to update this list. I think
31 // that should be fixed.
32 static COMMANDS: &'static [&'static str] =
33 &["build", "clean", "do", "info", "install", "list", "prefer", "test", "uninstall",
37 pub type ExitCode = int; // For now
46 fn to_str(&self) -> ~str {
51 pub fn root() -> Path {
52 match filesearch::get_rustpkg_root() {
53 result::Ok(path) => path,
54 result::Err(err) => fail!(err)
58 pub fn is_cmd(cmd: &str) -> bool {
59 COMMANDS.iter().any(|&c| c == cmd)
69 sess: session::Session,
76 fn fold_mod(_ctx: @mut ReadyCtx,
78 fold: @fold::ast_fold) -> ast::_mod {
79 fn strip_main(item: @ast::item) -> @ast::item {
81 attrs: do item.attrs.iter().filter_map |attr| {
82 if "main" != attr.name() {
92 fold::noop_fold_mod(&ast::_mod {
93 items: do m.items.map |item| {
100 fn fold_item(ctx: @mut ReadyCtx,
102 fold: @fold::ast_fold) -> Option<@ast::item> {
103 ctx.path.push(item.ident);
106 let mut had_pkg_do = false;
108 for attr in item.attrs.iter() {
109 if "pkg_do" == attr.name() {
111 match attr.node.value.node {
112 ast::MetaList(_, ref mis) => {
113 for mi in mis.iter() {
115 ast::MetaWord(cmd) => cmds.push(cmd.to_owned()),
120 _ => cmds.push(~"build")
126 ctx.fns.push(ListenerFn {
129 path: /*bad*/ctx.path.clone()
133 let res = fold::noop_fold_item(item, fold);
140 /// Generate/filter main function, add the list of commands, etc.
141 pub fn ready_crate(sess: session::Session,
142 crate: @ast::Crate) -> @ast::Crate {
143 let ctx = @mut ReadyCtx {
146 ext_cx: ExtCtxt::new(sess.parse_sess, sess.opts.cfg.clone()),
150 let precursor = @fold::AstFoldFns {
151 // fold_crate: fold::wrap(|a, b| fold_crate(ctx, a, b)),
152 fold_item: |a, b| fold_item(ctx, a, b),
153 fold_mod: |a, b| fold_mod(ctx, a, b),
154 .. *fold::default_ast_fold()
157 let fold = fold::make_fold(precursor);
159 @fold.fold_crate(crate)
162 // FIXME (#4432): Use workcache to only compile when needed
163 pub fn compile_input(ctxt: &Ctx,
170 what: OutputType) -> bool {
172 let workspace = out_dir.pop().pop();
174 assert!(in_file.components.len() > 1);
175 let input = driver::file_input((*in_file).clone());
176 debug!("compile_input: %s / %?", in_file.to_str(), what);
177 // tjc: by default, use the package ID name as the link name
178 // not sure if we should support anything else
180 let binary = os::args()[0].to_managed();
182 debug!("flags: %s", flags.connect(" "));
183 debug!("cfgs: %s", cfgs.connect(" "));
184 debug!("compile_input's sysroot = %?", ctxt.sysroot_opt);
186 let crate_type = match what {
188 Test | Bench | Main => bin_crate
190 let matches = getopts(debug_flags()
193 // --test compiles both #[test] and #[bench] fns
194 Test | Bench => ~[~"--test"],
198 + cfgs.flat_map(|c| { ~[~"--cfg", (*c).clone()] }),
199 driver::optgroups()).get();
200 let options = @session::options {
201 crate_type: crate_type,
202 optimize: if opt { session::Aggressive } else { session::No },
203 test: what == Test || what == Bench,
204 maybe_sysroot: ctxt.sysroot_opt,
205 addl_lib_search_paths: @mut (~[(*out_dir).clone()]),
206 // output_type should be conditional
207 output_type: output_type_exe, // Use this to get a library? That's weird
208 .. (*driver::build_session_options(binary, &matches, diagnostic::emit)).clone()
211 let addl_lib_search_paths = @mut options.addl_lib_search_paths;
212 // Make sure all the library directories actually exist, since the linker will complain
214 for p in addl_lib_search_paths.iter() {
215 assert!(os::path_is_dir(p));
218 let sess = driver::build_session(options, diagnostic::emit);
220 // Infer dependencies that rustpkg needs to build, by scanning for
221 // `extern mod` directives.
222 let cfg = driver::build_configuration(sess, binary, &input);
223 let mut crate = driver::phase_1_parse_input(sess, cfg.clone(), &input);
224 crate = driver::phase_2_configure_and_expand(sess, cfg, crate);
226 // Not really right. Should search other workspaces too, and the installed
227 // database (which doesn't exist yet)
228 find_and_install_dependencies(ctxt, sess, &workspace, crate,
230 debug!("a dependency: %s", p.to_str());
231 // Pass the directory containing a dependency
232 // as an additional lib search path
233 addl_lib_search_paths.push(p);
236 // Inject the link attributes so we get the right package name and version
237 if attr::find_linkage_metas(crate.attrs).is_empty() {
238 let short_name_to_use = match what {
239 Test => fmt!("%stest", pkg_id.short_name),
240 Bench => fmt!("%sbench", pkg_id.short_name),
241 _ => pkg_id.short_name.clone()
243 debug!("Injecting link name: %s", short_name_to_use);
245 ~[attr::mk_name_value_item_str(@"name", short_name_to_use.to_managed()),
246 attr::mk_name_value_item_str(@"vers", pkg_id.version.to_str().to_managed())];
248 crate = @ast::Crate {
249 attrs: ~[attr::mk_attr(attr::mk_list_item(@"link", link_options))],
254 debug!("calling compile_crate_from_input, out_dir = %s,
255 building_library = %?", out_dir.to_str(), sess.building_library);
256 compile_crate_from_input(&input, out_dir, sess, crate);
260 // Should use workcache to avoid recompiling when not necessary
261 // Should also rename this to something better
262 // If crate_opt is present, then finish compilation. If it's None, then
263 // call compile_upto and return the crate
264 // also, too many arguments
265 pub fn compile_crate_from_input(input: &driver::input,
267 sess: session::Session,
268 crate: @ast::Crate) {
269 debug!("Calling build_output_filenames with %s, building library? %?",
270 build_dir.to_str(), sess.building_library);
273 let outputs = driver::build_output_filenames(input, &Some((*build_dir).clone()), &None,
276 debug!("Outputs are %? and output type = %?", outputs, sess.opts.output_type);
277 debug!("additional libraries:");
278 for lib in sess.opts.addl_lib_search_paths.iter() {
279 debug!("an additional library: %s", lib.to_str());
281 let analysis = driver::phase_3_run_analysis_passes(sess, crate);
282 let translation = driver::phase_4_translate_to_llvm(sess, crate,
285 driver::phase_5_run_llvm_passes(sess, &translation, outputs);
286 if driver::stop_after_phase_5(sess) { return; }
287 driver::phase_6_link_output(sess, &translation, outputs);
291 pub fn exe_suffix() -> ~str { ~".exe" }
293 #[cfg(target_os = "linux")]
294 #[cfg(target_os = "android")]
295 #[cfg(target_os = "freebsd")]
296 #[cfg(target_os = "macos")]
297 pub fn exe_suffix() -> ~str { ~"" }
299 // Called by build_crates
300 // FIXME (#4432): Use workcache to only compile when needed
301 pub fn compile_crate(ctxt: &Ctx, pkg_id: &PkgId,
302 crate: &Path, dir: &Path,
303 flags: &[~str], cfgs: &[~str], opt: bool,
304 what: OutputType) -> bool {
305 debug!("compile_crate: crate=%s, dir=%s", crate.to_str(), dir.to_str());
306 debug!("compile_crate: short_name = %s, flags =...", pkg_id.to_str());
307 for fl in flags.iter() {
308 debug!("+++ %s", *fl);
310 compile_input(ctxt, pkg_id, crate, dir, flags, cfgs, opt, what)
314 /// Collect all `extern mod` directives in `c`, then
315 /// try to install their targets, failing if any target
317 pub fn find_and_install_dependencies(ctxt: &Ctx,
318 sess: session::Session,
324 debug!("In find_and_install_dependencies...");
325 let my_workspace = (*workspace).clone();
327 do c.each_view_item() |vi: &ast::view_item| {
328 debug!("A view item!");
330 // ignore metadata, I guess
331 ast::view_item_extern_mod(lib_ident, _, _) => {
332 match my_ctxt.sysroot_opt {
333 Some(ref x) => debug!("sysroot: %s", x.to_str()),
334 None => debug!("No sysroot given")
336 let lib_name = sess.str_of(lib_ident);
337 match find_library_in_search_path(my_ctxt.sysroot_opt, lib_name) {
338 Some(installed_path) => {
339 debug!("It exists: %s", installed_path.to_str());
343 let pkg_id = PkgId::new(lib_name, &os::getcwd());
344 my_ctxt.install(&my_workspace, &pkg_id);
345 // Also, add an additional search path
346 debug!("let installed_path...")
347 let installed_path = target_library_in_workspace(&pkg_id,
348 &my_workspace).pop();
349 debug!("Great, I installed %s, and it's in %s",
350 lib_name, installed_path.to_str());
351 save(installed_path);
363 pub fn link_exe(_src: &Path, _dest: &Path) -> bool {
364 /* FIXME (#1768): Investigate how to do this on win32
365 Node wraps symlinks by having a .bat,
366 but that won't work with minGW. */
371 #[cfg(target_os = "linux")]
372 #[cfg(target_os = "android")]
373 #[cfg(target_os = "freebsd")]
374 #[cfg(target_os = "macos")]
375 pub fn link_exe(src: &Path, dest: &Path) -> bool {
378 do src.to_c_str().with_ref |src_buf| {
379 do dest.to_c_str().with_ref |dest_buf| {
380 libc::link(src_buf, dest_buf) == 0 as libc::c_int &&
381 libc::chmod(dest_buf, 755) == 0 as libc::c_int
387 pub fn mk_string_lit(s: @str) -> ast::lit {
389 node: ast::lit_str(s),
400 assert!(is_cmd("build"));
401 assert!(is_cmd("clean"));
402 assert!(is_cmd("do"));
403 assert!(is_cmd("info"));
404 assert!(is_cmd("install"));
405 assert!(is_cmd("prefer"));
406 assert!(is_cmd("test"));
407 assert!(is_cmd("uninstall"));
408 assert!(is_cmd("unprefer"));
414 fn debug_flags() -> ~[~str] { ~[] }
415 // static DEBUG_FLAGS: ~[~str] = ~[~"-Z", ~"time-passes"];