diff --git a/NEWS.md b/NEWS.md index f7e08809..98252a46 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # cpp11 (development version) +* `cpp_register()` now also picks up a `{package}_types.h` or `{package}_types.hpp` in `src/include/`, letting a package keep that header private instead of installing it from `inst/include/`. + # cpp11 0.5.5 * Fixed an issue where `cpp11::stop()` and `cpp11::warning()` calls with the same template instantiation could cause a crash on some systems (#491, #295). diff --git a/R/register.R b/R/register.R index 68c7f585..6b9bbd73 100644 --- a/R/register.R +++ b/R/register.R @@ -114,17 +114,28 @@ cpp_register <- function( ) } + pkg_types_name <- paste0(package, c("_types.h", "_types.hpp")) + pkg_types <- c( - file.path(path, "src", paste0(package, "_types.h")), - file.path(path, "src", paste0(package, "_types.hpp")), - file.path(path, "inst", "include", paste0(package, "_types.h")), - file.path(path, "inst", "include", paste0(package, "_types.hpp")) + file.path(path, "src", pkg_types_name), + file.path(path, "src", "include", pkg_types_name), + file.path(path, "inst", "include", pkg_types_name) + ) + + # `src/cpp11.cpp` is generated next to the `src/` copies and compiled from + # `src/`, so a header in `src/include/` is included through that directory. + # A package keeping its own headers private this way then needs no include + # flag for them, and none of the other two locations changes. + pkg_types_include <- c( + pkg_types_name, + file.path("include", pkg_types_name), + pkg_types_name ) pkg_types_exist <- file.exists(pkg_types) if (any(pkg_types_exist)) { extra_includes <- c( - sprintf('#include "%s"', basename(pkg_types[pkg_types_exist])), + sprintf('#include "%s"', pkg_types_include[pkg_types_exist]), extra_includes ) } diff --git a/tests/testthat/test-register.R b/tests/testthat/test-register.R index 7f65bb8a..7e3c86b6 100644 --- a/tests/testthat/test-register.R +++ b/tests/testthat/test-register.R @@ -685,6 +685,50 @@ describe("cpp_register", { ) }) + it("includes pkg_types.h if included in src/include", { + pkg <- local_package() + p <- pkg_path(pkg) + dir.create(file.path(p, "src", "include"), recursive = TRUE) + file.copy(test_path("single.cpp"), file.path(p, "src", "single.cpp")) + writeLines( + "#include ", + file.path(p, "src", "include", "testPkg_types.h") + ) + cpp_register(p) + + expect_true( + any( + grepl( + pattern = '#include "include/testPkg_types.h"', + x = readLines(file.path(p, "src", "cpp11.cpp")), + fixed = TRUE + ) + ) + ) + }) + + it("includes pkg_types.hpp if included in src/include", { + pkg <- local_package() + p <- pkg_path(pkg) + dir.create(file.path(p, "src", "include"), recursive = TRUE) + file.copy(test_path("single.cpp"), file.path(p, "src", "single.cpp")) + writeLines( + "#include ", + file.path(p, "src", "include", "testPkg_types.hpp") + ) + cpp_register(p) + + expect_true( + any( + grepl( + pattern = '#include "include/testPkg_types.hpp"', + x = readLines(file.path(p, "src", "cpp11.cpp")), + fixed = TRUE + ) + ) + ) + }) + it("includes pkg_types.h if included in inst/include", { pkg <- local_package() p <- pkg_path(pkg)