| 1 | use helix_loader::grammar::{build_grammars, fetch_grammars}; |
| 2 | |
| 3 | const STRICT: bool = true; |
| 4 | |
| 5 | fn main() { |
| 6 | if std::env::var("HELIX_DISABLE_AUTO_GRAMMAR_BUILD").is_err() { |
| 7 | fetch_grammars(STRICT).expect("Failed to fetch tree-sitter grammars"); |
| 8 | build_grammars(Some(std::env::var("TARGET").unwrap()), STRICT) |
| 9 | .expect("Failed to compile tree-sitter grammars"); |
| 10 | } |
| 11 | |
| 12 | #[cfg(windows)] |
| 13 | windows_rc::link_icon_in_windows_exe("../contrib/helix-256p.ico"); |
| 14 | } |
| 15 | |
| 16 | #[cfg(windows)] |
| 17 | mod windows_rc { |
| 18 | use std::io::prelude::Write; |
| 19 | use std::{env, io, path::Path, path::PathBuf, process}; |
| 20 | |
| 21 | pub(crate) fn link_icon_in_windows_exe(icon_path: &str) { |
| 22 | let rc_exe = find_rc_exe().expect("Windows SDK is to be installed along with MSVC"); |
| 23 | |
| 24 | let output = env::var("OUT_DIR").expect("Env var OUT_DIR should have been set by compiler"); |
| 25 | let output_dir = PathBuf::from(output); |
| 26 | |
| 27 | let rc_path = output_dir.join("resource.rc"); |
| 28 | write_resource_file(&rc_path, icon_path).unwrap(); |
| 29 | |
| 30 | let resource_file = PathBuf::from(&output_dir).join("resource.lib"); |
| 31 | compile_with_toolkit_msvc(rc_exe, resource_file, rc_path); |
| 32 | |
| 33 | println!("cargo:rustc-link-search=native={}", output_dir.display()); |
| 34 | println!("cargo:rustc-link-lib=dylib=resource"); |
| 35 | } |
| 36 | |
| 37 | fn compile_with_toolkit_msvc(rc_exe: PathBuf, output: PathBuf, input: PathBuf) { |
| 38 | let mut command = process::Command::new(rc_exe); |
| 39 | let command = command.arg(format!( |
| 40 | "/I{}", |
| 41 | env::var("CARGO_MANIFEST_DIR") |
| 42 | .expect("CARGO_MANIFEST_DIR should have been set by Cargo") |
| 43 | )); |
| 44 | |
| 45 | let status = command |
| 46 | .arg(format!("/fo{}", output.display())) |
| 47 | .arg(format!("{}", input.display())) |
| 48 | .output() |
| 49 | .unwrap(); |
| 50 | |
| 51 | println!( |
| 52 | "RC Output:\n{}\n------", |
| 53 | String::from_utf8_lossy(&status.stdout) |
| 54 | ); |
| 55 | println!( |
| 56 | "RC Error:\n{}\n------", |
| 57 | String::from_utf8_lossy(&status.stderr) |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | fn find_rc_exe() -> io::Result<PathBuf> { |
| 62 | let find_reg_key = process::Command::new("reg") |
| 63 | .arg("query") |
| 64 | .arg(r"HKLM\SOFTWARE\Microsoft\Windows Kits\Installed Roots") |
| 65 | .arg("/reg:32") |
| 66 | .arg("/v") |
| 67 | .arg("KitsRoot10") |
| 68 | .output(); |
| 69 | |
| 70 | match find_reg_key { |
| 71 | Err(find_reg_key) => Err(io::Error::new( |
| 72 | io::ErrorKind::Other, |
| 73 | format!("Failed to run registry query: {}", find_reg_key), |
| 74 | )), |
| 75 | Ok(find_reg_key) => { |
| 76 | if find_reg_key.status.code().unwrap() != 0 { |
| 77 | Err(io::Error::new( |
| 78 | io::ErrorKind::Other, |
| 79 | "Can not find Windows SDK", |
| 80 | )) |
| 81 | } else { |
| 82 | let lines = String::from_utf8(find_reg_key.stdout) |
| 83 | .expect("Should be able to parse the output"); |
| 84 | let mut lines: Vec<&str> = lines.lines().collect(); |
| 85 | let mut rc_exe_paths: Vec<PathBuf> = Vec::new(); |
| 86 | lines.reverse(); |
| 87 | for line in lines { |
| 88 | if line.trim().starts_with("KitsRoot") { |
| 89 | let kit: String = line |
| 90 | .chars() |
| 91 | .skip(line.find("REG_SZ").unwrap() + 6) |
| 92 | .skip_while(|c| c.is_whitespace()) |
| 93 | .collect(); |
| 94 | |
| 95 | let p = PathBuf::from(&kit); |
| 96 | let rc = if cfg!(target_arch = "x86_64") { |
| 97 | p.join(r"bin\x64\rc.exe") |
| 98 | } else { |
| 99 | p.join(r"bin\x86\rc.exe") |
| 100 | }; |
| 101 | |
| 102 | if rc.exists() { |
| 103 | println!("{:?}", rc); |
| 104 | rc_exe_paths.push(rc.to_owned()); |
| 105 | } |
| 106 | |
| 107 | if let Ok(bin) = p.join("bin").read_dir() { |
| 108 | for e in bin.filter_map(|e| e.ok()) { |
| 109 | let p = if cfg!(target_arch = "x86_64") { |
| 110 | e.path().join(r"x64\rc.exe") |
| 111 | } else { |
| 112 | e.path().join(r"x86\rc.exe") |
| 113 | }; |
| 114 | if p.exists() { |
| 115 | println!("{:?}", p); |
| 116 | rc_exe_paths.push(p.to_owned()); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | if rc_exe_paths.is_empty() { |
| 123 | return Err(io::Error::new( |
| 124 | io::ErrorKind::Other, |
| 125 | "Can not find Windows SDK", |
| 126 | )); |
| 127 | } |
| 128 | |
| 129 | println!("{:?}", rc_exe_paths); |
| 130 | let rc_path = rc_exe_paths.pop().unwrap(); |
| 131 | |
| 132 | let rc_exe = if !rc_path.exists() { |
| 133 | if cfg!(target_arch = "x86_64") { |
| 134 | PathBuf::from(rc_path.parent().unwrap()).join(r"bin\x64\rc.exe") |
| 135 | } else { |
| 136 | PathBuf::from(rc_path.parent().unwrap()).join(r"bin\x86\rc.exe") |
| 137 | } |
| 138 | } else { |
| 139 | rc_path |
| 140 | }; |
| 141 | |
| 142 | println!("Selected RC path: '{}'", rc_exe.display()); |
| 143 | Ok(rc_exe) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | fn write_resource_file(rc_path: &Path, icon_path: &str) -> io::Result<()> { |
| 150 | let mut f = std::fs::File::create(rc_path)?; |
| 151 | writeln!(f, "{} ICON \"{}\"", 1, icon_path)?; |
| 152 | |
| 153 | Ok(()) |
| 154 | } |
| 155 | } |