Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Rcpp dependency with cpp11 #299

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
^docs$
^pkgdown$
^CRAN-SUBMISSION$
^\.vscode$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ NEWS.html
script.R
revdep/cloud.noindex
docs
.vscode
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: pkgload
Title: Simulate Package Installation and Attach
Version: 1.4.0.9000
Version: 1.4.0.9001
Authors@R: c(
person("Hadley", "Wickham", role = "aut"),
person("Winston", "Chang", role = "aut"),
Expand All @@ -20,6 +20,7 @@ Depends:
R (>= 3.4.0)
Imports:
cli (>= 3.3.0),
decor,
desc,
fs,
glue,
Expand All @@ -35,7 +36,7 @@ Suggests:
jsonlite,
mathjaxr,
pak,
Rcpp,
cpp11,
remotes,
rstudioapi,
testthat (>= 3.2.1.1),
Expand All @@ -48,3 +49,5 @@ Config/testthat/start-first: dll
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
LinkingTo:
cpp11
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export(run_example)
export(unload)
export(unregister)
import(rlang)
useDynLib(pkgload, .registration = TRUE)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* The generator of `compile_commands.json` is now more reliable in the presence
of extra whitespace in `make`'s output (#288, @TimTaylor).

* Refactored to use the "cpp11" package for C++ code instead of "Rcpp". (@pachadotdev, #299)
pachadotdev marked this conversation as resolved.
Show resolved Hide resolved

# pkgload 1.4.0

Expand Down
5 changes: 5 additions & 0 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Generated by cpp11: do not edit by hand

fun <- function() {
invisible(.Call(`_pkgload_fun`))
}
1 change: 1 addition & 0 deletions R/pkgload-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"_PACKAGE"

## usethis namespace: start
#' @useDynLib pkgload, .registration = TRUE
## usethis namespace: end
NULL
3 changes: 3 additions & 0 deletions R/side-effects.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
side_effect_decor <- function() {
decor::parse_cpp_function("void fun() {}")
}
1 change: 1 addition & 0 deletions man/pkgload-package.Rd

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

3 changes: 3 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.o
*.so
*.dll
6 changes: 6 additions & 0 deletions src/code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <cpp11.hpp>
using namespace cpp11;

// just for the side effect of registering the function
[[cpp11::register]]
void fun() {}

Check warning on line 6 in src/code.cpp

View check run for this annotation

Codecov / codecov/patch

src/code.cpp#L6

Added line #L6 was not covered by tests
28 changes: 28 additions & 0 deletions src/cpp11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Generated by cpp11: do not edit by hand
// clang-format off


#include "cpp11/declarations.hpp"
#include <R_ext/Visibility.h>

// code.cpp
void fun();
extern "C" SEXP _pkgload_fun() {
BEGIN_CPP11
fun();
return R_NilValue;
END_CPP11
}

extern "C" {
static const R_CallMethodDef CallEntries[] = {
{"_pkgload_fun", (DL_FUNC) &_pkgload_fun, 0},
{NULL, NULL, 0}
};
}

extern "C" attribute_visible void R_init_pkgload(DllInfo* dll){
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
R_forceSymbols(dll, TRUE);
}
14 changes: 7 additions & 7 deletions tests/testthat/test-dll.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,21 @@ test_that("Specific functions from DLLs listed in NAMESPACE can be called", {
})


test_that("load_all() can compile and load DLLs linked to Rcpp", {
test_that("load_all() can compile and load DLLs linked to cpp11", {

pkgbuild::clean_dll("testDllRcpp")
pkgbuild::clean_dll("testDllcpp11")

load_all("testDllRcpp", reset = TRUE, quiet = TRUE)
load_all("testDllcpp11", reset = TRUE, quiet = TRUE)

# Check that it's loaded properly by calling the hello world function
# which returns a list
expect_type(rcpp_hello_world(), "list")
expect_type(cpp11_hello_world(), "list")

# Check whether attribute compilation occurred and that exported
# names are available from load_all
expect_true(rcpp_test_attributes())
expect_true(cpp11_test_attributes())

# Unload and clean out compiled objects
unload("testDllRcpp")
pkgbuild::clean_dll("testDllRcpp")
unload("testDllcpp11")
pkgbuild::clean_dll("testDllcpp11")
})
3 changes: 3 additions & 0 deletions tests/testthat/test-side-effect-decor.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test_that("side effect decor", {
expect_type(side_effect_decor(), "list")
})
9 changes: 0 additions & 9 deletions tests/testthat/testDllRcpp/DESCRIPTION

This file was deleted.

3 changes: 0 additions & 3 deletions tests/testthat/testDllRcpp/NAMESPACE

This file was deleted.

1 change: 0 additions & 1 deletion tests/testthat/testDllRcpp/R/.gitignore

This file was deleted.

3 changes: 0 additions & 3 deletions tests/testthat/testDllRcpp/src/.gitignore

This file was deleted.

15 changes: 0 additions & 15 deletions tests/testthat/testDllRcpp/src/rcpp_hello_world.cpp

This file was deleted.

16 changes: 16 additions & 0 deletions tests/testthat/testDllcpp11/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Package: testDllcpp11
Title: Test package for compiling DLLs that link to cpp11
License: GPL (>= 3)
Description: Test package for compiling DLLs that link to cpp11.
Just for testing purposes.
Authors@R: c(
person("Hadley", "Wickham", role = c("aut", "cre"),
email = "[email protected]"),
person("Mauricio", "Vargas Sepulveda", role = "aut")
)
Version: 0.1
LinkingTo:
cpp11
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Encoding: UTF-8
3 changes: 3 additions & 0 deletions tests/testthat/testDllcpp11/NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by roxygen2: do not edit by hand

useDynLib(testDllcpp11, .registration = TRUE)
1 change: 1 addition & 0 deletions tests/testthat/testDllcpp11/R/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp11.R
14 changes: 14 additions & 0 deletions tests/testthat/testDllcpp11/R/testDllcpp11-package.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#' @useDynLib testDllcpp11, .registration = TRUE
"_PACKAGE"

#' Hello World
#' @export
cpp11_hello_world <- function() {
cpp11_hello_world_()
}

#' Test Attributes
#' @export
cpp11_test_attributes <- function() {
cpp11_test_attributes_()
}
19 changes: 19 additions & 0 deletions tests/testthat/testDllcpp11/man/testDllcpp11-package.Rd

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

4 changes: 4 additions & 0 deletions tests/testthat/testDllcpp11/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cpp11.cpp
*.o
*.so
*.dll
14 changes: 14 additions & 0 deletions tests/testthat/testDllcpp11/src/cpp11_hello_world.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <cpp11.hpp>
using namespace cpp11;

[[cpp11::register]] sexp cpp11_hello_world_() {
writable::strings x = {"foo", "bar"};
writable::doubles y = {0.0, 1.0};
writable::list z = {x, y};

return z;
}

[[cpp11::register]] bool cpp11_test_attributes_() {
return true;
}
Loading