libR-sys icon indicating copy to clipboard operation
libR-sys copied to clipboard

[Windows] Is it possible to include Rust toolchain in Rtools?

Open yutannihilation opened this issue 5 years ago • 18 comments

Currently libR-sys requires users to install standalone MSYS2 for libclang, but as https://github.com/extendr/extendr/issues/13 suggests, it might be good to use Rtools' MSYS2 for simplicity.

Rtools' MSYS uses a dedicated repository, which has only the limited number of packages available. To add some packages there, we can send a pull request to r-windows/rtools-packages repo, according to this document: System Libraries in Rtools40.

yutannihilation avatar Dec 10 '20 15:12 yutannihilation

Reading through the documents for Rtools, I wonder whether we may have to start a conversation with the maintainers first.

In this section it says that we can only contribute static libraries, but that probably won't work for libclang.

In this section it says that we can alternatively provide additional build tools but those should only be used to build more packman packages. But we need additional build tools to be used during the R package build.

@yutannihilation Do you want to initiate this conversation with the Rtools maintainers?

clauswilke avatar Dec 10 '20 16:12 clauswilke

In this section it says that we can only contribute static libraries, but that probably won't work for libclang.

Thanks, I didn't notice this... After some thinking, I now feel this is less important.

  • For Windows user who just use CRAN packages, they can install CRAN precompiled binaries, so this won't affect them.
  • For Windows user who try non-CRAN pakcages, they might be able to use a prebuilt static library (which seems undesirable according to https://github.com/extendr/extendr/pull/10#issuecomment-724577056). If we don't choose this approach, we might want to use Rtools' MSYS2 for the ease of installation.
  • For Windows developers, probably they need to install Rust toolchain anyway for debugging etc.

Let's hold this issue on hold for a while.

yutannihilation avatar Dec 20 '20 08:12 yutannihilation

Yes, I think we're good at this time. It might be more important to add Rust than libclang.

I've also been thinking that long-term, if this project gets widely used, we should try to convince the R core team to distribute the bindings as part of their official distribution. In this way we'd know that we always have the correct bindings for each R version and platform.

clauswilke avatar Dec 20 '20 18:12 clauswilke

Rtools cannot be used for libclang. However, it can be used with precomputed bindings, see discussion in https://github.com/extendr/rextendr/issues/19.

Ilia-Kosenkov avatar Jan 17 '21 15:01 Ilia-Kosenkov

Not sure if this issue is worth keeping open, but I removed libclang from the issue title because we basically agreed it's not very important.

yutannihilation avatar May 04 '21 07:05 yutannihilation

@yutannihilation, it looks like we have got the new Rtools version, like, today. https://cran.r-project.org/bin/windows/Rtools/ Here is also a PR to bring the v2 to chocolatey https://github.com/facorread/rtools-chocolatey/pull/3/files I am going to check if it contains fixes we need and if it works as expected. Perhaps the CI already pulls the latest Rtools version.

Ilia-Kosenkov avatar May 04 '21 10:05 Ilia-Kosenkov

Oh, cool. It might be just that they moved the artifacts from Bintray to somewhere because Bintray is retiring (I saw some commits related to this on r-windows/rtools-base recently), but it's worth checking!

yutannihilation avatar May 04 '21 11:05 yutannihilation

@yutannihilation, I checked that it contains our fix, so now I am working to reproduce the errors we had before to see if this fix actually helps. I'll post updates here and in https://github.com/extendr/rextendr/issues/91.

Ilia-Kosenkov avatar May 04 '21 11:05 Ilia-Kosenkov

FYI, the CI pulls Rtools by hard-coded name, so unless they change it we won't get a new version: https://github.com/r-lib/actions/blob/0d6c4b3efe82090bc03e99c585d9e89003117931/setup-r/src/installer.ts#L330-L336

Ilia-Kosenkov avatar May 04 '21 11:05 Ilia-Kosenkov

Can we specify rtools-version: 40v2?

https://github.com/r-lib/actions/blob/18d8b3cd131edfa357583fc071e138e82abdb0a9/setup-r/action.yml#L8-L10

yutannihilation avatar May 04 '21 11:05 yutannihilation

@yutannihilation, I think we can because the version string is inserted as-is in the tools downloading URL.

Ilia-Kosenkov avatar May 04 '21 11:05 Ilia-Kosenkov

I created a pull request: https://github.com/r-lib/actions/pull/287. Hope this works...

yutannihilation avatar May 04 '21 13:05 yutannihilation

I am struggling to make the CI work. It not only does not seem to care about Rtools version, it also runs incorrect snapshot tests, taking the snapshot for use_extendr from yet unmerged PR (https://github.com/extendr/rextendr/blob/233eaf861d21ecb749ae3954cc89fe9f0369192a/tests/testthat/_snaps/use_extendr.md). I think there is some sort of heavy caching that I cannot beat.

Locally though it seems to work fine.

Ilia-Kosenkov avatar May 04 '21 13:05 Ilia-Kosenkov

With old Rtools:

r_path <- normalizePath(dirname(system2("where", "R.exe", stdout = TRUE)[1]))
cargo_path <- normalizePath(dirname(system2("where", "cargo.exe", stdout = TRUE)[1]))
rtools_home <- normalizePath("C:\\rtools40") # Point this to a correct Rtools
rtools_path <- normalizePath(paste(rtools_home, "usr\\bin", sep = "\\"))

old_path <- Sys.getenv("PATH")
new_path <- paste(r_path, cargo_path, rtools_path, sep = ";")
Sys.setenv(PATH = new_path)
Sys.setenv(RTOOLS40_HOME = rtools_home)
rextendr::rust_function("fn add(a:i32, b:i32) -> i32 {a + b}", toolchain = "stable-x86_64-pc-windows-msvc")
#> i build directory: C:\Users\[redacted]\AppData\Local\Temp\RtmpQZ4isS\file13c42b8519a5
#> Error: Rust code could not be compiled successfully. Aborting.
add(42L, 100500L)
#> Error in add(42L, 100500L): could not find function "add"

Created on 2021-05-04 by the reprex package (v2.0.0)

With new Rtools:

r_path <- normalizePath(dirname(system2("where", "R.exe", stdout = TRUE)[1]))
cargo_path <- normalizePath(dirname(system2("where", "cargo.exe", stdout = TRUE)[1]))
rtools_home <- normalizePath("C:\\rtools40_test") # Point this to a correct Rtools
rtools_path <- normalizePath(paste(rtools_home, "usr\\bin", sep = "\\"))

old_path <- Sys.getenv("PATH")
new_path <- paste(r_path, cargo_path, rtools_path, sep = ";")
Sys.setenv(PATH = new_path)
Sys.setenv(RTOOLS40_HOME = rtools_home)
rextendr::rust_function("fn add(a:i32, b:i32) -> i32 {a + b}", toolchain = "stable-x86_64-pc-windows-msvc")
#> i build directory: C:\Users\[redacted]\AppData\Local\Temp\RtmpuCsL8P\file251c1567143f
#> v Writing file C:/Users/[redacted]/AppData/Local/Temp/RtmpuCsL8P/file251c1567143f/target/extendr_wrappers.R.
add(42L, 100500L)
#> [1] 100542

Created on 2021-05-04 by the reprex package (v2.0.0)

Ilia-Kosenkov avatar May 04 '21 14:05 Ilia-Kosenkov

One of the major annoyances of github actions that should be easy to fix but for some reason they don't is that you can't manually clear the cache. See here on how it can be done: https://github.community/t/how-to-clear-cache-in-github-actions/129038

clauswilke avatar May 04 '21 14:05 clauswilke

I have finally resolved all the issues. The faulty test was due to my local outdated version of {cli}, after updating it I got the new snapshot into the CI. The tests are run using nearly-empty PATH, which is set to point to binaries of R, cargo, and rtools' /usr/bin (see https://github.com/extendr/rextendr/issues/91#issuecomment-808755243). Here are the results:

  • Failing R CMD check using old rtools40 https://github.com/Ilia-Kosenkov/rextendr/runs/2501911452?check_suite_focus=true#step:9:314
  • Successful R CMD check using new rtools40v2 https://github.com/Ilia-Kosenkov/rextendr/runs/2501911453?check_suite_focus=true#step:10:182 (the error is actually due to a warning issued in the absence of qpdf...)

There are some problems with pulling the latest rtools40v2, see discussion here: https://github.com/r-lib/actions/pull/287

Ilia-Kosenkov avatar May 04 '21 15:05 Ilia-Kosenkov

Pleased to hear this is going well.

On Tue, 4 May 2021, 16:55 Ilia, @.***> wrote:

I have finally resolved all the issues. The faulty test was due to my local outdated version of {cli}, after updating it I got the new snapshot into the CI. The tests are run using nearly-empty PATH, which is set to point to binaries of R, cargo, and rtools' /usr/bin (see extendr/rextendr#91 (comment) https://github.com/extendr/rextendr/issues/91#issuecomment-808755243). Here are the results:

  • Failing R CMD check using old rtools40

https://github.com/Ilia-Kosenkov/rextendr/runs/2501911452?check_suite_focus=true#step:9:314

  • Successful R CMD check using new rtools40v2

https://github.com/Ilia-Kosenkov/rextendr/runs/2501911453?check_suite_focus=true#step:10:182 (the error is actually due to a warning issued in the absence of qpdf...)

There are some problems with pulling the latest rtools40v2, see discussion here: r-lib/actions#287 https://github.com/r-lib/actions/pull/287

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/extendr/libR-sys/issues/21#issuecomment-832051067, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAL36XCZNVKY5GLTD5GHZXLTMAKF5ANCNFSM4UVG2TPQ .

andy-thomason avatar May 04 '21 23:05 andy-thomason

It seems it's not impossible to add Rust to Rtools. This recent email from Tomas Kalibera clarifies how to add a new library.

https://stat.ethz.ch/pipermail/r-devel/2021-December/081382.html

It's probably not very easy, though. There's an existing issue about adding Rust on the upstream (MXE) repository, but it seems there has been little discussion so far.

https://github.com/mxe/mxe/issues/2499

yutannihilation avatar Dec 20 '21 11:12 yutannihilation

I'm closing this because

  • the CRAN Windows machines now have Rust installed, so it seems this is not necessary to release packages on CRAN; to me, this means they decided not to include Rust in Rtools.
  • non-CRAN packages can release the binary on R-universe, so the users can install it without Rust toolchain on their local

Feel free to reopen or file a new issue if necessary!

yutannihilation avatar Dec 17 '22 04:12 yutannihilation