From 9ad7f6a6303270043fe10e61f467aa06043db7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pach=C3=A1?= Date: Sun, 29 Jan 2023 16:50:19 -0500 Subject: [PATCH 1/9] add _matrix<> to reference table --- vignettes/converting.Rmd | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/vignettes/converting.Rmd b/vignettes/converting.Rmd index a0dbecb2..abcd27a1 100644 --- a/vignettes/converting.Rmd +++ b/vignettes/converting.Rmd @@ -26,22 +26,24 @@ It is also a place to highlight some of the largest differences between Rcpp and ## Class comparison table -| Rcpp | cpp11 (read-only) | cpp11 (writable) | cpp11 header | -| --- | --- | --- | --- | -| NumericVector | doubles | writable::doubles | | -| IntegerVector | integers | writable::integers | | -| CharacterVector | strings | writable::strings | | -| RawVector | raws | writable::raws | | -| List | list | writable::list | | -| RObject | sexp | | | -| XPtr | | external_pointer | | -| Environment | | environment | | -| Function | | function | | -| Environment (namespace) | | package | | -| wrap | | as_sexp | | -| as | | as_cpp | | -| stop | stop | | | -| checkUserInterrupt | check_user_interrupt | | | +| Rcpp | cpp11 (read-only) | cpp11 (writable) | cpp11 header | +| --- | --- | --- | --- | +| NumericVector | doubles | writable::doubles | | +| NumericMatrix | doubles_matrix<> | writable::doubles_matrix<> | | +| IntegerVector | integers | writable::integers | | +| IntegerMatrix | integers_matrix<> | writable::integers_matrix<> | | +| CharacterVector | strings | writable::strings | | +| RawVector | raws | writable::raws | | +| List | list | writable::list | | +| RObject | sexp | | | +| XPtr | | external_pointer | | +| Environment | | environment | | +| Function | | function | | +| Environment (namespace) | | package | | +| wrap | | as_sexp | | +| as | | as_cpp | | +| stop | stop | | | +| checkUserInterrupt | check_user_interrupt | | | ## Incomplete list of Rcpp features not included in cpp11 From 9febb60671cae3e111c4ff1b7c0aed5c9e4475b2 Mon Sep 17 00:00:00 2001 From: Mauricio Vargas Date: Wed, 31 May 2023 11:59:23 -0400 Subject: [PATCH 2/9] Additional steps to make vendoring work --- R/vendor.R | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/R/vendor.R b/R/vendor.R index 5fe10fe2..f7c4b150 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -59,5 +59,43 @@ cpp_vendor <- function(path = ".") { writeLines(c(cpp11_header, readLines(f)), file.path(new, basename(f))) } + # Additional steps to make vendoring work ---- + + # 1. Check if `src/Makevars` exists + makevars_exists <- file.exists("src/Makevars") + + # 2. If makevars exists, it should have a line that reads `PKG_CPPFLAGS = -I../inst/include` + if (isTRUE(makevars_exists)) { + makevars <- readLines("src/Makevars") + if (!any(grepl("PKG_CPPFLAGS = -I../inst/include", makevars))) { + # add the line + makevars <- c(makevars, "PKG_CPPFLAGS = -I../inst/include") + + writeLines(makevars, "src/Makevars") + + # warn about the change + cat("`PKG_CPPFLAGS = -I../inst/include` was added to src/Makevars\n") + } + } else { + # create the file + writeLines("PKG_CPPFLAGS = -I../inst/include", "src/Makevars") + + # warn about the change + cat("A new src/Makevars file was created\n") + } + + # 3. `DESCRIPTION` now should not have `LinkingTo: cpp11` or `LinkingTo: \n\tcpp11` + description <- readLines("DESCRIPTION") + + # remove the lines + description <- description[!grepl("LinkingTo: cpp11", description)] + description <- description[!grepl("LinkingTo: ", description)] + description <- description[!grepl(" cpp11", description)] + + writeLines(description, "DESCRIPTION") + + # warn about the change + cat("`LinkingTo: cpp11` was removed from DESCRIPTION\n") + invisible(new) } From e817b86134e7d35d423c223577fae574c7f59d0e Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Wed, 8 Nov 2023 21:46:59 -0500 Subject: [PATCH 3/9] vendor in src/ --- R/vendor.R | 72 ++++++++++++++++++++++++------------ man/cpp_vendor.Rd | 14 +++---- tests/testthat/test-vendor.R | 6 +-- 3 files changed, 58 insertions(+), 34 deletions(-) diff --git a/R/vendor.R b/R/vendor.R index f7c4b150..09148b63 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -4,18 +4,18 @@ #' 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. #' @return The file path to the vendored code (invisibly). #' @export #' @examples @@ -26,18 +26,18 @@ #' # vendor the cpp11 headers into the directory #' cpp_vendor(dir) #' -#' list.files(file.path(dir, "inst", "include", "cpp11")) +#' list.files(file.path(dir, "src", "vendor")) #' #' # cleanup #' unlink(dir, recursive = TRUE) -cpp_vendor <- function(path = ".") { - new <- file.path(path, "inst", "include", "cpp11") +cpp_vendor <- function(path = "./src/vendor") { + 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) + dir.create(new, recursive = TRUE, showWarnings = FALSE) current <- system.file("include", "cpp11", package = "cpp11") if (!nzchar(current)) { @@ -64,38 +64,62 @@ cpp_vendor <- function(path = ".") { # 1. Check if `src/Makevars` exists makevars_exists <- file.exists("src/Makevars") - # 2. If makevars exists, it should have a line that reads `PKG_CPPFLAGS = -I../inst/include` + # 2. If makevars exists, it should have a line that reads + # `PKG_CPPFLAGS = -I../inst/include` or similar + + vendor_line <- paste0(" -I", new) + if (isTRUE(makevars_exists)) { makevars <- readLines("src/Makevars") - if (!any(grepl("PKG_CPPFLAGS = -I../inst/include", makevars))) { - # add the line - makevars <- c(makevars, "PKG_CPPFLAGS = -I../inst/include") + + if (any(grepl("^PKG_CPPFLAGS", makevars))) { + cat("There is a `PKG_CPPFLAGS` line in src/Makevars. It will be modified.\n") + + # which line contains `PKG_CPPFLAGS`? + cppflags_line <- grep("^PKG_CPPFLAGS", makevars) + + # append the vendoring line + if (!grepl(vendor_line, makevars[cppflags_line])) { + makevars[cppflags_line] <- paste0(makevars[cppflags_line], vendor_line) + } writeLines(makevars, "src/Makevars") + } else { + # add the line + makevars <- c(makevars, paste0("PKG_CPPFLAGS = ", vendor_line)) - # warn about the change - cat("`PKG_CPPFLAGS = -I../inst/include` was added to src/Makevars\n") + writeLines(makevars, "src/Makevars") } + + cat("The existing src/Makevars was modified. Please check it.\n") } else { # create the file - writeLines("PKG_CPPFLAGS = -I../inst/include", "src/Makevars") + writeLines(paste0("PKG_CPPFLAGS = ", vendor_line), "src/Makevars") # warn about the change - cat("A new src/Makevars file was created\n") + cat("A new src/Makevars file was created.\n") } # 3. `DESCRIPTION` now should not have `LinkingTo: cpp11` or `LinkingTo: \n\tcpp11` description <- readLines("DESCRIPTION") - # remove the lines - description <- description[!grepl("LinkingTo: cpp11", description)] - description <- description[!grepl("LinkingTo: ", description)] - description <- description[!grepl(" cpp11", description)] + cpp11_in_desc <- any( + grepl("LinkingTo: cpp11", description), + grepl("LinkingTo: ", description), + grepl(" cpp11", description) + ) + + if (isTRUE(cpp11_in_desc)) { + # remove the lines + description <- description[!grepl("LinkingTo: cpp11", description)] + description <- description[!grepl("LinkingTo: ", description)] + description <- description[!grepl(" cpp11", description)] - writeLines(description, "DESCRIPTION") + writeLines(description, "DESCRIPTION") - # warn about the change - cat("`LinkingTo: cpp11` was removed from DESCRIPTION\n") + # warn about the change + cat("`LinkingTo: cpp11` was removed from DESCRIPTION.\n") + } invisible(new) } diff --git a/man/cpp_vendor.Rd b/man/cpp_vendor.Rd index 857e49cf..a02f36de 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 = "./src/vendor") } \arguments{ -\item{path}{The path to the package root directory} +\item{path}{The path to vendor the code into.} } \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 @@ -36,7 +36,7 @@ dir.create(dir) # vendor the cpp11 headers into the directory cpp_vendor(dir) -list.files(file.path(dir, "inst", "include", "cpp11")) +list.files(file.path(dir, "src", "vendor")) # cleanup unlink(dir, recursive = TRUE) diff --git a/tests/testthat/test-vendor.R b/tests/testthat/test-vendor.R index 361c9ad9..a667ce6b 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, "src", "vendor", "cpp11"))) + expect_true(file.exists(file.path(p, "src", "vendor", "cpp11.hpp"))) + expect_true(file.exists(file.path(p, "src", "vendor", "cpp11", "declarations.hpp"))) }) }) From d00114be009bf148e1ff273c7b4b61188f9676f9 Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Thu, 9 Nov 2023 11:50:56 -0500 Subject: [PATCH 4/9] fix vendor function --- R/vendor.R | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/R/vendor.R b/R/vendor.R index 09148b63..d4b10ec6 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -15,7 +15,7 @@ #' **you**. Bugfixes and new features in cpp11 will not be available for your #' code until you run `cpp_vendor()` again. #' -#' @param path The path to vendor the code into. +#' @param path The path to vendor the code into. The default is `src/vendor/`. #' @return The file path to the vendored code (invisibly). #' @export #' @examples @@ -31,13 +31,17 @@ #' # cleanup #' unlink(dir, recursive = TRUE) cpp_vendor <- function(path = "./src/vendor") { - new <- file.path(path, "cpp11") - - if (dir.exists(new)) { - stop("'", new, "' already exists\n * run unlink('", new, "', recursive = TRUE)", call. = FALSE) + if (dir.exists(path)) { + cat(sprintf("The directory '%s' already exists. Do you want to overwrite it?\n", path)) + if (utils::menu(c("Yes", "No"), graphics = FALSE) == 1L) { + unlink(path, recursive = TRUE) + } else { + return(FALSE) + } } - dir.create(new, recursive = TRUE, showWarnings = FALSE) + dir.create(path, recursive = TRUE, showWarnings = FALSE) + dir.create(file.path(path, "cpp11"), recursive = TRUE, showWarnings = FALSE) current <- system.file("include", "cpp11", package = "cpp11") if (!nzchar(current)) { @@ -52,11 +56,11 @@ cpp_vendor <- function(path = "./src/vendor") { writeLines( c(cpp11_header, readLines(system.file("include", "cpp11.hpp", package = "cpp11"))), - file.path(dirname(new), "cpp11.hpp") + file.path(path, "cpp11.hpp") ) for (f in files) { - writeLines(c(cpp11_header, readLines(f)), file.path(new, basename(f))) + writeLines(c(cpp11_header, readLines(f)), file.path(path, "cpp11", basename(f))) } # Additional steps to make vendoring work ---- @@ -67,7 +71,7 @@ cpp_vendor <- function(path = "./src/vendor") { # 2. If makevars exists, it should have a line that reads # `PKG_CPPFLAGS = -I../inst/include` or similar - vendor_line <- paste0(" -I", new) + vendor_line <- " -I vendor/" if (isTRUE(makevars_exists)) { makevars <- readLines("src/Makevars") @@ -121,5 +125,5 @@ cpp_vendor <- function(path = "./src/vendor") { cat("`LinkingTo: cpp11` was removed from DESCRIPTION.\n") } - invisible(new) + invisible(path) } From 02fe578113317c0e48b754a31eb620294ce18f4e Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Tue, 30 Jan 2024 09:59:06 -0500 Subject: [PATCH 5/9] non interactive + Windows makevars --- R/vendor.R | 172 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 121 insertions(+), 51 deletions(-) diff --git a/R/vendor.R b/R/vendor.R index d4b10ec6..f7560a0c 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -32,98 +32,168 @@ #' unlink(dir, recursive = TRUE) cpp_vendor <- function(path = "./src/vendor") { if (dir.exists(path)) { - cat(sprintf("The directory '%s' already exists. Do you want to overwrite it?\n", path)) - if (utils::menu(c("Yes", "No"), graphics = FALSE) == 1L) { - unlink(path, recursive = TRUE) + cat( + sprintf( + "The directory '%s' already exists. Do you want to overwrite it?\n", + path + ) + ) + if (interactive()) { + if (utils::menu(c("Yes", "No"), graphics = FALSE) == 1L) { + unlink(path, recursive = TRUE) + } else { + return(FALSE) + } } else { + message("Running in non-interactive mode. Exiting.") return(FALSE) } } dir.create(path, recursive = TRUE, showWarnings = FALSE) - dir.create(file.path(path, "cpp11"), recursive = TRUE, showWarnings = FALSE) - current <- system.file("include", "cpp11", package = "cpp11") - if (!nzchar(current)) { + # Vendor cpp11 ---- + + dir.create( + file.path(path, "cpp11"), + recursive = TRUE, + showWarnings = FALSE + ) + + current_cpp11 <- system.file( + "include", + "cpp11", + package = "cpp11" + ) + + 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()) - - files <- list.files(current, full.names = TRUE) + cpp11_header <- sprintf( + "// cpp11 version: %s\n// vendored on: %s", + cpp11_version, + Sys.Date() + ) - writeLines( - c(cpp11_header, readLines(system.file("include", "cpp11.hpp", package = "cpp11"))), - file.path(path, "cpp11.hpp") + write_header( + path, "cpp11.hpp", "cpp11", + cpp11_header ) - for (f in files) { - writeLines(c(cpp11_header, readLines(f)), file.path(path, "cpp11", basename(f))) - } + copy_files( + list.files(current_cpp11, full.names = TRUE), + path, "cpp11", cpp11_header + ) # Additional steps to make vendoring work ---- # 1. Check if `src/Makevars` exists makevars_exists <- file.exists("src/Makevars") + makevars.win_exists <- file.exists("src/Makevars.win") # 2. If makevars exists, it should have a line that reads # `PKG_CPPFLAGS = -I../inst/include` or similar vendor_line <- " -I vendor/" + makevars_file <- "src/Makevars" if (isTRUE(makevars_exists)) { - makevars <- readLines("src/Makevars") - - if (any(grepl("^PKG_CPPFLAGS", makevars))) { - cat("There is a `PKG_CPPFLAGS` line in src/Makevars. It will be modified.\n") - - # which line contains `PKG_CPPFLAGS`? - cppflags_line <- grep("^PKG_CPPFLAGS", makevars) - - # append the vendoring line - if (!grepl(vendor_line, makevars[cppflags_line])) { - makevars[cppflags_line] <- paste0(makevars[cppflags_line], vendor_line) - } - - writeLines(makevars, "src/Makevars") - } else { - # add the line - makevars <- c(makevars, paste0("PKG_CPPFLAGS = ", vendor_line)) - - writeLines(makevars, "src/Makevars") - } - - cat("The existing src/Makevars was modified. Please check it.\n") + makevars <- readLines(makevars_file) + alter_makevars(makevars, makevars_file, vendor_line) } else { - # create the file - writeLines(paste0("PKG_CPPFLAGS = ", vendor_line), "src/Makevars") + create_makevars(makevars_file) + } - # warn about the change - cat("A new src/Makevars file was created.\n") + makevars.win_file <- "src/Makevars.win" + if (isTRUE(makevars.win_exists)) { + makevars.win <- readLines(makevars.win_file) + alter_makevars(makevars.win, makevars.win_file, vendor_line) + } else { + create_makevars(makevars.win_file) } - # 3. `DESCRIPTION` now should not have `LinkingTo: cpp11` or `LinkingTo: \n\tcpp11` - description <- readLines("DESCRIPTION") + # 3. `DESCRIPTION` now should not have `LinkingTo: cpp11armadillo` or + # `LinkingTo: \n\tcpp11armadillo` + description_file <- "DESCRIPTION" + description <- readLines(description_file) - cpp11_in_desc <- any( - grepl("LinkingTo: cpp11", description), + cpp11armadillo_in_desc <- any( + grepl("LinkingTo: cpp11, cpp11armadillo", description), grepl("LinkingTo: ", description), - grepl(" cpp11", description) + grepl(" cpp11,", description), + grepl(" cpp11armadillo", description) ) - if (isTRUE(cpp11_in_desc)) { + if (isTRUE(cpp11armadillo_in_desc)) { # remove the lines - description <- description[!grepl("LinkingTo: cpp11", description)] + description <- description[!grepl( + "LinkingTo: cpp11, cpp11armadillo", + description + )] description <- description[!grepl("LinkingTo: ", description)] - description <- description[!grepl(" cpp11", description)] + description <- description[!grepl(" cpp11,", description)] + description <- description[!grepl(" cpp11armadillo", description)] - writeLines(description, "DESCRIPTION") + writeLines(description, description_file) # warn about the change - cat("`LinkingTo: cpp11` was removed from DESCRIPTION.\n") + cat("`LinkingTo: cpp11, cpp11armadillo` was removed from DESCRIPTION.\n") } invisible(path) } + +alter_makevars <- function(makevars, makevars_file, vendor_line) { + if (any(grepl("^PKG_CPPFLAGS", makevars))) { + cat( + "There is a `PKG_CPPFLAGS` line in src/Makevars. It will be modified.\n" + ) + + # which line contains `PKG_CPPFLAGS`? + cppflags_line <- grep("^PKG_CPPFLAGS", makevars) + + # append the vendoring line + if (!grepl(vendor_line, makevars[cppflags_line])) { + makevars[cppflags_line] <- paste0(makevars[cppflags_line], vendor_line) + } + + writeLines(makevars, makevars_file) + } else { + # add the line + makevars <- c(makevars, paste0("PKG_CPPFLAGS = ", vendor_line)) + + writeLines(makevars, makevars_file) + } +} + +create_makevars <- function(filename, vendor_line) { + # create the file + writeLines(paste0("PKG_CPPFLAGS = ", vendor_line), filename) + + # warn about the change + cat("A new src/Makevars file was created.\n") +} + +write_header <- function(path, header, pkg, cpp11_header) { + writeLines( + 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(path, out, basename(f)) + ) + } +} From ec46e62c42d0076dd71d399dc483c12b1cd1638c Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Tue, 30 Jan 2024 16:55:32 -0500 Subject: [PATCH 6/9] test-vendor --- R/vendor.R | 17 +---------------- tests/testthat/_snaps/register.new.md | 9 +++++++++ tests/testthat/_snaps/source.md | 5 +++-- tests/testthat/test-vendor.R | 6 +++--- 4 files changed, 16 insertions(+), 21 deletions(-) create mode 100644 tests/testthat/_snaps/register.new.md diff --git a/R/vendor.R b/R/vendor.R index f7560a0c..6b78494e 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -32,22 +32,7 @@ #' unlink(dir, recursive = TRUE) cpp_vendor <- function(path = "./src/vendor") { if (dir.exists(path)) { - cat( - sprintf( - "The directory '%s' already exists. Do you want to overwrite it?\n", - path - ) - ) - if (interactive()) { - if (utils::menu(c("Yes", "No"), graphics = FALSE) == 1L) { - unlink(path, recursive = TRUE) - } else { - return(FALSE) - } - } else { - message("Running in non-interactive mode. Exiting.") - return(FALSE) - } + stop("'", path, "' already exists\n * run unlink('", path, "', recursive = TRUE)", call. = FALSE) } dir.create(path, recursive = TRUE, showWarnings = FALSE) diff --git a/tests/testthat/_snaps/register.new.md b/tests/testthat/_snaps/register.new.md new file mode 100644 index 00000000..75125c45 --- /dev/null +++ b/tests/testthat/_snaps/register.new.md @@ -0,0 +1,9 @@ +# cpp_register: can be run with messages + + Code + cpp_register(p, quiet = FALSE) + 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 a667ce6b..61fd843b 100644 --- a/tests/testthat/test-vendor.R +++ b/tests/testthat/test-vendor.R @@ -3,17 +3,17 @@ describe("cpp_vendor", { pkg <- local_package() mockery::stub(cpp_vendor, "system.file", "") expect_error( - cpp_vendor(pkg_path(pkg)), + cpp_vendor(paste0(pkg_path(pkg), "/src/vendor")), "cpp11 is not installed" ) }) it("errors if cpp11 is already vendored", { pkg <- local_package() - cpp_vendor(pkg_path(pkg)) + cpp_vendor(paste0(pkg_path(pkg), "/src/vendor")) expect_error( - cpp_vendor(pkg_path(pkg)), + cpp_vendor(paste0(pkg_path(pkg), "/src/vendor")), "already exists" ) }) From 1588c009eb0da9485d30670d0b08e62e4ece0325 Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Tue, 30 Jan 2024 22:33:28 -0500 Subject: [PATCH 7/9] tests --- R/vendor.R | 6 +++--- tests/testthat/test-vendor.R | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/R/vendor.R b/R/vendor.R index 6b78494e..07e1a150 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -89,7 +89,7 @@ cpp_vendor <- function(path = "./src/vendor") { makevars <- readLines(makevars_file) alter_makevars(makevars, makevars_file, vendor_line) } else { - create_makevars(makevars_file) + create_makevars(makevars_file, vendor_line) } makevars.win_file <- "src/Makevars.win" @@ -97,7 +97,7 @@ cpp_vendor <- function(path = "./src/vendor") { makevars.win <- readLines(makevars.win_file) alter_makevars(makevars.win, makevars.win_file, vendor_line) } else { - create_makevars(makevars.win_file) + create_makevars(makevars.win_file, vendor_line) } # 3. `DESCRIPTION` now should not have `LinkingTo: cpp11armadillo` or @@ -138,7 +138,7 @@ alter_makevars <- function(makevars, makevars_file, vendor_line) { ) # which line contains `PKG_CPPFLAGS`? - cppflags_line <- grep("^PKG_CPPFLAGS", makevars) + cppflags_line <- grep("^PKG_CPPFLAGS|^# PKG_CPPFLAGS|^#PKG_CPPFLAGS", makevars) # append the vendoring line if (!grepl(vendor_line, makevars[cppflags_line])) { diff --git a/tests/testthat/test-vendor.R b/tests/testthat/test-vendor.R index 61fd843b..a667ce6b 100644 --- a/tests/testthat/test-vendor.R +++ b/tests/testthat/test-vendor.R @@ -3,17 +3,17 @@ describe("cpp_vendor", { pkg <- local_package() mockery::stub(cpp_vendor, "system.file", "") expect_error( - cpp_vendor(paste0(pkg_path(pkg), "/src/vendor")), + cpp_vendor(pkg_path(pkg)), "cpp11 is not installed" ) }) it("errors if cpp11 is already vendored", { pkg <- local_package() - cpp_vendor(paste0(pkg_path(pkg), "/src/vendor")) + cpp_vendor(pkg_path(pkg)) expect_error( - cpp_vendor(paste0(pkg_path(pkg), "/src/vendor")), + cpp_vendor(pkg_path(pkg)), "already exists" ) }) From e4fe8f601e4881e73f88cd230cb83efc6f43f438 Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Tue, 30 Jan 2024 23:22:27 -0500 Subject: [PATCH 8/9] vendoring --- R/vendor.R | 14 +-- cpp11test/R/cpp11.R | 104 ++++++++++----------- cpp11test/src/cpp11.cpp | 194 ++++++++++++++++++++-------------------- 3 files changed, 158 insertions(+), 154 deletions(-) diff --git a/R/vendor.R b/R/vendor.R index 07e1a150..896cc037 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -132,20 +132,24 @@ cpp_vendor <- function(path = "./src/vendor") { } alter_makevars <- function(makevars, makevars_file, vendor_line) { - if (any(grepl("^PKG_CPPFLAGS", makevars))) { - cat( - "There is a `PKG_CPPFLAGS` line in src/Makevars. It will be modified.\n" - ) - + if (any(grepl("^PKG_CPPFLAGS|^# PKG_CPPFLAGS|^#PKG_CPPFLAGS", makevars))) { # which line contains `PKG_CPPFLAGS`? cppflags_line <- grep("^PKG_CPPFLAGS|^# PKG_CPPFLAGS|^#PKG_CPPFLAGS", makevars) + if (length(cppflags_line) > 1) { + if (any(grepl(vendor_line, makevars[cppflags_line]))) { + return(TRUE) + } + } + # append the vendoring line if (!grepl(vendor_line, makevars[cppflags_line])) { makevars[cppflags_line] <- paste0(makevars[cppflags_line], vendor_line) } writeLines(makevars, makevars_file) + + cat(paste0(makevars_file, "was modified.\n")) } else { # add the line makevars <- c(makevars, paste0("PKG_CPPFLAGS = ", vendor_line)) 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() { From 11051ed729bfcfbb35fd944af84c8fe7e75aac7b Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Wed, 31 Jan 2024 09:58:23 -0500 Subject: [PATCH 9/9] pass tests --- DESCRIPTION | 2 +- R/vendor.R | 109 +++----------------------- man/cpp_vendor.Rd | 8 +- tests/testthat/_snaps/register.md | 2 +- tests/testthat/_snaps/register.new.md | 9 --- tests/testthat/test-vendor.R | 6 +- 6 files changed, 21 insertions(+), 115 deletions(-) delete mode 100644 tests/testthat/_snaps/register.new.md 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 896cc037..07cda1e3 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -15,32 +15,30 @@ #' **you**. Bugfixes and new features in cpp11 will not be available for your #' code until you run `cpp_vendor()` again. #' -#' @param path The path to vendor the code into. The default is `src/vendor/`. +#' @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, "src", "vendor")) -#' #' # cleanup #' unlink(dir, recursive = TRUE) -cpp_vendor <- function(path = "./src/vendor") { - if (dir.exists(path)) { - stop("'", path, "' already exists\n * run unlink('", path, "', recursive = TRUE)", call. = FALSE) - } +cpp_vendor <- function(path = "./inst/include") { + new <- file.path(path, "cpp11") - dir.create(path, recursive = TRUE, showWarnings = FALSE) + if (dir.exists(new)) { + stop("'", new, "' already exists\n * run unlink('", new, "', recursive = TRUE)", call. = FALSE) + } # Vendor cpp11 ---- dir.create( - file.path(path, "cpp11"), + new, recursive = TRUE, showWarnings = FALSE ) @@ -75,97 +73,16 @@ cpp_vendor <- function(path = "./src/vendor") { # Additional steps to make vendoring work ---- - # 1. Check if `src/Makevars` exists - makevars_exists <- file.exists("src/Makevars") - makevars.win_exists <- file.exists("src/Makevars.win") - - # 2. If makevars exists, it should have a line that reads - # `PKG_CPPFLAGS = -I../inst/include` or similar - - vendor_line <- " -I vendor/" - - makevars_file <- "src/Makevars" - if (isTRUE(makevars_exists)) { - makevars <- readLines(makevars_file) - alter_makevars(makevars, makevars_file, vendor_line) - } else { - create_makevars(makevars_file, vendor_line) - } - - makevars.win_file <- "src/Makevars.win" - if (isTRUE(makevars.win_exists)) { - makevars.win <- readLines(makevars.win_file) - alter_makevars(makevars.win, makevars.win_file, vendor_line) - } else { - create_makevars(makevars.win_file, vendor_line) - } - - # 3. `DESCRIPTION` now should not have `LinkingTo: cpp11armadillo` or - # `LinkingTo: \n\tcpp11armadillo` - description_file <- "DESCRIPTION" - description <- readLines(description_file) - - cpp11armadillo_in_desc <- any( - grepl("LinkingTo: cpp11, cpp11armadillo", description), - grepl("LinkingTo: ", description), - grepl(" cpp11,", description), - grepl(" cpp11armadillo", description) - ) - - if (isTRUE(cpp11armadillo_in_desc)) { - # remove the lines - description <- description[!grepl( - "LinkingTo: cpp11, cpp11armadillo", - description - )] - description <- description[!grepl("LinkingTo: ", description)] - description <- description[!grepl(" cpp11,", description)] - description <- description[!grepl(" cpp11armadillo", description)] + message(paste( + "Makevars and/or Makevars.win should have a line such as", + "'PKG_CPPFLAGS = -I../inst/include'" + )) - writeLines(description, description_file) - - # warn about the change - cat("`LinkingTo: cpp11, cpp11armadillo` was removed from DESCRIPTION.\n") - } + message("DESCRIPTION should not have lines such as 'LinkingTo: cpp11'") invisible(path) } -alter_makevars <- function(makevars, makevars_file, vendor_line) { - if (any(grepl("^PKG_CPPFLAGS|^# PKG_CPPFLAGS|^#PKG_CPPFLAGS", makevars))) { - # which line contains `PKG_CPPFLAGS`? - cppflags_line <- grep("^PKG_CPPFLAGS|^# PKG_CPPFLAGS|^#PKG_CPPFLAGS", makevars) - - if (length(cppflags_line) > 1) { - if (any(grepl(vendor_line, makevars[cppflags_line]))) { - return(TRUE) - } - } - - # append the vendoring line - if (!grepl(vendor_line, makevars[cppflags_line])) { - makevars[cppflags_line] <- paste0(makevars[cppflags_line], vendor_line) - } - - writeLines(makevars, makevars_file) - - cat(paste0(makevars_file, "was modified.\n")) - } else { - # add the line - makevars <- c(makevars, paste0("PKG_CPPFLAGS = ", vendor_line)) - - writeLines(makevars, makevars_file) - } -} - -create_makevars <- function(filename, vendor_line) { - # create the file - writeLines(paste0("PKG_CPPFLAGS = ", vendor_line), filename) - - # warn about the change - cat("A new src/Makevars file was created.\n") -} - write_header <- function(path, header, pkg, cpp11_header) { writeLines( c( diff --git a/man/cpp_vendor.Rd b/man/cpp_vendor.Rd index a02f36de..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 = "./src/vendor") +cpp_vendor(path = "./inst/include") } \arguments{ -\item{path}{The path to vendor the code into.} +\item{path}{The path to vendor the code into. The default is \verb{./inst/include/}.} } \value{ The file path to the vendored code (invisibly). @@ -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, "src", "vendor")) - # 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/register.new.md b/tests/testthat/_snaps/register.new.md deleted file mode 100644 index 75125c45..00000000 --- a/tests/testthat/_snaps/register.new.md +++ /dev/null @@ -1,9 +0,0 @@ -# cpp_register: can be run with messages - - Code - cpp_register(p, quiet = FALSE) - Message - i 1 functions decorated with [[cpp11::register]] - v generated file 'cpp11.R' - v generated file 'cpp11.cpp' - diff --git a/tests/testthat/test-vendor.R b/tests/testthat/test-vendor.R index a667ce6b..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, "src", "vendor", "cpp11"))) - expect_true(file.exists(file.path(p, "src", "vendor", "cpp11.hpp"))) - expect_true(file.exists(file.path(p, "src", "vendor", "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"))) }) })