Skip to content

Commit e0f72e2

Browse files
graydoncatamorphism
authored andcommitted
checkpoint
1 parent d6f455e commit e0f72e2

File tree

2 files changed

+200
-68
lines changed

2 files changed

+200
-68
lines changed

src/librustpkg/rustpkg.rc

Lines changed: 174 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,13 @@ impl Ctx {
281281

282282
match cmd {
283283
~"build" => {
284-
self.build(&os::getcwd(), true, false, false);
284+
if args.len() < 1 {
285+
return usage::build();
286+
}
287+
let pkgid = PkgId::new(args[0]);
288+
let mut src = PkgSrc::new(&Path("."), &pkgid);
289+
src.find_crates();
290+
src.build(&Path("."));
285291
}
286292
~"clean" => {
287293
self.clean();
@@ -927,7 +933,7 @@ pub fn main() {
927933

928934
/// A crate is a unit of Rust code to be compiled into a binary or library
929935
pub struct Crate {
930-
file: ~str,
936+
file: Path,
931937
flags: ~[~str],
932938
cfgs: ~[~str]
933939
}
@@ -959,44 +965,44 @@ pub fn run(listeners: ~[Listener]) {
959965
}
960966

961967
pub impl Crate {
962-
pub fn flag(&self, flag: ~str) -> Crate {
968+
969+
static fn new(p: &Path) -> Crate {
970+
Crate {
971+
file: copy *p,
972+
flags: ~[],
973+
cfgs: ~[]
974+
}
975+
}
976+
977+
fn flag(&self, flag: ~str) -> Crate {
963978
Crate {
964979
flags: vec::append(copy self.flags, ~[flag]),
965980
.. copy *self
966981
}
967982
}
968983

969-
pub fn flags(&self, flags: ~[~str]) -> Crate {
984+
fn flags(&self, flags: ~[~str]) -> Crate {
970985
Crate {
971986
flags: vec::append(copy self.flags, flags),
972987
.. copy *self
973988
}
974989
}
975990

976-
pub fn cfg(&self, cfg: ~str) -> Crate {
991+
fn cfg(&self, cfg: ~str) -> Crate {
977992
Crate {
978993
cfgs: vec::append(copy self.cfgs, ~[cfg]),
979994
.. copy *self
980995
}
981996
}
982997

983-
pub fn cfgs(&self, cfgs: ~[~str]) -> Crate {
998+
fn cfgs(&self, cfgs: ~[~str]) -> Crate {
984999
Crate {
9851000
cfgs: vec::append(copy self.cfgs, cfgs),
9861001
.. copy *self
9871002
}
9881003
}
9891004
}
9901005

991-
/// Create a crate target from a source file
992-
pub fn Crate(file: ~str) -> Crate {
993-
Crate {
994-
file: file,
995-
flags: ~[],
996-
cfgs: ~[]
997-
}
998-
}
999-
10001006
/**
10011007
* Get the working directory of the package script.
10021008
* Assumes that the package script has been compiled
@@ -1026,7 +1032,7 @@ pub fn build(crates: ~[Crate]) -> bool {
10261032
let test = args[3] == ~"true";
10271033

10281034
for crates.each |&crate| {
1029-
let path = &dir.push_rel(&Path(crate.file)).normalize();
1035+
let path = &dir.push_rel(&crate.file).normalize();
10301036

10311037
util::note(fmt!("compiling %s", path.to_str()));
10321038

@@ -1043,3 +1049,155 @@ pub fn build(crates: ~[Crate]) -> bool {
10431049

10441050
success
10451051
}
1052+
1053+
1054+
// Path-fragment identifier of a package such as
1055+
// 'github.com/graydon/test'; must be a relative
1056+
// path with >=1 component.
1057+
struct PkgId {
1058+
path: Path
1059+
}
1060+
1061+
condition! {
1062+
bad_pkg_id: (::core::path::Path, ~str) -> ::PkgId;
1063+
}
1064+
1065+
impl PkgId {
1066+
static fn new(s: &str) -> PkgId {
1067+
use bad_pkg_id::cond;
1068+
1069+
let p = Path(s);
1070+
if p.is_absolute {
1071+
return cond.raise((p, ~"absolute pkgid"));
1072+
}
1073+
if p.components.len() < 1 {
1074+
return cond.raise((p, ~"0-length pkgid"));
1075+
}
1076+
PkgId {
1077+
path: p
1078+
}
1079+
}
1080+
}
1081+
1082+
1083+
// An enumeration of the unpacked source of a package workspace.
1084+
// This contains a list of files found in the source workspace.
1085+
pub struct PkgSrc {
1086+
root: Path,
1087+
id: PkgId,
1088+
libs: ~[Crate],
1089+
mains: ~[Crate],
1090+
tests: ~[Crate],
1091+
benchs: ~[Crate],
1092+
}
1093+
1094+
condition! {
1095+
bad_path: (::core::path::Path, ~str) -> ::core::path::Path;
1096+
}
1097+
1098+
condition! {
1099+
build_err: (~str) -> ();
1100+
}
1101+
1102+
impl PkgSrc {
1103+
1104+
1105+
static fn new(fs_root: &Path, id: &PkgId) -> PkgSrc {
1106+
PkgSrc {
1107+
root: copy *fs_root,
1108+
id: copy *id,
1109+
libs: ~[],
1110+
mains: ~[],
1111+
tests: ~[],
1112+
benchs: ~[]
1113+
}
1114+
}
1115+
1116+
1117+
fn check_dir(&self) -> Path {
1118+
use bad_path::cond;
1119+
1120+
let dir = self.root.push_rel(&self.id.path).normalize();
1121+
1122+
if ! os::path_exists(&dir) {
1123+
return cond.raise((dir, ~"missing package dir"));
1124+
}
1125+
1126+
if ! os::path_is_dir(&dir) {
1127+
return cond.raise((dir, ~"missing package dir"));
1128+
}
1129+
1130+
dir
1131+
}
1132+
1133+
1134+
fn has_pkg_file(&self) -> bool {
1135+
let dir = self.check_dir();
1136+
dir.push("pkg.rs").exists()
1137+
}
1138+
1139+
1140+
static fn push_crate(cs: &mut ~[Crate], prefix: uint, p: &Path) {
1141+
assert p.components.len() > prefix;
1142+
let mut sub = Path("");
1143+
for vec::slice(p.components, prefix,
1144+
p.components.len()).each |c| {
1145+
sub = sub.push(*c);
1146+
}
1147+
debug!("found crate %s", sub.to_str());
1148+
cs.push(Crate::new(&sub));
1149+
}
1150+
1151+
fn find_crates(&mut self) {
1152+
use PkgSrc::push_crate;
1153+
assert ! self.has_pkg_file();
1154+
let dir = self.check_dir();
1155+
let prefix = dir.components.len();
1156+
for os::walk_dir(&dir) |pth| {
1157+
match pth.filename() {
1158+
Some(~"lib.rs") => push_crate(&mut self.libs,
1159+
prefix, pth),
1160+
Some(~"main.rs") => push_crate(&mut self.mains,
1161+
prefix, pth),
1162+
Some(~"test.rs") => push_crate(&mut self.tests,
1163+
prefix, pth),
1164+
Some(~"bench.rs") => push_crate(&mut self.benchs,
1165+
prefix, pth),
1166+
_ => ()
1167+
}
1168+
}
1169+
debug!("found %u libs, %u mains, %u tests, %u benchs",
1170+
self.libs.len(),
1171+
self.mains.len(),
1172+
self.tests.len(),
1173+
self.benchs.len())
1174+
}
1175+
1176+
1177+
static fn build_crates(dst_dir: &Path,
1178+
src_dir: &Path,
1179+
crates: &[Crate],
1180+
test: bool) {
1181+
1182+
for crates.each |&crate| {
1183+
let path = &src_dir.push_rel(&crate.file).normalize();
1184+
util::note(fmt!("compiling %s", path.to_str()));
1185+
if ! util::compile_crate(None, path,
1186+
dst_dir,
1187+
crate.flags,
1188+
crate.cfgs,
1189+
false, test) {
1190+
build_err::cond.raise(fmt!("build failure on %s",
1191+
path.to_str()));
1192+
}
1193+
}
1194+
}
1195+
1196+
fn build(&self, dst_dir: &Path) {
1197+
let dir = self.check_dir();
1198+
PkgSrc::build_crates(dst_dir, &dir, self.libs, false);
1199+
PkgSrc::build_crates(dst_dir, &dir, self.mains, false);
1200+
PkgSrc::build_crates(dst_dir, &dir, self.tests, true);
1201+
PkgSrc::build_crates(dst_dir, &dir, self.benchs, true);
1202+
}
1203+
}

src/librustpkg/util.rs

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,23 @@ pub fn remove_pkg(pkg: &Package) -> bool {
597597
true
598598
}
599599

600-
pub fn compile_input(sysroot: Option<Path>, input: driver::input, dir: &Path,
601-
flags: ~[~str], cfgs: ~[~str], opt: bool, test: bool) -> bool {
602-
let lib_dir = dir.push(~"lib");
603-
let bin_dir = dir.push(~"bin");
604-
let test_dir = dir.push(~"test");
600+
pub fn compile_input(sysroot: Option<Path>,
601+
in_file: &Path,
602+
out_dir: &Path,
603+
flags: ~[~str],
604+
cfgs: ~[~str],
605+
opt: bool,
606+
test: bool) -> bool {
607+
608+
assert in_file.components.len() > 1;
609+
let input = driver::file_input(copy *in_file);
610+
let short_name = in_file.pop().filename().get();
611+
let out_file = out_dir.push(os::dll_filename(short_name));
612+
613+
debug!("compiling %s into %s",
614+
in_file.to_str(),
615+
out_file.to_str());
616+
605617
let binary = os::args()[0];
606618
let matches = getopts(flags, driver::optgroups()).get();
607619
let options = @session::options {
@@ -630,15 +642,12 @@ pub fn compile_input(sysroot: Option<Path>, input: driver::input, dir: &Path,
630642

631643
let mut name = None;
632644
let mut vers = None;
633-
let mut uuid = None;
634645
let mut crate_type = None;
635646

636647
fn load_link_attr(mis: ~[@ast::meta_item]) -> (Option<~str>,
637-
Option<~str>,
638648
Option<~str>) {
639649
let mut name = None;
640650
let mut vers = None;
641-
let mut uuid = None;
642651

643652
for mis.each |a| {
644653
match a.node {
@@ -647,15 +656,14 @@ pub fn compile_input(sysroot: Option<Path>, input: driver::input, dir: &Path,
647656
match *v {
648657
~"name" => name = Some(*s),
649658
~"vers" => vers = Some(*s),
650-
~"uuid" => uuid = Some(*s),
651659
_ => { }
652660
}
653661
}
654662
_ => {}
655663
}
656664
}
657665

658-
(name, vers, uuid)
666+
(name, vers)
659667
}
660668

661669
for crate.node.attrs.each |a| {
@@ -670,11 +678,10 @@ pub fn compile_input(sysroot: Option<Path>, input: driver::input, dir: &Path,
670678
ast::meta_list(v, mis) => {
671679
match *v {
672680
~"link" => {
673-
let (n, v, u) = load_link_attr(mis);
681+
let (n, v) = load_link_attr(mis);
674682

675683
name = n;
676684
vers = v;
677-
uuid = u;
678685
}
679686
_ => {}
680687
}
@@ -683,16 +690,6 @@ pub fn compile_input(sysroot: Option<Path>, input: driver::input, dir: &Path,
683690
}
684691
}
685692

686-
if name.is_none() || vers.is_none() || uuid.is_none() {
687-
error(~"link attr without (name, vers, uuid) values");
688-
689-
return false;
690-
}
691-
692-
let name = name.get();
693-
let vers = vers.get();
694-
let uuid = uuid.get();
695-
696693
let is_bin = match crate_type {
697694
Some(crate_type) => {
698695
match crate_type {
@@ -712,29 +709,14 @@ pub fn compile_input(sysroot: Option<Path>, input: driver::input, dir: &Path,
712709
}
713710
};
714711

715-
if test {
716-
need_dir(&test_dir);
717-
718-
outputs = driver::build_output_filenames(input, &Some(test_dir),
719-
&None, sess)
720-
}
721-
else if is_bin {
722-
need_dir(&bin_dir);
723-
724-
let path = bin_dir.push(fmt!("%s-%s-%s%s", name,
725-
hash(name + uuid + vers),
726-
vers, exe_suffix()));
727-
outputs = driver::build_output_filenames(input, &None, &Some(path),
728-
sess);
729-
} else {
730-
need_dir(&lib_dir);
731-
732-
outputs = driver::build_output_filenames(input, &Some(lib_dir),
733-
&None, sess)
734-
}
712+
outputs = driver::build_output_filenames(input,
713+
&Some(copy *out_dir),
714+
&Some(out_file),
715+
sess);
735716

736717
driver::compile_rest(sess, cfg, driver::cu_everything,
737-
Some(outputs), Some(crate));
718+
Some(outputs),
719+
Some(crate));
738720

739721
true
740722
}
@@ -753,15 +735,7 @@ pub fn exe_suffix() -> ~str { ~"" }
753735
pub fn compile_crate(sysroot: Option<Path>, crate: &Path, dir: &Path,
754736
flags: ~[~str], cfgs: ~[~str], opt: bool,
755737
test: bool) -> bool {
756-
compile_input(sysroot, driver::file_input(*crate), dir, flags, cfgs,
757-
opt, test)
758-
}
759-
760-
pub fn compile_str(sysroot: Option<Path>, code: ~str, dir: &Path,
761-
flags: ~[~str], cfgs: ~[~str], opt: bool,
762-
test: bool) -> bool {
763-
compile_input(sysroot, driver::str_input(code), dir, flags, cfgs,
764-
opt, test)
738+
compile_input(sysroot, crate, dir, flags, cfgs, opt, test)
765739
}
766740

767741
#[cfg(windows)]

0 commit comments

Comments
 (0)