robius-packaging-commands 0.2.1

A multi-platform companion tool to help package your Rust app when using `cargo-packager`. This should be invoked by cargo-packager's "before-package" and "before-each-package" hooks, which you specify in your `Cargo.toml` file under the `[package.metadata.packager]` section.
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
//! This program should be invoked by `cargo-packager` during its before-packaging steps.
//!
//! This program uses the current working directory as the app root
//! (for ./resources and ./dist). The --path-to-binary argument is used
//! to locate the target directory (useful in workspaces).
//!
//! ## Modes and CLI Arguments
//! This program runs in two modes, one for each kind of before-packaging step in cargo-packager.
//! It requires passing in three arguments:
//! 1. An operating mode, either `before-packaging` or `before-each-packager`.
//!    * Passing in `before-packaging` specifies that the `before-packaging-command` is being run
//!      by cargo-packager, which gets executed only *once* before cargo-packager generates any package bundles.
//!    * Passing in `before-each-package` specifies that the `before-each-package-command` is being run
//!      by cargo-packager, which gets executed multiple times: once for *each* package that
//!      cargo-packager is going to generate.
//!       * The environment variable `CARGO_PACKAGER_FORMAT` is set by cargo-packager to
//!         the declare which package format is about to be generated, which include the values
//!         given here: <https://docs.rs/cargo-packager/latest/cargo_packager/enum.PackageFormat.html>.
//!         * `app`, `dmg`: for macOS.
//!         * `deb`, `appimage`, `pacman`: for Linux.
//!         * `nsis`: for Windows; `nsis` generates an installer `setup.exe`.
//!         * `wix`: (UNSUPPORTED) for Windows; generates an `.msi` installer package.
//! 2. The `--binary-name` argument, which specifies the name of the main binary that cargo-packager
//!    is going to package. This is the name of the binary that is generated by your app's main crate.
//! 3. The `--path-to-binary` argument, which specifies the path to the main binary that cargo-packager
//!    is going to package. This is the path to the binary that is generated by cargo when compiling
//!    your app's main crate.
//!
//! The following optional arguments can be passed in:
//! * `--force-makepad`/`--force-no-makepad`: Forces the program to treat the app as a Makepad app
//!   or not a Makepad app, respectively. This means that Makepad-specific resource files must be copied in
//!   (and must succeed), or will not be copied in, respectively, and that
//!   Makepad-specific build configs will be enabled or disabled, respectively.
//!   * If neither `--force-makepad` nor `--force-no-makepad` is set, then the program will
//!     automatically detect whether the app is a Makepad app by checking if the `makepad-widgets` crate
//!     is a dependency of the app's main crate.
//! * `--host_os=<os>`: Overrides the host OS that is detected by the program. This is useful for testing
//!   the program on a different OS than the one that cargo-packager is running on.
//!
//! This program uses the `CARGO_PACKAGER_FORMAT` environment variable to determine
//! which specific build commands and configuration options should be used.
//!

pub mod makepad;
use makepad::*;

use core::panic;
use std::{ffi::OsStr, fs, path::{Path, PathBuf}, process::{Command, Stdio}};

const EMPTY_ARGS: std::iter::Empty<&str> = std::iter::empty::<&str>();
const EMPTY_ENVS: std::iter::Empty<(&str, &str)> = std::iter::empty::<(&str, &str)>();


fn resolve_target_dir(path_to_binary: &Path, cwd: &Path) -> PathBuf {
    let abs_path = if path_to_binary.is_absolute() {
        path_to_binary.to_path_buf()
    } else {
        cwd.join(path_to_binary)
    };
    abs_path
        .parent()
        .map(|p| p.to_path_buf())
        .unwrap_or_else(|| cwd.to_path_buf())
}


fn main() -> std::io::Result<()> {
    let mut is_before_packaging = false;
    let mut is_before_each_package = false;
    let mut main_binary_name = None;
    let mut path_to_binary = None;
    let mut host_os_opt: Option<String> = None;

    let mut args = std::env::args().peekable();
    while let Some(arg) = args.next() {
        if arg.ends_with("before-packaging") || arg.ends_with("before_packaging") {
            is_before_packaging = true;
        }
        else if arg.contains("before-each") || arg.contains("before_each") {
            is_before_each_package = true;
        }
        else if arg == "--binary-name" {
            main_binary_name = Some(args.next().expect("Expected a binary name after '--binary-name'."));
        }
        else if arg == "--path-to-binary" {
            let path = PathBuf::from(args.next().expect("Expected a path after '--path-to-binary'."));
            path_to_binary = Some(path);
        }
        else if arg == "--force-makepad" {
            FORCE_MAKEPAD.set(true)
                .and(IS_MAKEPAD_APP.set(true))
                .expect("only --force-makepad OR --force-no-makepad can be set.");
        }
        else if arg == "--force-no-makepad" {
            FORCE_MAKEPAD.set(false)
                .and(IS_MAKEPAD_APP.set(false))
                .expect("only --force-makepad OR --force-no-makepad can be set.");
        }
        else if host_os_opt.is_none() && (arg.contains("host_os") || arg.contains("host-os")) {
            host_os_opt = arg
                .split("=")
                .last()
                .map(|s| s.to_string())
                .or_else(|| args.peek().map(|s| s.to_string()));
        }
    }

    let main_binary_name = main_binary_name.expect("Missing required argument '--binary-name'");
    let path_to_binary = path_to_binary.expect("Missing required argument '--path-to-binary'");
    let host_os = host_os_opt.as_deref().unwrap_or(std::env::consts::OS);

    match (is_before_packaging, is_before_each_package) {
        (true, false) => before_packaging(host_os, &main_binary_name),
        (false, true) => before_each_package(host_os, &main_binary_name, &path_to_binary),
        (true, true) => panic!("Cannot run both 'before-packaging' and 'before-each-package' commands at the same time."),
        (false, false) => panic!("Please specify either the 'before-packaging' or 'before-each-package' command."),
    }
}

/// This function is run only *once* before cargo-packager generates any package bundles.
///
/// In fact, the current Makepad project doesn't need to use this function,
/// and we've kept all the infrastructure for the before-packaging command (in case we ever need it in the future).
fn before_packaging(_host_os: &str, _main_binary_name: &str) -> std::io::Result<()> {
    Ok(())
}


/// Copy files from source to destination recursively.
fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> std::io::Result<()> {
    fs::create_dir_all(&destination)?;
    for entry in fs::read_dir(source)? {
        let entry = entry?;
        let filetype = entry.file_type()?;
        if filetype.is_dir() {
            copy_recursively(entry.path(), destination.as_ref().join(entry.file_name()))?;
        } else {
            fs::copy(entry.path(), destination.as_ref().join(entry.file_name()))?;
        }
    }
    Ok(())
}


/// The function that is run by cargo-packager's `before-each-package-command`.
///
/// It's just a simple wrapper that invokes the function for each specific package format.
fn before_each_package<P: AsRef<Path>>(
    host_os: &str,
    main_binary_name: &str,
    path_to_binary: P,
) -> std::io::Result<()> {
    // The `CARGO_PACKAGER_FORMAT` environment variable is required.
    let format = std::env::var("CARGO_PACKAGER_FORMAT")
        .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;

    let package_format = format.as_str();
    println!("Running before-each-package-command for {package_format:?}");

    // First run compilation - only copy resources if compilation succeeds
    match package_format {
        "app" | "dmg" => before_each_package_macos(   package_format, host_os, &main_binary_name, &path_to_binary)?,
        "deb"         => before_each_package_deb(     package_format, host_os, &main_binary_name, &path_to_binary)?,
        "appimage"    => before_each_package_appimage(package_format, host_os, &main_binary_name, &path_to_binary)?,
        "pacman"      => before_each_package_pacman(  package_format, host_os, &main_binary_name, &path_to_binary)?,
        "nsis"        => before_each_package_windows( package_format, host_os, &main_binary_name, &path_to_binary)?,
        _other => return Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            format!("Unknown/unsupported package format {_other:?}"),
        )),
    };

    let cwd = std::env::current_dir()?;
    let target_dir = resolve_target_dir(path_to_binary.as_ref(), &cwd);
    let dist_resources_dir = cwd.join("dist").join("resources");

    // Clear/delete existing resources directory to ensure no stale files
    if dist_resources_dir.exists() {
        fs::remove_dir_all(&dist_resources_dir)?;
    }
    fs::create_dir_all(&dist_resources_dir)?;

    {
        let app_resources_dest = dist_resources_dir.join(main_binary_name).join("resources");
        let app_resources_src = cwd.join("resources");
        println!("Copying app-specific resources...\n  --> From: {}\n      to:   {}", app_resources_src.display(), app_resources_dest.display());
        copy_recursively(&app_resources_src, &app_resources_dest)?;
    }

    // If this is a Makepad app, copy Makepad-specific resources
    let should_copy_makepad_resources = match FORCE_MAKEPAD.get() {
        Some(forced) => *forced,
        None => is_makepad_app(),
    };
    if should_copy_makepad_resources {
        copy_makepad_resources(&dist_resources_dir, &target_dir)?;
    }
    println!("All resources copied successfully to: {}", dist_resources_dir.display());
    println!("  --> Done!");
    Ok(())
}


/// Runs the macOS-specific build commands for "app" and "dmg" package formats.
///
/// This function effectively runs the following shell commands:
/// ```sh
///    MAKEPAD_PACKAGE_DIR=../Resources  cargo build --workspace --release \
///    && install_name_tool -add_rpath "@executable_path/../Frameworks" ./target/release/_moly_app;
/// ```
fn before_each_package_macos<P: AsRef<Path>>(
    package_format: &str,
    host_os: &str,
    main_binary_name: &str,
    path_to_binary: P,
) -> std::io::Result<()> {
    assert!(host_os == "macos", "'app' and 'dmg' packages can only be created on macOS.");

    let extra_envs = if is_makepad_app() {
        Some(("MAKEPAD", "apple_bundle")).into_iter()
    } else {
        None.into_iter()
    };

    cargo_build(
        package_format,
        host_os,
        main_binary_name,
        EMPTY_ARGS,
        extra_envs,
    )?;

    // Use `install_name_tool` to add the `@executable_path` rpath to the binary.
    let install_name_tool_cmd = Command::new("install_name_tool")
        .arg("-add_rpath")
        .arg("@executable_path/../Frameworks")
        .arg(path_to_binary.as_ref())
        .spawn()?;

    let output = install_name_tool_cmd.wait_with_output()?;
    if !output.status.success() {
        eprintln!("Failed to run install_name_tool command: {}
            ------------------------- stderr: -------------------------
            {:?}",
            output.status,
            String::from_utf8_lossy(&output.stderr),
        );
        return Err(std::io::Error::new(std::io::ErrorKind::Other, "Failed to run install_name_tool command for macOS"));
    }

    Ok(())
}

/// Runs the Linux-specific build commands for AppImage packages.
fn before_each_package_appimage<P: AsRef<Path>>(
    package_format: &str,
    host_os: &str,
    main_binary_name: &str,
    path_to_binary: P,
) -> std::io::Result<()> {
    assert!(host_os == "linux", "AppImage packages can only be created on Linux.");

    cargo_build(
        package_format,
        host_os,
        main_binary_name,
        EMPTY_ARGS,
        EMPTY_ENVS,
    )?;

    strip_unneeded_linux_binaries(host_os, path_to_binary)?;
    Ok(())
}


/// Runs the Linux-specific build commands for Debian `.deb` packages.
///
/// This function effectively runs the following shell commands:
/// ```sh
///    for path in $(ldd target/release/_moly_app | awk '{print $3}'); do \
///        basename "$/path" ; \
///    done \
///    | xargs dpkg -S 2> /dev/null | awk '{print $1}' | awk -F ':' '{print $1}' | sort | uniq > ./dist/depends_deb.txt; \
///    echo "curl" >> ./dist/depends_deb.txt; \
///    
fn before_each_package_deb<P: AsRef<Path>>(
    package_format: &str,
    host_os: &str,
    main_binary_name: &str,
    path_to_binary: P,
) -> std::io::Result<()> {
    assert!(host_os == "linux", "'deb' packages can only be created on Linux.");

    cargo_build(
        package_format,
        host_os,
        main_binary_name,
        EMPTY_ARGS,
        EMPTY_ENVS,
    )?;


    // Create Debian dependencies file by running `ldd` on the binary
    // and then running `dpkg -S` on each unique shared libraries outputted by `ldd`.
    let ldd_output = Command::new("ldd")
        .arg(path_to_binary.as_ref())
        .output()?;

    let ldd_output = if ldd_output.status.success() {
        String::from_utf8_lossy(&ldd_output.stdout)
    } else {
        eprintln!("Failed to run ldd command: {}
            ------------------------- stderr: -------------------------
            {:?}",
            ldd_output.status,
            String::from_utf8_lossy(&ldd_output.stderr),
        );
        return Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            format!("Failed to run ldd command on {host_os} for package format {package_format:?}")
        ));
    };

    let mut dpkgs = Vec::new();
    for line_raw in ldd_output.lines() {
        let line = line_raw.trim();
        let lib_name_opt = line.split_whitespace()
            .next()
            .and_then(|path| Path::new(path)
                .file_name()
                .and_then(|f| f.to_str().to_owned())
            );
        let Some(lib_name) = lib_name_opt else { continue };

        let dpkg_output = Command::new("dpkg")
            .arg("-S")
            .arg(lib_name)
            .stderr(Stdio::null())
            .output()?;
        let dpkg_output = if dpkg_output.status.success() {
            String::from_utf8_lossy(&dpkg_output.stdout)
        } else {
            // Skip shared libraries that dpkg doesn't know about, e.g., `linux-vdso.so*`
            continue;
        };

        let Some(package_name) = dpkg_output.split(':').next() else { continue };
        println!("Got dpkg dependency {package_name:?} from ldd output: {line:?}");
        dpkgs.push(package_name.to_string());
    }
    dpkgs.sort();
    dpkgs.dedup();
    println!("Sorted and de-duplicated dependencies: {:#?}", dpkgs);
    std::fs::write("./dist/depends_deb.txt", dpkgs.join("\n"))?;
    
    strip_unneeded_linux_binaries(host_os, path_to_binary)?;
    Ok(())
}


/// Runs the Linux-specific build commands for PacMan packages.
///
/// This is untested and may be incomplete, e.g., dependencies are not determined.
fn before_each_package_pacman<P: AsRef<Path>>(
    package_format: &str,
    host_os: &str,
    main_binary_name: &str,
    path_to_binary: P,
) -> std::io::Result<()> {
    assert!(host_os == "linux", "Pacman packages can only be created on Linux.");

    cargo_build(
        package_format,
        host_os,
        main_binary_name,
        EMPTY_ARGS,
        EMPTY_ENVS,
    )?;

    strip_unneeded_linux_binaries(host_os, path_to_binary)?;
    Ok(())
}
    
/// Runs the Windows-specific build commands for WiX (`.msi`) and NSIS (`.exe`) packages.
fn before_each_package_windows<P: AsRef<Path>>(
    package_format: &str,
    host_os: &str,
    main_binary_name: &str,
    _path_to_binary: P,
) -> std::io::Result<()> {
    assert!(host_os == "windows", "'.exe' and '.msi' packages can only be created on Windows.");

    cargo_build(
        package_format,
        host_os,
        main_binary_name,
        EMPTY_ARGS,
        EMPTY_ENVS,
    )?;

    Ok(())
}

fn cargo_build<I, A, E, K, V>(
    package_format: &str,
    _host_os: &str,
    _main_binary_name: &str,
    extra_args: I,
    extra_envs: E,
) -> std::io::Result<()>
where
    I: IntoIterator<Item = A>,
    A: AsRef<OsStr>,
    E: IntoIterator<Item = (K, V)>,
    K: AsRef<OsStr>,
    V: AsRef<OsStr>,
{
    let mut cargo_build_cmd = Command::new("cargo");
    cargo_build_cmd
        .arg("build")
        .arg("--workspace")
        .arg("--release")
        .args(extra_args)
        .envs(extra_envs);

    if is_makepad_app() {
        cargo_build_cmd.env(
            "MAKEPAD_PACKAGE_DIR",
            &makepad_package_dir_value(package_format, _main_binary_name),
        );
    }

    let output = cargo_build_cmd
        .spawn()?
        .wait_with_output()?;
    if !output.status.success() {
        eprintln!("Failed to run cargo build command: {}
            ------------------------- stderr: -------------------------
            {:?}",
            output.status,
            String::from_utf8_lossy(&output.stderr),
        );
        return Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            format!("Failed to run cargo build command on {_host_os} for package format {package_format:?}")
        ));
    }

    Ok(())
}

/// Strips unneeded symbols from the Linux binary, which is required for Debian `.deb` packages
/// and recommended for all other Linux package formats.
fn strip_unneeded_linux_binaries<P: AsRef<Path>>(host_os: &str, path_to_binary: P) -> std::io::Result<()> {
    assert!(host_os == "linux", "'strip --strip-unneeded' can only be run on Linux.");
    let strip_cmd = Command::new("strip")
        .arg("--strip-unneeded")
        .arg("--remove-section=.comment")
        .arg("--remove-section=.note")
        .arg(path_to_binary.as_ref())
        .spawn()?;

    let output = strip_cmd.wait_with_output()?;
    if !output.status.success() {
        eprintln!("Failed to run strip command: {}
            ------------------------- stderr: -------------------------
            {:?}",
            output.status,
            String::from_utf8_lossy(&output.stderr),
        );
        return Err(std::io::Error::new(std::io::ErrorKind::Other, "Failed to run strip command for Linux"));
    }

    Ok(())
}