Skip to content

Commit

Permalink
Merge pull request #705 from RcppCore/feature/module-routine-registra…
Browse files Browse the repository at this point in the history
…iton

Automatically register init functions for RcppModules (closes #704)
  • Loading branch information
eddelbuettel committed Jun 2, 2017
2 parents f8d1e93 + 7f872ca commit c73730a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
@@ -1,3 +1,8 @@
2017-06-02 JJ Allaire <jj@rstudio.com>

* src/attributes.cpp: Automatically register init functions for RcppModules
* R/Rcpp.package.skeleton.R: compileAttributes only after all code is generated

2017-06-01 JJ Allaire <jj@rstudio.com>

* src/attributes.cpp: Fix registration for exports with name attribute.
Expand Down
10 changes: 5 additions & 5 deletions R/Rcpp.package.skeleton.R
Expand Up @@ -138,18 +138,13 @@ Rcpp.package.skeleton <- function(name = "anRpackage", list = character(),
file.copy(file, src)
message(" >> copied ", file, " to src directory" )
}
compileAttributes(root)
}

if (example_code) {
if (isTRUE(attributes)) {
file.copy(file.path( skeleton, "rcpp_hello_world_attributes.cpp"),
file.path( src, "rcpp_hello_world.cpp"))
message(" >> added example src file using Rcpp attributes")
compileAttributes(root)
message(" >> compiled Rcpp attributes")
message(" >> do NOT modify by hand either RcppExports.cpp or ",
"RcppExports.R")
} else {
header <- readLines(file.path(skeleton, "rcpp_hello_world.h"))
header <- gsub("@PKG@", name, header, fixed = TRUE)
Expand Down Expand Up @@ -218,6 +213,11 @@ Rcpp.package.skeleton <- function(name = "anRpackage", list = character(),
rm("rcpp_hello_world", envir = env)
}

if (attributes) {
compileAttributes(root)
message(" >> compiled Rcpp attributes ")
}

invisible(NULL)
}

Expand Down
1 change: 1 addition & 0 deletions inst/NEWS.Rd
Expand Up @@ -17,6 +17,7 @@
or newer (Elias Pipping in \ghpr{698}).
\item Fix native registration for exports with name attribute (\ghpr{703}
addressing \ghit{702}).
\item Automatically register init functions for RcppModules.
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions src/attributes.cpp
Expand Up @@ -675,8 +675,11 @@ namespace attributes {
// for generating C++ interfaces
std::vector<Attribute> cppExports_;

// for generating native routine registration
// for generating Rcpp::export native routine registration
std::vector<Attribute> nativeRoutines_;

// for generating module native routine registration
std::vector<std::string> modules_;
};

// Class which manages generating PackageName_RcppExports.h header file
Expand Down Expand Up @@ -1888,6 +1891,9 @@ namespace attributes {
}
} // #nocov end

// record modules
const std::vector<std::string>& modules = attributes.modules();
modules_.insert(modules_.end(), modules.begin(), modules.end());

// verbose if requested
if (verbose) { // #nocov start
Expand Down Expand Up @@ -1955,7 +1961,7 @@ namespace attributes {
}

// write native routines
if (!hasPackageInit && !nativeRoutines_.empty()) {
if (!hasPackageInit && (!nativeRoutines_.empty() || !modules_.empty())) {

// build list of routines we will register
std::vector<std::string> routineNames;
Expand All @@ -1965,6 +1971,11 @@ namespace attributes {
routineNames.push_back(package() + "_" + attr.function().name());
routineArgs.push_back(attr.function().arguments().size());
}
std::string kRcppModuleBoot = "_rcpp_module_boot_";
for (std::size_t i=0;i<modules_.size(); i++) {
routineNames.push_back(kRcppModuleBoot + modules_[i]);
routineArgs.push_back(0);
}
if (hasCppInterface()) {
routineNames.push_back(registerCCallableExportedName());
routineArgs.push_back(0);
Expand All @@ -1976,6 +1987,11 @@ namespace attributes {
std::vector<std::string> declarations = extraRoutines["declarations"];
std::vector<std::string> callEntries = extraRoutines["call_entries"];

// add declarations for modules
for (std::size_t i=0;i<modules_.size(); i++) {
declarations.push_back("RcppExport SEXP " + kRcppModuleBoot + modules_[i] + "();");
}

// generate declarations
if (declarations.size() > 0) {
ostr() << std::endl;
Expand Down

0 comments on commit c73730a

Please sign in to comment.