Skip to main content

Handling Native Code

Regular R packages have decades of established convention for compiled codes: through src/, Makevars, useDynLib, all built automatically as part of R CMD INSTALL. As what {box}’s own vignette on compiled code says so directly: {box} doesn’t have a built-in foreign function interface, and integrating compiled code means doing the steps in that vignette by hand, compiling it yourself, loading it yourself, wiring .Call() yourself. {baler} exists to attempt to automate that manual work, not to add polish to something {box} was already handling.

After a long time of developing {baler}, whom initially started as {carrier}, box-bale modules has now more stable way to contain and cache native codes (C, C++, …), and they are compiled via R CMD SHLIB mechanism. Though, in the R-side loading, it requires a little boilerplate, that is dyn.load() / dyn.unload() wired to a module’s .on_load() / .on_unload() hooks. As of now, for regular box modules, the handling isn’t automatic just because a native directory exists, unlike the traditional R packages. The current solution {baler} has comes from the auto-generated hook.r, which baler init --native scaffolds once, at that moment. If you add native code to a module some other way, for example dropping a Makevars into an existing module without re-running --native init, nothing writes that loading glue for you. You’re back to the manual steps the {box} vignette describes, for that module.

The practical difference from classic R packages is rather explicit: You are not forced to put native sources in a directory named src/. Any directory name is fine, as long as {baler} can find it, either by automatic discovery or by an explicit [compiled-code] entry (for more details, see The [compiled-code] block).

Enabling native code in a module

{baler} ships scaffolding for modules with native compiled code. Two ways to get there:

  1. baler init <module-name> --native <lang> on a fresh module. This scaffolds functional example code (hello_world/add) in both the compiled language and the respective R code, being already wired together.

  2. Either drop sources into a directory that baler will discover automatically, or add a [compiled-code] block to baler.toml. You do not need the [compiled-code] block at all if the sources live under a discoverable directory and you have no build-time R package dependencies.

Supported languages and backends

{baler} recognizes three languages, and the build supports two of them:

  1. C language, which the codes are written under .c supported file. This is currently supported by the R CMD SHLIB mechanism.

  2. C++ language, saved under .cc and .cpp. Also currently supported by the R CMD SHLIB mechanism.

  3. FORTRAN is recognized by the language parser, but the build does not support it yet.

To be clear, FORTRAN is skipped on purpose for now, so it hasn’t fully featured yet. Current development emphasizes that gfortran doesn’t ship with Xcode Command Line Tools or Rtools, both already required for C/C++, so supporting it would mean toolchain errors for a chunk of users. Real FORTRAN / R interop also needs a C shim (iso_c_binding), it doesn’t fit the direct extern "C" SEXP pattern the other three backends share. And also, the demand has been low too, most native R code written today is either C, C++, or Rust.

C++ has two backends, chosen independently of language:

Backend Value Notes
Rcpp rcpp (default) PKG_CXXFLAGS/PKG_LIBS pulled from Rcpp:::CxxFlags()/Rcpp:::LdFlags() at build time
cpp11 cpp11 Header-only; PKG_CPPFLAGS points at the installed cpp11 headers, CXX_STD = CXX11
Note

When you initialize a module project that applies native C++ code as {Rcpp} as your backend, {baler} will then generates the hook.r file that begins with box::use(Rcpp[...]) so the package is available when the shared library is loaded. This is the only difference between C++ in {Rcpp} backend and plain C / cpp11 versions of hook.r, everything else is identical.

There’s a reason for that. {Rcpp} ships its own compiled shared library, while {cpp11} is header-only. Code generated by {Rcpp} calls functions that live inside {Rcpp}’s library, like dataptr. Your module’s compiled shared object (e.g. .so) doesn’t have those functions built in, it looks them up in R at runtime instead. R only knows where to find them once {Rcpp} itself has been loaded, through box::use(Rcpp[...]) here.

If a module’s .so is dyn.load()ed before {Rcpp}’s own package DLL has loaded, calls into it fail the moment something needs an Rcpp-registered symbol:

box::use(sm = statmodule)

sm$square(5)
#> Error in sm$square(5) : function 'dataptr' not provided by package 'Rcpp'

box::use(Rcpp[...]) at the top of hook.r has to force {Rcpp} to be loaded before .on_load’s dyn.load() runs, so that never happens. {cpp11}, on the other hand, has no equivalent runtime shared-library dependency at all, which is why its .on_load’s dyn.load() call skips this line entirely rather than needing some {cpp11} equivalent.

The [compiled-code] block

[compiled-code] is entirely optional. You don’t need it at all if your compiled code lives somewhere {baler} already discovers on its own (see Directory conventions) and you have no build-time R package dependencies. If you do need to configure it, here’s the shape:

[compiled-code]
path = "cpp/"
# path can also be an array: path = ["cpp/", "extra/src"]
build_deps = { Rcpp = "*" }
binary = false        # If true, `baler bundle` also compiles and ships a binary
  • path is where this module’s native source lives, relative to the module’s own resolved source directory (the same base [project].src uses), not the project root where baler.toml lives. It accepts either a single string, for one native-code location, or an array, for a module with more than one (e.g. path = ["cpp1", "cpp2"]). A configuration here, either a single location or a multiple locations, are just two shapes of the same source (src) field, not two separate keys.

    When path isn’t set, {baler} scans the module’s whole source tree and treats every qualifying directory as its own native dir (see below). Also, when src/ is named, the name of the compiled artifact comes from the name of the module.

  • build_deps configures external dependencies, most commonly R packages, needed only to compile (e.g. {Rcpp}, {cpp11}, whatever Makevars needs via system.file()). They can come from CRAN, from another CRAN-like repository (repo), or from GitHub (git), the same forms [dependencies] takes. If the compiled code also needs those packages loaded at runtime, list them under [dependencies] too. See Sharing native code between modules.

    In {carrier} before version 0.2.1, build_deps were only installed if they also appeared under [package_deps] (now [dependencies]). Since then they are part of the ordinary package-resolution plan, and they still are in {baler}.

  • binary is a boolean, false by default, that controls whether baler bundle ships a compiled binary next to the native source, which is always shipped. See Binary bundles.

Directory conventions

A directory counts as native source (detect::has_native_src()) if either:

  • It has a Makevars or Makevars.win file, or
  • It contains at least one .c/.cpp/.cc/.cxx file.

As covered under this explanation, a module can declare more than one native directory by setting path to an array. Each qualifying directory is its own independent compilation unit, built and cached separately, with its artifact named after its own folder name. This matters if you’re mixing, say, a cpp/ dir and a hand-authored legacy-c/ dir in the same module.

Module template generation

For example, you want to write a module that contains C++ code as its background, named as cppModule. Running baler init cppModule --native cpp --backend rcpp produces:

cppModule-proj/
├── baler.toml
├── README.md
└── cppModule/                 
    ├── __init__.r
    ├── hook.r
    ├── hello.r
    ├── add.r
    └── cpp/
        ├── hello.cpp
        ├── add.cpp
        └── Makevars

After baler compile (or a successful install-time build) the shared library appears here:

cppModule/
├── .lib/
│   └── cpp.dll                 # (or .so / .dylib)
├── __init__.r
├── hook.r
└── ...

Loading native compiled codes

If you start your project by running baler init <module-name> --native ... command, this will generate one file: hook.r, which contains the example .on_load() / .on_unload() hooks which runs the following dyn.load() / dyn.unload() calls that load the compiled shared object. Once the command is executed, this file is written at the top level of the module’s own source directory, next to __init__.r, not inside the native subdirectory. It loads (and unloads) the compiled shared object from .lib/, the directory a finished build’s artifact is copied into, not the build cache (see Build cache below, that’s a different location entirely). Here’s what it looks like:

hook.r (Rcpp backend):

box::use(Rcpp[...])

#' @export
dll = NULL

.on_load = function(ns) {
    ns$dll = dyn.load(box::file(paste0(".lib/cpp", .Platform$dynlib.ext)))
}

.on_unload = function(ns) {
    dyn.unload(box::file(paste0(".lib/cpp", .Platform$dynlib.ext)))
}

The pure C / cpp11 variant is identical except that it omits the box::use(Rcpp[...]) line. The cpp/ is the name of the native directory and just a placeholder. box::file() resolves relative to the calling module, so the library is found next to the R files.

If you run baler init <module-name> without --native, then the generation tends to writes the pure R equivalent instead. The hello_world() / add() logic are pretty much the same as how they are implemented under the chosen “low-level” language, same file layout, but implemented directly in R, thus no file like hook.r which calls dyn.load() / dyn.unload() calls. A fresh module always starts from runnable examples, compiled or not.

Binary bundles

baler bundle always ships the module’s native source. Set binary = true under [compiled-code] and it also runs the compile step first, then forces the normally-hidden .lib/ directory into the archive. If the module has no native code, baler bundle warns and bundles without a binary.

[compiled-code]
binary = true                # ships the native source and a binary for this platform

Each binary is stored in a folder named after the platform it was built for, .lib/<platform>/, where the platform is the target triple and the R version, for example x86_64-unknown-linux-gnu_r4.4. Next to every artifact is an .abi.json sidecar that records the target triple, the R major.minor, and a hash of the native source it was built from. A bundle can hold the folders of several platforms, see Bundling for more than one platform.

On baler install, {baler} compares each sidecar with the machine, and there are three cases:

  1. The binary fits: same target triple, same R major.minor, and the same source hash as the source that shipped next to it. The files of that platform’s folder are moved up into .lib/, the other folders are deleted, and no compiler is needed.
  2. It doesn’t fit, or the bundle has no binary: the module is built on that machine from the source that shipped, the way R builds a source package. A failed build fails the install and shows what the compiler said.
  3. It doesn’t fit and there is no source, which only a bundle from an older {baler} can be: the install fails with a message that names every platform the bundle holds and the one of this machine, instead of leaving a binary that can’t load.

Once the native directory is built, or its shipped binary is in use, its source is deleted. An installed module keeps the compiled artifact and not the source it came from. A binary with no sidecar has no recorded platform, so it can’t be checked and is left alone.

--install-deps only decides whether build_deps are installed before such a build. Building itself doesn’t need it.

This is the same idea as how {baler} handles R package binaries from CRAN, where the architecture of a downloaded binary is read from its header and checked before it is trusted. Now a module’s own compiled artifacts get a check too, through the sidecar.

Sharing native code between modules

Because the native source is deleted after install, a module can’t point its Makevars at the headers of another installed module. When native code or headers have to be shared, put them in an R package and make that package a build dependency, as {Rcpp}, {cpp11} and {BH} already do:

[compiled-code]
build_deps = { BH = { git = "https://github.com/eddelbuettel/bh" } }

A header-only package keeps its headers in inst/include, which ends up at system.file("include", package = "BH") once it is installed, and that is the path Makevars points -I at. Since the package is a normal build_deps entry, baler lock pins it (the exact version, or for GitHub the exact commit) in the build group of baler.lock, so everyone who builds the module compiles against the same headers. If the compiled code also loads the package at runtime, list it under [dependencies] too.

Build cache

Successful builds are cached, keyed by module, platform, R version, and a hash of the native directory’s contents, so a cache hit copies the library into place instead of invoking R CMD SHLIB again.

The global cache lives at ~/.baler/native-cache/<module>/<platform>/<R-major.minor>/<source-hash>/. <source-hash> is a SHA-256 digest over every file’s relative path and bytes under the native directory, sorted for determinism, so the same source always hashes the same way regardless of the order the filesystem happens to list files in. target/ is excluded from that hash: Cargo’s own build output for a module mixing in Rust isn’t deterministic between builds, so hashing it would change the cache key on every build even when nothing real changed, quietly defeating the cache.

Set BALER_CACHE_DIR to relocate the cache root away from ~/.baler/native-cache/. This matters on a machine without a conventional home directory (some CI runners), or when several users share a machine and want separate caches.

Cleaning, or forcing a rebuild

Clearing .lib/, the directory a finished artifact is copied into, is not the same as clearing the cache above, and it’s important to keep the two apart:

  • .lib/ holds the compiled shared objects box::file() actually loads at runtime for the current project. baler compile clears and rebuilds it on every run, whether or not anything changed.
  • The cache above is a separate, global store shared across every project on the machine. A plain baler compile still checks it first: if the source hash, platform, and R version all match a previous build, the cached artifact is copied straight into the freshly-cleared .lib/ and R CMD SHLIB never runs.

So a normal baler compile, assuming your working directory lives at the root of the project, can look like it rebuilt when it actually just replayed a cache hit.

To remove the caches, both locally and globally, without rebuilding, pass --clean:

baler compile --clean
# or, naming the project root explicitly:
baler compile . --clean

This removes every cached entry for the current module, across all platforms, R versions, and source hashes, and clears .lib/, then stops. Nothing gets compiled. Think of this as what you did with devtools::clean() and/or pkgbuild::clean_dll().

To force an actual recompile instead, pass --rebuild:

baler compile --rebuild
# or, naming the project root explicitly:
baler compile . --rebuild

This evicts the same cache entries first, then compiles anyway. The next build has no cache entry to match against, so R CMD SHLIB has to run for real, even if the source hasn’t changed since the last build.

--clean and --rebuild conflict with each other. Passing both is a hard error at the CLI level, not a runtime choice between them.