[education/rkward] rkward/rbackend: Generate R.lib from within cmake, when compiling with MSVC

Thomas Friedrichsmeier null at kde.org
Sat Apr 9 07:32:35 BST 2022


Git commit 91d713daad510c149f2b47cb4f645f63335f6e6b by Thomas Friedrichsmeier.
Committed on 09/04/2022 at 06:28.
Pushed by tfry into branch 'master'.

Generate R.lib from within cmake, when compiling with MSVC

M  +5    -0    rkward/rbackend/CMakeLists.txt
A  +23   -0    rkward/rbackend/GenLibFile.py

https://invent.kde.org/education/rkward/commit/91d713daad510c149f2b47cb4f645f63335f6e6b

diff --git a/rkward/rbackend/CMakeLists.txt b/rkward/rbackend/CMakeLists.txt
index 828e8fee..1ef80dbb 100644
--- a/rkward/rbackend/CMakeLists.txt
+++ b/rkward/rbackend/CMakeLists.txt
@@ -54,6 +54,11 @@ IF(APPLE)
     COMMENT "Adding entitlment for rkward.rbackend to link to external R lib..."
 )
 ENDIF()
+IF(MSVC)
+FIND_PACKAGE(Python3 COMPONENTS Interpreter REQUIRED)
+ADD_CUSTOM_TARGET(R_lib COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/GenLibFile.py ${LIBR_SO} ${CMAKE_BINARY_DIR} ${CMAKE_SYSTEM_PROCESSOR})
+ADD_DEPENDENCIES(rkward.rbackend R_lib)
+ENDIF(MSVC)
 
 IF(WIN32)
 	# on Widows, we install to the rbackend subdirectory, because 1) LIBEXEC_INSTALL_DIR == BIN_INSTALL_DIR and 2) we don't want the backend to pick up
diff --git a/rkward/rbackend/GenLibFile.py b/rkward/rbackend/GenLibFile.py
new file mode 100644
index 00000000..5432282a
--- /dev/null
+++ b/rkward/rbackend/GenLibFile.py
@@ -0,0 +1,23 @@
+import subprocess
+import os
+import sys
+
+dllfile = sys.argv[1]
+workdir = sys.argv[2]
+arch = sys.argv[3]
+base = os.path.basename(dllfile).replace(".dll", "")
+deffile = base + ".def"
+libfile = base + ".lib"
+
+dump = subprocess.check_output(["dumpbin", "/exports", dllfile]).decode("latin1").splitlines()
+exports = []
+for line in dump:
+    fields = line.split()
+    if len(fields) != 4:
+        continue
+    exports.append(fields[3])
+os.chdir(workdir)
+with open(os.path.join(workdir, deffile), "wt+") as outdef:
+    outdef.write("EXPORTS\n")
+    outdef.write("\n".join(exports))
+subprocess.call(["lib", f"/def:{deffile}", f"/out:{libfile}", f"/machine:{arch}"])


More information about the rkward-tracker mailing list