Skip to content

Commit

Permalink
Empty R package with Rust implementation
Browse files Browse the repository at this point in the history
Using in R:
```R
usethis::create_package(‘path/to/libkrigingtemplate’)
setwd(‘/path/to/libkrigingtemplate/‘)

cf https://extendr.github.io/rextendr/articles/package.html
```
  • Loading branch information
hpwxf committed Sep 3, 2023
1 parent fbd5e60 commit 17ccccc
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.idea
/*.tar.gz
/.Rproj.user
/.Rhistory
/.RData
/.Ruserdata
*.a
*.o
*.so
/src/rust/vendor
/src/rust/vendor.tar.xz
/src/rust/**/target
12 changes: 12 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Package: libkrigingtemplate
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"),
comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
license
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.1
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Generated by roxygen2: do not edit by hand

8 changes: 8 additions & 0 deletions R/extendr-wrappers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#' @docType package
#' @usage NULL
#' @useDynLib libkrigingtemplate, .registration = TRUE
NULL

#' Return string `"Hello world!"` to R.
#' @export
hello_world <- function() .Call(wrap__hello_world)
4 changes: 4 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.o
*.so
*.dll
target
16 changes: 16 additions & 0 deletions src/Makevars
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
LIBDIR = ./rust/target/release
STATLIB = $(LIBDIR)/liblibkrigingtemplate.a
PKG_LIBS = -L$(LIBDIR) -llibkrigingtemplate

all: C_clean

$(SHLIB): $(STATLIB)

$(STATLIB):
cargo build --lib --release --manifest-path=./rust/Cargo.toml

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

clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) rust/target
17 changes: 17 additions & 0 deletions src/Makevars.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
TARGET = $(subst 64,x86_64,$(subst 32,i686,$(WIN)))-pc-windows-gnu
LIBDIR = ./rust/target/$(TARGET)/release
STATLIB = $(LIBDIR)/liblibkrigingtemplate.a
PKG_LIBS = -L$(LIBDIR) -llibkrigingtemplate -lws2_32 -ladvapi32 -luserenv

all: C_clean

$(SHLIB): $(STATLIB)

$(STATLIB):
cargo build --target=$(TARGET) --lib --release --manifest-path=./rust/Cargo.toml

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

clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) rust/target
8 changes: 8 additions & 0 deletions 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_libkrigingtemplate_extendr(void *dll);

void R_init_libkrigingtemplate(void *dll) {
R_init_libkrigingtemplate_extendr(dll);
}
96 changes: 96 additions & 0 deletions src/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = 'libkrigingtemplate'
version = '0.1.0'
edition = '2018'

[lib]
crate-type = [ 'staticlib' ]

[dependencies]
extendr-api = '*'
16 changes: 16 additions & 0 deletions src/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use extendr_api::prelude::*;

/// Return string `"Hello world!"` to R.
/// @export
#[extendr]
fn hello_world() -> &'static str {
"Hello world!"
}

// Macro to generate exports.
// This ensures exported functions are registered with R.
// See corresponding C code in `entrypoint.c`.
extendr_module! {
mod libkrigingtemplate;
fn hello_world;
}

0 comments on commit 17ccccc

Please sign in to comment.