Skip to content

Commit

Permalink
Continue
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofer-Julian committed Apr 23, 2024
1 parent 0182ae0 commit 8e68ce7
Show file tree
Hide file tree
Showing 11 changed files with 317 additions and 158 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ docs/build/
docs/site/

/generated_testmodels
build/ribasim_cli/
build/libribasim/
build/ribasim/
build/cli_wrapper/target

JuliaSysimage.dll
LocalPreferences.toml
Expand Down
71 changes: 59 additions & 12 deletions build/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,71 @@ using TOML
using LibGit2

include("src/add_metadata.jl")
include("src/create_app.jl")
include("src/create_lib.jl")

"""
Build the Ribasim CLI, libribasim, or both, using PackageCompiler.
Run from the command line with:
# Ribasim CLI
julia --project build.jl --app --lib
In order to find out about it's usage call `ribasim --help`
# Libribasim
Libribasim is a shared library that exposes Ribasim functionality to external (non-Julian)
programs. It can be compiled using [PackageCompiler's
create_lib](https://julialang.github.io/PackageCompiler.jl/stable/libs.html), which is set
up in this directory. The C API that is offered to control Ribasim is the C API of the
[Basic Model Interface](https://bmi.readthedocs.io/en/latest/), also known as BMI.
Not all BMI functions are implemented yet, this has been set up as a proof of concept to
demonstrate that we can use other software such as
[`imod_coupler`](https://github.com/Deltares/imod_coupler) to control Ribasim and couple it to
other models.
Here is an example of using libribasim from Python:
```python
In [1]: from ctypes import CDLL, c_int, c_char_p, create_string_buffer, byref
In [2]: c_dll = CDLL("libribasim", winmode=0x08) # winmode for Windows
In [3]: argument = create_string_buffer(0)
...: c_dll.init_julia(c_int(0), byref(argument))
Out[3]: 1
In [4]: config_path = "ribasim.toml"
In [5]: c_dll.initialize(c_char_p(config_path.encode()))
Out[5]: 0
In [6]: c_dll.update()
Out[6]: 0
```
"""
function main(ARGS)
function main()
project_dir = "../core"
license_file = "../LICENSE"
output_dir = "ribasim"
git_repo = ".."

# change directory to this script's location
cd(@__DIR__)

if "--app" in ARGS
build_app()
elseif "--lib" in ARGS
build_lib()
end
create_library(
project_dir,
output_dir;
lib_name = "libribasim",
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

main(ARGS)

main()
3 changes: 3 additions & 0 deletions build/cli_wrapper/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.cargo.sysroot": "C:\\Users\\hofer_jn\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc"
}
225 changes: 224 additions & 1 deletion build/cli_wrapper/Cargo.lock

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

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

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

[dependencies]
clap = { version = "4.5.4", features = ["derive"] }
24 changes: 18 additions & 6 deletions build/cli_wrapper/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
use std::env;
use std::{env, path::PathBuf};

fn main() {
use clap::{CommandFactory, Parser};
use std::process::ExitCode;

#[derive(Parser)]
#[command(version)]
struct Cli {
/// Path to the TOML file
toml_path: PathBuf,
}

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

Expand All @@ -18,11 +28,13 @@ fn main() {

// 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!();
let cli = Cli::parse();

if !cli.toml_path.is_file() {
eprintln!("File not found {:?}", cli.toml_path);
return ExitCode::FAILURE;
}

// Call ribasim shared library and check for errors
todo!()
Expand Down
Loading

0 comments on commit 8e68ce7

Please sign in to comment.