Skip to content

Commit

Permalink
Fix path issues on Windows, misc cleanup
Browse files Browse the repository at this point in the history
The main binary needs the location of the main `wasmedge.dll`
to be on the system PATH, otherwise it fails to load.

Still having some trouble with MAKEPAD_PACKAGE_DIR, that is,
Makepad cannot properly find the resources bundled with the app.
  • Loading branch information
kevinaboos committed Jul 26, 2024
1 parent 875a81b commit ca0d403
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@ chmod +x moxin_0.1.0_x86_64.AppImage
### Packaging for Windows
This can only be run on an actual Windows machine, due to platform restrictions.

Ensure you are in the root `moxin` directory, and then you can use `cargo packager` to generate all three package types at once:
Ensure you are in the root `moxin` directory, and then you can use `cargo packager` to generate a `setup.exe` file using NSIS:
```sh
cargo packager --release --verbose ## --verbose is optional
WASMEDGE_DIR=path/to/WasmEdge-0.14.0-Windows cargo packager --release --formats nsis --verbose ## --verbose is optional
```

After the command completes, you should see a Windows installer called `moxin_0.1.0_x64-setup` in the `dist/` directory.
Double-click that file to install Moxin on your machine, and then run it as you would a regular application.


### Packaging for macOS
This can only be run on an actual macOS machine, due to platform restrictions.

Expand Down
30 changes: 27 additions & 3 deletions moxin-runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn main() -> std::io::Result<()> {
std::env::var(ENV_WASMEDGE_PLUGIN_PATH).ok()
);

run_moxin().unwrap();
run_moxin(None).unwrap();
Ok(())
}

Expand Down Expand Up @@ -205,7 +205,7 @@ fn main() -> std::io::Result<()> {

apply_env_vars(&wasmedge_root_dir_in_use);

run_moxin()
run_moxin(main_dylib_path.parent())
}


Expand Down Expand Up @@ -428,7 +428,10 @@ fn wasmedge_root_dir_from_env_vars() -> Option<PathBuf> {
}

/// Runs the `_moxin_app` binary, which must be located in the same directory as this moxin-runner binary.
fn run_moxin() -> std::io::Result<()> {
///
/// An optional path to the directory containing the main WasmEdge dylib can be provided,
/// which is currently only used to set the path on Windows.
fn run_moxin(_main_wasmedge_dylib_dir: Option<&Path>) -> std::io::Result<()> {
let current_exe = std::env::current_exe()?;
let current_exe_dir = current_exe.parent().unwrap();
let args = std::env::args().collect::<Vec<_>>();
Expand All @@ -440,6 +443,27 @@ fn run_moxin() -> std::io::Result<()> {
args,
);

// On Windows, the MOXIN_APP_BINARY needs to be able to find the WASMEDGE_MAIN_DYLIB (wasmedge.dll),
// so we prepend it to the PATH.
#[cfg(windows)] {
match (std::env::var_os(ENV_PATH), _main_wasmedge_dylib_dir) {
(Some(path), Some(dylib_parent)) => {
println!("Old path: {:?}", path.to_string_lossy());
println!("Prepending \"{}\" to Windows PATH", dylib_parent.display());
let new_path = std::env::join_paths(
Some(dylib_parent.to_path_buf())
.into_iter()
.chain(std::env::split_paths(&path))
)
.expect("BUG: failed to join paths for the main Moxin binary.");
std::env::set_var(ENV_PATH, &new_path);
println!("New path: {:?}", std::env::var(ENV_PATH));

}
_ => eprintln!("BUG: failed to set PATH for the main Moxin binary."),
}
}

let _output = Command::new(current_exe_dir.join(MOXIN_APP_BINARY))
.current_dir(current_exe_dir)
.args(args.into_iter().skip(1)) // skip the first arg (the binary name)
Expand Down
19 changes: 18 additions & 1 deletion packaging/before-packaging-command/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,29 @@ use cargo_metadata::MetadataCommand;

/// Returns the value of the `MAKEPAD_PACKAGE_DIR` environment variable
/// that must be set for the given package format.
///
/// * For macOS app bundles, this should be set to `../Resources`.
/// * This only works because the `moxin-runner` binary sets the current working directory
/// to the directory where the binary is located, which is `Moxin.app/Contents/MacOS/`.
/// (See the `run_moxin` function in `moxin-runner/src/main.rs` for more details.)
/// * In a macOS app bundle, the resources directory is in `Moxin.app/Context/Resources/`,
/// so that's why we set `MAKEPAD_PACKAGE_DIR` to `../Resources`.
/// This must be relative to the binary's location, i.e. up one parent directory.
/// * For AppImage packages, this should be set to a relative path that goes up two parent directories
/// to account for the fact that AppImage binaries run in a simulated `usr/bin/` directory.
/// * Thus, we need to get to the simulated `usr/lib/` directory for Moxin's resources,
/// which currently works out to `../../usr/lib/moxin`.
/// * Note that this must be a relative path, not an absolute path.
/// * For Debian `.deb` packages, this should be set to `/usr/lib/<main-binary-name>`,
/// which is currently `/usr/lib/moxin-runner`.
/// * This is the directory in which `dpkg` copies app resource files to
/// when a user installs the `.deb` package.
fn makepad_package_dir_value(package_format: &str) -> &'static str {
match package_format {
"app" | "dmg" => "../Resources",
"appimage" => "../../usr/lib/moxin",
"deb" | "pacman" => "/usr/share/moxin",
"wix" | "nsis" => "C:\\Program Files\\Moxin",
"nsis" => ".\\",
_other => panic!("Unsupported package format: {}", _other),
}
}
Expand Down

0 comments on commit ca0d403

Please sign in to comment.