diff --git a/DESCRIPTION b/DESCRIPTION index d668e342..bea26372 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -55,4 +55,4 @@ Config/Needs/cpp11/cpp_register: vctrs Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.0 diff --git a/R/vendor.R b/R/vendor.R index 5fe10fe2..07cda1e3 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -4,60 +4,102 @@ #' project is using. It is often used in the go language community. #' #' This function vendors cpp11 into your package by copying the cpp11 -#' headers into the `inst/include` folder of your package and adding -#' 'cpp11 version: XYZ' to the top of the files, where XYZ is the version of -#' cpp11 currently installed on your machine. +#' headers into the `src/vendor` folder and adding 'cpp11 version: XYZ' to the +#' top of the files, where XYZ is the version of cpp11 currently installed on +#' your machine. #' #' If you choose to vendor the headers you should _remove_ `LinkingTo: -#' cpp11` from your DESCRIPTION. +#' cpp11` from your DESCRIPTION. This is done automatically by this function. #' #' **Note**: vendoring places the responsibility of updating the code on #' **you**. Bugfixes and new features in cpp11 will not be available for your #' code until you run `cpp_vendor()` again. #' -#' @inheritParams cpp_register +#' @param path The path to vendor the code into. The default is `./inst/include/`. #' @return The file path to the vendored code (invisibly). #' @export #' @examples #' # create a new directory -#' dir <- tempfile() +#' dir <- tempdir() #' dir.create(dir) #' #' # vendor the cpp11 headers into the directory #' cpp_vendor(dir) #' -#' list.files(file.path(dir, "inst", "include", "cpp11")) -#' #' # cleanup #' unlink(dir, recursive = TRUE) -cpp_vendor <- function(path = ".") { - new <- file.path(path, "inst", "include", "cpp11") +cpp_vendor <- function(path = "./inst/include") { + new <- file.path(path, "cpp11") if (dir.exists(new)) { stop("'", new, "' already exists\n * run unlink('", new, "', recursive = TRUE)", call. = FALSE) } - dir.create(new , recursive = TRUE, showWarnings = FALSE) + # Vendor cpp11 ---- + + dir.create( + new, + recursive = TRUE, + showWarnings = FALSE + ) + + current_cpp11 <- system.file( + "include", + "cpp11", + package = "cpp11" + ) - current <- system.file("include", "cpp11", package = "cpp11") - if (!nzchar(current)) { + if (!nzchar(current_cpp11)) { stop("cpp11 is not installed", call. = FALSE) } cpp11_version <- utils::packageVersion("cpp11") - cpp11_header <- sprintf("// cpp11 version: %s\n// vendored on: %s", cpp11_version, Sys.Date()) + cpp11_header <- sprintf( + "// cpp11 version: %s\n// vendored on: %s", + cpp11_version, + Sys.Date() + ) + + write_header( + path, "cpp11.hpp", "cpp11", + cpp11_header + ) + + copy_files( + list.files(current_cpp11, full.names = TRUE), + path, "cpp11", cpp11_header + ) + + # Additional steps to make vendoring work ---- + + message(paste( + "Makevars and/or Makevars.win should have a line such as", + "'PKG_CPPFLAGS = -I../inst/include'" + )) - files <- list.files(current, full.names = TRUE) + message("DESCRIPTION should not have lines such as 'LinkingTo: cpp11'") + invisible(path) +} + +write_header <- function(path, header, pkg, cpp11_header) { writeLines( - c(cpp11_header, readLines(system.file("include", "cpp11.hpp", package = "cpp11"))), - file.path(dirname(new), "cpp11.hpp") + c( + cpp11_header, + readLines( + system.file("include", header, package = pkg) + ) + ), + file.path(path, header) ) +} +copy_files <- function(files, path, out, cpp11_header) { for (f in files) { - writeLines(c(cpp11_header, readLines(f)), file.path(new, basename(f))) + writeLines( + c(cpp11_header, readLines(f)), + file.path(path, out, basename(f)) + ) } - - invisible(new) } diff --git a/cpp11test/R/cpp11.R b/cpp11test/R/cpp11.R index da84b9ec..51e62dc3 100644 --- a/cpp11test/R/cpp11.R +++ b/cpp11test/R/cpp11.R @@ -8,30 +8,6 @@ data_frame_ <- function() { .Call(`_cpp11test_data_frame_`) } -my_stop_n1fmt <- function(mystring) { - invisible(.Call(`_cpp11test_my_stop_n1fmt`, mystring)) -} - -my_stop_n2fmt <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_stop_n2fmt`, mystring, myarg)) -} - -my_warning_n1fmt <- function(mystring) { - invisible(.Call(`_cpp11test_my_warning_n1fmt`, mystring)) -} - -my_warning_n2fmt <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_warning_n2fmt`, mystring, myarg)) -} - -my_message_n1fmt <- function(mystring) { - invisible(.Call(`_cpp11test_my_message_n1fmt`, mystring)) -} - -my_message_n2fmt <- function(mystring, myarg) { - invisible(.Call(`_cpp11test_my_message_n2fmt`, mystring, myarg)) -} - my_stop <- function(mystring, myarg) { invisible(.Call(`_cpp11test_my_stop`, mystring, myarg)) } @@ -56,6 +32,30 @@ my_message_n1 <- function(mystring) { invisible(.Call(`_cpp11test_my_message_n1`, mystring)) } +my_stop_n1fmt <- function(mystring) { + invisible(.Call(`_cpp11test_my_stop_n1fmt`, mystring)) +} + +my_stop_n2fmt <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_stop_n2fmt`, mystring, myarg)) +} + +my_warning_n1fmt <- function(mystring) { + invisible(.Call(`_cpp11test_my_warning_n1fmt`, mystring)) +} + +my_warning_n2fmt <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_warning_n2fmt`, mystring, myarg)) +} + +my_message_n1fmt <- function(mystring) { + invisible(.Call(`_cpp11test_my_message_n1fmt`, mystring)) +} + +my_message_n2fmt <- function(mystring, myarg) { + invisible(.Call(`_cpp11test_my_message_n2fmt`, mystring, myarg)) +} + remove_altrep <- function(x) { .Call(`_cpp11test_remove_altrep`, x) } @@ -160,34 +160,6 @@ cpp11_safe_ <- function(x_sxp) { .Call(`_cpp11test_cpp11_safe_`, x_sxp) } -sum_dbl_for_ <- function(x) { - .Call(`_cpp11test_sum_dbl_for_`, x) -} - -sum_dbl_for2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_for2_`, x_sxp) -} - -sum_dbl_for3_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_for3_`, x_sxp) -} - -sum_dbl_foreach_ <- function(x) { - .Call(`_cpp11test_sum_dbl_foreach_`, x) -} - -sum_dbl_foreach2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_foreach2_`, x_sxp) -} - -sum_dbl_accumulate_ <- function(x) { - .Call(`_cpp11test_sum_dbl_accumulate_`, x) -} - -sum_dbl_accumulate2_ <- function(x_sxp) { - .Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp) -} - sum_int_for_ <- function(x) { .Call(`_cpp11test_sum_int_for_`, x) } @@ -224,6 +196,34 @@ rcpp_grow_ <- function(n_sxp) { .Call(`_cpp11test_rcpp_grow_`, n_sxp) } +sum_dbl_for_ <- function(x) { + .Call(`_cpp11test_sum_dbl_for_`, x) +} + +sum_dbl_for2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_for2_`, x_sxp) +} + +sum_dbl_for3_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_for3_`, x_sxp) +} + +sum_dbl_foreach_ <- function(x) { + .Call(`_cpp11test_sum_dbl_foreach_`, x) +} + +sum_dbl_foreach2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_foreach2_`, x_sxp) +} + +sum_dbl_accumulate_ <- function(x) { + .Call(`_cpp11test_sum_dbl_accumulate_`, x) +} + +sum_dbl_accumulate2_ <- function(x_sxp) { + .Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp) +} + test_destruction_inner <- function() { invisible(.Call(`_cpp11test_test_destruction_inner`)) } diff --git a/cpp11test/src/cpp11.cpp b/cpp11test/src/cpp11.cpp index 6120c333..50883fa2 100644 --- a/cpp11test/src/cpp11.cpp +++ b/cpp11test/src/cpp11.cpp @@ -21,54 +21,6 @@ extern "C" SEXP _cpp11test_data_frame_() { return cpp11::as_sexp(data_frame_()); END_CPP11 } -// errors.cpp -void my_stop_n1fmt(std::string mystring); -extern "C" SEXP _cpp11test_my_stop_n1fmt(SEXP mystring) { - BEGIN_CPP11 - my_stop_n1fmt(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} -// errors.cpp -void my_stop_n2fmt(std::string mystring, std::string myarg); -extern "C" SEXP _cpp11test_my_stop_n2fmt(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_stop_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} -// errors.cpp -void my_warning_n1fmt(std::string mystring); -extern "C" SEXP _cpp11test_my_warning_n1fmt(SEXP mystring) { - BEGIN_CPP11 - my_warning_n1fmt(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} -// errors.cpp -void my_warning_n2fmt(std::string mystring, std::string myarg); -extern "C" SEXP _cpp11test_my_warning_n2fmt(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_warning_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} -// errors.cpp -void my_message_n1fmt(std::string mystring); -extern "C" SEXP _cpp11test_my_message_n1fmt(SEXP mystring) { - BEGIN_CPP11 - my_message_n1fmt(cpp11::as_cpp>(mystring)); - return R_NilValue; - END_CPP11 -} -// errors.cpp -void my_message_n2fmt(std::string mystring, std::string myarg); -extern "C" SEXP _cpp11test_my_message_n2fmt(SEXP mystring, SEXP myarg) { - BEGIN_CPP11 - my_message_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); - return R_NilValue; - END_CPP11 -} // errors_fmt.cpp void my_stop(std::string mystring, int myarg); extern "C" SEXP _cpp11test_my_stop(SEXP mystring, SEXP myarg) { @@ -117,6 +69,54 @@ extern "C" SEXP _cpp11test_my_message_n1(SEXP mystring) { return R_NilValue; END_CPP11 } +// errors.cpp +void my_stop_n1fmt(std::string mystring); +extern "C" SEXP _cpp11test_my_stop_n1fmt(SEXP mystring) { + BEGIN_CPP11 + my_stop_n1fmt(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} +// errors.cpp +void my_stop_n2fmt(std::string mystring, std::string myarg); +extern "C" SEXP _cpp11test_my_stop_n2fmt(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_stop_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} +// errors.cpp +void my_warning_n1fmt(std::string mystring); +extern "C" SEXP _cpp11test_my_warning_n1fmt(SEXP mystring) { + BEGIN_CPP11 + my_warning_n1fmt(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} +// errors.cpp +void my_warning_n2fmt(std::string mystring, std::string myarg); +extern "C" SEXP _cpp11test_my_warning_n2fmt(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_warning_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} +// errors.cpp +void my_message_n1fmt(std::string mystring); +extern "C" SEXP _cpp11test_my_message_n1fmt(SEXP mystring) { + BEGIN_CPP11 + my_message_n1fmt(cpp11::as_cpp>(mystring)); + return R_NilValue; + END_CPP11 +} +// errors.cpp +void my_message_n2fmt(std::string mystring, std::string myarg); +extern "C" SEXP _cpp11test_my_message_n2fmt(SEXP mystring, SEXP myarg) { + BEGIN_CPP11 + my_message_n2fmt(cpp11::as_cpp>(mystring), cpp11::as_cpp>(myarg)); + return R_NilValue; + END_CPP11 +} // find-intervals.cpp SEXP remove_altrep(SEXP x); extern "C" SEXP _cpp11test_remove_altrep(SEXP x) { @@ -310,55 +310,6 @@ extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) { return cpp11::as_sexp(cpp11_safe_(cpp11::as_cpp>(x_sxp))); END_CPP11 } -// sum.cpp -double sum_dbl_for_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_for2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_for2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_for3_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_for3_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_for3_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_foreach_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_foreach_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_foreach_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_foreach2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_foreach2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_foreach2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} -// sum.cpp -double sum_dbl_accumulate_(cpp11::doubles x); -extern "C" SEXP _cpp11test_sum_dbl_accumulate_(SEXP x) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_accumulate_(cpp11::as_cpp>(x))); - END_CPP11 -} -// sum.cpp -double sum_dbl_accumulate2_(SEXP x_sxp); -extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) { - BEGIN_CPP11 - return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp>(x_sxp))); - END_CPP11 -} // sum_int.cpp double sum_int_for_(cpp11::integers x); extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) { @@ -422,6 +373,55 @@ extern "C" SEXP _cpp11test_rcpp_grow_(SEXP n_sxp) { return cpp11::as_sexp(rcpp_grow_(cpp11::as_cpp>(n_sxp))); END_CPP11 } +// sum.cpp +double sum_dbl_for_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_for2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_for2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_for3_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_for3_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_for3_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_foreach_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_foreach_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_foreach_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_foreach2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_foreach2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_foreach2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} +// sum.cpp +double sum_dbl_accumulate_(cpp11::doubles x); +extern "C" SEXP _cpp11test_sum_dbl_accumulate_(SEXP x) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_accumulate_(cpp11::as_cpp>(x))); + END_CPP11 +} +// sum.cpp +double sum_dbl_accumulate2_(SEXP x_sxp); +extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) { + BEGIN_CPP11 + return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp>(x_sxp))); + END_CPP11 +} // test-protect-nested.cpp void test_destruction_inner(); extern "C" SEXP _cpp11test_test_destruction_inner() { diff --git a/man/cpp_vendor.Rd b/man/cpp_vendor.Rd index 857e49cf..1e9253fc 100644 --- a/man/cpp_vendor.Rd +++ b/man/cpp_vendor.Rd @@ -4,10 +4,10 @@ \alias{cpp_vendor} \title{Vendor the cpp11 dependency} \usage{ -cpp_vendor(path = ".") +cpp_vendor(path = "./inst/include") } \arguments{ -\item{path}{The path to the package root directory} +\item{path}{The path to vendor the code into. The default is \verb{./inst/include/}.} } \value{ The file path to the vendored code (invisibly). @@ -18,11 +18,11 @@ project is using. It is often used in the go language community. } \details{ This function vendors cpp11 into your package by copying the cpp11 -headers into the \code{inst/include} folder of your package and adding -'cpp11 version: XYZ' to the top of the files, where XYZ is the version of -cpp11 currently installed on your machine. +headers into the \code{src/vendor} folder and adding 'cpp11 version: XYZ' to the +top of the files, where XYZ is the version of cpp11 currently installed on +your machine. -If you choose to vendor the headers you should \emph{remove} \code{LinkingTo: cpp11} from your DESCRIPTION. +If you choose to vendor the headers you should \emph{remove} \code{LinkingTo: cpp11} from your DESCRIPTION. This is done automatically by this function. \strong{Note}: vendoring places the responsibility of updating the code on \strong{you}. Bugfixes and new features in cpp11 will not be available for your @@ -30,14 +30,12 @@ code until you run \code{cpp_vendor()} again. } \examples{ # create a new directory -dir <- tempfile() +dir <- tempdir() dir.create(dir) # vendor the cpp11 headers into the directory cpp_vendor(dir) -list.files(file.path(dir, "inst", "include", "cpp11")) - # cleanup unlink(dir, recursive = TRUE) } diff --git a/tests/testthat/_snaps/register.md b/tests/testthat/_snaps/register.md index 0e4b143a..75125c45 100644 --- a/tests/testthat/_snaps/register.md +++ b/tests/testthat/_snaps/register.md @@ -2,7 +2,7 @@ Code cpp_register(p, quiet = FALSE) - Message + Message i 1 functions decorated with [[cpp11::register]] v generated file 'cpp11.R' v generated file 'cpp11.cpp' diff --git a/tests/testthat/_snaps/source.md b/tests/testthat/_snaps/source.md index 15935031..ad9152aa 100644 --- a/tests/testthat/_snaps/source.md +++ b/tests/testthat/_snaps/source.md @@ -2,7 +2,8 @@ Code cpp_source(i_do_not_exist) - Error - Can't find `file` at this path: + Condition + Error: + ! Can't find `file` at this path: {NON_EXISTENT_FILEPATH} diff --git a/tests/testthat/test-vendor.R b/tests/testthat/test-vendor.R index 361c9ad9..3cf21f8f 100644 --- a/tests/testthat/test-vendor.R +++ b/tests/testthat/test-vendor.R @@ -24,8 +24,8 @@ describe("cpp_vendor", { cpp_vendor(pkg_path(pkg)) - expect_true(dir.exists(file.path(p, "inst", "include", "cpp11"))) - expect_true(file.exists(file.path(p, "inst", "include", "cpp11.hpp"))) - expect_true(file.exists(file.path(p, "inst", "include", "cpp11", "declarations.hpp"))) + expect_true(dir.exists(file.path(p, "cpp11"))) + expect_true(file.exists(file.path(p, "cpp11.hpp"))) + expect_true(file.exists(file.path(p, "cpp11", "declarations.hpp"))) }) })