Skip to content

Commit b499c91

Browse files
committed
Don't copy sources in OUT_DIR anymore (fixes #218)
This messes things up on Windows. :(
1 parent 6a1a918 commit b499c91

File tree

1 file changed

+23
-48
lines changed

1 file changed

+23
-48
lines changed

build.rs

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,14 @@ fn main() {
4343
env::set_var("MOZCONFIG", "");
4444

4545
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
46-
let src_dir = out_dir.join("mozjs");
4746
let build_dir = out_dir.join("build");
4847

4948
// Used by rust-mozjs downstream, don't remove.
5049
println!("cargo:outdir={}", build_dir.display());
5150

52-
copy_sources("mozjs".as_ref(), &src_dir);
53-
5451
fs::create_dir_all(&build_dir).expect("could not create build dir");
5552

56-
build_jsapi(&src_dir, &build_dir);
53+
build_jsapi(&build_dir);
5754
build_jsglue(&build_dir);
5855
build_jsapi_bindings(&build_dir);
5956

@@ -62,7 +59,11 @@ fn main() {
6259
}
6360

6461
for entry in WalkDir::new("mozjs") {
65-
println!("cargo:rerun-if-changed={}", entry.path().display());
62+
let entry = entry.unwrap();
63+
let path = entry.path();
64+
if !ignore(path) {
65+
println!("cargo:rerun-if-changed={}", path.display());
66+
}
6667
}
6768

6869
for file in EXTRA_FILES {
@@ -125,7 +126,7 @@ fn cc_flags() -> Vec<&'static str> {
125126
result
126127
}
127128

128-
fn build_jsapi(src_dir: &Path, build_dir: &Path) {
129+
fn build_jsapi(build_dir: &Path) {
129130
let target = env::var("TARGET").unwrap();
130131
let mut make = find_make();
131132

@@ -167,7 +168,7 @@ fn build_jsapi(src_dir: &Path, build_dir: &Path) {
167168
.args(&["-R", "-f"])
168169
.arg(cargo_manifest_dir.join("makefile.cargo"))
169170
.current_dir(&build_dir)
170-
.env("SRC_DIR", &src_dir)
171+
.env("SRC_DIR", &cargo_manifest_dir.join("mozjs"))
171172
.env("NO_RUST_PANIC_HOOK", "1")
172173
.status()
173174
.expect("Failed to run `make`");
@@ -402,45 +403,19 @@ const MODULE_RAW_LINES: &'static [(&'static str, &'static str)] = &[
402403
("root::JS", "pub type Rooted<T> = ::jsgc::Rooted<T>;"),
403404
];
404405

405-
fn copy_sources(source: &Path, target: &Path) {
406-
for entry in WalkDir::new(source) {
407-
let entry = entry.expect("could not walk source tree");
408-
let relative_path = entry.path().strip_prefix(&source).unwrap();
409-
let target_path = target.join(relative_path);
410-
411-
if entry.path().is_dir() {
412-
if !target_path.exists() {
413-
fs::create_dir(target_path).expect("could not create directory");
414-
}
415-
} else {
416-
copy_file(entry.path(), &target_path)
417-
}
418-
}
419-
}
420-
421-
fn copy_file(source: &Path, target: &Path) {
422-
if !target_is_up_to_date(&source, &target) {
423-
fs::copy(&source, &target).expect("could not copy file");
424-
}
425-
}
426-
427-
fn target_is_up_to_date(source: &Path, target: &Path) -> bool {
428-
if !target.exists() {
429-
return false;
430-
}
431-
432-
let source_metadata = source.metadata().expect("could not read source metadata");
433-
let target_metadata = target.metadata().expect("could not read target metadata");
434-
435-
let source_modified = match source_metadata.modified() {
436-
Ok(modified) => modified,
437-
Err(_) => return false,
438-
};
439-
440-
let target_modified = match target_metadata.modified() {
441-
Ok(modified) => modified,
442-
Err(_) => return false,
443-
};
444-
445-
source_modified < target_modified
406+
/// Rerun this build script if files under mozjs/ changed, unless this returns true.
407+
/// Keep this in sync with .gitignore
408+
fn ignore(path: &Path) -> bool {
409+
let ignored_extensions = ["pyc", "so", "dll", "dylib"];
410+
let ignored_trailing_paths = [["psutil", "build"], ["psutil", "tmp"]];
411+
412+
path.extension().map_or(false, |extension| {
413+
ignored_extensions.iter().any(|&ignored| extension == ignored)
414+
}) ||
415+
ignored_trailing_paths.iter().any(|trailing| {
416+
let mut components = path.components().rev();
417+
trailing.iter().rev().all(|&ignored| {
418+
components.next().map_or(false, |component| component.as_os_str() == ignored)
419+
})
420+
})
446421
}

0 commit comments

Comments
 (0)