Skip to content
Open
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: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
21 changes: 16 additions & 5 deletions R/register.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
44 changes: 44 additions & 0 deletions tests/testthat/test-register.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sstream>",
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 <sstream>",
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)
Expand Down