lux_lib/build/
external_dependency.rs

1use itertools::Itertools;
2use path_slash::{PathBufExt, PathExt};
3use pkg_config::{Config as PkgConfig, Library};
4use std::{
5    collections::HashMap,
6    path::{Path, PathBuf},
7};
8use thiserror::Error;
9
10use crate::{
11    config::external_deps::ExternalDependencySearchConfig, lua_rockspec::ExternalDependencySpec,
12    variables::HasVariables,
13};
14
15use super::utils::{c_lib_extension, format_path};
16
17#[derive(Error, Debug)]
18pub enum ExternalDependencyError {
19    #[error("{}", not_found_error_msg(.0))]
20    NotFound(String),
21    #[error("IO error while trying to detect external dependencies: {0}")]
22    Io(#[from] std::io::Error),
23    #[error("{0} was probed successfully, but the header {1} could not be found")]
24    SuccessfulProbeHeaderNotFound(String, String),
25    #[error("error probing external dependency {0}: the header {1} could not be found")]
26    HeaderNotFound(String, String),
27    #[error("error probing external dependency {0}: the library {1} could not be found")]
28    LibraryNotFound(String, String),
29}
30
31#[derive(Debug)]
32pub struct ExternalDependencyInfo {
33    pub(crate) include_dir: Option<PathBuf>,
34    pub(crate) lib_dir: Option<PathBuf>,
35    pub(crate) bin_dir: Option<PathBuf>,
36    /// Name of the static library, (without the 'lib' prefix or file extension on unix targets),
37    /// for example, "foo" or "foo.dll"
38    pub(crate) lib_name: Option<String>,
39    /// pkg-config library information if available
40    pub(crate) lib_info: Option<Library>,
41}
42
43fn pkg_config_probe(name: &str) -> Option<Library> {
44    PkgConfig::new()
45        .print_system_libs(false)
46        .cargo_metadata(false)
47        .env_metadata(false)
48        .probe(&name.to_lowercase())
49        .ok()
50}
51
52impl ExternalDependencyInfo {
53    pub fn probe(
54        name: &str,
55        dependency: &ExternalDependencySpec,
56        config: &ExternalDependencySearchConfig,
57    ) -> Result<Self, ExternalDependencyError> {
58        let lib_info = pkg_config_probe(name)
59            .or(pkg_config_probe(&format!("lib{}", name.to_lowercase())))
60            .or(dependency.library.as_ref().and_then(|lib_name| {
61                let lib_name = lib_name.to_string_lossy().to_string();
62                let lib_name_without_ext = lib_name.split('.').next().unwrap_or(&lib_name);
63                pkg_config_probe(lib_name_without_ext)
64                    .or(pkg_config_probe(&format!("lib{}", lib_name_without_ext)))
65            }));
66        if let Some(info) = lib_info {
67            let include_dir = if let Some(header) = &dependency.header {
68                Some(
69                    info.include_paths
70                        .iter()
71                        .find(|path| path.join(header).exists())
72                        .ok_or(ExternalDependencyError::SuccessfulProbeHeaderNotFound(
73                            name.to_string(),
74                            header.to_slash_lossy().to_string(),
75                        ))?
76                        .clone(),
77                )
78            } else {
79                info.include_paths.first().cloned()
80            };
81            let lib_dir = if let Some(lib) = &dependency.library {
82                info.link_paths
83                    .iter()
84                    .find(|path| library_exists(path, lib, &config.lib_patterns))
85                    .cloned()
86                    .or(info.link_paths.first().cloned())
87            } else {
88                info.link_paths.first().cloned()
89            };
90            let bin_dir = lib_dir.as_ref().and_then(|lib_dir| {
91                lib_dir
92                    .parent()
93                    .map(|parent| parent.join("bin"))
94                    .filter(|dir| dir.is_dir())
95            });
96            let lib_name = lib_dir.as_ref().and_then(|lib_dir| {
97                let prefix = dependency
98                    .library
99                    .as_ref()
100                    .map(|lib_name| lib_name.to_string_lossy().to_string())
101                    .unwrap_or(name.to_lowercase());
102                get_lib_name(lib_dir, &prefix)
103            });
104            return Ok(ExternalDependencyInfo {
105                include_dir,
106                lib_dir,
107                bin_dir,
108                lib_name,
109                lib_info: Some(info),
110            });
111        }
112        Self::fallback_probe(name, dependency, config)
113    }
114
115    fn fallback_probe(
116        name: &str,
117        dependency: &ExternalDependencySpec,
118        config: &ExternalDependencySearchConfig,
119    ) -> Result<Self, ExternalDependencyError> {
120        let env_prefix = std::env::var(format!("{}_DIR", name.to_uppercase())).ok();
121
122        let mut search_prefixes = Vec::new();
123        if let Some(dir) = env_prefix {
124            search_prefixes.push(PathBuf::from(dir));
125        }
126        if let Some(prefix) = config.prefixes.get(&format!("{}_DIR", name.to_uppercase())) {
127            search_prefixes.push(prefix.clone());
128        }
129        search_prefixes.extend(config.search_prefixes.iter().cloned());
130
131        let mut include_dir = get_incdir(name, config);
132
133        if let Some(header) = &dependency.header {
134            if !&include_dir
135                .as_ref()
136                .is_some_and(|inc_dir| inc_dir.join(header).exists())
137            {
138                // Search prefixes
139                let inc_dir = search_prefixes
140                    .iter()
141                    .find_map(|prefix| {
142                        let inc_dir = prefix.join(&config.include_subdir);
143                        if inc_dir.join(header).exists() {
144                            Some(inc_dir)
145                        } else {
146                            None
147                        }
148                    })
149                    .ok_or(ExternalDependencyError::HeaderNotFound(
150                        name.to_string(),
151                        header.to_slash_lossy().to_string(),
152                    ))?;
153                include_dir = Some(inc_dir);
154            }
155        }
156
157        let mut lib_dir = get_libdir(name, config);
158
159        if let Some(lib) = &dependency.library {
160            if !lib_dir
161                .as_ref()
162                .is_some_and(|lib_dir| library_exists(lib_dir, lib, &config.lib_patterns))
163            {
164                let probed_lib_dir = search_prefixes
165                    .iter()
166                    .find_map(|prefix| {
167                        for lib_subdir in &config.lib_subdirs {
168                            let lib_dir_candidate = prefix.join(lib_subdir);
169                            if library_exists(&lib_dir_candidate, lib, &config.lib_patterns) {
170                                return Some(lib_dir_candidate);
171                            }
172                        }
173                        None
174                    })
175                    .ok_or(ExternalDependencyError::LibraryNotFound(
176                        name.to_string(),
177                        lib.to_slash_lossy().to_string(),
178                    ))?;
179                lib_dir = Some(probed_lib_dir);
180            }
181        }
182
183        if let (None, None) = (&include_dir, &lib_dir) {
184            return Err(ExternalDependencyError::NotFound(name.into()));
185        }
186        let bin_dir = lib_dir.as_ref().and_then(|lib_dir| {
187            lib_dir
188                .parent()
189                .map(|parent| parent.join("bin"))
190                .filter(|dir| dir.is_dir())
191        });
192        let lib_name = lib_dir.as_ref().and_then(|lib_dir| {
193            let prefix = dependency
194                .library
195                .as_ref()
196                .map(|lib_name| lib_name.to_string_lossy().to_string())
197                .unwrap_or(name.to_lowercase());
198            get_lib_name(lib_dir, &prefix)
199        });
200        Ok(ExternalDependencyInfo {
201            include_dir,
202            lib_dir,
203            bin_dir,
204            lib_name,
205            lib_info: None,
206        })
207    }
208
209    pub(crate) fn define_flags(&self) -> Vec<String> {
210        if let Some(info) = &self.lib_info {
211            info.defines
212                .iter()
213                .map(|(k, v)| match v {
214                    Some(val) => {
215                        format!("-D{}={}", k, val)
216                    }
217                    None => format!("-D{}", k),
218                })
219                .collect_vec()
220        } else {
221            Vec::new()
222        }
223    }
224
225    pub(crate) fn lib_link_args(&self, compiler: &cc::Tool) -> Vec<String> {
226        if let Some(info) = &self.lib_info {
227            info.link_paths
228                .iter()
229                .map(|p| lib_dir_compile_arg(p, compiler))
230                .chain(
231                    info.libs
232                        .iter()
233                        .map(|lib| format_lib_link_arg(lib, compiler)),
234                )
235                .chain(info.ld_args.iter().map(|ld_arg_group| {
236                    ld_arg_group
237                        .iter()
238                        .map(|arg| format_linker_arg(arg, compiler))
239                        .collect::<Vec<_>>()
240                        .join(" ")
241                }))
242                .collect_vec()
243        } else {
244            self.lib_dir
245                .iter()
246                .map(|lib_dir| lib_dir_compile_arg(lib_dir, compiler))
247                .chain(
248                    self.lib_name
249                        .as_ref()
250                        .and_then(|lib_name| {
251                            if compiler.is_like_msvc() {
252                                self.lib_dir.as_ref().map(|lib_dir| {
253                                    lib_dir.join(lib_name).to_slash_lossy().to_string()
254                                })
255                            } else {
256                                Some(format!("-l{}", lib_name))
257                            }
258                        })
259                        .iter()
260                        .cloned(),
261                )
262                .collect_vec()
263        }
264    }
265}
266
267impl HasVariables for HashMap<String, ExternalDependencyInfo> {
268    fn get_variable(&self, input: &str) -> Option<String> {
269        input.split_once('_').and_then(|(dep_key, dep_dir_type)| {
270            self.get(dep_key)
271                .and_then(|dep| match dep_dir_type {
272                    "DIR" => dep
273                        .include_dir
274                        .as_ref()
275                        .and_then(|dir| dir.parent().map(|parent| parent.to_path_buf())),
276                    "INCDIR" => dep.include_dir.clone(),
277                    "LIBDIR" => dep.lib_dir.clone(),
278                    "BINDIR" => dep.bin_dir.clone(),
279                    _ => None,
280                })
281                .as_deref()
282                .map(format_path)
283        })
284    }
285}
286
287fn library_exists(lib_dir: &Path, lib: &Path, patterns: &[String]) -> bool {
288    patterns.iter().any(|pattern| {
289        let file_name = pattern.replace('?', &format!("{}", lib.display()));
290        lib_dir.join(&file_name).exists()
291    })
292}
293
294fn get_incdir(name: &str, config: &ExternalDependencySearchConfig) -> Option<PathBuf> {
295    let var_name = format!("{}_INCDIR", name.to_uppercase());
296    if let Ok(env_incdir) = std::env::var(&var_name) {
297        Some(env_incdir.into())
298    } else {
299        config.prefixes.get(&var_name).cloned()
300    }
301    .filter(|dir| dir.is_dir())
302}
303
304fn get_libdir(name: &str, config: &ExternalDependencySearchConfig) -> Option<PathBuf> {
305    let var_name = format!("{}_LIBDIR", name.to_uppercase());
306    if let Ok(env_incdir) = std::env::var(&var_name) {
307        Some(env_incdir.into())
308    } else {
309        config.prefixes.get(&var_name).cloned()
310    }
311    .filter(|dir| dir.is_dir())
312}
313
314fn not_found_error_msg(name: &String) -> String {
315    let env_dir = format!("{}_DIR", &name.to_uppercase());
316    let env_inc = format!("{}_INCDIR", &name.to_uppercase());
317    let env_lib = format!("{}_LIBDIR", &name.to_uppercase());
318
319    format!(
320        r#"External dependency not found: {}.
321Consider one of the following:
3221. Set environment variables:
323   - {} for the installation prefix, or
324   - {} and {} for specific directories
3252. Add the installation prefix to the configuration:
326   {} = "/path/to/installation""#,
327        name, env_dir, env_inc, env_lib, env_dir,
328    )
329}
330
331fn lib_dir_compile_arg(dir: &Path, compiler: &cc::Tool) -> String {
332    if compiler.is_like_msvc() {
333        format!("/LIBPATH:{}", dir.to_slash_lossy())
334    } else {
335        format!("-L{}", dir.to_slash_lossy())
336    }
337}
338
339fn format_lib_link_arg(lib: &str, compiler: &cc::Tool) -> String {
340    if compiler.is_like_msvc() {
341        format!("{}.lib", lib)
342    } else {
343        format!("-l{}", lib)
344    }
345}
346
347fn format_linker_arg(arg: &str, compiler: &cc::Tool) -> String {
348    if compiler.is_like_msvc() {
349        format!("-Wl,{}", arg)
350    } else {
351        format!("/link {}", arg)
352    }
353}
354
355pub(crate) fn to_lib_name(file: &Path) -> String {
356    let file_name = file.file_name().unwrap();
357    if cfg!(target_family = "unix") {
358        file_name
359            .to_string_lossy()
360            .trim_start_matches("lib")
361            .trim_end_matches(".a")
362            .to_string()
363    } else {
364        file_name.to_string_lossy().to_string()
365    }
366}
367
368fn get_lib_name(lib_dir: &Path, prefix: &str) -> Option<String> {
369    std::fs::read_dir(lib_dir)
370        .ok()
371        .and_then(|entries| {
372            entries
373                .filter_map(Result::ok)
374                .map(|entry| entry.path().to_path_buf())
375                .filter(|file| file.extension().is_some_and(|ext| ext == c_lib_extension()))
376                .filter(|file| {
377                    file.file_name()
378                        .is_some_and(|name| is_lib_name(&name.to_string_lossy(), prefix))
379                })
380                .collect_vec()
381                .first()
382                .cloned()
383        })
384        .map(|file| to_lib_name(&file))
385}
386fn is_lib_name(file_name: &str, prefix: &str) -> bool {
387    #[cfg(target_family = "unix")]
388    let file_name = file_name.trim_start_matches("lib");
389    file_name == format!("{}.{}", prefix, c_lib_extension())
390}
391
392#[cfg(test)]
393mod tests {
394    use super::*;
395    use assert_fs::{prelude::*, TempDir};
396
397    #[tokio::test]
398    async fn test_detect_zlib_pkg_config_header() {
399        // requires zlib to be in the nativeCheckInputs or dev environment
400        let config = ExternalDependencySearchConfig::default();
401        ExternalDependencyInfo::probe(
402            "zlib",
403            &ExternalDependencySpec {
404                header: Some("zlib.h".into()),
405                library: None,
406            },
407            &config,
408        )
409        .unwrap();
410    }
411
412    #[tokio::test]
413    async fn test_detect_zlib_pkg_config_library_libz() {
414        // requires zlib to be in the nativeCheckInputs or dev environment
415        let config = ExternalDependencySearchConfig::default();
416        ExternalDependencyInfo::probe(
417            "zlib",
418            &ExternalDependencySpec {
419                library: Some("libz".into()),
420                header: None,
421            },
422            &config,
423        )
424        .unwrap();
425    }
426
427    #[tokio::test]
428    async fn test_detect_zlib_pkg_config_library_z() {
429        // requires zlib to be in the nativeCheckInputs or dev environment
430        let config = ExternalDependencySearchConfig::default();
431        ExternalDependencyInfo::probe(
432            "zlib",
433            &ExternalDependencySpec {
434                library: Some("z".into()),
435                header: None,
436            },
437            &config,
438        )
439        .unwrap();
440    }
441
442    #[tokio::test]
443    async fn test_detect_zlib_pkg_config_library_zlib() {
444        // requires zlib to be in the nativeCheckInputs or dev environment
445        let config = ExternalDependencySearchConfig::default();
446        ExternalDependencyInfo::probe(
447            "zlib",
448            &ExternalDependencySpec {
449                library: Some("zlib".into()),
450                header: None,
451            },
452            &config,
453        )
454        .unwrap();
455    }
456
457    #[tokio::test]
458    async fn test_fallback_detect_header_prefix() {
459        let temp = TempDir::new().unwrap();
460        let prefix_dir = temp.child("usr");
461        let include_dir = prefix_dir.child("include");
462        include_dir.create_dir_all().unwrap();
463
464        let header = include_dir.child("foo.h");
465        header.touch().unwrap();
466
467        let mut config = ExternalDependencySearchConfig::default();
468        config
469            .prefixes
470            .insert("FOO_DIR".into(), prefix_dir.path().to_path_buf());
471
472        ExternalDependencyInfo::fallback_probe(
473            "foo",
474            &ExternalDependencySpec {
475                header: Some("foo.h".into()),
476                library: None,
477            },
478            &config,
479        )
480        .unwrap();
481    }
482
483    #[tokio::test]
484    async fn test_fallback_detect_header_prefix_incdir() {
485        let temp = TempDir::new().unwrap();
486        let include_dir = temp.child("include");
487        include_dir.create_dir_all().unwrap();
488
489        let header = include_dir.child("foo.h");
490        header.touch().unwrap();
491
492        let mut config = ExternalDependencySearchConfig::default();
493        config
494            .prefixes
495            .insert("FOO_INCDIR".into(), include_dir.path().to_path_buf());
496
497        ExternalDependencyInfo::fallback_probe(
498            "foo",
499            &ExternalDependencySpec {
500                header: Some("foo.h".into()),
501                library: None,
502            },
503            &config,
504        )
505        .unwrap();
506    }
507
508    #[tokio::test]
509    async fn test_fallback_detect_library_prefix() {
510        let temp = TempDir::new().unwrap();
511        let prefix_dir = temp.child("usr");
512        let include_dir = prefix_dir.child("include");
513        let lib_dir = prefix_dir.child("lib");
514        include_dir.create_dir_all().unwrap();
515        lib_dir.create_dir_all().unwrap();
516
517        #[cfg(target_os = "linux")]
518        let lib = lib_dir.child("libfoo.so");
519        #[cfg(target_os = "macos")]
520        let lib = lib_dir.child("libfoo.dylib");
521        #[cfg(target_family = "windows")]
522        let lib = lib_dir.child("foo.dll");
523
524        lib.touch().unwrap();
525
526        let mut config = ExternalDependencySearchConfig::default();
527        config
528            .prefixes
529            .insert("FOO_DIR".to_string(), prefix_dir.path().to_path_buf());
530
531        ExternalDependencyInfo::fallback_probe(
532            "foo",
533            &ExternalDependencySpec {
534                library: Some("foo".into()),
535                header: None,
536            },
537            &config,
538        )
539        .unwrap();
540    }
541
542    #[tokio::test]
543    async fn test_fallback_detect_library_dirs() {
544        let temp = TempDir::new().unwrap();
545
546        let include_dir = temp.child("include");
547        include_dir.create_dir_all().unwrap();
548
549        let lib_dir = temp.child("lib");
550        lib_dir.create_dir_all().unwrap();
551
552        #[cfg(target_os = "linux")]
553        let lib = lib_dir.child("libfoo.so");
554        #[cfg(target_os = "macos")]
555        let lib = lib_dir.child("libfoo.dylib");
556        #[cfg(target_family = "windows")]
557        let lib = lib_dir.child("foo.dll");
558
559        lib.touch().unwrap();
560
561        let mut config = ExternalDependencySearchConfig::default();
562        config
563            .prefixes
564            .insert("FOO_INCDIR".into(), include_dir.path().to_path_buf());
565        config
566            .prefixes
567            .insert("FOO_LIBDIR".into(), lib_dir.path().to_path_buf());
568
569        ExternalDependencyInfo::fallback_probe(
570            "foo",
571            &ExternalDependencySpec {
572                library: Some("foo".into()),
573                header: None,
574            },
575            &config,
576        )
577        .unwrap();
578    }
579
580    #[tokio::test]
581    async fn test_fallback_detect_search_prefixes() {
582        let temp = TempDir::new().unwrap();
583        let prefix_dir = temp.child("usr");
584        let include_dir = prefix_dir.child("include");
585        let lib_dir = prefix_dir.child("lib");
586        include_dir.create_dir_all().unwrap();
587        lib_dir.create_dir_all().unwrap();
588
589        #[cfg(target_os = "linux")]
590        let lib = lib_dir.child("libfoo.so");
591        #[cfg(target_os = "macos")]
592        let lib = lib_dir.child("libfoo.dylib");
593        #[cfg(target_family = "windows")]
594        let lib = lib_dir.child("foo.dll");
595
596        lib.touch().unwrap();
597
598        let mut config = ExternalDependencySearchConfig::default();
599        config.search_prefixes.push(prefix_dir.path().to_path_buf());
600
601        ExternalDependencyInfo::fallback_probe(
602            "foo",
603            &ExternalDependencySpec {
604                library: Some("foo".into()),
605                header: None,
606            },
607            &config,
608        )
609        .unwrap();
610    }
611
612    #[tokio::test]
613    async fn test_fallback_detect_not_found() {
614        let config = ExternalDependencySearchConfig::default();
615
616        let result = ExternalDependencyInfo::fallback_probe(
617            "foo",
618            &ExternalDependencySpec {
619                header: Some("foo.h".into()),
620                library: None,
621            },
622            &config,
623        );
624
625        assert!(matches!(
626            result,
627            Err(ExternalDependencyError::HeaderNotFound { .. })
628        ));
629    }
630
631    #[cfg(not(target_env = "msvc"))]
632    #[tokio::test]
633    async fn test_to_lib_name() {
634        assert_eq!(to_lib_name(&PathBuf::from("lua.a")), "lua".to_string());
635        assert_eq!(
636            to_lib_name(&PathBuf::from("lua-5.1.a")),
637            "lua-5.1".to_string()
638        );
639        assert_eq!(
640            to_lib_name(&PathBuf::from("lua5.1.a")),
641            "lua5.1".to_string()
642        );
643        assert_eq!(to_lib_name(&PathBuf::from("lua51.a")), "lua51".to_string());
644        assert_eq!(
645            to_lib_name(&PathBuf::from("luajit-5.2.a")),
646            "luajit-5.2".to_string()
647        );
648        assert_eq!(
649            to_lib_name(&PathBuf::from("lua-5.2.a")),
650            "lua-5.2".to_string()
651        );
652        assert_eq!(to_lib_name(&PathBuf::from("liblua.a")), "lua".to_string());
653        assert_eq!(
654            to_lib_name(&PathBuf::from("liblua-5.1.a")),
655            "lua-5.1".to_string()
656        );
657        assert_eq!(
658            to_lib_name(&PathBuf::from("liblua53.a")),
659            "lua53".to_string()
660        );
661        assert_eq!(
662            to_lib_name(&PathBuf::from("liblua-54.a")),
663            "lua-54".to_string()
664        );
665    }
666}