coatless-rd-rcpp / rcpp-headers-subdirs

Rcpp: Including C++ code in a Subdirectory within Src
http://rd-rcpp.thecoatlessprofessor.com/rcpp-headers-subdirs/
17 stars 1 forks source link

better file finding #3

Open Bijaelo opened 3 years ago

Bijaelo commented 3 years ago

Hey, thank you for your example. In all honesty Makevars and configure are massive roadblocks and the documentation is nowhere near user friendly. One thing I noticed however, was that you only search certain (specified) directories in your example

# Dynamically generate list of sources from subdirectories via shell
SUBDIR_SOURCES="$(cd src/ && ls {A,B}/*.cpp | tr '\n' ' ')"

And this is not really mentioned in your post. But if one changed this to

# Dynamically generate list of sources from subdirectories via shell
SUBDIR_SOURCES="$(cd src/ && find -name \*.cpp -prune | sed 's|^./||' | tr '\n' ' ')"

This should:

  1. Search all subdirectories for *.cpp files
  2. remove the leading ./ (output from find).
  3. Like before replace \n with a whitespace

This might be dangerous if someone were building a package linking to an external library (boost, tensorflow whatever). But for these users I am expecting them to have enough knowledge, to actually make their own scripts. Meanwhile I'll shamelessly copy-paste your scripts, and continue bashing my phase against the extension manual to hopefully understand these parts in the future.

Edit:

If this was to be used one should change

# Include all C++ files in src/ and its subdirectories: src/A and src/B 
SOURCES=r-accessor-to-code.cpp RcppExports.cpp @SUBDIR_SOURCES@
              # Variable to place contents     ^^^^^^^^^^^^^^^^

to

# Include all C++ files in src/ and its subdirectories: src/A and src/B 
SOURCES=@SUBDIR_SOURCES@
              # ^^^^^^^^^^^^^^^ Variable to place contents     

(as the main directory is already included)

coatless commented 3 years ago

@Bijaelo sorry for the late response on this.

You are correct. The example only searched pre-specified directories. The suggested change addresses this limitation nicely by yielding all files instead of just the subdirectories. So, thanks for taking the time to write this up!

As it was a long time ago that I worked on this example, I think I might have held off on using find as I was worried about more GNU restrictions/compatibilities. Have you experienced any issues with packages on CRAN using this approach?

Bijaelo commented 3 years ago

Hi @coatless, I have not yet published my package to CRAN yet (It turned out to be somewhat of a more time-taking task). That being said, I have since read the R-extension manual potentially a few too many times, and the SOURCES directory is described in section 1.2.1.3 Compiling in sub-directories and specifically suggests using SOURCES and OBJECTS of makevars in this fashion. This should give reason that the change is within the bounds of CRAN requirements. :-)