Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[R-package] replaced gendef.exe with R code to create R.def (fixes #3064) #24

Closed
wants to merge 9 commits into from

Conversation

jameslamb
Copy link
Owner

Experimental fix for microsoft#3064.

Short description

Building the LightGBM R package on Windows with Visual Studio compilers requires gendef.exe. That was bundled in previous versions of RTools but isn't part of RTools 4.0.

This PR replaces it with an R script that uses objdump.exe, software bundled in old and new distributions of RTools.

How you can test this

In a Windows environment with R 4.0, RTools 4.0, CMake, and Visual Studio, run the following:

git clone https://github.com/jameslamb/LightGBM.git
cd LightGBM
git fetch origin fix/r-4.0
git checkout fix/r-4.0

# install LightGBM
Rscript build_r.R

# test that it worked
cd R-package/tests
Rscript testthat.R

Long Description

Here's how building the R package with Visual Studio works today:

  1. The LightGBM R package has to be linked to R.dll to use R-provided C/C++ functions such as Rprintf for printing.
  2. Linking with Visual Studio compilers requires a .lib file (https://docs.microsoft.com/en-us/cpp/build/reference/link-input-files?redirectedfrom=MSDN&view=vs-2019)
  3. To create a .lib file from R.dll on Windows, we create a definition file (.def) and then use that to make a library file (.lib).
    • R.dll ships with R
    • A file R.def is created from R.dll, using gendef.exe
    • A file R.lib is created from R.def using dlltool.exe

The issue: gendefe.exe is not part of RTools 4.0.

My solution in this PR is to use objdump.exe + an R script to generate R.def. objdump creates output intended for humans not machines...it uses indentation and special chracters to make the file visually appealing. So the R script is used to run objdump, but more importantly to read in the text it produces and do some simple cleaning to get it into a machine-friendly format that can be re-written as a .def file.

From the "How to create a def file from a dll" section of this amazing MinGW documentation.

If the previous options don't work, you can still try to create a def file using the output from the objdump program (from the mingw distribution).
Here's an example.
objdump -p file.dll > dll.fil

Search for [Ordinal/Name Pointer] Table in dll.fil and use the list of functions following it to create your def file.

References

A lot of the content for this PR was totally new to me. Here are some links I found very helpful.

@jameslamb jameslamb closed this May 10, 2020
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants