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

Vendoring #341

Closed
wants to merge 11 commits into from
Closed
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
82 changes: 62 additions & 20 deletions R/vendor.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
104 changes: 52 additions & 52 deletions cpp11test/R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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`))
}
Expand Down
Loading
Loading