Bioconductor / Rhtslib

HTSlib high-throughput sequencing library as an R package
https://bioconductor.org/packages/Rhtslib
11 stars 12 forks source link

Linking package fails R CMD check on Mac OS X #6

Closed lucasnell closed 5 years ago

lucasnell commented 5 years ago

Hello,

Thanks for this great package. It's saved me a bunch of headaches.

I'm using it for an R package I'm developing (link to relevant branch here), which is working well on Linux and Windows. On Mac, it's building fine on my local computer, but when I run R CMD check locally or on Travis-CI (link here), I get the following output:

── R CMD check results ────────────────────────────── jackalope 0.1.0 ────
Duration: 4m 12s

❯ checking whether the package can be unloaded cleanly ... WARNING
  Error: package or namespace load failed for ‘jackalope’ in dyn.load(file, DLLpath = DLLpath, ...):
   unable to load shared object '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so':
    dlopen(/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so, 6): Symbol not found: _lzma_easy_buffer_encode
    Referenced from: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so
    Expected in: flat namespace
   in /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so
  Execution halted

❯ checking whether the namespace can be loaded with stated dependencies ... WARNING
  Error in dyn.load(file, DLLpath = DLLpath, ...) : 
    unable to load shared object '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so':
    dlopen(/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so, 6): Symbol not found: _lzma_easy_buffer_encode
    Referenced from: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so
    Expected in: flat namespace
   in /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so
  Calls: <Anonymous> ... namespaceImport -> loadNamespace -> library.dynam -> dyn.load
  Execution halted

  A namespace must be able to be loaded with just the base namespace
  loaded: otherwise if the namespace gets loaded by a saved object, the
  session will be unable to start.

  Probably some imports need to be declared in the NAMESPACE file.

❯ checking dependencies in R code ... NOTE
  Error: package or namespace load failed for ‘jackalope’ in dyn.load(file, DLLpath = DLLpath, ...):
   unable to load shared object '/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so':
    dlopen(/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so, 6): Symbol not found: _lzma_easy_buffer_encode
    Referenced from: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so
    Expected in: flat namespace
   in /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rhtslib/libs/Rhtslib.so
  Call sequence:
  6: stop(msg, call. = FALSE, domain = NA)
  5: value[[3L]](cond)
  4: tryCatchOne(expr, names, parentenv, handlers[[1L]])
  3: tryCatchList(expr, classes, parentenv, handlers)
  2: tryCatch({
         attr(package, "LibPath") <- which.lib.loc
         ns <- loadNamespace(package, lib.loc)
         env <- attachNamespace(ns, pos = pos, deps, exclude, include.only)
     }, error = function(e) {
         P <-
  Execution halted

Any ideas on how to fix this? Thanks!

lucasnell commented 5 years ago

The issue seems to stem from the lzma_... functions defined in os/lzma_stub.h not being present in the shared library for some reason. I made a much simpler package here that only includes the bgzf.h header to bgzip-compress an input file. This reproduces the same R CMD check warnings.

I manually added the lzma_... functions defined in os/lzma_stub.h on a fork of Rhtslib, and this fixes the issue for me. You can find the changes here.

mtmorgan commented 5 years ago

I think the problem instead is that your package uses liblzma and that needs to be available on travis. It would seem also that simply loading Rhtslib on your travis builder would generate this problem?

lucasnell commented 5 years ago

You're right that a package that simply loads Rhtslib (on my local Mac) causes this problem. I tried installing xz on travis and on my local machine (per this comment) to check my package, and in neither case is the problem gone.

Next I ran R CMD check on a clone of the most recent version of Rhtslib on my Mac, and the checks produce the same warnings.

It seems like maybe it's weirdness with how xz (which contains the lzma library on macOS) links to the lzma library? There are some other issues in other programs that seem related to this, such as this.

I tried doing R CMD check on Rhtslib on osx on travis, and that fails in the same way (link here).

I next changed line 14 of Rhtslib's Makevars to the following:

PKG_LIBS="${USRLIB_DIR}/libhts.a" "/usr/local/lib/liblzma.a"

This fixes the problem when checking Rhtslib (link here), although I'm guessing that there's a more robust way of implementing this. It also fixes it for linking packages on my local machine (as long as those packages also have PKG_LIBS=/usr/local/lib/liblzma.a added to their Makevars).

hpages commented 5 years ago

Hi,

Don't run R CMD check on Rhtslib and just install the binary. The binary is statically linked to lzma so will load on any system. See https://bioconductor.org/packages/Rhtslib for the details (in particular on how to install the package). We put a lot of effort in producing Windows and Mac binaries for all Bioconductor packages and most of them are statically linked to avoid the kind of problems you're encountering.

H.

lucasnell commented 5 years ago

Okay, thanks for the sanity check. I fixed the problem by no longer importing Rhtslib, just zlibbioc and linking the package to the static lzma library. Thanks again for your help.