on master help sign in
always build grammars with c++14 and c11 (#6792)
commitca65d312e715413cd5675cafd599d74534900d3f authorPascal Kuthe <pascal.kuthe@semimod.de> dateTue 18 Apr 2023, 03:10 (4 years ago) parent2ccb5b7
3 files changed+43−7
Cargo.lock+1 helix-loader/Cargo.toml+1 helix-loader/src/grammar.rs+41−7
Cargo.lock+1
@@ -1134,6 +1134,7 @@ dependencies = [
11341134 "log",
11351135 "once_cell",
11361136 "serde",
1137+ "tempfile",
11371138 "threadpool",
11381139 "toml",
11391140 "tree-sitter",
helix-loader/Cargo.toml+1
@@ -27,6 +27,7 @@ log = "0.4"
2727# cloning/compiling tree-sitter grammars
2828cc = { version = "1" }
2929threadpool = { version = "1.0" }
30+tempfile = "3.5.0"
3031
3132[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
3233libloading = "0.8"
helix-loader/src/grammar.rs+41−7
@@ -8,6 +8,7 @@ use std::{
88 process::Command,
99 sync::mpsc::channel,
1010};
11+use tempfile::TempPath;
1112use tree_sitter::Language;
1213
1314#[cfg(unix)]
@@ -446,13 +447,17 @@ fn build_tree_sitter_library(
446447 command.env(key, value);
447448 }
448449 command.args(compiler.args());
450+ // used to delay dropping the temporary object file until after the compilation is complete
451+ let _path_guard;
449452
450453 if cfg!(all(windows, target_env = "msvc")) {
451454 command
452455 .args(["/nologo", "/LD", "/I"])
453456 .arg(header_path)
454457 .arg("/Od")
455− .arg("/utf-8");
458+ .arg("/utf-8")
459+ .arg("/std:c++14")
460+ .arg("/std:c11");
456461 if let Some(scanner_path) = scanner_path.as_ref() {
457462 command.arg(scanner_path);
458463 }
@@ -466,20 +471,49 @@ fn build_tree_sitter_library(
466471 .arg("-shared")
467472 .arg("-fPIC")
468473 .arg("-fno-exceptions")
469− .arg("-g")
470474 .arg("-I")
471475 .arg(header_path)
472476 .arg("-o")
473− .arg(&library_path)
474− .arg("-O3");
477+ .arg(&library_path);
478+
475479 if let Some(scanner_path) = scanner_path.as_ref() {
476480 if scanner_path.extension() == Some("c".as_ref()) {
477− command.arg("-xc").arg("-std=c99").arg(scanner_path);
481+ command.arg("-xc").arg("-std=c11").arg(scanner_path);
478482 } else {
479− command.arg(scanner_path);
483+ let mut cpp_command = Command::new(compiler.path());
484+ cpp_command.current_dir(src_path);
485+ for (key, value) in compiler.env() {
486+ cpp_command.env(key, value);
487+ }
488+ cpp_command.args(compiler.args());
489+ let object_file =
490+ library_path.with_file_name(format!("{}_scanner.o", &grammar.grammar_id));
491+ cpp_command
492+ .arg("-fPIC")
493+ .arg("-fno-exceptions")
494+ .arg("-I")
495+ .arg(header_path)
496+ .arg("-o")
497+ .arg(&object_file)
498+ .arg("-std=c++14")
499+ .arg("-c")
500+ .arg(scanner_path);
501+ let output = cpp_command
502+ .output()
503+ .context("Failed to execute C++ compiler")?;
504+ if !output.status.success() {
505+ return Err(anyhow!(
506+ "Parser compilation failed.\nStdout: {}\nStderr: {}",
507+ String::from_utf8_lossy(&output.stdout),
508+ String::from_utf8_lossy(&output.stderr)
509+ ));
510+ }
511+
512+ command.arg(&object_file);
513+ _path_guard = TempPath::from_path(object_file);
480514 }
481515 }
482− command.arg("-xc").arg(parser_path);
516+ command.arg("-xc").arg("-std=c11").arg(parser_path);
483517 if cfg!(all(
484518 unix,
485519 not(any(target_os = "macos", target_os = "illumos"))