|
1 | 1 | // Copyright (c) 2025 ETH Zurich |
2 | 2 | // Tim Fischer <fischeti@iis.ee.ethz.ch> |
3 | 3 |
|
| 4 | +#[cfg(unix)] |
| 5 | +// We create a symlink from the generated include directory to a stable location in the target directory |
| 6 | +// so that tools like clangd can find the headers without needing to know the exact OUT_DIR path. |
| 7 | +// This is purely for improving the development experience and is not necessary for the build itself. |
| 8 | +fn refresh_include_symlink(generated_include_dir: &std::path::Path) { |
| 9 | + use std::ffi::OsStr; |
| 10 | + use std::fs; |
| 11 | + use std::os::unix::fs::symlink; |
| 12 | + use std::path::PathBuf; |
| 13 | + |
| 14 | + let Ok(out_dir) = std::env::var("OUT_DIR") else { |
| 15 | + return; |
| 16 | + }; |
| 17 | + let out_dir = PathBuf::from(out_dir); |
| 18 | + |
| 19 | + let Some(target_root) = out_dir |
| 20 | + .ancestors() |
| 21 | + .find(|path| path.file_name() == Some(OsStr::new("target"))) |
| 22 | + else { |
| 23 | + return; |
| 24 | + }; |
| 25 | + |
| 26 | + let stable_link = target_root.join("slang-generated-include"); |
| 27 | + let _ = fs::remove_file(&stable_link); |
| 28 | + let _ = fs::remove_dir_all(&stable_link); |
| 29 | + let _ = symlink(generated_include_dir, &stable_link); |
| 30 | +} |
| 31 | + |
| 32 | +#[cfg(not(unix))] |
| 33 | +fn refresh_include_symlink(_generated_include_dir: &std::path::Path) {} |
| 34 | + |
4 | 35 | fn main() { |
5 | 36 | let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap(); |
6 | 37 | let target_env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap(); |
@@ -64,6 +95,11 @@ fn main() { |
64 | 95 | let dst = slang_lib.build(); |
65 | 96 | let lib_dir = dst.join("lib"); |
66 | 97 |
|
| 98 | + // Create a symlink for the generated include directory |
| 99 | + if target_os == "linux" || target_os == "macos" { |
| 100 | + refresh_include_symlink(&dst.join("include")); |
| 101 | + } |
| 102 | + |
67 | 103 | // Configure Linker to find Slang static library |
68 | 104 | println!("cargo:rustc-link-search=native={}", lib_dir.display()); |
69 | 105 | println!("cargo:rustc-link-lib=static=svlang"); |
@@ -100,7 +136,7 @@ fn main() { |
100 | 136 | let compiler = std::env::var("CXX").unwrap_or_else(|_| "g++".to_string()); |
101 | 137 | // We search for the static libstdc++ file using g++ |
102 | 138 | let output = std::process::Command::new(&compiler) |
103 | | - .args(&["-print-file-name=libstdc++.a"]) |
| 139 | + .args(["-print-file-name=libstdc++.a"]) |
104 | 140 | .output() |
105 | 141 | .expect("Failed to run g++"); |
106 | 142 |
|
|
0 commit comments