Skip to content

Commit

Permalink
vendor to ./src/vendor
Browse files Browse the repository at this point in the history
  • Loading branch information
pachadotdev committed Nov 8, 2023
1 parent 8e83408 commit ae7faa4
Show file tree
Hide file tree
Showing 66 changed files with 275 additions and 250 deletions.
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.2.2
4 changes: 2 additions & 2 deletions R/register.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ cpp_register <- function(path = ".", quiet = !is_interactive(), extension = c(".

extra_includes <- character()
if (pkg_links_to_rcpp(path)) {
extra_includes <- c(extra_includes, "#include <cpp11/R.hpp>", "#include <Rcpp.h>", "using namespace Rcpp;")
extra_includes <- c(extra_includes, "#include <headers/R.hpp>", "#include <Rcpp.h>", "using namespace Rcpp;")
}

pkg_types <- c(
Expand All @@ -114,7 +114,7 @@ cpp_register <- function(path = ".", quiet = !is_interactive(), extension = c(".
// clang-format off
{extra_includes}
#include "cpp11/declarations.hpp"
#include "headers/declarations.hpp"
#include <R_ext/Visibility.h>
{cpp_functions_definitions}
Expand Down
6 changes: 3 additions & 3 deletions R/source.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#' @examples
#'
#' cpp_source(
#' code = '#include "cpp11/integers.hpp"
#' code = '#include "headers/integers.hpp"
#'
#' [[cpp11::register]]
#' int num_odd(cpp11::integers x) {
Expand All @@ -44,7 +44,7 @@
#'
#' cpp_source(
#' code = '
#' #include <cpp11/R.hpp>
#' #include <headers/R.hpp>
#' #include <RProgress.h>
#'
#' [[cpp11::linking_to("progress")]]
Expand Down Expand Up @@ -113,7 +113,7 @@ cpp_source <- function(file, code = NULL, env = parent.frame(), clean = TRUE, qu
cpp_functions_definitions <- generate_cpp_functions(funs, package = package)

cpp_path <- file.path(dirname(new_file_path), "cpp11.cpp")
brio::write_lines(c('#include "cpp11/declarations.hpp"', "using namespace ::cpp11;", cpp_functions_definitions), cpp_path)
brio::write_lines(c('#include "headers/declarations.hpp"', "using namespace ::cpp11;", cpp_functions_definitions), cpp_path)

linking_to <- union(get_linking_to(all_decorations), "cpp11")

Expand Down
68 changes: 46 additions & 22 deletions R/vendor.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,12 +26,12 @@
#' # 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)
Expand Down Expand Up @@ -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")

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)]
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)
}
2 changes: 1 addition & 1 deletion cpp11test/src/add.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cpp11/doubles.hpp"
#include "headers/doubles.hpp"

[[cpp11::register]] SEXP cpp11_add_vec_for_(cpp11::writable::doubles x, double num) {
R_xlen_t n = x.size();
Expand Down
4 changes: 2 additions & 2 deletions cpp11test/src/cpp11.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Generated by cpp11: do not edit by hand
// clang-format off

#include <cpp11/R.hpp>
#include <headers/R.hpp>
#include <Rcpp.h>
using namespace Rcpp;
#include "cpp11/declarations.hpp"
#include "headers/declarations.hpp"
#include <R_ext/Visibility.h>

// add.cpp
Expand Down
6 changes: 3 additions & 3 deletions cpp11test/src/data_frame.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "cpp11/integers.hpp"
#include "cpp11/list.hpp"
#include "cpp11/strings.hpp"
#include "headers/integers.hpp"
#include "headers/list.hpp"
#include "headers/strings.hpp"

[[cpp11::register]] SEXP data_frame_() {
using namespace cpp11::literals;
Expand Down
4 changes: 2 additions & 2 deletions cpp11test/src/errors.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "cpp11/function.hpp"
#include "cpp11/protect.hpp"
#include "headers/function.hpp"
#include "headers/protect.hpp"
using namespace cpp11;

[[cpp11::register]] void my_stop_n1fmt(std::string mystring) { cpp11::stop(mystring); }
Expand Down
4 changes: 2 additions & 2 deletions cpp11test/src/errors_fmt.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#define CPP11_USE_FMT
#include "cpp11/function.hpp"
#include "cpp11/protect.hpp"
#include "headers/function.hpp"
#include "headers/protect.hpp"
using namespace cpp11;

[[cpp11::register]] void my_stop(std::string mystring, int myarg) {
Expand Down
2 changes: 1 addition & 1 deletion cpp11test/src/grow.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cpp11/doubles.hpp"
#include "headers/doubles.hpp"

[[cpp11::register]] cpp11::writable::doubles grow_(R_xlen_t n) {
cpp11::writable::doubles x;
Expand Down
2 changes: 1 addition & 1 deletion cpp11test/src/insert.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cpp11/doubles.hpp"
#include "headers/doubles.hpp"

[[cpp11::register]] SEXP cpp11_insert_(SEXP num_sxp) {
R_xlen_t num = INTEGER(num_sxp)[0];
Expand Down
4 changes: 2 additions & 2 deletions cpp11test/src/matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "cpp11/matrix.hpp"
#include "headers/matrix.hpp"
#include "Rmath.h"
#include "cpp11/doubles.hpp"
#include "headers/doubles.hpp"
using namespace cpp11;

[[cpp11::register]] SEXP gibbs_cpp(int N, int thin) {
Expand Down
2 changes: 1 addition & 1 deletion cpp11test/src/release.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <vector>
#include "cpp11/sexp.hpp"
#include "headers/sexp.hpp"

#include "Rcpp.h"

Expand Down
4 changes: 2 additions & 2 deletions cpp11test/src/safe.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <cstring>
#include "cpp11/doubles.hpp"
#include "cpp11/protect.hpp"
#include "headers/doubles.hpp"
#include "headers/protect.hpp"

[[cpp11::register]] SEXP cpp11_safe_(SEXP x_sxp) {
SEXP err = R_NilValue;
Expand Down
2 changes: 1 addition & 1 deletion cpp11test/src/sum.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <numeric>
#include "cpp11/doubles.hpp"
#include "headers/doubles.hpp"

[[cpp11::register]] double sum_dbl_for_(cpp11::doubles x) {
double sum = 0.;
Expand Down
2 changes: 1 addition & 1 deletion cpp11test/src/sum_int.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <numeric>
#include "cpp11/integers.hpp"
#include "headers/integers.hpp"

[[cpp11::register]] double sum_int_for_(cpp11::integers x) {
double sum = 0.;
Expand Down
2 changes: 1 addition & 1 deletion cpp11test/src/test-as.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <string>
#include <vector>

#include "cpp11/declarations.hpp"
#include "headers/declarations.hpp"

#include <testthat.h>

Expand Down
8 changes: 4 additions & 4 deletions cpp11test/src/test-data_frame.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "cpp11/data_frame.hpp"
#include "cpp11/function.hpp"
#include "cpp11/integers.hpp"
#include "cpp11/strings.hpp"
#include "headers/data_frame.hpp"
#include "headers/function.hpp"
#include "headers/integers.hpp"
#include "headers/strings.hpp"

#include <testthat.h>

Expand Down
10 changes: 5 additions & 5 deletions cpp11test/src/test-doubles.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <cstring>
#include "cpp11/doubles.hpp"
#include "cpp11/function.hpp"
#include "cpp11/integers.hpp"
#include "cpp11/sexp.hpp"
#include "cpp11/strings.hpp"
#include "headers/doubles.hpp"
#include "headers/function.hpp"
#include "headers/integers.hpp"
#include "headers/sexp.hpp"
#include "headers/strings.hpp"

#include <testthat.h>

Expand Down
8 changes: 4 additions & 4 deletions cpp11test/src/test-environment.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "cpp11/as.hpp"
#include "cpp11/environment.hpp"
#include "cpp11/function.hpp"
#include "cpp11/strings.hpp"
#include "headers/as.hpp"
#include "headers/environment.hpp"
#include "headers/function.hpp"
#include "headers/strings.hpp"

#include <testthat.h>

Expand Down
2 changes: 1 addition & 1 deletion cpp11test/src/test-external_pointer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>
#include "cpp11/external_pointer.hpp"
#include "headers/external_pointer.hpp"

#include <testthat.h>

Expand Down
2 changes: 1 addition & 1 deletion cpp11test/src/test-function.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cpp11/function.hpp"
#include "headers/function.hpp"

#include <testthat.h>

Expand Down
8 changes: 4 additions & 4 deletions cpp11test/src/test-integers.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Rversion.h"
#include "cpp11/doubles.hpp"
#include "cpp11/function.hpp"
#include "cpp11/integers.hpp"
#include "cpp11/strings.hpp"
#include "headers/doubles.hpp"
#include "headers/function.hpp"
#include "headers/integers.hpp"
#include "headers/strings.hpp"

#include <testthat.h>

Expand Down
12 changes: 6 additions & 6 deletions cpp11test/src/test-list.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "cpp11/doubles.hpp"
#include "cpp11/integers.hpp"
#include "cpp11/list.hpp"
#include "cpp11/logicals.hpp"
#include "cpp11/raws.hpp"
#include "cpp11/strings.hpp"
#include "headers/doubles.hpp"
#include "headers/integers.hpp"
#include "headers/list.hpp"
#include "headers/logicals.hpp"
#include "headers/raws.hpp"
#include "headers/strings.hpp"

#include <testthat.h>

Expand Down
8 changes: 4 additions & 4 deletions cpp11test/src/test-list_of.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "cpp11/doubles.hpp"
#include "cpp11/list.hpp"
#include "cpp11/list_of.hpp"
#include "cpp11/strings.hpp"
#include "headers/doubles.hpp"
#include "headers/list.hpp"
#include "headers/list_of.hpp"
#include "headers/strings.hpp"

#include <testthat.h>

Expand Down
2 changes: 1 addition & 1 deletion cpp11test/src/test-logicals.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "cpp11/logicals.hpp"
#include "headers/logicals.hpp"

#include <testthat.h>

Expand Down
8 changes: 4 additions & 4 deletions cpp11test/src/test-matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "cpp11/doubles.hpp"
#include "cpp11/function.hpp"
#include "cpp11/integers.hpp"
#include "cpp11/matrix.hpp"
#include "headers/doubles.hpp"
#include "headers/function.hpp"
#include "headers/integers.hpp"
#include "headers/matrix.hpp"

#include <testthat.h>

Expand Down
8 changes: 4 additions & 4 deletions cpp11test/src/test-nas.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "cpp11/doubles.hpp"
#include "cpp11/integers.hpp"
#include "cpp11/r_bool.hpp"
#include "cpp11/r_string.hpp"
#include "headers/doubles.hpp"
#include "headers/integers.hpp"
#include "headers/r_bool.hpp"
#include "headers/r_string.hpp"

#include <testthat.h>

Expand Down
Loading

0 comments on commit ae7faa4

Please sign in to comment.