lux_lib/build/compile/
c_files.rs1use std::{
2 collections::HashMap,
3 path::{Path, PathBuf},
4};
5
6use miette::Diagnostic;
7use target_lexicon::Triple;
8use thiserror::Error;
9
10use crate::{
11 build::external_dependency::ExternalDependencyInfo, config::Config, fs, lua, rockspec,
12};
13
14use super::{c_link, CCExt};
15
16#[derive(Error, Debug, Diagnostic)]
17#[non_exhaustive]
18pub enum Error {
19 #[error(transparent)]
20 #[diagnostic(transparent)]
21 Fs(#[from] fs::Error),
22 #[error("failed to compile intermediates from C files")]
23 #[diagnostic(help(
24 r#"check the compiler output above for missing headers or syntax errors.
25ensure the C compiler has access to the required include paths."#
26 ))]
27 CompileIntermediates { source: cc::Error },
28 #[error("error compiling C files (compilation failed):")]
29 #[diagnostic(help(
30 r#"check the compiler output above for details.
31if no C compiler was found, run `lx debug toolchains` to verify your build tools."#
32 ))]
33 Compilation(#[from] cc::Error),
34 #[error("error compiling C files (linking failed)")]
35 #[diagnostic(forward(0))]
36 Link(#[from] c_link::Error),
37}
38
39#[tracing::instrument(
43 name = "Compiling C files",
44 level = "info",
45 skip_all,
46 fields(profile = config.build_profile().to_string()),
47)]
48pub(crate) async fn compile(
49 files: &Vec<PathBuf>,
50 target_module: &rockspec::build::backend::Module,
51 target_dir: &Path,
52 lua: &lua::Installation,
53 external_dependencies: &HashMap<String, ExternalDependencyInfo>,
54 config: &Config,
55) -> Result<(), Error> {
56 let target = target_dir.join(target_module.to_lib_path());
57 let target_parent_dir = target.parent().unwrap_or_else(|| {
58 unreachable!(
59 "couldn't determine build target parent directory:\n{}",
60 target.display()
61 )
62 });
63 let target_file_name = target
64 .file_name()
65 .unwrap_or_default()
66 .to_string_lossy()
67 .to_string();
68
69 fs::sync::create_dir_all(target_parent_dir)?;
70
71 let host = Triple::host();
72
73 let mut build = cc::Build::new();
76 let intermediate_dir = fs::tempfile::tempdir()?;
77 let build = build
78 .cargo_output(config.verbose())
79 .cargo_debug(config.verbose())
80 .cargo_metadata(config.verbose())
81 .cargo_warnings(config.verbose())
82 .warnings(config.verbose())
83 .files(files)
84 .host(&host.to_string())
85 .target(&host.to_string())
86 .includes(lua.includes())
87 .includes(
88 external_dependencies
89 .values()
90 .filter_map(|dep| dep.include_dir.as_ref()),
91 )
92 .opt_level(config.build_profile().opt_level())
93 .out_dir(intermediate_dir);
94
95 let compiler = build.try_get_compiler()?;
96 if compiler.is_like_msvc() {
98 build.flag("-W0");
99 } else {
100 build.flag("-w");
101 }
102 for arg in lua.define_flags() {
103 build.flag(&arg);
104 }
105
106 let objects = build
107 .try_compile_objects(config)
108 .map_err(|source| Error::CompileIntermediates { source })?;
109
110 c_link::artifacts(
111 build,
112 lua,
113 external_dependencies,
114 &target_file_name,
115 Vec::new(),
116 Vec::new(),
117 target_module,
118 target_parent_dir,
119 objects,
120 config,
121 )
122 .await?;
123 Ok(())
124}