Skip to content

Commit

Permalink
Try to add cmake support
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Dec 21, 2024
1 parent 2be11a7 commit 0f6a106
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
65 changes: 64 additions & 1 deletion src/compiler/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,14 @@ impl<'a> Clang<'a> {

cmd.env("PATH", env::join_paths(env_paths.clone())?);

// TODO: CMake support
// CMake support
let cmake_toolchain = self.setup_cmake_toolchain(target, &sysroot_dir)?;
cmd.env("CMAKE_GENERATOR", "Ninja")
.env("CMAKE_SYSTEM_NAME", "Windows")
.env(
format!("CMAKE_TOOLCHAIN_FILE_{}", env_target),
cmake_toolchain,
);
}
}
Ok(())
Expand Down Expand Up @@ -194,6 +201,62 @@ impl<'a> Clang<'a> {
archive.unpack(cache_dir)?;
Ok(())
}

fn setup_cmake_toolchain(&self, target: &str, sysroot_dir: &str) -> Result<PathBuf> {
// x86_64-pc-windows-msvc -> x86_64-windows-msvc
let target_no_vendor = target.replace("-pc-", "-");
let target_unknown_vendor = target.replace("-pc-", "-unknown-");
let cmake_cache_dir = self
.xwin_options
.xwin_cache_dir
.clone()
.unwrap_or_else(|| {
dirs::cache_dir()
// If the really is no cache dir, cwd will also do
.unwrap_or_else(|| env::current_dir().expect("Failed to get current dir"))
.join(env!("CARGO_PKG_NAME"))
})
.join("cmake")
.join("clang");
fs::create_dir_all(&cmake_cache_dir)?;

let toolchain_file = cmake_cache_dir.join(format!("{}-toolchain.cmake", target));
let target_arch = target
.split_once('-')
.map(|(x, _)| x)
.context("invalid target triple")?;
let processor = match target_arch {
"i586" | "i686" => "X86",
"x86_64" => "AMD64",
"aarch64" => "ARM64",
_ => target_arch,
};

let content = format!(
r#"
set(CMAKE_SYSTEM_NAME Windows)
set(CMAKE_SYSTEM_PROCESSOR {processor})
set(CMAKE_C_COMPILER clang CACHE FILEPATH "")
set(CMAKE_CXX_COMPILER clang++ CACHE FILEPATH "")
set(CMAKE_LINKER lld-link CACHE FILEPATH "")
set(CMAKE_RC_COMPILER llvm-rc CACHE FILEPATH "")
set(COMPILE_FLAGS
--target={target_no_vendor}
-fuse-ld=lld-link
-I{dir}/include
-I{dir}/include/c++/stl)
set(LINK_FLAGS
/manifest:no
-libpath:"{dir}/lib/{target_unknown_vendor}")
"#,
dir = sysroot_dir,
);
fs::write(&toolchain_file, content)?;
Ok(toolchain_file)
}
}

#[derive(Debug, Deserialize)]
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/clang_cl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ impl<'a> ClangCl<'a> {
.unwrap_or_else(|| env::current_dir().expect("Failed to get current dir"))
.join(env!("CARGO_PKG_NAME"))
})
.join("cmake");
.join("cmake")
.join("clang-cl");
fs::create_dir_all(&cmake_cache_dir)?;

let override_file = cmake_cache_dir.join("override.cmake");
Expand Down

0 comments on commit 0f6a106

Please sign in to comment.