-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathbuild.rs
More file actions
165 lines (146 loc) Β· 5.02 KB
/
build.rs
File metadata and controls
165 lines (146 loc) Β· 5.02 KB
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
// Clippy errors in this file should not stop build errors being
// reported elsewhere.
// https://github.com/rust-lang/rust-clippy/issues/9534
#![warn(clippy::all)]
// Has false positives on else if chains that sometimes have the same
// body for readability.
#![allow(clippy::if_same_then_else)]
use std::path::PathBuf;
use std::process::Command;
use rayon::prelude::*;
use version_check as rustc;
struct TreeSitterParser {
name: &'static str,
src_dir: &'static str,
extra_files: Vec<&'static str>,
}
impl TreeSitterParser {
fn build(&self) {
let dir = PathBuf::from(&self.src_dir);
let mut c_files = vec!["parser.c"];
let mut cpp_files = vec![];
for file in &self.extra_files {
if file.ends_with(".c") {
c_files.push(file);
} else {
cpp_files.push(file);
}
}
if !cpp_files.is_empty() {
let mut cpp_build = cc::Build::new();
cpp_build
.include(&dir)
.cpp(true)
.std("c++14")
.flag_if_supported("-Wno-implicit-fallthrough")
.flag_if_supported("-Wno-unused-parameter")
.flag_if_supported("-Wno-ignored-qualifiers")
.link_lib_modifier("+whole-archive");
for file in cpp_files {
cpp_build.file(dir.join(file));
}
cpp_build.compile(&format!("{}-cpp", self.name));
}
let mut build = cc::Build::new();
if cfg!(target_env = "msvc") {
build.flag("/utf-8");
}
build.include(&dir).warnings(false); // ignore unused parameter warnings
for file in c_files {
build.file(dir.join(file));
}
build.link_lib_modifier("+whole-archive");
build.compile(self.name);
}
}
fn main() {
let parsers = vec![
TreeSitterParser {
name: "tree-sitter-commonlisp",
src_dir: "vendored_parsers/tree-sitter-commonlisp-src",
extra_files: vec![],
},
TreeSitterParser {
name: "tree-sitter-elvish",
src_dir: "vendored_parsers/tree-sitter-elvish-src",
extra_files: vec![],
},
TreeSitterParser {
name: "tree-sitter-hack",
src_dir: "vendored_parsers/tree-sitter-hack-src",
extra_files: vec!["scanner.cc"],
},
TreeSitterParser {
name: "tree-sitter-hare",
src_dir: "vendored_parsers/tree-sitter-hare-src",
extra_files: vec![],
},
TreeSitterParser {
name: "tree-sitter-janet-simple",
src_dir: "vendored_parsers/tree-sitter-janet-simple-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-kotlin",
src_dir: "vendored_parsers/tree-sitter-kotlin-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-latex",
src_dir: "vendored_parsers/tree-sitter-latex-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-scss",
src_dir: "vendored_parsers/tree-sitter-scss-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-smali",
src_dir: "vendored_parsers/tree-sitter-smali-src",
extra_files: vec!["scanner.c"],
},
TreeSitterParser {
name: "tree-sitter-vhdl",
src_dir: "vendored_parsers/tree-sitter-vhdl-src",
extra_files: vec![],
},
];
// Only rerun if relevant files in the vendored_parsers/ directory change.
for parser in &parsers {
println!("cargo:rerun-if-changed={}", parser.src_dir);
}
parsers.par_iter().for_each(|p| p.build());
commit_info();
if let Some((version, _, _)) = rustc::triple() {
println!("cargo:rustc-env=DFT_RUSTC_VERSION={}", version);
}
// Use 64-KiB pages with jemalloc. This solves "<jemalloc>:
// Unsupported system page size" errors, and performs the same as
// jemalloc's default settings.
//
// Note that difftastic does not use jemalloc on all operating
// systems, but it's harmless to set this unconditionally.
println!("cargo:rustc-env=JEMALLOC_SYS_WITH_LG_PAGE=16");
}
fn commit_info() {
if !PathBuf::from(".git").exists() {
return;
}
let output = match Command::new("git")
.arg("log")
.arg("-1")
.arg("--date=short")
.arg("--format=%H %h %cd")
.output()
{
Ok(output) if output.status.success() => output,
_ => return,
};
let stdout = String::from_utf8(output.stdout).unwrap();
let mut parts = stdout.split_whitespace();
let mut next = || parts.next().unwrap();
let _commit_hash = next();
println!("cargo:rustc-env=DFT_COMMIT_SHORT_HASH={}", next());
println!("cargo:rustc-env=DFT_COMMIT_DATE={}", next())
}