KlausVigo / phangorn

Phylogenetic analysis in R
http://klausvigo.github.io/phangorn/
203 stars 38 forks source link

phangorn for webR / wasm #161

Closed isbool closed 8 months ago

isbool commented 8 months ago

Hey, Would it be possible that this packages gets complied to emscripten to be able to be used with webR.

UPDATE when i try to compile it i get the following error:

> build("phangorn")
trying URL 'https://packagemanager.posit.co/cran/latest/src/contrib/phangorn_2.11.1.tar.gz'
Content type 'binary/octet-stream' length 1934352 bytes (1.8 MB)
==================================================
downloaded 1.8 MB

> Will download 1 package with unknown size.
v All system requirements are already installed.

i No downloads are needed
v 17 deps: kept 14 [816ms]
* installing *source* package 'phangorn' ...
** package 'phangorn' successfully unpacked and MD5 sums checked
** using non-staged installation
** libs
using C compiler: 'emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.47 (431685f05c67f0424c11473cc16798b9587bb536)'
using C++ compiler: 'emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.47 (431685f05c67f0424c11473cc16798b9587bb536)'
using C++11
em++ -std=gnu++11 -DNDEBUG  -I'/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include' -I/opt/webr/wasm/include -I/opt/webr/R/build/R-4.3.3/build/include -I/opt/webr/R/build/R-4.3.3/src/include -s USE_BZIP2=1 -s USE_ZLIB=1  -fpic  -std=gnu++11 -Oz -fPIC -fwasm-exceptions -s SUPPORT_LONGJMP=wasm -DRCPP_DEMANGLER_ENABLED=0 -D__STRICT_ANSI__ -c RcppExports.cpp -o RcppExports.o
emcc -DNDEBUG  -I'/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include' -I/opt/webr/wasm/include -I/opt/webr/R/build/R-4.3.3/build/include -I/opt/webr/R/build/R-4.3.3/src/include -s USE_BZIP2=1 -s USE_ZLIB=1  -fpic  -Oz -fPIC -fwasm-exceptions -s SUPPORT_LONGJMP=wasm -std=gnu11 -c dist.c -o dist.o
em++ -std=gnu++11 -DNDEBUG  -I'/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include' -I/opt/webr/wasm/include -I/opt/webr/R/build/R-4.3.3/build/include -I/opt/webr/R/build/R-4.3.3/src/include -s USE_BZIP2=1 -s USE_ZLIB=1  -fpic  -std=gnu++11 -Oz -fPIC -fwasm-exceptions -s SUPPORT_LONGJMP=wasm -DRCPP_DEMANGLER_ENABLED=0 -D__STRICT_ANSI__ -c dupAtomMat.cpp -o dupAtomMat.o
em++ -std=gnu++11 -DNDEBUG  -I'/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include' -I/opt/webr/wasm/include -I/opt/webr/R/build/R-4.3.3/build/include -I/opt/webr/R/build/R-4.3.3/src/include -s USE_BZIP2=1 -s USE_ZLIB=1  -fpic  -std=gnu++11 -Oz -fPIC -fwasm-exceptions -s SUPPORT_LONGJMP=wasm -DRCPP_DEMANGLER_ENABLED=0 -D__STRICT_ANSI__ -c fitch64.cpp -o fitch64.o
fitch64.cpp:887:35: error: use of overloaded operator '[]' is ambiguous (with operand types
      'NumericVector' (aka 'Vector<14>') and 'long')
  887 |         if((tmp >> l) & 1ull) pars[anc[k] - 1L]+= obj->weight[i*BIT_SIZE + l];
      |                               ~~~~^~~~~~~~~~~~
/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include/Rcpp/vector/Vector.h:340:18: note: candidate
      function
  340 |     inline Proxy operator[]( R_xlen_t i ){ return cache.ref(i) ; }
      |                  ^
/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include/Rcpp/vector/Vector.h:341:24: note: candidate
      function
  341 |     inline const_Proxy operator[]( R_xlen_t i ) const { return cache.ref(i) ; }
      |                        ^
fitch64.cpp:887:35: note: built-in candidate operator[](struct SEXPREC *, long)
  887 |         if((tmp >> l) & 1ull) pars[anc[k] - 1L]+= obj->weight[i*BIT_SIZE + l];
      |                                   ^
fitch64.cpp:887:35: note: built-in candidate operator[](const struct SEXPREC *, long)
fitch64.cpp:887:35: note: built-in candidate operator[](volatile struct SEXPREC *, long)
fitch64.cpp:887:35: note: built-in candidate operator[](const volatile struct SEXPREC *, long)
fitch64.cpp:900:11: error: use of overloaded operator '[]' is ambiguous (with operand types
      'NumericVector' (aka 'Vector<14>') and 'long')
  900 |       pars[anc[k] - 1L] += popcnt64(tmp);
      |       ~~~~^~~~~~~~~~~~
/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include/Rcpp/vector/Vector.h:340:18: note: candidate
      function
  340 |     inline Proxy operator[]( R_xlen_t i ){ return cache.ref(i) ; }
      |                  ^
/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include/Rcpp/vector/Vector.h:341:24: note: candidate
      function
  341 |     inline const_Proxy operator[]( R_xlen_t i ) const { return cache.ref(i) ; }
      |                        ^
fitch64.cpp:900:11: note: built-in candidate operator[](struct SEXPREC *, long)
  900 |       pars[anc[k] - 1L] += popcnt64(tmp);
      |           ^
fitch64.cpp:900:11: note: built-in candidate operator[](const struct SEXPREC *, long)
fitch64.cpp:900:11: note: built-in candidate operator[](volatile struct SEXPREC *, long)
fitch64.cpp:900:11: note: built-in candidate operator[](const volatile struct SEXPREC *, long)
fitch64.cpp:918:35: error: use of overloaded operator '[]' is ambiguous (with operand types
      'NumericVector' (aka 'Vector<14>') and 'long')
  918 |         if((tmp >> l) & 1ull) pars[anc[nl] - 1L]+= obj->weight[i*BIT_SIZE + l];
      |                               ~~~~^~~~~~~~~~~~~
/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include/Rcpp/vector/Vector.h:340:18: note: candidate
      function
  340 |     inline Proxy operator[]( R_xlen_t i ){ return cache.ref(i) ; }
      |                  ^
/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include/Rcpp/vector/Vector.h:341:24: note: candidate
      function
  341 |     inline const_Proxy operator[]( R_xlen_t i ) const { return cache.ref(i) ; }
      |                        ^
fitch64.cpp:918:35: note: built-in candidate operator[](struct SEXPREC *, long)
  918 |         if((tmp >> l) & 1ull) pars[anc[nl] - 1L]+= obj->weight[i*BIT_SIZE + l];
      |                                   ^
fitch64.cpp:918:35: note: built-in candidate operator[](const struct SEXPREC *, long)
fitch64.cpp:918:35: note: built-in candidate operator[](volatile struct SEXPREC *, long)
fitch64.cpp:918:35: note: built-in candidate operator[](const volatile struct SEXPREC *, long)
fitch64.cpp:932:12: error: use of overloaded operator '[]' is ambiguous (with operand types
      'NumericVector' (aka 'Vector<14>') and 'long')
  932 |       pars [anc[nl] - 1L]+= popcnt64(tmp);
      |       ~~~~ ^~~~~~~~~~~~~
/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include/Rcpp/vector/Vector.h:340:18: note: candidate
      function
  340 |     inline Proxy operator[]( R_xlen_t i ){ return cache.ref(i) ; }
      |                  ^
/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include/Rcpp/vector/Vector.h:341:24: note: candidate
      function
  341 |     inline const_Proxy operator[]( R_xlen_t i ) const { return cache.ref(i) ; }
      |                        ^
fitch64.cpp:932:12: note: built-in candidate operator[](struct SEXPREC *, long)
  932 |       pars [anc[nl] - 1L]+= popcnt64(tmp);
      |            ^
fitch64.cpp:932:12: note: built-in candidate operator[](const struct SEXPREC *, long)
fitch64.cpp:932:12: note: built-in candidate operator[](volatile struct SEXPREC *, long)
fitch64.cpp:932:12: note: built-in candidate operator[](const volatile struct SEXPREC *, long)
4 errors generated.
em++: error: '/opt/emsdk/upstream/bin/clang++ -target wasm32-unknown-emscripten -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -wasm-enable-sjlj -mllvm -disable-lsr -DEMSCRIPTEN --sysroot=/opt/emsdk/upstream/emscripten/cache/sysroot -Xclang -iwithsysroot/include/fakesdl -Xclang -iwithsysroot/include/compat -std=gnu++11 -DNDEBUG -I/root/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include -I/opt/webr/wasm/include -I/opt/webr/R/build/R-4.3.3/build/include -I/opt/webr/R/build/R-4.3.3/src/include -fpic -std=gnu++11 -Oz -fPIC -fwasm-exceptions -DRCPP_DEMANGLER_ENABLED=0 -D__STRICT_ANSI__ -c fitch64.cpp -o fitch64.o' failed (returned 1)
make: *** [/opt/R/4.3.2/lib/R/etc/Makeconf:200: fitch64.o] Error 1
ERROR: compilation failed for package 'phangorn'
* removing '/tmp/RtmpWgLbzO/file15c418f48/phangorn'
Error in wasm_build(pkg, tarball_path, out_dir) :
  Building wasm binary for package 'phangorn' failed.

ChatGPT explanation:

The error messages indicate that there's an issue with the overloaded operator '[]' being ambiguous when used with NumericVector types in the fitch64.cpp file within the phangorn package. This is likely because the emcc compiler (Emscripten) used for compiling to WebAssembly is more strict or behaves differently than traditional C++ compilers regarding operator overloading and ambiguity resolution.

The error stems from the fact that the [] operator is overloaded in the Rcpp::NumericVector class, and the compiler cannot decide which version of the operator to use when given a long index type. Specifically, the error points out that the compiler is confused between using NumericVector's operator[] and the built-in array operator[].

To address this issue, explicit type conversion to the appropriate index type expected by the NumericVector class might help. R_xlen_t is the index type used by Rcpp for indexing, which is compatible with the indexing of Rcpp vectors. Modifying the code to explicitly cast the index to R_xlen_t could resolve the ambiguity.

However, as you cannot directly modify the source code of the phangorn package without forking or creating a pull request to the original repository, one solution could be to locally modify a copy of phangorn, apply the necessary changes, and then attempt to recompile. This approach requires a good understanding of both C++ and R package development.

For a more general solution that doesn't involve modifying the package's source code, you might consider reporting this issue to the package maintainers. They might be able to provide a fix or update that ensures compatibility with WebAssembly compilation.

As a temporary workaround, if your project allows, you could try using alternative methods or packages that offer similar functionalities as phangorn but don't encounter the same compilation issues with WebAssembly.

Also from what i can see this package has the following dependencies:

So its a pity for this package to not support webR / wasm