forked from mozilla/rust-code-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
172 lines (158 loc) · 4.84 KB
/
Copy pathbuild.rs
File metadata and controls
172 lines (158 loc) · 4.84 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
166
167
168
169
170
171
172
extern crate cc;
extern crate phf_codegen;
use std::io::{BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use std::{env, fs};
const TREE_SITTER: &str = "tree-sitter-";
fn get_opt_level() -> u32 {
env::var("OPT_LEVEL").unwrap().parse::<u32>().unwrap()
}
fn get_debug() -> bool {
env::var("DEBUG").unwrap() == "true"
}
fn get_cwd() -> &'static str {
if is_enums() {
".."
} else {
"."
}
}
fn is_enums() -> bool {
let path = env::current_dir().unwrap();
path.ends_with("enums")
}
fn mk_predef(data_name: &str, set_name: &str) {
let mut set = phf_codegen::Set::new();
let mut file = fs::File::open(PathBuf::from(format!("./data/{}.txt", data_name))).unwrap();
let mut data = Vec::new();
file.read_to_end(&mut data).unwrap();
for tok in data.split(|c| *c == b'\n') {
let tok = std::str::from_utf8(tok).unwrap().trim();
if !tok.is_empty() {
set.entry(tok);
}
}
let path = Path::new(&env::var("OUT_DIR").unwrap()).join(format!("gen_{}.rs", data_name));
let mut file = BufWriter::new(fs::File::create(&path).unwrap());
writeln!(&mut file, "#[allow(clippy::unreadable_literal)]").unwrap();
writeln!(
&mut file,
"static {}: phf::Set<&'static str> =\n{};\n",
set_name,
set.build()
)
.unwrap();
}
fn collect_tree_sitter_dirs(ignore: Vec<String>) -> Vec<String> {
let mut dirs = Vec::new();
for entry in fs::read_dir(get_cwd()).unwrap() {
if let Ok(entry) = entry {
let path = entry.path();
let dir = path.file_name().unwrap().to_str().unwrap().to_string();
if dir.starts_with(TREE_SITTER) && !ignore.contains(&dir) {
dirs.push(dir);
}
}
}
dirs
}
fn collect_src_files(dir: &str) -> (Vec<String>, Vec<String>) {
eprintln!("Collect files for {}", dir);
let mut c_files = Vec::new();
let mut cpp_files = Vec::new();
let path = PathBuf::from(get_cwd()).join(&dir).join("src");
for entry in fs::read_dir(path).unwrap() {
if let Ok(entry) = entry {
let path = entry.path();
if path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.starts_with("binding")
{
continue;
}
if let Some(ext) = path.extension() {
if ext == "c" {
c_files.push(path.to_str().unwrap().to_string());
} else if ext == "cc" || ext == "cpp" || ext == "cxx" {
cpp_files.push(path.to_str().unwrap().to_string());
}
}
}
}
(c_files, cpp_files)
}
fn build_c(files: Vec<String>, language: &str) {
let mut build = cc::Build::new();
for file in files {
build
.file(&file)
.include(PathBuf::from(file).parent().unwrap())
.pic(true)
.opt_level(get_opt_level())
.debug(get_debug())
.warnings(false)
.flag_if_supported("-std=c99");
}
build.compile(&format!("tree-sitter-{}-c", language));
}
fn build_cpp(files: Vec<String>, language: &str) {
let mut build = cc::Build::new();
for file in files {
build
.file(&file)
.include(PathBuf::from(file).parent().unwrap())
.pic(true)
.opt_level(get_opt_level())
.debug(get_debug())
.warnings(false)
.cpp(true);
}
build.compile(&format!("tree-sitter-{}-cpp", language));
}
fn build_dir(dir: &str, language: &str) {
println!("Build language {}", language);
if PathBuf::from(get_cwd())
.join(dir)
.read_dir()
.unwrap()
.next()
.is_none()
{
eprintln!(
"The directory {} is empty, did you use 'git clone --recursive'?",
dir
);
eprintln!("You can fix in using 'git submodule init && git submodule update --recursive'.");
std::process::exit(1);
}
let (c, cpp) = collect_src_files(&dir);
if !c.is_empty() {
build_c(c, &language);
}
if !cpp.is_empty() {
build_cpp(cpp, &language);
}
}
fn main() {
if !is_enums() {
mk_predef("c_macros", "PREDEFINED_MACROS");
mk_predef("c_specials", "SPECIALS");
}
let ignore = vec![
"tree-sitter-preproc".to_string(),
"tree-sitter-ccomment".to_string(),
"tree-sitter-mozcpp".to_string(),
"tree-sitter-mozjs".to_string(),
"tree-sitter-typescript".to_string(),
];
let dirs = collect_tree_sitter_dirs(ignore);
for dir in dirs {
let language = &dir[TREE_SITTER.len()..];
build_dir(&dir, &language);
}
build_dir("tree-sitter-typescript/tsx", "tsx");
build_dir("tree-sitter-typescript/typescript", "typescript");
}