Skip to content

Commit

Permalink
rustpkg: Add preliminary imperative API support
Browse files Browse the repository at this point in the history
  • Loading branch information
z0w0 authored and graydon committed Feb 16, 2013
1 parent bd28fa4 commit 7079441
Show file tree
Hide file tree
Showing 5 changed files with 811 additions and 281 deletions.
28 changes: 19 additions & 9 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ pub enum compile_upto {
cu_everything,
}

pub fn compile_upto(sess: Session, cfg: ast::crate_cfg,
input: input, upto: compile_upto,
outputs: Option<output_filenames>)
-> {crate: @ast::crate, tcx: Option<ty::ctxt>} {
// For continuing compilation after a parsed crate has been
// modified
pub fn compile_rest(sess: Session, cfg: ast::crate_cfg,
upto: compile_upto, outputs: Option<output_filenames>,
curr: Option<@ast::crate>)
-> {crate: @ast::crate, tcx: Option<ty::ctxt>} {
let time_passes = sess.time_passes();
let mut crate = time(time_passes, ~"parsing",
|| parse_input(sess, copy cfg, input) );
if upto == cu_parse { return {crate: crate, tcx: None}; }
let mut crate = curr.get();

*sess.building_library = session::building_library(
sess.opts.crate_type, crate, sess.opts.test);
Expand Down Expand Up @@ -322,7 +322,6 @@ pub fn compile_upto(sess: Session, cfg: ast::crate_cfg,

};


time(time_passes, ~"LLVM passes", ||
link::write::run_passes(sess, llmod,
&outputs.obj_filename));
Expand All @@ -342,9 +341,20 @@ pub fn compile_upto(sess: Session, cfg: ast::crate_cfg,
return {crate: crate, tcx: None};
}

pub fn compile_upto(sess: Session, +cfg: ast::crate_cfg,
input: input, upto: compile_upto,
outputs: Option<output_filenames>)
-> {crate: @ast::crate, tcx: Option<ty::ctxt>} {
let time_passes = sess.time_passes();
let mut crate = time(time_passes, ~"parsing",
|| parse_input(sess, copy cfg, input) );
if upto == cu_parse { return {crate: crate, tcx: None}; }

compile_rest(sess, cfg, upto, outputs, Some(crate))
}

pub fn compile_input(sess: Session, +cfg: ast::crate_cfg, input: input,
outdir: &Option<Path>, output: &Option<Path>) {

let upto = if sess.opts.parse_only { cu_parse }
else if sess.opts.no_trans { cu_no_trans }
else { cu_everything };
Expand Down
116 changes: 111 additions & 5 deletions src/librustpkg/api.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,130 @@
// Copyright 2012 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 core::*;
use util::{compile_crate, note};

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

pub struct Listener {
cmd: ~str,
cb: fn~()
}

pub fn run(listeners: ~[Listener]) {
io::println(src_dir().to_str());
io::println(work_dir().to_str());

let cmd = os::args()[1];

for listeners.each |listener| {
if listener.cmd == cmd {
(listener.cb)();
}
}
}

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

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

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

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

/// Create a crate target from a source file
pub fn Crate(file: ~str) -> Crate {
Crate {
file: file,
flags: ~[],
cfgs: ~[]
}
}

pub fn build(_targets: ~[Crate]) {
// TODO: magic
/**
* Get the working directory of the package script.
* Assumes that the package script has been compiled
* in is the working directory.
*/
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.
*/
fn src_dir() -> Path {
os::getcwd()
}

pub fn args() -> ~[~str] {
let mut args = os::args();

args.shift();
args.shift();

args
}

/// Build a set of crates, should be called once
pub fn build(crates: ~[Crate]) -> bool {
let dir = src_dir();
let work_dir = work_dir();
let mut success = true;

for crates.each |&crate| {
let path = &dir.push_rel(&Path(crate.file)).normalize();

note(fmt!("compiling %s", path.to_str()));

success = compile_crate(path, &work_dir, crate.flags, crate.cfgs,
false, false);

if !success { break; }
}

os::set_exit_status(101);

success
}

pub mod util {

// TODO: utilities for working with things like autotools
}
Loading

0 comments on commit 7079441

Please sign in to comment.