From 8f95ce13a7e4f357f4744fc28f3ab7248def3304 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Thu, 7 Nov 2024 19:12:15 +0100 Subject: [PATCH] fix: install `src/.c` modules correctly --- rocks-lib/src/build/builtin.rs | 37 ++++++++++++++++------- rocks-lib/src/rockspec/build/utils/mod.rs | 18 ++++++++--- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/rocks-lib/src/build/builtin.rs b/rocks-lib/src/build/builtin.rs index ff0e9cf78..a630c2632 100644 --- a/rocks-lib/src/build/builtin.rs +++ b/rocks-lib/src/build/builtin.rs @@ -36,17 +36,32 @@ impl Build for BuiltinBuildSpec { for (counter, (destination_path, module_type)) in modules.iter().enumerate() { match module_type { ModuleSpec::SourcePath(source) => { - bar.set_message(format!( - "Copying {} to {}...", - &source.to_string_lossy(), - &destination_path - )); - let absolute_source_path = build_dir.join(source); - utils::copy_lua_to_module_path( - &absolute_source_path, - destination_path, - &output_paths.src, - )? + if source.extension().map(|ext| ext == "c").unwrap_or(false) { + bar.set_message(format!( + "Compiling {} -> {}...", + &source.to_string_lossy(), + &destination_path + )); + let absolute_source_paths = vec![build_dir.join(source)]; + utils::compile_c_files( + &absolute_source_paths, + destination_path, + &output_paths.lib, + lua, + )? + } else { + bar.set_message(format!( + "Copying {} to {}...", + &source.to_string_lossy(), + &destination_path + )); + let absolute_source_path = build_dir.join(source); + utils::copy_lua_to_module_path( + &absolute_source_path, + destination_path, + &output_paths.src, + )? + } } ModuleSpec::SourcePaths(files) => { bar.set_message("Compiling C files..."); diff --git a/rocks-lib/src/rockspec/build/utils/mod.rs b/rocks-lib/src/rockspec/build/utils/mod.rs index ea262e8bd..735ee2daf 100644 --- a/rocks-lib/src/rockspec/build/utils/mod.rs +++ b/rocks-lib/src/rockspec/build/utils/mod.rs @@ -58,7 +58,10 @@ pub fn compile_c_files( // TODO: Use `target-lexicon` data here instead, it's more reliable. - cc::Build::new() + // See https://github.com/rust-lang/cc-rs/issues/594#issuecomment-2110551057 + + let mut build = cc::Build::new(); + let build = build .cargo_metadata(false) .debug(false) .files(files) @@ -66,10 +69,15 @@ pub fn compile_c_files( .includes(&lua.include_dir) .opt_level(3) .out_dir(parent) - .shared_flag(true) - .target(std::env::consts::ARCH) - .try_compile(file.to_str().unwrap())?; - + .target(std::env::consts::ARCH); + let objects = build.compile_intermediates(); + build + .get_compiler() + .to_command() + .args(["-shared", "-o"]) + .arg(parent.join(file)) + .args(&objects) + .status()?; Ok(()) }