on master help sign in
always build grammars with c++14 and c11 (#6792)
ca65d31Pascal Kuthe
Cargo.lock+1
@@ -1134,6 +1134,7 @@ dependencies = [
1134 "log",
1134 "log",
1135 "once_cell",
1135 "once_cell",
1136 "serde",
1136 "serde",
1137+ "tempfile",
1137 "threadpool",
1138 "threadpool",
1138 "toml",
1139 "toml",
1139 "tree-sitter",
1140 "tree-sitter",
helix-loader/Cargo.toml+1
@@ -27,6 +27,7 @@ log = "0.4"
27# cloning/compiling tree-sitter grammars
27# cloning/compiling tree-sitter grammars
28cc = { version = "1" }
28cc = { version = "1" }
29threadpool = { version = "1.0" }
29threadpool = { version = "1.0" }
30+tempfile = "3.5.0"
30
31
31[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
32[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
32libloading = "0.8"
33libloading = "0.8"
helix-loader/src/grammar.rs+41−7
@@ -8,6 +8,7 @@ use std::{
8 process::Command,
8 process::Command,
9 sync::mpsc::channel,
9 sync::mpsc::channel,
10};
10};
11+use tempfile::TempPath;
11use tree_sitter::Language;
12use tree_sitter::Language;
12
13
13#[cfg(unix)]
14#[cfg(unix)]
@@ -446,13 +447,17 @@ fn build_tree_sitter_library(
446 command.env(key, value);
447 command.env(key, value);
447 }
448 }
448 command.args(compiler.args());
449 command.args(compiler.args());
450+ // used to delay dropping the temporary object file until after the compilation is complete
451+ let _path_guard;
449
452
450 if cfg!(all(windows, target_env = "msvc")) {
453 if cfg!(all(windows, target_env = "msvc")) {
451 command
454 command
452 .args(["/nologo", "/LD", "/I"])
455 .args(["/nologo", "/LD", "/I"])
453 .arg(header_path)
456 .arg(header_path)
454 .arg("/Od")
457 .arg("/Od")
455− .arg("/utf-8");
458+ .arg("/utf-8")
459+ .arg("/std:c++14")
460+ .arg("/std:c11");
456 if let Some(scanner_path) = scanner_path.as_ref() {
461 if let Some(scanner_path) = scanner_path.as_ref() {
457 command.arg(scanner_path);
462 command.arg(scanner_path);
458 }
463 }
@@ -466,20 +471,49 @@ fn build_tree_sitter_library(
466 .arg("-shared")
471 .arg("-shared")
467 .arg("-fPIC")
472 .arg("-fPIC")
468 .arg("-fno-exceptions")
473 .arg("-fno-exceptions")
469− .arg("-g")
470 .arg("-I")
474 .arg("-I")
471 .arg(header_path)
475 .arg(header_path)
472 .arg("-o")
476 .arg("-o")
473− .arg(&library_path)
477+ .arg(&library_path);
474− .arg("-O3");
478+
475 if let Some(scanner_path) = scanner_path.as_ref() {
479 if let Some(scanner_path) = scanner_path.as_ref() {
476 if scanner_path.extension() == Some("c".as_ref()) {
480 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);
478 } else {
482 } else {
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")
479− command.arg(scanner_path);
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);
480 }
514 }
481 }
515 }
482− command.arg("-xc").arg(parser_path);
516+ command.arg("-xc").arg("-std=c11").arg(parser_path);
483 if cfg!(all(
517 if cfg!(all(
484 unix,
518 unix,
485 not(any(target_os = "macos", target_os = "illumos"))
519 not(any(target_os = "macos", target_os = "illumos"))