Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "mocoal"
version = "0.1.0"
edition = "2024"
authors = ["John Tinnerholm <jtinnerholm@gmail.com>"]
license = "AGPL-3.0-only OR LicenseRef-OSMC-PL-1.8"
license = "LicenseRef-OSMC-PL-1.8 OR AGPL-3.0-only"
description = "Gives the components of a Modelica model a Placement and its connections a Line"
publish = false

Expand Down
12 changes: 6 additions & 6 deletions LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ mocoal-rs

Copyright (c) 2026 Open Source Modelica Consortium (OSMC)

It is provided under the terms of the GNU Affero General Public License version 3 (AGPL v3) or
the OSMC Public License (OSMC-PL) version 1.8, according to the recipient's choice:
It is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or the GNU Affero
General Public License version 3 (AGPL v3), according to the recipient's choice:

- **AGPL v3:** the full text is in [COPYING](COPYING), and at
https://www.gnu.org/licenses/agpl-3.0.html.
- **OSMC-PL 1.8:** the full text is in [OSMC-License.txt](OSMC-License.txt), as distributed with
OpenModelica (https://github.com/OpenModelica/OpenModelica).
- **AGPL v3:** the full text is in [COPYING](COPYING), and at
https://www.gnu.org/licenses/agpl-3.0.html.

This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH IN THE BY
Expand Down Expand Up @@ -92,7 +92,7 @@ Not in this repository; the build fetches them.

- The parser of OpenModelica: the crates `openmodelica_ast`, `openmodelica_error`,
`openmodelica_wasi`, `metamodelica` and `metamodelica_derive`
(https://github.com/OpenModelica/OpenModelica, `OMCompiler/Compiler/OpenModelica.rs`), AGPL v3
or OSMC-PL 1.8. `scripts/fetch-omc.sh` fetches them at the commit in `omc/COMMIT`.
(https://github.com/OpenModelica/OpenModelica, `OMCompiler/Compiler/OpenModelica.rs`), OSMC-PL
1.8 or AGPL v3. `scripts/fetch-omc.sh` fetches them at the commit in `omc/COMMIT`.
- Crates from crates.io, at the versions in `Cargo.lock`, each under MIT, Apache-2.0 or a licence
as permissive: `cargo metadata --format-version 1` lists the licence of each.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ One file without -o, -i or -n: the result is printed.
-o, --output FILE write the result to FILE (one input file)
--stdin FILE read the text of FILE from the standard input (FILE need not
exist; its place finds the libraries next to it)
--stack MB stack of the layout in MB (256); a level of nesting in the
model takes about 3 KB
-i, --in-place change the input files
-n, --dry-run report only, write nothing
-c, --class NAME lay out only this class or package (may be repeated)
Expand Down Expand Up @@ -292,4 +294,4 @@ to crates.io (`publish = false`).

## Licence

AGPL v3 or OSMC-PL 1.8, according to the recipient's choice. See [LICENSE.md](LICENSE.md).
OSMC-PL 1.8 or AGPL v3, according to the recipient's choice. See [LICENSE.md](LICENSE.md).
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

fn main() {
println!("cargo::rerun-if-changed=build.rs");
Expand Down
2 changes: 1 addition & 1 deletion interface/INTERFACE.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Not part of the interface; what a caller of this implementation (Rust) has to kn
`internal error, no layout`.
- Time: Modelica Standard Library 4.1.0, 2552 files, 1.1 to 1.5 s for all of them with the
program `mocoal`.
- Licence: AGPL v3 or OSMC-PL 1.8, according to the recipient's choice.
- Licence: OSMC-PL 1.8 or AGPL v3, according to the recipient's choice.

## Test

Expand Down
4 changes: 2 additions & 2 deletions interface/test_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
This file is part of ModelicaComponentAutolayout.jl.
Copyright (c) 2026 Open Source Modelica Consortium (OSMC)

This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.
*/

/* Test of the shared library libmocoal: test_lib MODEL.mo (see check.sh). */
Expand Down
4 changes: 2 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! The layout of the classes of a source text: parse, extract the diagrams, place, route, and
//! write the annotations back.
Expand Down
11 changes: 6 additions & 5 deletions src/capi.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! The C interface of the shared library `libmocoal`, as INTERFACE.md gives it.
//! Mirrors `juliac/libmocoal.jl` of ModelicaComponentAutolayout.jl.

use std::ffi::{CStr, CString, c_char};
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::sync::Mutex;

use crate::cli::layout_text;
use crate::stack::on_large_stack;

/// The message of the last `mocoal_layout`.
static MESSAGE: Mutex<Option<CString>> = Mutex::new(None);
Expand Down Expand Up @@ -66,8 +66,9 @@ pub unsafe extern "C" fn mocoal_layout(
let args = unsafe { (c_text(text), c_text(file_name), c_text(options)) };
let result = match args {
(Ok(text), Ok(file_name), Ok(options)) => {
// No panic may cross the interface; deep nesting needs more stack than the caller's thread may have.
on_large_stack(|| layout(text, file_name, options)).unwrap_or_else(|_| Err(INTERNAL_ERROR.to_owned()))
// No panic may cross the interface. `layout_text` lays out on a large stack.
catch_unwind(AssertUnwindSafe(|| layout(text, file_name, options)))
.unwrap_or_else(|_| Err(INTERNAL_ERROR.to_owned()))
}
(Err(e), _, _) | (_, Err(e), _) | (_, _, Err(e)) => Err(e),
};
Expand Down
50 changes: 46 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! The command line of `mocoal`, and the entry of the shared library, which takes its options as
//! the same command-line text.
Expand All @@ -12,6 +12,7 @@ use crate::api::layout_string;
use crate::numbers::parse_float;
use crate::options::Options;
use crate::source::lookup::{is_dir, is_file, join_path};
use crate::stack::{MAX_STACK_MB, STACK_MB, on_large_stack};

pub const USAGE_TEXT: &str = "usage: mocoal [options] file.mo|directory ...
mocoal [options] --stdin file.mo
Expand All @@ -22,6 +23,8 @@ One file without -o, -i or -n: the result is printed.
-o, --output FILE write the result to FILE (one input file)
--stdin FILE read the text of FILE from the standard input (FILE need not
exist; its place finds the libraries next to it)
--stack MB stack of the layout in MB (256); a level of nesting in the
model takes about 3 KB
-i, --in-place change the input files
-n, --dry-run report only, write nothing
-c, --class NAME lay out only this class or package (may be repeated)
Expand All @@ -41,13 +44,15 @@ One file without -o, -i or -n: the result is printed.
";

/// A parsed command line.
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug)]
pub struct CommandLine {
pub opts: Options,
pub inputs: Vec<String>,
pub output: String,
/// With `--stdin`: the name of the file whose text is read from the standard input.
pub stdin: String,
/// The stack of the layout thread in MB.
pub stack_mb: usize,
pub in_place: bool,
pub dry_run: bool,
pub quiet: bool,
Expand All @@ -56,13 +61,38 @@ pub struct CommandLine {
pub version: bool,
}

impl Default for CommandLine {
fn default() -> Self {
Self {
opts: Options::default(),
inputs: Vec::new(),
output: String::new(),
stdin: String::new(),
stack_mb: STACK_MB,
in_place: false,
dry_run: false,
quiet: false,
svg_dir: String::new(),
help: false,
version: false,
}
}
}

impl CommandLine {
/// Set the option `name` that takes the value `value`.
fn set_value_option(&mut self, name: &str, value: &str) -> Result<(), String> {
let o = &mut self.opts;
match name {
"-o" | "--output" => self.output = value.to_owned(),
"--stdin" => self.stdin = value.to_owned(),
"--stack" => {
self.stack_mb = value
.parse()
.ok()
.filter(|mb| (1..=MAX_STACK_MB).contains(mb))
.ok_or_else(|| format!("--stack: expected MB from 1 to {MAX_STACK_MB}: {value}"))?
}
"-c" | "--class" => o.classes.push(value.to_owned()),
"-L" | "--library" => o.libraries.push(value.to_owned()),
"--layout" => o.layout = value.to_owned(),
Expand Down Expand Up @@ -111,6 +141,7 @@ fn takes_value(name: &str) -> bool {
name,
"-o" | "--output"
| "--stdin"
| "--stack"
| "-c"
| "--class"
| "-L"
Expand Down Expand Up @@ -214,7 +245,9 @@ pub fn layout_text(text: &str, file_name: &str, options: &str) -> Result<(String
{
return Err(format!("only layout options are allowed here: {options}"));
}
let res = layout_string(text, file_name, &cl.opts)?;
// Deep nesting in a model needs more stack than the caller's thread may have.
let res = on_large_stack(cl.stack_mb, || layout_string(text, file_name, &cl.opts))
.unwrap_or_else(|_| Err("internal error, no layout".to_owned()))?;
Ok((res.text, res.warnings.join("\n")))
}

Expand Down Expand Up @@ -276,6 +309,15 @@ mod tests {
"/a b/M.mo"
);
assert!(layout_text("model M end M;", "M.mo", "--stdin M.mo").is_err());
assert_eq!(parse_arguments(&[]).unwrap().stack_mb, 256);
assert_eq!(parse_arguments(&words("--stack=1024")).unwrap().stack_mb, 1024);
for bad in ["0", "x", "65537"] {
assert_eq!(
parse_arguments(&words(&format!("--stack {bad}"))).unwrap_err(),
format!("--stack: expected MB from 1 to 65536: {bad}")
);
}
assert!(layout_text("model M end M;", "M.mo", "--stack 512").is_ok());
assert_eq!(
parse_arguments(&words("--scale")).unwrap_err(),
"--scale: a value is missing"
Expand Down
4 changes: 2 additions & 2 deletions src/layout.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! The layout: the diagram of a model, the placement of its components and the routing of its lines.

Expand Down
4 changes: 2 additions & 2 deletions src/layout/diagram.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! The diagram of one class: nodes (components and the class's own connectors), their ports, and
//! links (connect equations). Placement and routing work on this, independent of the syntax tree.
Expand Down
4 changes: 2 additions & 2 deletions src/layout/ends.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! The ends of the links: where a line meets a node, and on which side it leaves it.
//! Links that meet in a connector form a net.
Expand Down
4 changes: 2 additions & 2 deletions src/layout/extract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! From a parsed class to its diagram: nodes from the component declarations, links from the
//! connect equations, and the tokens at which the annotations are written back.
Expand Down
4 changes: 2 additions & 2 deletions src/layout/grid.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! The routing grid: the lines on which connection lines may run, the nodes as obstacles, and the
//! record of which net uses which piece of the grid.
Expand Down
4 changes: 2 additions & 2 deletions src/layout/layered.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! Layered placement of one connected group of nodes: the signal flows from layer to layer.
//!
Expand Down
4 changes: 2 additions & 2 deletions src/layout/placement.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! Placement of the nodes of a diagram.
//!
Expand Down
4 changes: 2 additions & 2 deletions src/layout/routing.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! Routing of the connection lines.
//!
Expand Down
4 changes: 2 additions & 2 deletions src/layout/spring.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! Spring placement of one connected group of nodes.
//!
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! Gives the components of a Modelica model a `Placement` and its connections a `Line`.

Expand Down
13 changes: 3 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! The standalone program `mocoal`.

Expand All @@ -13,12 +13,5 @@ fn main() -> ExitCode {
.skip(1)
.map(|a| a.to_string_lossy().into_owned())
.collect();
// Deep nesting in a model needs more stack than the main thread has.
match mocoal::stack::on_large_stack(|| mocoal::program::run(&args)) {
Ok(status) => ExitCode::from(status),
Err(_) => {
eprintln!("mocoal: internal error");
ExitCode::from(1)
}
}
ExitCode::from(mocoal::program::run(&args))
}
4 changes: 2 additions & 2 deletions src/numbers.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of mocoal-rs.
// Copyright (c) 2026 Open Source Modelica Consortium (OSMC)
//
// This program is provided under the terms of the GNU AGPL version 3 license or the OSMC Public
// License (OSMC-PL) version 1.8, according to the recipient's choice. See LICENSE.md.
// This program is provided under the terms of the OSMC Public License (OSMC-PL) version 1.8 or
// the GNU AGPL version 3 license, according to the recipient's choice. See LICENSE.md.

//! Number functions that Julia has built in, written out here so that both implementations
//! compute the same coordinates: `sind`, `cosd` (after Julia's `base/special/trig.jl`), the order
Expand Down
Loading
Loading