porter

The goal of {porter} is to generate port files for rdyncall to support Foreign Function Interface (FFI) for C Libraries in R. It uses CastXML, a C-family abstract syntax tree XML output tool to parse C header files.

Installation

You can install porter from CRAN with:

install.packages("porter")

You can install the development version of porter from GitHub with:

remotes::install_github("hongyuanjia/porter")

Example

Locate CastXML

porter uses CastXML but does not download or install it. Install CastXML with your system package manager before calling port().

locate_castxml() returns the CastXML executable porter will use. By default it checks Sys.which("castxml"), then common package-manager paths for Homebrew, Linuxbrew, Scoop, Chocolatey, and conda. Set options(porter.castxml = ...) to force a specific executable or installation directory.

library(porter)

locate_castxml()
#>                         0.7.0
#> "/opt/homebrew/bin/castxml"

Generate port files for C libraries

Run port() with the path of a C header file:

library(porter)

header <- tempfile(fileext = ".h")
writeLines(c(
  "typedef struct Point { int x; double y; } Point;",
  "int add(int a, int b);",
  "int message(const char *fmt, ...);"
), header)

p <- port(header)

p <- port_set(p,
  Package = "Example",
  Version = "1.0",
  Library = "example"
)

p
#> Package: Example
#> Version: 1.0
#> Library: example
#> Function: add(ii)i a b;
#> Variadic: message(Z)i fmt;
#> FuncPtr: <None>
#> Enum: <None>
#> Struct: Point{id}x y;
#> Union: <None>

Use port_write() to save the port file.

port_write(p, file.path(tempdir(), "Example.dynport"))

Author

Hongyuan Jia

License

The project is released under the terms of MIT License.

Copyright © 2023 Hongyuan Jia