Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always build grammars with c++14 and c11 #6792

Merged
merged 1 commit into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions helix-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ log = "0.4"
# cloning/compiling tree-sitter grammars
cc = { version = "1" }
threadpool = { version = "1.0" }
tempfile = "3.5.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
libloading = "0.7"
48 changes: 41 additions & 7 deletions helix-loader/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
process::Command,
sync::mpsc::channel,
};
use tempfile::TempPath;
use tree_sitter::Language;

#[cfg(unix)]
Expand Down Expand Up @@ -446,13 +447,17 @@ fn build_tree_sitter_library(
command.env(key, value);
}
command.args(compiler.args());
// used to delay dropping the temporary object file until after the compilation is complete
let _path_guard;

if cfg!(all(windows, target_env = "msvc")) {
command
.args(["/nologo", "/LD", "/I"])
.arg(header_path)
.arg("/Od")
.arg("/utf-8");
.arg("/utf-8")
.arg("/std:c++14")
.arg("/std:c11");
if let Some(scanner_path) = scanner_path.as_ref() {
command.arg(scanner_path);
}
Expand All @@ -466,20 +471,49 @@ fn build_tree_sitter_library(
.arg("-shared")
.arg("-fPIC")
.arg("-fno-exceptions")
.arg("-g")
.arg("-I")
.arg(header_path)
.arg("-o")
.arg(&library_path)
.arg("-O3");
.arg(&library_path);

if let Some(scanner_path) = scanner_path.as_ref() {
if scanner_path.extension() == Some("c".as_ref()) {
command.arg("-xc").arg("-std=c99").arg(scanner_path);
command.arg("-xc").arg("-std=c11").arg(scanner_path);
} else {
command.arg(scanner_path);
let mut cpp_command = Command::new(compiler.path());
cpp_command.current_dir(src_path);
for (key, value) in compiler.env() {
cpp_command.env(key, value);
}
cpp_command.args(compiler.args());
let object_file =
library_path.with_file_name(format!("{}_scanner.o", &grammar.grammar_id));
cpp_command
.arg("-fPIC")
.arg("-fno-exceptions")
.arg("-I")
.arg(header_path)
.arg("-o")
.arg(&object_file)
.arg("-std=c++14")
.arg("-c")
.arg(scanner_path);
let output = cpp_command
.output()
.context("Failed to execute C++ compiler")?;
if !output.status.success() {
return Err(anyhow!(
"Parser compilation failed.\nStdout: {}\nStderr: {}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
));
}

command.arg(&object_file);
_path_guard = TempPath::from_path(object_file);
}
}
command.arg("-xc").arg(parser_path);
command.arg("-xc").arg("-std=c11").arg(parser_path);
if cfg!(all(
unix,
not(any(target_os = "macos", target_os = "illumos"))
Expand Down