Skip to content

Commit

Permalink
add R package
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahParry committed Nov 26, 2023
1 parent 229af65 commit 577f15c
Show file tree
Hide file tree
Showing 15 changed files with 1,666 additions and 0 deletions.
2 changes: 2 additions & 0 deletions r/.Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
1 change: 1 addition & 0 deletions r/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.Rproj.user
14 changes: 14 additions & 0 deletions r/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Package: serdesri
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("Josiah", "Parry", , "josiah.parry@gmail.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-9910-865X"))
Description: What the package does (one paragraph).
License: MIT + file LICENSE
Encoding: UTF-8
Language: en
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Config/rextendr/version: 0.3.1.9000
SystemRequirements: Cargo (rustc package manager)
5 changes: 5 additions & 0 deletions r/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(parse_esri_json_raw)
export(parse_esri_json_str)
useDynLib(serdesri, .registration = TRUE)
21 changes: 21 additions & 0 deletions r/R/extendr-wrappers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by extendr: Do not edit by hand

# nolint start

#
# This file was created with the following call:
# .Call("wrap__make_serdesri_wrappers", use_symbols = TRUE, package_name = "serdesri")

#' @docType package
#' @usage NULL
#' @useDynLib serdesri, .registration = TRUE
NULL

#' @export
parse_esri_json_str <- function(str, n_dim) .Call(wrap__parse_esri_json_str, str, n_dim)

#' @export
parse_esri_json_raw <- function(raw, n_dim) .Call(wrap__parse_esri_json_raw, raw, n_dim)


# nolint end
22 changes: 22 additions & 0 deletions r/serdesri.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
5 changes: 5 additions & 0 deletions r/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.o
*.so
*.dll
target
.cargo
30 changes: 30 additions & 0 deletions r/src/Makevars
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
TARGET_DIR = ./rust/target
LIBDIR = $(TARGET_DIR)/release
STATLIB = $(LIBDIR)/libserdesri.a
PKG_LIBS = -L$(LIBDIR) -lserdesri

all: C_clean

$(SHLIB): $(STATLIB)

CARGOTMP = $(CURDIR)/.cargo

$(STATLIB):
# In some environments, ~/.cargo/bin might not be included in PATH, so we need
# to set it here to ensure cargo can be invoked. It is appended to PATH and
# therefore is only used if cargo is absent from the user's PATH.
if [ "$(NOT_CRAN)" != "true" ]; then \
export CARGO_HOME=$(CARGOTMP); \
fi && \
export PATH="$(PATH):$(HOME)/.cargo/bin" && \
cargo build --lib --release --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR)
if [ "$(NOT_CRAN)" != "true" ]; then \
rm -Rf $(CARGOTMP) && \
rm -Rf $(LIBDIR)/build; \
fi

C_clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS)

clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) rust/target
5 changes: 5 additions & 0 deletions r/src/Makevars.ucrt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Rtools42 doesn't have the linker in the location that cargo expects, so we
# need to overwrite it via configuration.
CARGO_LINKER = x86_64-w64-mingw32.static.posix-gcc.exe

include Makevars.win
40 changes: 40 additions & 0 deletions r/src/Makevars.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
TARGET = $(subst 64,x86_64,$(subst 32,i686,$(WIN)))-pc-windows-gnu

TARGET_DIR = ./rust/target
LIBDIR = $(TARGET_DIR)/$(TARGET)/release
STATLIB = $(LIBDIR)/libserdesri.a
PKG_LIBS = -L$(LIBDIR) -lserdesri -lws2_32 -ladvapi32 -luserenv -lbcrypt -lntdll

all: C_clean

$(SHLIB): $(STATLIB)

CARGOTMP = $(CURDIR)/.cargo

$(STATLIB):
mkdir -p $(TARGET_DIR)/libgcc_mock
# `rustc` adds `-lgcc_eh` flags to the compiler, but Rtools' GCC doesn't have
# `libgcc_eh` due to the compilation settings. So, in order to please the
# compiler, we need to add empty `libgcc_eh` to the library search paths.
#
# For more details, please refer to
# https://github.com/r-windows/rtools-packages/blob/2407b23f1e0925bbb20a4162c963600105236318/mingw-w64-gcc/PKGBUILD#L313-L316
touch $(TARGET_DIR)/libgcc_mock/libgcc_eh.a

# CARGO_LINKER is provided in Makevars.ucrt for R >= 4.2
if [ "$(NOT_CRAN)" != "true" ]; then \
export CARGO_HOME=$(CARGOTMP); \
fi && \
export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER="$(CARGO_LINKER)" && \
export LIBRARY_PATH="$${LIBRARY_PATH};$(CURDIR)/$(TARGET_DIR)/libgcc_mock" && \
cargo build --target=$(TARGET) --lib --release --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR)
if [ "$(NOT_CRAN)" != "true" ]; then \
rm -Rf $(CARGOTMP) && \
rm -Rf $(LIBDIR)/build; \
fi

C_clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS)

clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) $(TARGET_DIR)
8 changes: 8 additions & 0 deletions r/src/entrypoint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// We need to forward routine registration from C to Rust
// to avoid the linker removing the static library.

void R_init_serdesri_extendr(void *dll);

void R_init_serdesri(void *dll) {
R_init_serdesri_extendr(dll);
}

0 comments on commit 577f15c

Please sign in to comment.