Skip to content

Commit

Permalink
Use a Rust binary to expose Ribasim CLI
Browse files Browse the repository at this point in the history
- libribasim and ribasim_cli can share the same libraries
- we get `ribasim.exe` instead of `ribasim.cmd`
  • Loading branch information
Hofer-Julian committed Apr 22, 2024
1 parent d861737 commit 0182ae0
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 27 deletions.
7 changes: 7 additions & 0 deletions build/cli_wrapper/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions build/cli_wrapper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "ribasim"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
29 changes: 29 additions & 0 deletions build/cli_wrapper/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::env;

fn main() {
// Get the path to the directory containing the current executable
let exe_dir = env::current_exe().unwrap().parent().unwrap().to_owned();

// Set the appropriate environment variable for the current platform
if std::env::consts::OS == "windows" {
env::set_var(
"PATH",
format!(
"{};{}",
exe_dir.join("bin").display(),
env::var("PATH").unwrap_or_default()
),
);
}

// TODO: Do I need to set LD_LIBRARY_PATH on linux?

// Get the command line arguments
let args: Vec<String> = std::env::args().skip(1).collect();

// Parse command line arguments
todo!();

// Call ribasim shared library and check for errors
todo!()
}
45 changes: 18 additions & 27 deletions build/src/create_app.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,22 @@ function build_app()
output_dir = "ribasim_cli"
git_repo = ".."

create_app(
project_dir,
output_dir;
# map from binary name to julia function name
executables = ["ribasim" => "main"],
precompile_execution_file = "precompile.jl",
include_lazy_artifacts = false,
include_transitive_dependencies = false,
include_preferences = true,
force = true,
)

readme = @doc(build_app)
add_metadata(project_dir, license_file, output_dir, git_repo, readme)

# On Windows, write ribasim.cmd in the output_dir, that starts ribasim.exe.
# Since the bin dir contains a julia.exe and many DLLs that you may not want in your path,
# with this script you can put output_dir in your path instead.
if Sys.iswindows()
cmd = raw"""
@echo off
"%~dp0bin\ribasim.exe" %*
"""
open(normpath(output_dir, "ribasim.cmd"); write = true) do io
print(io, cmd)
end
end
# create_app(
# project_dir,
# output_dir;
# # map from binary name to julia function name
# executables = ["ribasim" => "main"],
# precompile_execution_file = "precompile.jl",
# include_lazy_artifacts = false,
# include_transitive_dependencies = false,
# include_preferences = true,
# force = true,
# )

# readme = @doc(build_app)
# add_metadata(project_dir, license_file, output_dir, git_repo, readme)

run(Cmd(`cargo build --release`; dir = "cli_wrapper"))
ribasim = Sys.iswindows() ? "ribasim.exe" : "ribasim"
cp("cli_wrapper/target/release/$ribasim", "libribasim/$ribasim"; force = true)
end
Loading

0 comments on commit 0182ae0

Please sign in to comment.