diff --git a/.Rbuildignore b/.Rbuildignore index 485da9a2..39a6934c 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -13,3 +13,4 @@ ^docs$ ^pkgdown$ ^CRAN-SUBMISSION$ +^\.vscode$ diff --git a/.gitignore b/.gitignore index a54c159a..64c0f93c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ NEWS.html script.R revdep/cloud.noindex docs +.vscode diff --git a/DESCRIPTION b/DESCRIPTION index 41f2d7ec..2ae38764 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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"), @@ -20,6 +20,7 @@ Depends: R (>= 3.4.0) Imports: cli (>= 3.3.0), + decor, desc, fs, glue, @@ -35,7 +36,7 @@ Suggests: jsonlite, mathjaxr, pak, - Rcpp, + cpp11, remotes, rstudioapi, testthat (>= 3.2.1.1), @@ -48,3 +49,5 @@ Config/testthat/start-first: dll Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.2 +LinkingTo: + cpp11 diff --git a/NAMESPACE b/NAMESPACE index cc2a17b5..9f34189f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -34,3 +34,4 @@ export(run_example) export(unload) export(unregister) import(rlang) +useDynLib(pkgload, .registration = TRUE) diff --git a/NEWS.md b/NEWS.md index b14601e0..4f381442 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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) # pkgload 1.4.0 diff --git a/R/cpp11.R b/R/cpp11.R new file mode 100644 index 00000000..2f8b9f12 --- /dev/null +++ b/R/cpp11.R @@ -0,0 +1,5 @@ +# Generated by cpp11: do not edit by hand + +fun <- function() { + invisible(.Call(`_pkgload_fun`)) +} diff --git a/R/pkgload-package.R b/R/pkgload-package.R index a65cf643..0238c5d9 100644 --- a/R/pkgload-package.R +++ b/R/pkgload-package.R @@ -2,5 +2,6 @@ "_PACKAGE" ## usethis namespace: start +#' @useDynLib pkgload, .registration = TRUE ## usethis namespace: end NULL diff --git a/R/side-effects.R b/R/side-effects.R new file mode 100644 index 00000000..1942d3f8 --- /dev/null +++ b/R/side-effects.R @@ -0,0 +1,3 @@ +side_effect_decor <- function() { + decor::parse_cpp_function("void fun() {}") +} \ No newline at end of file diff --git a/man/pkgload-package.Rd b/man/pkgload-package.Rd index 6f9a366d..261a43f5 100644 --- a/man/pkgload-package.Rd +++ b/man/pkgload-package.Rd @@ -29,6 +29,7 @@ Authors: Other contributors: \itemize{ + \item Mauricio Vargas Sepulveda (\href{https://orcid.org/0000-0003-1017-7574}{ORCID}) [contributor] \item Posit Software, PBC [copyright holder, funder] \item R Core team (Some namespace and vignette code extracted from base R) [contributor] } diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 00000000..22034c46 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,3 @@ +*.o +*.so +*.dll diff --git a/src/code.cpp b/src/code.cpp new file mode 100644 index 00000000..24637257 --- /dev/null +++ b/src/code.cpp @@ -0,0 +1,6 @@ +#include +using namespace cpp11; + +// just for the side effect of registering the function +[[cpp11::register]] +void fun() {} diff --git a/src/cpp11.cpp b/src/cpp11.cpp new file mode 100644 index 00000000..2da69d07 --- /dev/null +++ b/src/cpp11.cpp @@ -0,0 +1,28 @@ +// Generated by cpp11: do not edit by hand +// clang-format off + + +#include "cpp11/declarations.hpp" +#include + +// 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); +} diff --git a/tests/testthat/test-dll.R b/tests/testthat/test-dll.R index d2d1d1bf..f223cb91 100644 --- a/tests/testthat/test-dll.R +++ b/tests/testthat/test-dll.R @@ -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") }) diff --git a/tests/testthat/test-side-effect-decor.R b/tests/testthat/test-side-effect-decor.R new file mode 100644 index 00000000..fe90b494 --- /dev/null +++ b/tests/testthat/test-side-effect-decor.R @@ -0,0 +1,3 @@ +test_that("side effect decor", { + expect_type(side_effect_decor(), "list") +}) diff --git a/tests/testthat/testDllRcpp/DESCRIPTION b/tests/testthat/testDllRcpp/DESCRIPTION deleted file mode 100644 index b218be52..00000000 --- a/tests/testthat/testDllRcpp/DESCRIPTION +++ /dev/null @@ -1,9 +0,0 @@ -Package: testDllRcpp -Title: Test package for compiling DLLs that link to Rcpp -License: GPL-2 -Description: -Author: Hadley -Maintainer: Hadley -Version: 0.1 -Depends: Rcpp (>= 0.10.0) -LinkingTo: Rcpp diff --git a/tests/testthat/testDllRcpp/NAMESPACE b/tests/testthat/testDllRcpp/NAMESPACE deleted file mode 100644 index 49939634..00000000 --- a/tests/testthat/testDllRcpp/NAMESPACE +++ /dev/null @@ -1,3 +0,0 @@ -useDynLib(testDllRcpp, .registration = TRUE) -export(rcpp_hello_world) -export(rcpp_test_attributes) diff --git a/tests/testthat/testDllRcpp/R/.gitignore b/tests/testthat/testDllRcpp/R/.gitignore deleted file mode 100644 index b1f3275a..00000000 --- a/tests/testthat/testDllRcpp/R/.gitignore +++ /dev/null @@ -1 +0,0 @@ -RcppExports.R diff --git a/tests/testthat/testDllRcpp/src/.gitignore b/tests/testthat/testDllRcpp/src/.gitignore deleted file mode 100644 index 48efaf8d..00000000 --- a/tests/testthat/testDllRcpp/src/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -RcppExports.cpp -*.o -*.so diff --git a/tests/testthat/testDllRcpp/src/rcpp_hello_world.cpp b/tests/testthat/testDllRcpp/src/rcpp_hello_world.cpp deleted file mode 100644 index e77afd3a..00000000 --- a/tests/testthat/testDllRcpp/src/rcpp_hello_world.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include - -// [[Rcpp::export]] -SEXP rcpp_hello_world() { - using namespace Rcpp; - - CharacterVector x = CharacterVector::create("foo", "bar"); - NumericVector y = NumericVector::create(0.0, 1.0); - List z = List::create(x, y); - - return z; -} - -// [[Rcpp::export]] -bool rcpp_test_attributes() { return true; } diff --git a/tests/testthat/testDllcpp11/DESCRIPTION b/tests/testthat/testDllcpp11/DESCRIPTION new file mode 100644 index 00000000..34e422b7 --- /dev/null +++ b/tests/testthat/testDllcpp11/DESCRIPTION @@ -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 = "hadley@posit.co"), + person("Mauricio", "Vargas Sepulveda", role = "aut") + ) +Version: 0.1 +LinkingTo: + cpp11 +Roxygen: list(markdown = TRUE) +RoxygenNote: 7.3.2 +Encoding: UTF-8 diff --git a/tests/testthat/testDllcpp11/NAMESPACE b/tests/testthat/testDllcpp11/NAMESPACE new file mode 100644 index 00000000..f8242617 --- /dev/null +++ b/tests/testthat/testDllcpp11/NAMESPACE @@ -0,0 +1,3 @@ +# Generated by roxygen2: do not edit by hand + +useDynLib(testDllcpp11, .registration = TRUE) diff --git a/tests/testthat/testDllcpp11/R/.gitignore b/tests/testthat/testDllcpp11/R/.gitignore new file mode 100644 index 00000000..96dae645 --- /dev/null +++ b/tests/testthat/testDllcpp11/R/.gitignore @@ -0,0 +1 @@ +cpp11.R diff --git a/tests/testthat/testDllcpp11/R/testDllcpp11-package.R b/tests/testthat/testDllcpp11/R/testDllcpp11-package.R new file mode 100644 index 00000000..258b7bbb --- /dev/null +++ b/tests/testthat/testDllcpp11/R/testDllcpp11-package.R @@ -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_() +} diff --git a/tests/testthat/testDllcpp11/man/testDllcpp11-package.Rd b/tests/testthat/testDllcpp11/man/testDllcpp11-package.Rd new file mode 100644 index 00000000..3a87af12 --- /dev/null +++ b/tests/testthat/testDllcpp11/man/testDllcpp11-package.Rd @@ -0,0 +1,19 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/testDllcpp11-package.R +\docType{package} +\name{testDllcpp11-package} +\alias{testDllcpp11} +\alias{testDllcpp11-package} +\title{testDllcpp11: Test package for compiling DLLs that link to cpp11} +\description{ +Test package for compiling DLLs that link to cpp11. Just for testing purposes. +} +\author{ +\strong{Maintainer}: Hadley Wickham \email{hadley@posit.co} + +Authors: +\itemize{ + \item Mauricio Vargas Sepulveda +} + +} diff --git a/tests/testthat/testDllcpp11/src/.gitignore b/tests/testthat/testDllcpp11/src/.gitignore new file mode 100644 index 00000000..32523300 --- /dev/null +++ b/tests/testthat/testDllcpp11/src/.gitignore @@ -0,0 +1,4 @@ +cpp11.cpp +*.o +*.so +*.dll diff --git a/tests/testthat/testDllcpp11/src/cpp11_hello_world.cpp b/tests/testthat/testDllcpp11/src/cpp11_hello_world.cpp new file mode 100644 index 00000000..edab927d --- /dev/null +++ b/tests/testthat/testDllcpp11/src/cpp11_hello_world.cpp @@ -0,0 +1,14 @@ +#include +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; +}