-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathtest-windows-make.js
108 lines (97 loc) · 3.63 KB
/
test-windows-make.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
const { describe, it } = require('mocha')
const assert = require('assert')
const path = require('path')
const gracefulFs = require('graceful-fs')
const cp = require('child_process')
const util = require('../lib/util')
const { platformTimeout } = require('./common')
const addonPath = path.resolve(__dirname, 'node_modules', 'hello_napi')
const nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js')
const execFileSync = (...args) => cp.execFileSync(...args).toString().trim()
const execFile = async (cmd, env) => {
const [err,, stderr] = await util.execFile(process.execPath, cmd, {
env: {
...process.env,
NODE_GYP_NULL_LOGGER: undefined,
...env
},
encoding: 'utf-8'
})
return [err, stderr.toString().trim().split(/\r?\n/)]
}
function runHello (hostProcess = process.execPath) {
const testCode = "console.log(require('hello_napi').hello())"
return execFileSync(hostProcess, ['--experimental-wasi-unstable-preview1', '-e', testCode], { cwd: __dirname })
}
function executable (name) {
return name + (process.platform === 'win32' ? '.exe' : '')
}
function getEnv (target) {
const env = {
GYP_CROSSCOMPILE: '1',
AR_host: 'ar',
CC_host: 'clang',
CXX_host: 'clang++'
}
if (target === 'emscripten') {
env.AR_target = 'emar'
env.CC_target = 'emcc'
env.CXX_target = 'em++'
} else if (target === 'wasi') {
env.AR_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('ar'))
env.CC_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang'))
env.CXX_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang++'))
} else if (target === 'wasm') {
env.AR_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('ar'))
env.CC_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang'))
env.CXX_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang++'))
env.CFLAGS = '--target=wasm32'
} else if (target === 'win-clang') {
let vsdir = 'C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise'
if (!gracefulFs.existsSync(vsdir)) {
vsdir = 'C:\\Program Files\\Microsoft Visual Studio\\2022\\Community'
}
const llvmBin = 'VC\\Tools\\Llvm\\x64\\bin'
env.AR_target = path.join(vsdir, llvmBin, 'llvm-ar.exe')
env.CC_target = path.join(vsdir, llvmBin, 'clang.exe')
env.CXX_target = path.join(vsdir, llvmBin, 'clang++.exe')
env.CFLAGS = '--target=wasm32'
}
return env
}
function quote (path) {
if (path.includes(' ')) {
return `"${path}"`
}
}
describe('windows-cross-compile', function () {
it('build simple node-api addon', async function () {
if (process.platform !== 'win32') {
return this.skip('This test is only for windows')
}
const env = getEnv('win-clang')
if (!gracefulFs.existsSync(env.CC_target)) {
return this.skip('Visual Studio Clang is not installed')
}
// handle bash whitespace
env.AR_target = quote(env.AR_target)
env.CC_target = quote(env.CC_target)
env.CXX_target = quote(env.CXX_target)
this.timeout(platformTimeout(1, { win32: 5 }))
const cmd = [
nodeGyp,
'rebuild',
'-C', addonPath,
'--loglevel=verbose',
`--nodedir=${addonPath}`,
'--arch=wasm32',
'--', '-f', 'make'
]
const [err, logLines] = await execFile(cmd, env)
const lastLine = logLines[logLines.length - 1]
assert.strictEqual(err, null)
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
assert.strictEqual(runHello(), 'world')
})
})