lux-lib 0.12.0

Library for the lux package manager for Lua
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
use crate::{
    config::Config,
    lua_installation::LuaInstallation,
    lua_rockspec::{DeploySpec, LuaModule, ModulePaths},
    tree::{RockLayout, Tree},
    variables::{self, Environment, VariableSubstitutionError},
};
use itertools::Itertools;
use mlua::{Lua, LuaSerdeExt};
use path_slash::PathExt;
use shlex::try_quote;
use std::{
    collections::HashMap,
    io,
    path::{Path, PathBuf},
    process::{ExitStatus, Output},
    string::FromUtf8Error,
};
use target_lexicon::Triple;
use thiserror::Error;

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

use super::external_dependency::ExternalDependencyInfo;

/// Copies a lua source file to a specific destination. The destination is described by a
/// `module.path` syntax (equivalent to the syntax provided to Lua's `require()` function).
pub(crate) fn copy_lua_to_module_path(
    source: &PathBuf,
    target_module: &LuaModule,
    target_dir: &Path,
) -> io::Result<()> {
    let target = target_dir.join(target_module.to_lua_path());

    std::fs::create_dir_all(target.parent().unwrap())?;

    std::fs::copy(source, target)?;

    Ok(())
}

/// Get the files that Lux treats as project files
/// This respects ignore files and excludes hidden files and directories.
pub(crate) fn project_files(src: &PathBuf) -> Vec<PathBuf> {
    ignore::WalkBuilder::new(src)
        .follow_links(false)
        .build()
        .filter_map(Result::ok)
        .filter(|entry| entry.file_type().is_some_and(|ft| ft.is_file()))
        .map(|entry| entry.into_path())
        .collect_vec()
}

/// Recursively copy a directory.
/// This respects ignore files and excludes hidden files and directories.
pub(crate) async fn recursive_copy_dir(src: &PathBuf, dest: &Path) -> Result<(), io::Error> {
    if src.exists() {
        for file in project_files(src) {
            let relative_src_path: PathBuf =
                pathdiff::diff_paths(src.join(&file), src).expect("failed to copy directories!");
            let target = dest.join(relative_src_path);
            if let Some(parent) = target.parent() {
                tokio::fs::create_dir_all(parent).await?;
            }
            tokio::fs::copy(&file, target).await?;
        }
    }
    Ok(())
}

#[derive(Error, Debug)]
pub enum OutputValidationError {
    #[error("compilation failed.\nstatus: {status}\nstdout: {stdout}\nstderr: {stderr}")]
    CommandFailure {
        status: ExitStatus,
        stdout: String,
        stderr: String,
    },
}

fn validate_output(output: Output) -> Result<(), OutputValidationError> {
    if !output.status.success() {
        return Err(OutputValidationError::CommandFailure {
            status: output.status,
            stdout: String::from_utf8_lossy(&output.stdout).into(),
            stderr: String::from_utf8_lossy(&output.stderr).into(),
        });
    }
    Ok(())
}

#[derive(Error, Debug)]
pub enum CompileCFilesError {
    #[error("IO operation while compiling C files: {0}")]
    Io(#[from] io::Error),
    #[error("failed to compile intermediates from C files: {0}")]
    CompileIntermediates(cc::Error),
    #[error("error compiling C files (compilation failed): {0}")]
    Compilation(#[from] cc::Error),
    #[error("error compiling C files (output validation failed): {0}")]
    OutputValidation(#[from] OutputValidationError),
    #[error("compiling C files succeeded, but the expected library {0} was not created")]
    LibOutputNotCreated(String),
}

/// Compiles a set of C files into a single dynamic library and places them under `{target_dir}/{target_file}`.
/// # Panics
/// Panics if no parent or no filename can be determined for the target path.
pub(crate) async fn compile_c_files(
    files: &Vec<PathBuf>,
    target_module: &LuaModule,
    target_dir: &Path,
    lua: &LuaInstallation,
    external_dependencies: &HashMap<String, ExternalDependencyInfo>,
    config: &Config,
) -> Result<(), CompileCFilesError> {
    let target = target_dir.join(target_module.to_lib_path());

    let parent = target.parent().expect("Couldn't determine parent");
    let file = target
        .file_name()
        .expect("Couldn't determine filename")
        .to_string_lossy()
        .to_string();

    std::fs::create_dir_all(parent)?;

    let host = Triple::host();

    // See https://github.com/rust-lang/cc-rs/issues/594#issuecomment-2110551057

    let mut build = cc::Build::new();
    let intermediate_dir = tempdir::TempDir::new(target_module.as_str())?;
    let build = build
        .cargo_output(false)
        .cargo_metadata(false)
        .cargo_warnings(false)
        .warnings(config.verbose())
        .files(files)
        .host(std::env::consts::OS)
        .includes(lua.includes())
        .includes(
            external_dependencies
                .iter()
                .filter_map(|(_, dep)| dep.include_dir.as_ref()),
        )
        .opt_level(3)
        .out_dir(intermediate_dir)
        .target(&host.to_string());

    let compiler = build.try_get_compiler()?;
    // Suppress all warnings
    if compiler.is_like_msvc() {
        build.flag("-W0");
    } else {
        build.flag("-w");
    }
    for arg in lua.define_flags() {
        build.flag(&arg);
    }

    let objects = build
        .try_compile_intermediates()
        .map_err(CompileCFilesError::CompileIntermediates)?;

    let output_path = parent.join(&file);

    let output = if compiler.is_like_msvc() {
        let def_temp_dir = tempdir::TempDir::new("msvc-def")?.into_path().to_path_buf();
        let def_file = mk_def_file(def_temp_dir, &file, target_module)?;
        let cmd = compiler.to_command();
        let mut cmd: tokio::process::Command = cmd.into();
        cmd.arg("/NOLOGO")
            .args(&objects)
            .arg("/LD")
            .arg("/link")
            .arg(format!("/DEF:{}", def_file.display()))
            .arg(format!("/OUT:{}", output_path.display()))
            .args(lua.lib_link_args(&compiler))
            .args(
                external_dependencies
                    .iter()
                    .flat_map(|(_, dep)| dep.lib_link_args(&compiler)),
            )
            .output()
            .await?
    } else {
        let cmd = build.shared_flag(true).try_get_compiler()?.to_command();
        let mut cmd: tokio::process::Command = cmd.into();
        cmd.args(vec!["-o".into(), output_path.to_string_lossy().to_string()])
            .args(lua.lib_link_args(&compiler))
            .args(
                external_dependencies
                    .iter()
                    .flat_map(|(_, dep)| dep.lib_link_args(&compiler)),
            )
            .args(&objects)
            .output()
            .await?
    };

    if config.verbose() {
        if !&output.stdout.is_empty() {
            println!("{}", String::from_utf8_lossy(&output.stdout));
        }
        if !&output.stderr.is_empty() {
            eprintln!("{}", String::from_utf8_lossy(&output.stderr));
        }
    }

    validate_output(output)?;

    if output_path.exists() {
        Ok(())
    } else {
        Err(CompileCFilesError::LibOutputNotCreated(
            output_path.to_slash_lossy().to_string(),
        ))
    }
}

/// On MSVC, we need to create Lua definitions manually
fn mk_def_file(
    dir: PathBuf,
    output_file_name: &str,
    target_module: &LuaModule,
) -> io::Result<PathBuf> {
    let mut def_file: PathBuf = dir.join(output_file_name);
    def_file.set_extension(".def");
    let exported_name = target_module.to_string().replace(".", "_");
    let exported_name = exported_name
        .split_once('-')
        .map(|(_, after_hyphen)| after_hyphen.to_string())
        .unwrap_or_else(|| exported_name.clone());
    let content = format!(
        r#"EXPORTS
luaopen_{}
"#,
        exported_name
    );
    std::fs::write(&def_file, content)?;
    Ok(def_file)
}

// TODO: (#261): special cases for mingw/cygwin?

/// the extension for C shared libraries.
pub(crate) fn c_dylib_extension() -> &'static str {
    if cfg!(target_env = "msvc") {
        "dll"
    } else {
        "so"
    }
}

/// the extension for C static libraries.
pub(crate) fn c_lib_extension() -> &'static str {
    if cfg!(target_env = "msvc") {
        "lib"
    } else {
        "a"
    }
}

/// the extension for C objects.
pub(crate) fn c_obj_extension() -> &'static str {
    if cfg!(target_env = "msvc") {
        "obj"
    } else {
        "o"
    }
}

pub(crate) fn default_cflags() -> &'static str {
    if cfg!(target_env = "msvc") {
        "/NOLOGO /MD /O2"
    } else {
        "-O2"
    }
}

pub(crate) fn default_libflag() -> &'static str {
    if cfg!(target_os = "macos") {
        "-bundle -undefined dynamic_lookup -all_load"
    } else if cfg!(target_env = "msvc") {
        "/NOLOGO /DLL"
    } else {
        "-shared"
    }
}

#[derive(Error, Debug)]
pub enum CompileCModulesError {
    #[error("IO operation while compiling C modules: {0}")]
    Io(#[from] io::Error),
    #[error("failed to compile intermediates from C modules: {0}")]
    CompileIntermediates(cc::Error),
    #[error("error compiling C modules (compilation failed): {0}")]
    Compilation(#[from] cc::Error),
    #[error("error compiling C modules (output validation failed): {0}")]
    OutputValidation(#[from] OutputValidationError),
    #[error("compiling C modules succeeded, but the expected library {0} was not created")]
    LibOutputNotCreated(String),
}

/// Compiles a set of C files (with extra metadata) to a given destination.
/// # Panics
/// Panics if no filename for the target path can be determined.
pub(crate) async fn compile_c_modules(
    data: &ModulePaths,
    source_dir: &Path,
    target_module: &LuaModule,
    target_dir: &Path,
    lua: &LuaInstallation,
    external_dependencies: &HashMap<String, ExternalDependencyInfo>,
    config: &Config,
) -> Result<(), CompileCModulesError> {
    let target = target_dir.join(target_module.to_lib_path());

    let parent = target.parent().expect("Couldn't determine parent");
    std::fs::create_dir_all(parent)?;

    let host = Triple::host();

    let mut build = cc::Build::new();
    let source_files = data
        .sources
        .iter()
        .map(|dir| source_dir.join(dir))
        .collect_vec();
    let include_dirs = data
        .incdirs
        .iter()
        .map(|dir| source_dir.join(dir))
        .chain(
            external_dependencies
                .iter()
                .filter_map(|(_, dep)| dep.include_dir.clone()),
        )
        .collect_vec();

    let intermediate_dir = tempdir::TempDir::new(target_module.as_str())?;
    let build = build
        .cargo_output(false)
        .cargo_metadata(false)
        .cargo_warnings(false)
        .warnings(config.verbose())
        .files(source_files)
        .host(std::env::consts::OS)
        .includes(&include_dirs)
        .includes(lua.includes())
        .includes(
            external_dependencies
                .iter()
                .filter_map(|(_, dep)| dep.include_dir.as_ref()),
        )
        .opt_level(3)
        .out_dir(intermediate_dir)
        .target(&host.to_string());

    let compiler = build.try_get_compiler()?;
    let is_msvc = compiler.is_like_msvc();
    // Suppress all warnings
    if is_msvc {
        build.flag("-W0");
    } else {
        build.flag("-w");
    }
    for arg in lua.define_flags() {
        build.flag(&arg);
    }

    // `cc::Build` has no `defines()` function, so we manually feed in the
    // definitions in a verbose loop
    for (name, value) in &data.defines {
        build.define(name, value.as_deref());
    }

    let file = target
        .file_name()
        .expect("Couldn't determine filename")
        .to_string_lossy()
        .to_string();
    // See https://github.com/rust-lang/cc-rs/issues/594#issuecomment-2110551057
    let objects = build
        .try_compile_intermediates()
        .map_err(CompileCModulesError::CompileIntermediates)?;

    let libdir_args = data.libdirs.iter().map(|libdir| {
        if is_msvc {
            format!("/LIBPATH:{}", source_dir.join(libdir).display())
        } else {
            format!("-L{}", source_dir.join(libdir).display())
        }
    });

    let library_args = data.libraries.iter().map(|library| {
        if is_msvc {
            format!("{}.lib", library.to_str().unwrap())
        } else {
            format!("-l{}", library.to_str().unwrap())
        }
    });

    let output_path = parent.join(&file);
    let output = if is_msvc {
        let def_temp_dir = tempdir::TempDir::new("msvc-def")?.into_path().to_path_buf();
        let def_file = mk_def_file(def_temp_dir, &file, target_module)?;
        let cmd = build.try_get_compiler()?.to_command();
        let mut cmd: tokio::process::Command = cmd.into();
        cmd.arg("/NOLOGO")
            .args(&objects)
            .arg("/LD")
            .arg("/link")
            .arg(format!("/DEF:{}", def_file.display()))
            .arg(format!("/OUT:{}", output_path.display()))
            .args(lua.lib_link_args(&build.try_get_compiler()?))
            .args(
                external_dependencies
                    .iter()
                    .flat_map(|(_, dep)| dep.lib_link_args(&compiler)),
            )
            .args(libdir_args)
            .args(library_args)
            .output()
            .await?
    } else {
        let cmd = build.shared_flag(true).try_get_compiler()?.to_command();
        let mut cmd: tokio::process::Command = cmd.into();
        cmd.args(vec!["-o".into(), output_path.to_string_lossy().to_string()])
            .args(lua.lib_link_args(&build.try_get_compiler()?))
            .args(
                external_dependencies
                    .iter()
                    .flat_map(|(_, dep)| dep.lib_link_args(&compiler)),
            )
            .args(&objects)
            .args(libdir_args)
            .args(library_args)
            .output()
            .await?
    };

    if config.verbose() {
        if !&output.stdout.is_empty() {
            println!("{}", String::from_utf8_lossy(&output.stdout));
        }
        if !&output.stderr.is_empty() {
            eprintln!("{}", String::from_utf8_lossy(&output.stderr));
        }
    }

    validate_output(output)?;

    if output_path.exists() {
        Ok(())
    } else {
        Err(CompileCModulesError::LibOutputNotCreated(
            output_path.to_slash_lossy().to_string(),
        ))
    }
}

#[derive(Debug, Error)]
pub enum InstallBinaryError {
    #[error(transparent)]
    Io(#[from] io::Error),
    #[error("error wrapping binary: {0}")]
    Wrap(#[from] WrapBinaryError),
}

#[derive(Debug, Error)]
pub enum WrapBinaryError {
    #[error(transparent)]
    Io(#[from] io::Error),
    #[error(transparent)]
    Utf8(#[from] FromUtf8Error),
    #[error("no `lua` executable found")]
    NoLuaBinary,
}

/// Returns the file path of the installed binary
pub(crate) async fn install_binary(
    source: &Path,
    target: &str,
    tree: &Tree,
    lua: &LuaInstallation,
    deploy: &DeploySpec,
    config: &Config,
) -> Result<PathBuf, InstallBinaryError> {
    tokio::fs::create_dir_all(&tree.bin()).await?;
    let script = if deploy.wrap_bin_scripts && is_compatible_lua_script(source, lua, config).await {
        install_wrapped_binary(source, target, tree, lua, config).await?
    } else {
        let target = tree.bin().join(target);
        tokio::fs::copy(source, &target).await?;
        target
    };

    #[cfg(unix)]
    set_executable_permissions(&script).await?;

    Ok(script)
}

async fn install_wrapped_binary(
    source: &Path,
    target: &str,
    tree: &Tree,
    lua: &LuaInstallation,
    config: &Config,
) -> Result<PathBuf, WrapBinaryError> {
    let unwrapped_bin_dir = tree.unwrapped_bin();
    tokio::fs::create_dir_all(&unwrapped_bin_dir).await?;
    let unwrapped_bin = unwrapped_bin_dir.join(target);
    tokio::fs::copy(source, &unwrapped_bin).await?;

    #[cfg(target_family = "unix")]
    let target = tree.bin().join(target);
    #[cfg(target_family = "windows")]
    let target = tree.bin().join(format!("{}.bat", target));

    let lua_bin = lua
        .lua_binary_or_config_override(config)
        .ok_or(WrapBinaryError::NoLuaBinary)?;

    #[cfg(target_family = "unix")]
    let content = format!(
        r#"#!/bin/sh

exec {0} "{1}" "$@"
"#,
        lua_bin,
        unwrapped_bin.display(),
    );
    #[cfg(target_family = "windows")]
    let content = format!(
        r#"@echo off
setlocal

{0} "{1}" %*

exit /b %ERRORLEVEL%
"#,
        lua_bin,
        unwrapped_bin.display(),
    );

    tokio::fs::write(&target, content).await?;
    Ok(target)
}

#[cfg(unix)]
async fn set_executable_permissions(script: &Path) -> std::io::Result<()> {
    let mut perms = tokio::fs::metadata(&script).await?.permissions();
    perms.set_mode(0o744);
    tokio::fs::set_permissions(&script, perms).await?;
    Ok(())
}

/// Tries to load the file with Lua. If the file can be loaded,
/// we treat it as a valid Lua script.
///
/// NOTE: Lua may successfully load very short bash scripts (like `echo "Hello"`).
/// But that's unlikely to be the case in practise.
/// If a script is mistaken for a Lua script, package authors can disable
/// wrapping with the `deploy.wrap_bin_scripts` rockspec config.
async fn is_compatible_lua_script(
    file: &Path,
    lua_installation: &LuaInstallation,
    config: &Config,
) -> bool {
    lua_installation
        .lua_binary_or_config_override(config)
        .is_some_and(|_| {
            let lua = Lua::new();
            lua.load(format!(
                "is_compatible_lua_script = loadfile('{}') ~= nil",
                file.to_slash_lossy()
            ))
            .exec()
            .is_ok_and(|()| {
                lua.globals()
                    .get("is_compatible_lua_script")
                    .is_ok_and(|value| {
                        lua.from_value(value)
                            .is_ok_and(|is_compatible| is_compatible)
                    })
            })
        })
}

pub(crate) fn substitute_variables(
    input: &str,
    output_paths: &RockLayout,
    lua: &LuaInstallation,
    external_dependencies: &HashMap<String, ExternalDependencyInfo>,
    config: &Config,
) -> Result<String, VariableSubstitutionError> {
    variables::substitute(
        &[
            output_paths,
            lua,
            external_dependencies,
            &Environment {},
            config,
        ],
        input,
    )
}

pub(crate) fn format_path(path: &Path) -> String {
    let path_str = path.to_slash_lossy();
    if cfg!(windows) {
        path_str.to_string()
    } else {
        try_quote(&path_str)
            .map(|str| str.to_string())
            .unwrap_or(format!("'{}'", path_str))
    }
}

#[cfg(test)]
mod tests {
    use tokio::process::Command;

    use crate::config::ConfigBuilder;

    use super::*;

    #[tokio::test]
    async fn test_is_compatible_lua_script() {
        let config = ConfigBuilder::new().unwrap().build().unwrap();
        let lua_version = config.lua_version().unwrap();
        let lua = LuaInstallation::new(lua_version, &config).await.unwrap();
        let valid_script = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("resources/test/sample_lua_bin_script_valid");
        assert!(is_compatible_lua_script(&valid_script, &lua, &config).await);
        let invalid_script = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("resources/test/sample_lua_bin_script_invalid");
        assert!(!is_compatible_lua_script(&invalid_script, &lua, &config).await);
    }

    #[tokio::test]
    async fn test_install_wrapped_binary() {
        let temp = assert_fs::TempDir::new().unwrap();
        let config = ConfigBuilder::new()
            .unwrap()
            .user_tree(Some(temp.to_path_buf()))
            .build()
            .unwrap();
        let lua_version = config.lua_version().unwrap();
        let lua = LuaInstallation::new(lua_version, &config).await.unwrap();
        let tree = config.user_tree(lua_version.clone()).unwrap();
        let valid_script = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("resources/test/sample_lua_bin_script_valid");
        let script_name = "test_script";
        let script_path = install_wrapped_binary(&valid_script, script_name, &tree, &lua, &config)
            .await
            .unwrap();

        #[cfg(unix)]
        set_executable_permissions(&script_path).await.unwrap();

        assert!(Command::new(script_path.to_string_lossy().to_string())
            .status()
            .await
            .is_ok_and(|status| status.success()));
    }
}