Skip to content

Commit 7e7cd54

Browse files
authored
Merge pull request github#12546 from hmac/extractor-shared-library
Introduce a shared extractor library
2 parents 4964f86 + 6b2e884 commit 7e7cd54

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+253
-2544
lines changed

.github/workflows/ruby-build.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ jobs:
5050
echo "/usr/local/opt/gnu-tar/libexec/gnubin" >> $GITHUB_PATH
5151
- name: Install cargo-cross
5252
if: runner.os == 'Linux'
53-
run: cargo install cross --version 0.2.1
53+
run: cargo install cross --version 0.2.5
5454
- uses: ./.github/actions/os-version
5555
id: os_version
5656
- name: Cache entire extractor
@@ -85,7 +85,12 @@ jobs:
8585
# This ensures we don't depend on glibc > 2.17.
8686
- name: Release build (linux)
8787
if: steps.cache-extractor.outputs.cache-hit != 'true' && runner.os == 'Linux'
88-
run: cd extractor && cross build --release
88+
run: |
89+
cd extractor
90+
cross build --release
91+
mv target/x86_64-unknown-linux-gnu/release/extractor target/release/
92+
mv target/x86_64-unknown-linux-gnu/release/autobuilder target/release/
93+
mv target/x86_64-unknown-linux-gnu/release/generator target/release/
8994
- name: Release build (windows and macos)
9095
if: steps.cache-extractor.outputs.cache-hit != 'true' && runner.os != 'Linux'
9196
run: cd extractor && cargo build --release

ql/Cargo.lock

Lines changed: 83 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ql/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
[workspace]
22
members = [
3-
"autobuilder",
43
"extractor",
5-
"generator",
6-
"node-types",
74
"buramu",
85
]

ql/autobuilder/Cargo.toml

Lines changed: 0 additions & 9 deletions
This file was deleted.

ql/extractor/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
flate2 = "1.0"
11-
node-types = { path = "../node-types" }
1210
tree-sitter = ">= 0.20, < 0.21"
1311
tree-sitter-ql = { git = "https://github.com/tree-sitter/tree-sitter-ql.git", rev = "d08db734f8dc52f6bc04db53a966603122bc6985"}
1412
tree-sitter-ql-dbscheme = { git = "https://github.com/erik-krogh/tree-sitter-ql-dbscheme.git", rev = "63e1344353f63931e88bfbc2faa2e78e1421b213"}
@@ -19,5 +17,5 @@ clap = "2.33"
1917
tracing = "0.1"
2018
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
2119
rayon = "1.7.0"
22-
num_cpus = "1.14.0"
2320
regex = "1.7.2"
21+
codeql-extractor = { path = "../../shared/tree-sitter-extractor" }
File renamed without changes.

ql/extractor/src/main.rs renamed to ql/extractor/src/bin/extractor.rs

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,9 @@
1-
mod extractor;
2-
mod trap;
3-
4-
extern crate num_cpus;
5-
61
use rayon::prelude::*;
72
use std::fs;
83
use std::io::BufRead;
94
use std::path::{Path, PathBuf};
105

11-
/**
12-
* Gets the number of threads the extractor should use, by reading the
13-
* CODEQL_THREADS environment variable and using it as described in the
14-
* extractor spec:
15-
*
16-
* "If the number is positive, it indicates the number of threads that should
17-
* be used. If the number is negative or zero, it should be added to the number
18-
* of cores available on the machine to determine how many threads to use
19-
* (minimum of 1). If unspecified, should be considered as set to -1."
20-
*/
21-
fn num_codeql_threads() -> usize {
22-
let threads_str = std::env::var("CODEQL_THREADS").unwrap_or_else(|_| "-1".to_owned());
23-
match threads_str.parse::<i32>() {
24-
Ok(num) if num <= 0 => {
25-
let reduction = -num as usize;
26-
std::cmp::max(1, num_cpus::get() - reduction)
27-
}
28-
Ok(num) => num as usize,
29-
30-
Err(_) => {
31-
tracing::error!(
32-
"Unable to parse CODEQL_THREADS value '{}'; defaulting to 1 thread.",
33-
&threads_str
34-
);
35-
1
36-
}
37-
}
38-
}
6+
use codeql_extractor::{diagnostics, extractor, node_types, trap};
397

408
fn main() -> std::io::Result<()> {
419
tracing_subscriber::fmt()
@@ -45,7 +13,23 @@ fn main() -> std::io::Result<()> {
4513
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
4614
.init();
4715

48-
let num_threads = num_codeql_threads();
16+
let diagnostics = diagnostics::DiagnosticLoggers::new("ql");
17+
let mut main_thread_logger = diagnostics.logger();
18+
let num_threads = match codeql_extractor::options::num_threads() {
19+
Ok(num) => num,
20+
Err(e) => {
21+
main_thread_logger.write(
22+
main_thread_logger
23+
.new_entry("configuration-error", "Configuration error")
24+
.message(
25+
"{}; defaulting to 1 thread.",
26+
&[diagnostics::MessageArg::Code(&e)],
27+
)
28+
.severity(diagnostics::Severity::Warning),
29+
);
30+
1
31+
}
32+
};
4933
tracing::info!(
5034
"Using {} {}",
5135
num_threads,
@@ -55,6 +39,20 @@ fn main() -> std::io::Result<()> {
5539
"threads"
5640
}
5741
);
42+
let trap_compression = match trap::Compression::from_env("CODEQL_QL_TRAP_COMPRESSION") {
43+
Ok(x) => x,
44+
Err(e) => {
45+
main_thread_logger.write(
46+
main_thread_logger
47+
.new_entry("configuration-error", "Configuration error")
48+
.message("{}; using gzip.", &[diagnostics::MessageArg::Code(&e)])
49+
.severity(diagnostics::Severity::Warning),
50+
);
51+
trap::Compression::Gzip
52+
}
53+
};
54+
drop(main_thread_logger);
55+
5856
rayon::ThreadPoolBuilder::new()
5957
.num_threads(num_threads)
6058
.build_global()
@@ -79,7 +77,6 @@ fn main() -> std::io::Result<()> {
7977
.value_of("output-dir")
8078
.expect("missing --output-dir");
8179
let trap_dir = PathBuf::from(trap_dir);
82-
let trap_compression = trap::Compression::from_env("CODEQL_QL_TRAP_COMPRESSION");
8380

8481
let file_list = matches.value_of("file-list").expect("missing --file-list");
8582
let file_list = fs::File::open(file_list)?;
@@ -119,26 +116,29 @@ fn main() -> std::io::Result<()> {
119116
let source = std::fs::read(&path)?;
120117
let code_ranges = vec![];
121118
let mut trap_writer = trap::Writer::new();
119+
let mut diagnostics_writer = diagnostics.logger();
122120
if line.ends_with(".dbscheme") {
123121
extractor::extract(
124122
dbscheme,
125123
"dbscheme",
126124
&dbscheme_schema,
125+
&mut diagnostics_writer,
127126
&mut trap_writer,
128127
&path,
129128
&source,
130129
&code_ranges,
131-
)?
130+
)
132131
} else if line.ends_with("qlpack.yml") {
133132
extractor::extract(
134133
yaml,
135134
"yaml",
136135
&yaml_schema,
136+
&mut diagnostics_writer,
137137
&mut trap_writer,
138138
&path,
139139
&source,
140140
&code_ranges,
141-
)?
141+
)
142142
} else if line.ends_with(".json")
143143
|| line.ends_with(".jsonl")
144144
|| line.ends_with(".jsonc")
@@ -147,31 +147,34 @@ fn main() -> std::io::Result<()> {
147147
json,
148148
"json",
149149
&json_schema,
150+
&mut diagnostics_writer,
150151
&mut trap_writer,
151152
&path,
152153
&source,
153154
&code_ranges,
154-
)?
155+
)
155156
} else if line.ends_with(".blame") {
156157
extractor::extract(
157158
blame,
158159
"blame",
159160
&blame_schema,
161+
&mut diagnostics_writer,
160162
&mut trap_writer,
161163
&path,
162164
&source,
163165
&code_ranges,
164-
)?
166+
)
165167
} else {
166168
extractor::extract(
167169
language,
168170
"ql",
169171
&schema,
172+
&mut diagnostics_writer,
170173
&mut trap_writer,
171174
&path,
172175
&source,
173176
&code_ranges,
174-
)?
177+
)
175178
}
176179
std::fs::create_dir_all(&src_archive_file.parent().unwrap())?;
177180
std::fs::copy(&path, &src_archive_file)?;

0 commit comments

Comments
 (0)