From c9feedb1fbfdb545d2181299722ebb3f7b976107 Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Fri, 10 Nov 2023 17:21:42 -0500 Subject: [PATCH 1/7] cpp11 dynlib --- R/cpp11.R | 17 +++++++++++++++++ inst/templates/code-cpp11-dynlib.R | 2 ++ 2 files changed, 19 insertions(+) create mode 100644 inst/templates/code-cpp11-dynlib.R diff --git a/R/cpp11.R b/R/cpp11.R index 0d93fd075..5de0a16f6 100644 --- a/R/cpp11.R +++ b/R/cpp11.R @@ -24,6 +24,8 @@ use_cpp11 <- function() { check_cpp_register_deps() + check_cpp_dynlib() + invisible() } @@ -40,3 +42,18 @@ check_cpp_register_deps <- function() { ui_todo("Now install {ui_value(cpp_register_deps[!installed])} to use cpp11.") } } + +get_cpp_dynlib <- function() { + desc <- desc::desc(package = NULL) + desc$get_list("Package")[[1]] +} + +check_cpp_dynlib <- function() { + pkgname <- get_cpp_dynlib() + use_template( + "code-cpp11-dynlib.R", + data = list(Package = pkgname), + save_as = path("R", paste0(pkgname, "-package.R")), + open = is_interactive() + ) +} diff --git a/inst/templates/code-cpp11-dynlib.R b/inst/templates/code-cpp11-dynlib.R new file mode 100644 index 000000000..b2d1d3a4d --- /dev/null +++ b/inst/templates/code-cpp11-dynlib.R @@ -0,0 +1,2 @@ +#' @useDynLib {{Package}}, .registration = TRUE +NULL From 4404d650311cdba59f9d84d4b6a27ace74fe0dc1 Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Sat, 11 Nov 2023 15:12:31 -0500 Subject: [PATCH 2/7] Hadley's suggestion --- .Rbuildignore | 1 + R/cpp11.R | 8 +++----- inst/templates/code-cpp11-dynlib.R | 2 -- 3 files changed, 4 insertions(+), 7 deletions(-) delete mode 100644 inst/templates/code-cpp11-dynlib.R diff --git a/.Rbuildignore b/.Rbuildignore index f972cfa65..17a7bd594 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -24,3 +24,4 @@ ^internal$ ^revdep-cloud$ ^CRAN-SUBMISSION$ +^\.vscode$ diff --git a/R/cpp11.R b/R/cpp11.R index 5de0a16f6..36879910c 100644 --- a/R/cpp11.R +++ b/R/cpp11.R @@ -50,10 +50,8 @@ get_cpp_dynlib <- function() { check_cpp_dynlib <- function() { pkgname <- get_cpp_dynlib() - use_template( - "code-cpp11-dynlib.R", - data = list(Package = pkgname), - save_as = path("R", paste0(pkgname, "-package.R")), - open = is_interactive() + roxygen_ns_append( + sprintf("@useDynLib %s, .registration = TRUE", pkgname) ) + roxygen_update_ns() } diff --git a/inst/templates/code-cpp11-dynlib.R b/inst/templates/code-cpp11-dynlib.R deleted file mode 100644 index b2d1d3a4d..000000000 --- a/inst/templates/code-cpp11-dynlib.R +++ /dev/null @@ -1,2 +0,0 @@ -#' @useDynLib {{Package}}, .registration = TRUE -NULL From a6f6167acc3e3f5724e01ca9462c8c7e917751db Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Mon, 13 Nov 2023 16:50:52 -0500 Subject: [PATCH 3/7] cpp11 test --- .vscode/settings.json | 59 +++++++++++++++++++++++++++++++++++++ NEWS.md | 4 +++ tests/testthat/test-cpp11.R | 3 ++ 3 files changed, 66 insertions(+) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..3e5eb956e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/NEWS.md b/NEWS.md index a819c3eb8..cef57e748 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,10 @@ * Implicit usage of `numeric_version()` via comparison now always provides character input. This is in response to a request from CRAN to anticipate future solutions to . +* `use_cpp11()` creates/edits DESCRIPTION and NAMESPACE, allowing to use C++ + code directly. +* `.gitignore` now ignores `.vscode` for those who contribute to `usethis` from + that editor. # usethis 2.2.1 diff --git a/tests/testthat/test-cpp11.R b/tests/testthat/test-cpp11.R index 128dca1d0..d7abd2bc9 100644 --- a/tests/testthat/test-cpp11.R +++ b/tests/testthat/test-cpp11.R @@ -21,6 +21,9 @@ test_that("use_cpp11() creates files/dirs, edits DESCRIPTION and .gitignore", { ignores <- read_utf8(proj_path("src", ".gitignore")) expect_true(all(c("*.o", "*.so", "*.dll") %in% ignores)) + + namespace <- read_utf8(proj_path("NAMESPACE")) + expect_true("# Generated by roxygen2: do not edit by hand" %in% namespace) }) test_that("check_cpp_register_deps is silent if all installed, emits todo if not", { From dcbdde137347294a52e007e952bf8ccf6e00635b Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Mon, 13 Nov 2023 16:51:51 -0500 Subject: [PATCH 4/7] cpp11 test --- .vscode/settings.json | 59 ------------------------------------------- R/cpp11.R | 18 +++---------- 2 files changed, 4 insertions(+), 73 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 3e5eb956e..000000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,59 +0,0 @@ -{ - "C_Cpp_Runner.cCompilerPath": "gcc", - "C_Cpp_Runner.cppCompilerPath": "g++", - "C_Cpp_Runner.debuggerPath": "gdb", - "C_Cpp_Runner.cStandard": "", - "C_Cpp_Runner.cppStandard": "", - "C_Cpp_Runner.msvcBatchPath": "", - "C_Cpp_Runner.useMsvc": false, - "C_Cpp_Runner.warnings": [ - "-Wall", - "-Wextra", - "-Wpedantic", - "-Wshadow", - "-Wformat=2", - "-Wcast-align", - "-Wconversion", - "-Wsign-conversion", - "-Wnull-dereference" - ], - "C_Cpp_Runner.msvcWarnings": [ - "/W4", - "/permissive-", - "/w14242", - "/w14287", - "/w14296", - "/w14311", - "/w14826", - "/w44062", - "/w44242", - "/w14905", - "/w14906", - "/w14263", - "/w44265", - "/w14928" - ], - "C_Cpp_Runner.enableWarnings": true, - "C_Cpp_Runner.warningsAsError": false, - "C_Cpp_Runner.compilerArgs": [], - "C_Cpp_Runner.linkerArgs": [], - "C_Cpp_Runner.includePaths": [], - "C_Cpp_Runner.includeSearch": [ - "*", - "**/*" - ], - "C_Cpp_Runner.excludeSearch": [ - "**/build", - "**/build/**", - "**/.*", - "**/.*/**", - "**/.vscode", - "**/.vscode/**" - ], - "C_Cpp_Runner.useAddressSanitizer": false, - "C_Cpp_Runner.useUndefinedSanitizer": false, - "C_Cpp_Runner.useLeakSanitizer": false, - "C_Cpp_Runner.showCompilationTime": false, - "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false -} \ No newline at end of file diff --git a/R/cpp11.R b/R/cpp11.R index 36879910c..a5f5b666d 100644 --- a/R/cpp11.R +++ b/R/cpp11.R @@ -24,7 +24,10 @@ use_cpp11 <- function() { check_cpp_register_deps() - check_cpp_dynlib() + roxygen_ns_append( + sprintf("@useDynLib %s, .registration = TRUE", project_name()) + ) + roxygen_update_ns() invisible() } @@ -42,16 +45,3 @@ check_cpp_register_deps <- function() { ui_todo("Now install {ui_value(cpp_register_deps[!installed])} to use cpp11.") } } - -get_cpp_dynlib <- function() { - desc <- desc::desc(package = NULL) - desc$get_list("Package")[[1]] -} - -check_cpp_dynlib <- function() { - pkgname <- get_cpp_dynlib() - roxygen_ns_append( - sprintf("@useDynLib %s, .registration = TRUE", pkgname) - ) - roxygen_update_ns() -} From ad72cd1c38743fdf8588acbeb8ed37e0704b4a88 Mon Sep 17 00:00:00 2001 From: Mauricio 'Pacha' Vargas Sepulveda Date: Tue, 14 Nov 2023 15:38:43 -0500 Subject: [PATCH 5/7] https://github.com/r-lib/usethis/pull/1921#discussion_r1391780839 --- NEWS.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index cef57e748..a819c3eb8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,10 +5,6 @@ * Implicit usage of `numeric_version()` via comparison now always provides character input. This is in response to a request from CRAN to anticipate future solutions to . -* `use_cpp11()` creates/edits DESCRIPTION and NAMESPACE, allowing to use C++ - code directly. -* `.gitignore` now ignores `.vscode` for those who contribute to `usethis` from - that editor. # usethis 2.2.1 From 2e77417db03f5f1711a131bc2e85098f6bf6acfd Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 14 Nov 2023 14:52:03 -0600 Subject: [PATCH 6/7] Add news bullet --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index a819c3eb8..0546860e7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # usethis (development version) +* `use_cpp11()` now automatically updates the `NAMESPACE` (@pachadotdev, #1921). + # usethis 2.2.2 * Implicit usage of `numeric_version()` via comparison now always provides From ec385e291e1240f9b257ed068a814886510cf62c Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 14 Nov 2023 14:58:55 -0600 Subject: [PATCH 7/7] Tweak test; require use_package_doc() --- R/cpp11.R | 1 + tests/testthat/test-cpp11.R | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/R/cpp11.R b/R/cpp11.R index a5f5b666d..56d4de224 100644 --- a/R/cpp11.R +++ b/R/cpp11.R @@ -12,6 +12,7 @@ use_cpp11 <- function() { check_installed("cpp11") check_is_package("use_cpp11()") check_uses_roxygen("use_cpp11()") + check_has_package_doc("use_cpp11()") use_src() use_dependency("cpp11", "LinkingTo") diff --git a/tests/testthat/test-cpp11.R b/tests/testthat/test-cpp11.R index d7abd2bc9..b26be2060 100644 --- a/tests/testthat/test-cpp11.R +++ b/tests/testthat/test-cpp11.R @@ -7,10 +7,14 @@ test_that("use_cpp11() requires a package", { test_that("use_cpp11() creates files/dirs, edits DESCRIPTION and .gitignore", { create_local_package() use_roxygen_md() + use_package_doc() local_interactive(FALSE) local_check_installed() - local_mocked_bindings(check_cpp_register_deps = function() invisible()) + local_mocked_bindings( + check_cpp_register_deps = function() invisible() + # project_name = function() "testpkg" + ) use_cpp11() @@ -23,7 +27,7 @@ test_that("use_cpp11() creates files/dirs, edits DESCRIPTION and .gitignore", { expect_true(all(c("*.o", "*.so", "*.dll") %in% ignores)) namespace <- read_utf8(proj_path("NAMESPACE")) - expect_true("# Generated by roxygen2: do not edit by hand" %in% namespace) + expect_true(any(grepl("useDynLib", namespace))) }) test_that("check_cpp_register_deps is silent if all installed, emits todo if not", {