This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbuild.rs
167 lines (150 loc) · 4.91 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// extern crate bindgen;
#[cfg(target_env = "msvc")]
extern crate cc;
extern crate pkg_config;
extern crate num_cpus;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
// Automatically write bindings to libsass
//#[allow(dead_code)]
//fn write_bindings() {
// let bindings = bindgen::Builder::default()
// .header("libsass/include/sass.h")
// .clang_arg("-Ilibsass/include")
// // To avoid a test failing
// .blacklist_type("max_align_t")
// // we do static linking so it should be fine
// // https://github.com/rust-lang/rust/issues/36927
// .rustified_enum(".*")
// .generate()
// .expect("Unable to generate bindings");
//
// // Write the bindings to the $OUT_DIR/bindings.rs file.
// let out_path = PathBuf::from("src");
// bindings
// .write_to_file(out_path.join("bindings.rs"))
// .expect("Couldn't write bindings!");
//}
macro_rules! t {
($e:expr) => (match $e {
Ok(n) => n,
Err(e) => panic!("\n{} failed with {}\n", stringify!($e), e),
})
}
fn cp_r(dir: &Path, dest: &Path) {
for entry in t!(fs::read_dir(dir)) {
let entry = t!(entry);
let path = entry.path();
let dst = dest.join(path.file_name().unwrap());
if t!(fs::metadata(&path)).is_file() {
t!(fs::copy(path, dst));
} else {
t!(fs::create_dir_all(&dst));
cp_r(&path, &dst);
}
}
}
fn get_libsass_folder() -> PathBuf {
env::current_dir().unwrap().join("libsass")
}
// linux/unix
#[cfg(not(target_env = "msvc"))]
fn compile() {
let target = env::var("TARGET").expect("TARGET not found");
let src = get_libsass_folder();
let dest = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let build = dest.join("build");
t!(fs::create_dir_all(&build));
cp_r(&src, &build);
let is_bsd = target.contains("dragonfly")
|| target.contains("freebsd")
|| target.contains("netbsd")
|| target.contains("openbsd");
let r = Command::new(if is_bsd { "gmake" } else { "make" })
.current_dir(&build)
.args(&["--jobs", &num_cpus::get().to_string()])
.output()
.expect("error running make");
if !r.status.success() {
let err = String::from_utf8_lossy(&r.stderr);
let out = String::from_utf8_lossy(&r.stdout);
panic!("Build error:\nSTDERR:{}\nSTDOUT:{}", err, out);
}
println!(
"cargo:rustc-link-search=native={}",
build.join("lib").display()
);
println!("cargo:rustc-link-lib=static=sass");
println!(
"cargo:rustc-link-lib=dylib={}",
if target.contains("darwin") || is_bsd {
"c++"
} else {
"stdc++"
}
);
}
// windows
#[cfg(target_env = "msvc")]
fn compile() {
let src = get_libsass_folder();
let target = env::var("TARGET").expect("TARGET not found in environment");
let msvc_platform = if target.contains("x86_64") {
"Win64"
} else {
"Win32"
};
let dest = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let build = dest.join("build");
t!(fs::create_dir_all(&build));
cp_r(&src, &build);
// Find an instance of devenv.exe from Visual Studio IDE in order to upgrade
// libsass.sln to the current available IDE. Do nothing if no devenv.exe found
let d = cc::windows_registry::find(target.as_str(), "devenv.exe");
if let Some(mut d) = d {
let d = d
.args(&["/upgrade", "win\\libsass.sln"])
.current_dir(&build)
.output()
.expect("error running devenv");
if !d.status.success() {
let err = String::from_utf8_lossy(&d.stderr);
let out = String::from_utf8_lossy(&d.stdout);
println!("Upgrade error:\nSTDERR:{}\nSTDOUT:{}", err, out);
}
}
let r = cc::windows_registry::find(target.as_str(), "msbuild.exe")
.expect("could not find msbuild")
.args(&[
"win\\libsass.sln",
"/p:LIBSASS_STATIC_LIB=1",
"/p:Configuration=Release",
"/p:WholeProgramOptimization=false",
format!("/p:Platform={}", msvc_platform).as_str(),
])
.current_dir(&build)
.output()
.expect("error running msbuild");
if !r.status.success() {
let err = String::from_utf8_lossy(&r.stderr);
let out = String::from_utf8_lossy(&r.stdout);
panic!("Build error:\nSTDERR:{}\nSTDOUT:{}", err, out);
}
println!(
"cargo:rustc-link-search=native={}",
build.join("win").join("bin").display()
);
println!("cargo:rustc-link-lib=static=libsass");
}
fn main() {
// Uncomment the line below to generate bindings. Doesn't work on CI as it
// requires additional tooling
// write_bindings();
// Is it already built?
if let Ok(_) = pkg_config::find_library("sass") {
return;
}
compile();
}