Skip to content

Rtools42

kaskr edited this page Nov 21, 2023 · 3 revisions

Rtools42 developer notes

Background

The old Windows toolchain 'Rtools' has been replaced several times. First by the community driven 'Rtools40' and later by the R-core maintained 'Rtools42'. Going forward, we have to support the official Rtools (Rtools42). This temporary wiki page collects information about how to install and use the new toolchain. These notes should eventually be converted to proper documentation. Topics of interest:

  • How to install Rtools (must be portable)
  • How to get gdb working (in a portable way). gdb no longer comes with Rtools by default.
  • How to use custom SuiteSparse installation (current ?compile assumes Rtools40 and are therefore outdated). Update: Might not be relevant when Matrix-1.7.0 is published.
  • How to fix problem with very long linking time when using precompiled TMB and framework='TMBad'.

gdb

Is this a reliable way to install gdb on Windows?:

system("pacman -Sy --noconfirm gdb")

Custom SuiteSparse

Rtools42 breaks the instructions in ?compile on the use of super=TRUE and/or longint=TRUE.

We now have to install SuiteSparse using

pacman -Sy
pacman -S mingw-w64-ucrt-x86_64-suitesparse

or alternatively using a one-liner from R (should this be the recommended way, i.e. is it portable?)

system("pacman -Sy --noconfirm mingw-w64-ucrt-x86_64-suitesparse")

However, compiling a TMB model with SuiteSparse using compile("ar1_4D.cpp",super=TRUE,framework="TMBad") no longer works.

The code fragment causing the problem is:

        PKG_LIBS %+=% if (.Platform$OS.type == "windows") 
            "-lcholmod -lcolamd -lamd -lsuitesparseconfig -lopenblas $(SHLIB_OPENMP_CXXFLAGS)"
        else "-lcholmod"

Solution idea that seems to work (needs tidying):

## mingw keeps dynamic libs in 'bin' and static libs in 'lib'.
## To find the static libraries (in right order) corresponding to a given dll, we can use this function:
get_static <- function(dll="libcholmod.dll") {
  cmd <- paste0("ldd C:/rtools42/ucrt64/bin/", dll)
  tmp <- system(cmd, TRUE)
  tmp <- c(paste0("C:/rtools42/ucrt64/bin/", dll), tmp)
  tmp <- grep("ucrt64", tmp, val=TRUE)
  tmp <- sub(".*(/ucrt64.*dll).*","\\1",tmp)
  tmp <- sub("/bin/","/lib/",tmp)
  tmp <- sub(".dll",".a",tmp)
  tmp <- paste0("C:/rtools42",tmp)
  tmp <- tmp[file.exists(tmp)]
  paste(tmp, collapse=" ")
}
libs <- paste(get_static(), "$(SHLIB_OPENMP_CXXFLAGS)")
compile("ar1_4D.cpp",super=FALSE,framework="TMBad",CPPFLAGS=" -DTMBAD_SUPERNODAL -DEIGEN_USE_BLAS -DEIGEN_USE_LAPACKE", PKG_LIBS=libs, CLINK_CPPFLAGS="-I/ucrt64/include")

Explanation