-
Notifications
You must be signed in to change notification settings - Fork 7
/
dll2lib.R
51 lines (38 loc) · 1.52 KB
/
dll2lib.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
args <- commandArgs(TRUE)
# Put the path containing the C compiler on the PATH.
Sys.setenv(PATH = paste(dirname(args[1]), Sys.getenv("PATH"), sep = ";"))
# Find R DLLs.
dlls <- list.files(R.home("bin"), pattern = "dll$", full.names = TRUE)
# Generate corresponding 'lib' file for each DLL.
for (dll in dlls) {
# check to see if we've already generated our exports
def <- sub("dll$", "def", dll)
if (file.exists(def))
next
# Call it on R.dll to generate exports.
command <- sprintf("dumpbin.exe /EXPORTS /NOLOGO %s", dll)
output <- system(paste(command), intern = TRUE)
# Remove synonyms.
output <- sub("=.*$", "", output)
# Find start, end markers
start <- grep("ordinal\\s+hint\\s+RVA\\s+name", output)
end <- grep("^\\s*Summary\\s*$", output)
contents <- output[start:(end - 1)]
contents <- contents[nzchar(contents)]
# Remove forwarded fields
contents <- grep("forwarded to", contents, invert = TRUE, value = TRUE, fixed = TRUE)
# parse into a table
tbl <- read.table(text = contents, header = TRUE, stringsAsFactors = FALSE)
exports <- tbl$name
# sort and re-format exports
exports <- sort(exports)
exports <- c("EXPORTS", paste("\t", tbl$name, sep = ""))
# Write the exports to a def file
def <- sub("dll$", "def", dll)
cat(exports, file = def, sep = "\n")
# Call 'lib.exe' to generate the library file.
outfile <- sub("dll$", "lib", dll)
fmt <- "lib.exe /def:%s /out:%s /machine:%s"
cmd <- sprintf(fmt, def, outfile, .Platform$r_arch)
system(cmd)
}