Skip to content

fabro-sh/graphviz-sys

Repository files navigation

graphviz-sys

Rust crate that statically links a vendored Graphviz 14.1.5 and exposes a single function for rendering DOT source to SVG.

No system dependency on libgraphviz, no bindgen at build time — the C sources are compiled via cc and the eight FFI symbols we need are declared by hand in src/lib.rs.

Usage

[dependencies]
graphviz-sys = { git = "https://github.com/fabro-sh/graphviz-sys" }
let svg = graphviz_sys::render_dot_to_svg("digraph { a -> b }")?;
std::fs::write("graph.svg", svg)?;

render_dot_to_svg returns the SVG bytes, or an error string if the DOT source is invalid or Graphviz fails to lay out or render it.

What's included

Only what's needed to go from DOT to SVG:

  • dot layout engine (plugin/dot_layout)
  • Core renderer plugin (plugin/core) — includes the SVG backend
  • Supporting libraries: cdt, cgraph, common, dotgen, gvc, label, ortho, pack, pathplan, util, xdot

Other layout engines (neato, fdp, sfdp, twopi, circo, osage, patchwork) and other output formats are not compiled in.

Thread safety

Graphviz has non-thread-safe global state (string interning, error counters, layout state). render_dot_to_svg serializes all calls through a global Mutex. Callers should run it on a blocking thread pool (e.g. Tokio's spawn_blocking).

Build

cargo build --release
cargo test --release

The build script compiles ~12 static archives and links them into the final binary. On Linux the archives are merged with ar -M to resolve circular symbol references between common and gvc; macOS's linker handles cycles natively.

Supported targets

CI builds and tests:

  • aarch64-apple-darwin
  • x86_64-unknown-linux-gnu
  • aarch64-unknown-linux-gnu
  • x86_64-unknown-linux-musl
  • aarch64-unknown-linux-musl

musl builds require musl-tools (musl-gcc).

Layout

build.rs              compiles vendored Graphviz C into static archives
builtins.c            registers the statically-linked plugins with Graphviz
config.h              minimal replacement for the autotools-generated config
src/lib.rs            FFI declarations and render_dot_to_svg
vendor/graphviz-14.1.5  upstream Graphviz source (pruned)

License

Graphviz is licensed under the Eclipse Public License 2.0. This crate is distributed under the same license.

About

Vendored Graphviz C library compiled via cc, exposing DOT-to-SVG rendering

Resources

Stars

Watchers

Forks

Contributors