-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
70 lines (54 loc) · 1.71 KB
/
index.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
var spawn = require('child_process').spawnSync
var path = require('path')
var fs = require('fs')
var argv = require('minimist')(process.argv, {
string: ['arch'],
default: {
'arch': 'X64'
}
})
var src = argv._[2]
var dest = argv._[3]
var arch = argv.arch.toUpperCase()
if (arch == 'IA32') arch = 'X86'
if (src == null) {
console.error('Missing SRC argument')
process.exit(1)
}
if (dest == null) {
console.error('Missing DEST argument')
process.exit(1)
}
if (['X86', 'X64', 'ARM', 'ARM64'].indexOf(arch) < 0) {
console.error('Invalid architecture')
process.exit(1)
}
var prefixSrc = path.isAbsolute(src) ? '' : process.cwd()
var prefixDest = path.isAbsolute(dest) ? '' : process.cwd()
var definitions = spawn('dumpbin', ['/EXPORTS', path.join(prefixSrc, src)], {stdio: ['ignore', 'pipe', 'inherit']})
if (definitions.error) {
console.error(definitions.error)
process.exit(2)
}
var functionRegex = /^\s*(\d+)\s+[A-Z0-9]+\s+[A-Z0-9]{8}\s+([^ ]+(?: = [^ ]+)?)\s*$/i
var exports = definitions.stdout.toString()
.split('\n')
.filter(l => functionRegex.test(l))
.map(s => s.match(functionRegex))
.map(m => ({ordinal: m[1], fn: m[2]}))
var defContent = `
LIBRARY "${path.basename(dest)}"
EXPORTS
${exports.map(e => e.fn.replace(/ += +/g, '=') + ' @' + e.ordinal).join('\n')}
`
var defBasename = path.basename(dest, '.dll') + '.def'
var defDirname = path.join(prefixDest, path.dirname(dest))
var defPath = path.join(defDirname, defBasename)
fs.writeFileSync(defPath, defContent)
var lib = spawn('lib', ['/MACHINE:' + arch, '/DEF:' + defBasename], {cwd: defDirname}, {stdio: ['ignore', 'pipe', 'inherit']})
if (lib.error) {
console.error(lib.error)
process.exit(2)
}
fs.renameSync(src, dest)