---
title: "Linking downstream R packages to VTK"
vignette: >
  %\VignetteIndexEntry{Linking downstream R packages to VTK}
  %\VignetteEngine{quarto::html}
  %\VignetteEncoding{UTF-8}
knitr:
  opts_chunk:
    collapse: true
    comment: '#>'
---

```{r}
#| label: setup
library(rvtk)
```

## What rvtk does

rvtk is an *infrastructure* package: it does not itself build VTK meshes or
render scenes. Instead, it makes the
[Visualization Toolkit (VTK)](https://vtk.org/) available to **other R
packages** that need to link against it from C++. If you are writing an R
package with native code that `#include`s VTK headers and calls VTK classes,
rvtk gives you four things:

1. **A universally installed VTK.** Headers and libraries are found in a
   system VTK installation when one exists, and are otherwise downloaded at
   install time as pre-built static libraries — so your users do not need to
   install VTK themselves.
2. **Compiler flags** — `rvtk::CppFlags()` returns the `-I` (include) paths.
3. **Linker flags** — `rvtk::LdFlags()` (and its response-file variant
   `rvtk::LdFlagsFile()`) returns the `-L`/`-l` arguments.
4. **Automated setup** — `rvtk::use_rvtk()` writes all the build-time glue
   files for you.

This vignette explains how VTK is located on each platform, what files a
downstream package needs, how the two setup strategies (`use_rvtk()` versus
hand-written files) relate, and how to verify that linking actually worked.
A companion vignette,
[rvtk-integration](rvtk-integration.html), covers how rvtk is itself tested
with real downstream packages on CI.

## How rvtk finds VTK

rvtk's own `configure` script runs at *install time* and writes the result to
`inst/vtk.conf`. The strategies differ by platform.

### macOS and Linux

1. The environment variable `VTK_DIR` (path to a VTK build/install tree).
2. [Homebrew](https://brew.sh/) (`brew --prefix vtk`) — macOS only.
3. `pkg-config` (`vtk-9.7`, `vtk-9.6`, `vtk-9.5`, …, `vtk-9.1`).
4. Well-known system prefixes (`/usr`, `/usr/local`) — Linux only.
5. Download pre-built static libraries from
   <https://github.com/astamm/rvtk/releases> as a fallback.

### Windows

`configure.win` tries, in order:

1. The environment variable `VTK_DIR`.
2. Rtools45 `pacman` (queries installed packages; never installs
   automatically).
3. Common Rtools45 / MSYS2 prefixes (`/x86_64-w64-mingw32.static.posix`,
   `/ucrt64`, `/mingw64`, …).
4. Download pre-built libraries (static `.a` by default, or shared DLLs when
   `VTK_LINK_TYPE=shared` is set before installation).

Static `.a` libraries are preferred over shared `.dll.a` import libraries at
the same prefix; if only shared libraries are available, rvtk accepts them and
stages the VTK DLLs itself so downstream packages need no extra configuration.

### Compatibility matrix

The pre-built fallback is available for the following platform × strategy
combinations; a system VTK installation (≥ 9.1.0) found via `VTK_DIR`,
`pkg-config`, Homebrew, or pacman is always preferred when present.

| Platform | Pre-built fallback | Notes |
|---|---|---|
| macOS arm64 | static `.a` | Apple Clang / libc++ |
| macOS x86_64 | static `.a` | Apple Clang / libc++ |
| Linux x86_64 (glibc) | static `.a` | GCC / libstdc++ |
| Linux x86_64 (glibc, libc++) | static `.a` | Clang `-stdlib=libc++` (selected by `configure`; see ABI note) |
| Linux x86_64 (musl / Alpine) | static `.a` | headless build: `VTK_USE_X=OFF`, `RenderingOpenGL2=NO` |
| Linux aarch64 | static `.a` | |
| Windows x64 | static `.a` (default) or shared DLLs (`VTK_LINK_TYPE=shared`) | |

The archives are the same on every platform: the impossible-to-ship optional
third-party stacks (see below) are disabled in the build, so any platform
difference is limited to the C++ toolchain the static libraries were compiled
with.

### Bundled VTK modules

The pre-built bundles are built with `VTK_BUILD_ALL_MODULES=ON` and a curated
deny-list of modules that pull in external dependencies unavailable in a
headless, self-contained static build (rendering toolkits such as Qt, OpenGL,
OpenXR/OpenVR, Vulkan; Python bindings; MPI and parallel IO; HDF5, netCDF,
Exodus, ADIOS2, GDAL, FFmpeg, and similar third-party IO stacks; remote
modules). The archive layout and the resulting `vtk_modules.txt` are generated
from the installed libraries, so the module list shipped in an archive always
reflects exactly what that archive contains.

The result is a complete VTK without those exotics: every I/O reader (Legacy,
XML, PLY, STL, VTU/VTP, …), every filter group, imaging, rendering
infrastructure, plus the Viskores acceleration modules. Concretely, each
archive ships **160 static libraries**. The module file lists the 110 VTK kit
modules (e.g. `vtkIOLegacy`, `vtkFiltersCore`, …), and the archive also
contains `vtksys`, the 23 `libviskores*` libraries, and 26 third-party
libraries under the `libvtk` prefix (expat, fmt, freetype, jpeg, jsoncpp,
libxml2, lz4, ogg, png, sqlite, tiff, theora, zlib, …) that VTK needs at link
time even when your code never calls them directly.

### Pre-built ABI compatibility

Each pre-built archive is compiled with the platform's default C++ toolchain,
so downstream packages must link with a compatible C++ runtime. VTK headers
pull in `<vector>`, `<string>`, etc., and the statically linked archives embed
the C++ standard library symbols they reference (in particular the
third-party objects such as `libvtkscn` that are always linked), so mixing
runtimes at load time fails with an undefined symbol:

* **Linux x86_64 (glibc).** Two variants are shipped: the default archive is
  compiled with **GCC and libstdc++**; a second archive, suffixed
  `-linux-x86_64-libcxx`, is compiled with Clang `-stdlib=libc++` for
  toolchains that target libc++ (CRAN's
  `r-devel-linux-x86_64-fedora-clang` flavor compiles packages with clang and
  defaults to libc++). rvtk's `configure` detects which standard library the
  build compiler targets and downloads the matching archive automatically; a
  manual install can force the choice with `CXX` (or `R_MAKEVARS_USER`).
* **macOS.** The archives are compiled with the default Apple Clang toolchain,
  which targets **libc++** (symbols land in the `std::__1` namespace). Do not
  force `-stdlib=libstdc++` when linking them: the libstdc++-built objects
  will not interoperate.
* **Much newer GCC than the build toolchain.** Even a matching
  libstdc++/GCC pairing can fail if the downstream compiler release changed
  the `libstdc++` ABI stub; the failure shows up at load time as an undefined
  `__cxa_call_terminate`. Match the compiler generation, or install a system
  VTK (via Homebrew, apt, …) and rvtk will link that instead.
* **Musl/Alpine and Windows.** Built with the platform's native C++ toolchain
  (musl's GCC libstdc++; Rtools45 MinGW on Windows); link with the matching
  R toolchain for the platform.

The macOS archives are compiled with `-DCMAKE_OSX_DEPLOYMENT_TARGET=13.0` and
are compatible with macOS 13 and newer.

### Memory checkers

A minimal downstream program that reads a VTK file reports ~1.5 KB
"definitely lost" under Valgrind, allocated inside VTK's pipeline internals
(`vtkInformation`, pipeline executives). This is inherent to the statically
linked VTK build and does not indicate a leak in downstream code. A
suppression file is shipped at `inst/valgrind/vtk.supp`; point Valgrind at it
(or set `_R_CHECK_VALGRIND_SUPPRESSIONS_` for `R CMD check`'s Valgrind leg) to
get a clean report. The `rvtk-integration` vignette discusses how rvtk itself
exercises memory-checker legs on CRAN platforms.

Once rvtk is installed, `CppFlags()`, `LdFlags()`, `LdFlagsFile()`, and
`VtkVersion()` read `vtk.conf` and report whatever VTK was found:

```{r}
VtkVersion()
CppFlags()
```

The exact flags depend on the detected VTK strategy: system shared libraries
on macOS/Linux, `.a` archives in the pre-built tree, `.dll.a` import
libraries + DLLs on Windows, etc. Your downstream package never has to figure
this out itself — it simply calls back into rvtk.

## The two setup strategies

There are two equivalent ways to configure a downstream package. They
produce **byte-identical files** (this equivalence is asserted in rvtk's own
CI on every platform and VTK strategy).

### Strategy A — `use_rvtk()`

The recommended one-call approach, in the spirit of `usethis`:

```{r, eval = FALSE}
# from the root of your package (e.g. yourpackage/)
rvtk::use_rvtk()
```

This does the following, in order:

1. Adds `rvtk` to the `Imports` field of `DESCRIPTION`.
2. Writes `src/Makevars` (macOS/Linux build rules).
3. Writes `src/Makevars.win` (Windows build rules).
4. Writes `tools/configure.R`, a small R helper that resolves the flags at
   install time.
5. Adds `src/vtk_libs.rsp` to `.gitignore` (the response file is generated at
   install time and must not be committed).
6. Creates `R/rvtk_imports.R` with a minimal `@importFrom rvtk` roxygen tag so
   that `R CMD check` does not warn about an unused `Imports` entry.

If some of these files already exist, `use_rvtk()` asks before overwriting in
an interactive session and skips them silently otherwise.

### Strategy B — manual setup

Everything `use_rvtk()` writes can also be written by hand, exactly as
documented below (the README's "Manual setup" section also walks through it).
The files are:

**`DESCRIPTION`** — declare the dependency:

```
Imports: rvtk
```

**`src/Makevars`**:

```makefile
PKG_CPPFLAGS = `"$(R_HOME)/bin/Rscript" ../tools/configure.R --cppflags`
PKG_LIBS = `"$(R_HOME)/bin/Rscript" ../tools/configure.R --libs`
```

**`src/Makevars.win`**:

```makefile
PKG_CPPFLAGS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" ../tools/configure.R --cppflags)
PKG_LIBS = $(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" ../tools/configure.R --libs)
```

**`tools/configure.R`**:

```r
args <- commandArgs(trailingOnly = TRUE)
flag <- if (length(args)) args[1L] else "--cppflags"

vtk_modules <- NULL

if (flag == "--cppflags") {
  rvtk::CppFlags()
} else if (flag == "--libs") {
  rvtk::LdFlagsFile(path = "vtk_libs.rsp", modules = vtk_modules)
}
```

**`.gitignore`** — add `src/vtk_libs.rsp`.

**`R/rvtk_imports.R`** — import at least one rvtk function so that
`R CMD check` sees the `Imports` entry as used:

```r
#' @importFrom rvtk CppFlags LdFlagsFile
NULL
```

### How the build-time resolution works

There is no shell `configure` script in the downstream package. Instead, the
`Makevars` files shell out to `Rscript` at compile time:

```sh
"$(R_HOME)/bin/Rscript" ../tools/configure.R --cppflags
```

`tools/configure.R` is the only file that knows about rvtk's functions; it
prints the flags to standard output, and Make captures them into
`PKG_CPPFLAGS` / `PKG_LIBS`. This redirection through `Rscript` is exactly how
the flags stay correct no matter where VTK was found — the R session doing the
compilation has the same library paths as the rvtk installation being linked
against.

On Windows the Make syntax is different
(`$(shell "$(R_HOME)/bin$(R_ARCH_BIN)/Rscript" …)`) because GNU make on
Windows needs the `$(shell …)` form and the `R_ARCH_BIN` subdirectory.

The `--libs` path calls `LdFlagsFile()`, not `LdFlags()`, because the full set
of VTK `-l` flags can exceed the 8 191-character Windows command-line limit.
On Windows the flags are written to `src/vtk_libs.rsp` and
`PKG_LIBS` receives the short token `@vtk_libs.rsp`. On macOS and Linux the
raw flags are returned directly.

## Choosing the VTK modules to link against

The pre-built bundles shipped with rvtk contain the full buildable VTK
(160 static archives on each platform: every I/O reader, every filter group,
rendering, imaging, Viskores, …). By default `use_rvtk()` links **all** of
them:

```{r}
modules = NULL
```

This is safe because the archives are static libraries and the linker
extracts only the object files your code actually references — linking
everything does not mean your final binary contains every VTK feature. It
also means a downstream package can never fail with a "module missing from
the default list" error (earlier releases curated a fixed 18- or 24-module
default, and any package using a module outside it — for example the
`vtkParallelDIY` / `vtkIOCellGrid` dependencies of `vtkFiltersExtraction`,
or `vtkIOPLY` for PLY files — failed at link or load time with an
undefined-symbol error).

Passing a subset of modules is an *optimisation*, not a requirement. It
reduces link time and keeps the compiled package smaller, which is worth it
for packages that use only a small part of VTK:

```{r, eval = FALSE}
rvtk::use_rvtk(
  modules = c(
    "vtkIOLegacy",
    "vtkCommonCore",
    "vtkCommonDataModel"
  )
)
```

When you restrict the list, make sure every module your code uses (including
transitive dependencies such as `vtkParallelDIY` and `vtkIOCellGrid`) is
included. If you set up files manually, set the `vtk_modules` variable in
`tools/configure.R` to `NULL` (all modules) or your chosen subset.

The `modules` argument is also accepted directly by `LdFlags()` and
`LdFlagsFile()` if you need finer control in your own build scripts:

```{r}
# flags for just the I/O + core modules
LdFlags(modules = c("vtkIOLegacy", "vtkCommonCore", "vtkCommonDataModel"))
```

### Module availability

Since rvtk 0.3.0, `LdFlags()` and `LdFlagsFile()` validate the requested
modules by default (`check = TRUE`): if a module is not available in the
detected VTK installation, configuration fails immediately with an actionable
error listing the missing modules and the available set, instead of surfacing
much later as a confusing linker error.

Use `available_modules()` to see exactly which modules the installation can
link against:

```{r}
# modules available in this VTK installation (pre-built archives ship the
# complete set recorded in vtk_modules.txt; system VTK is derived from the
# recorded linker flags)
available_modules()
```

With the pre-built archives this is the full module set the bundle was built
with (every VTK module except a deny-list of modules that pull in unavailable
external dependencies). With a system VTK it reflects that installation, so a
request for a module the system VTK does not provide fails fast. Pass
`check = FALSE` only if you intentionally link against whatever the
installation provides.

## Writing the C++ that uses VTK

A minimal `src/code.cpp` that exercises the link looks like this:

```cpp
// src/code.cpp
#include <cpp11.hpp>
#include <vtkNew.h>
#include <vtkPolyData.h>

[[cpp11::register]]
int vtk_poly_data_npoints() {
  vtkNew<vtkPolyData> pd;
  return static_cast<int>(pd->GetNumberOfPoints());
}
```

Include the headers exactly as the VTK include directory is laid out in the
detected installation (e.g. `vtk-9.7`). `-isystem` (from `CppFlags()` on
macOS/Linux) makes these includes searchable without turning their content
into warnings.

If you use `cpp11` (as in rvtk's own downstream CI), run
`cpp11::cpp_register()` after adding your functions. **Note:** `cpp_register()`
copies only the function name, return type, and arguments into `R/cpp11.R` —
roxygen tags such as `#' @export` must live in a separate R file for the
wrapper function you export, or the function will not appear in `NAMESPACE`.

## Shared VTK on Windows

If rvtk is installed with `VTK_LINK_TYPE=shared` (or a system installation
provides only shared libraries), downstream packages link against `.dll.a`
import libraries and load the VTK DLLs at run time from rvtk's
`inst/vtk-dlls/` (prepended to `PATH` by rvtk's `.onLoad` hook). Two
consequences for package authors:

1. **Version sync matters.** Your package is compiled against the specific
   VTK version shipped with the rvtk installed at compile time. If rvtk is
   updated to a new VTK version, your package must be **recompiled** against
   the new rvtk to match the new DLL names (e.g. `vtkCommonCore-9.8.dll`).
2. **DLL footprint lives in rvtk only.** Downstream packages do not receive a
   copy of the DLLs; they rely on rvtk's staged directory at run time.

For CRAN submissions the **static pre-built build is recommended**: it is
self-contained inside your package binary and has no run-time coupling to
rvtk's DLLs.

## Verifying that linking worked

Before submitting your package, run a full `R CMD check`; then load the
installed package in a fresh session and call the C++ entry point:

```{r, eval = FALSE}
# in a fresh R session, after R CMD INSTALL
library(yourpackage)
yourpackage::vtk_poly_data_npoints() # expect 0: a brand-new vtkPolyData has no points
```

If the package builds but fails to load with an undefined-symbol error, the
cause is a symbol being dragged in from a module outside the *requested*
`modules` subset (older rvtk versions curated a fixed default list, so e.g.
`vtkCellGridReader` failed to resolve when `vtkIOCellGrid` was not listed).
With the default `modules = NULL` this cannot happen. If you restricted the
list, widen it back to `NULL` or add the module that defines the missing
symbol, then rebuild.

## Summary

- rvtk locates VTK at install time and exposes the resulting flags through
  `CppFlags()`, `LdFlags()`, `LdFlagsFile()`, and `VtkVersion()`.
- `use_rvtk()` writes every build-time file for a downstream package; the same
  files can be written manually, byte-for-byte identically.
- By default the full bundled VTK is linked; use the `modules` argument only
  to restrict linking when you want shorter link times or smaller packages.
- Prefer `LdFlagsFile()` on Windows to avoid the command-line length limit.
- For CRAN, prefer the static pre-built build; for Windows shared builds,
  keep rvtk and the downstream package in lockstep.
