Skip to content

Commit c73730a

Browse files
authored
Merge pull request #705 from RcppCore/feature/module-routine-registraiton
Automatically register init functions for RcppModules (closes #704)
2 parents f8d1e93 + 7f872ca commit c73730a

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2017-06-02 JJ Allaire <[email protected]>
2+
3+
* src/attributes.cpp: Automatically register init functions for RcppModules
4+
* R/Rcpp.package.skeleton.R: compileAttributes only after all code is generated
5+
16
2017-06-01 JJ Allaire <[email protected]>
27

38
* src/attributes.cpp: Fix registration for exports with name attribute.

R/Rcpp.package.skeleton.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,13 @@ Rcpp.package.skeleton <- function(name = "anRpackage", list = character(),
138138
file.copy(file, src)
139139
message(" >> copied ", file, " to src directory" )
140140
}
141-
compileAttributes(root)
142141
}
143142

144143
if (example_code) {
145144
if (isTRUE(attributes)) {
146145
file.copy(file.path( skeleton, "rcpp_hello_world_attributes.cpp"),
147146
file.path( src, "rcpp_hello_world.cpp"))
148147
message(" >> added example src file using Rcpp attributes")
149-
compileAttributes(root)
150-
message(" >> compiled Rcpp attributes")
151-
message(" >> do NOT modify by hand either RcppExports.cpp or ",
152-
"RcppExports.R")
153148
} else {
154149
header <- readLines(file.path(skeleton, "rcpp_hello_world.h"))
155150
header <- gsub("@PKG@", name, header, fixed = TRUE)
@@ -218,6 +213,11 @@ Rcpp.package.skeleton <- function(name = "anRpackage", list = character(),
218213
rm("rcpp_hello_world", envir = env)
219214
}
220215

216+
if (attributes) {
217+
compileAttributes(root)
218+
message(" >> compiled Rcpp attributes ")
219+
}
220+
221221
invisible(NULL)
222222
}
223223

inst/NEWS.Rd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
or newer (Elias Pipping in \ghpr{698}).
1818
\item Fix native registration for exports with name attribute (\ghpr{703}
1919
addressing \ghit{702}).
20+
\item Automatically register init functions for RcppModules.
2021
}
2122
}
2223
}

src/attributes.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,11 @@ namespace attributes {
675675
// for generating C++ interfaces
676676
std::vector<Attribute> cppExports_;
677677

678-
// for generating native routine registration
678+
// for generating Rcpp::export native routine registration
679679
std::vector<Attribute> nativeRoutines_;
680+
681+
// for generating module native routine registration
682+
std::vector<std::string> modules_;
680683
};
681684

682685
// Class which manages generating PackageName_RcppExports.h header file
@@ -1888,6 +1891,9 @@ namespace attributes {
18881891
}
18891892
} // #nocov end
18901893

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

18921898
// verbose if requested
18931899
if (verbose) { // #nocov start
@@ -1955,7 +1961,7 @@ namespace attributes {
19551961
}
19561962

19571963
// write native routines
1958-
if (!hasPackageInit && !nativeRoutines_.empty()) {
1964+
if (!hasPackageInit && (!nativeRoutines_.empty() || !modules_.empty())) {
19591965

19601966
// build list of routines we will register
19611967
std::vector<std::string> routineNames;
@@ -1965,6 +1971,11 @@ namespace attributes {
19651971
routineNames.push_back(package() + "_" + attr.function().name());
19661972
routineArgs.push_back(attr.function().arguments().size());
19671973
}
1974+
std::string kRcppModuleBoot = "_rcpp_module_boot_";
1975+
for (std::size_t i=0;i<modules_.size(); i++) {
1976+
routineNames.push_back(kRcppModuleBoot + modules_[i]);
1977+
routineArgs.push_back(0);
1978+
}
19681979
if (hasCppInterface()) {
19691980
routineNames.push_back(registerCCallableExportedName());
19701981
routineArgs.push_back(0);
@@ -1976,6 +1987,11 @@ namespace attributes {
19761987
std::vector<std::string> declarations = extraRoutines["declarations"];
19771988
std::vector<std::string> callEntries = extraRoutines["call_entries"];
19781989

1990+
// add declarations for modules
1991+
for (std::size_t i=0;i<modules_.size(); i++) {
1992+
declarations.push_back("RcppExport SEXP " + kRcppModuleBoot + modules_[i] + "();");
1993+
}
1994+
19791995
// generate declarations
19801996
if (declarations.size() > 0) {
19811997
ostr() << std::endl;

0 commit comments

Comments
 (0)