diff --git a/DESCRIPTION b/DESCRIPTION index 5f69870b..e312ad37 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.1 diff --git a/R/vendor.R b/R/vendor.R index 5fe10fe2..a7e40fbb 100644 --- a/R/vendor.R +++ b/R/vendor.R @@ -8,14 +8,12 @@ #' '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. -#' #' **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 dir The directoyy to vendor the code into. +#' @param subdir The subdirectory to vendor the code into. #' @return The file path to the vendored code (invisibly). #' @export #' @examples @@ -30,34 +28,83 @@ #' #' # cleanup #' unlink(dir, recursive = TRUE) -cpp_vendor <- function(path = ".") { - new <- file.path(path, "inst", "include", "cpp11") +cpp_vendor <- function(dir = NULL, subdir = "/inst/include") { + if (is.null(dir)) { + stop("You must provide a path to vendor the code into", call. = FALSE) + } + + path <- paste0(dir, subdir) - if (dir.exists(new)) { - stop("'", new, "' already exists\n * run unlink('", new, "', recursive = TRUE)", call. = FALSE) + path2 <- file.path(path, "cpp11") + if (dir.exists(path2)) { + stop("'", path2, "' already exists\n * run unlink('", path2, "', recursive = TRUE)", call. = FALSE) } - dir.create(new , recursive = TRUE, showWarnings = FALSE) + # Vendor cpp11 ---- + + dir.create( + path2, + recursive = TRUE, + showWarnings = FALSE + ) - current <- system.file("include", "cpp11", package = "cpp11") - if (!nzchar(current)) { + 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()) + 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, cpp11armadillo_header) { writeLines( - c(cpp11_header, readLines(system.file("include", "cpp11.hpp", package = "cpp11"))), - file.path(dirname(new), "cpp11.hpp") + c( + cpp11armadillo_header, + readLines( + system.file("include", header, package = pkg) + ) + ), + file.path(path, header) ) +} +copy_files <- function(files, path, out, cpp11armadillo_header) { for (f in files) { - writeLines(c(cpp11_header, readLines(f)), file.path(new, basename(f))) + writeLines( + c(cpp11armadillo_header, readLines(f)), + file.path(path, out, basename(f)) + ) } - - invisible(new) } diff --git a/man/cpp_vendor.Rd b/man/cpp_vendor.Rd index 857e49cf..0fd75e0a 100644 --- a/man/cpp_vendor.Rd +++ b/man/cpp_vendor.Rd @@ -4,10 +4,12 @@ \alias{cpp_vendor} \title{Vendor the cpp11 dependency} \usage{ -cpp_vendor(path = ".") +cpp_vendor(dir = NULL, subdir = "/inst/include") } \arguments{ -\item{path}{The path to the package root directory} +\item{dir}{The directoyy to vendor the code into.} + +\item{subdir}{The subdirectory to vendor the code into.} } \value{ The file path to the vendored code (invisibly). @@ -22,8 +24,6 @@ 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. -If you choose to vendor the headers you should \emph{remove} \code{LinkingTo: cpp11} from your DESCRIPTION. - \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 code until you run \code{cpp_vendor()} again.