|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { describe, it } = require('mocha') |
| 4 | +const assert = require('assert') |
| 5 | +const path = require('path') |
| 6 | +const cp = require('child_process') |
| 7 | +const util = require('../lib/util') |
| 8 | +const { platformTimeout } = require('./common') |
| 9 | + |
| 10 | +const wasmAddonPath = path.resolve(__dirname, 'node_modules', 'hello_wasm') |
| 11 | +const nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js') |
| 12 | + |
| 13 | +const execFileSync = (...args) => cp.execFileSync(...args).toString().trim() |
| 14 | + |
| 15 | +const execFile = async (cmd, env) => { |
| 16 | + const [err,, stderr] = await util.execFile(process.execPath, cmd, { |
| 17 | + env: { |
| 18 | + ...process.env, |
| 19 | + NODE_GYP_NULL_LOGGER: undefined, |
| 20 | + ...env |
| 21 | + }, |
| 22 | + encoding: 'utf-8' |
| 23 | + }) |
| 24 | + return [err, stderr.toString().trim().split(/\r?\n/)] |
| 25 | +} |
| 26 | + |
| 27 | +function runWasm (hostProcess = process.execPath) { |
| 28 | + const testCode = "console.log(require('hello_wasm').hello())" |
| 29 | + return execFileSync(hostProcess, ['--experimental-wasi-unstable-preview1', '-e', testCode], { cwd: __dirname }) |
| 30 | +} |
| 31 | + |
| 32 | +function executable (name) { |
| 33 | + return name + (process.platform === 'win32' ? '.exe' : '') |
| 34 | +} |
| 35 | + |
| 36 | +function getWasmEnv (target) { |
| 37 | + const env = { |
| 38 | + GYP_CROSSCOMPILE: '1', |
| 39 | + AR_host: 'ar', |
| 40 | + CC_host: 'clang', |
| 41 | + CXX_host: 'clang++' |
| 42 | + } |
| 43 | + if (target === 'emscripten') { |
| 44 | + env.AR_target = 'emar' |
| 45 | + env.CC_target = 'emcc' |
| 46 | + env.CXX_target = 'em++' |
| 47 | + } else if (target === 'wasi') { |
| 48 | + env.AR_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('ar')) |
| 49 | + env.CC_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang')) |
| 50 | + env.CXX_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang++')) |
| 51 | + } else if (target === 'wasm') { |
| 52 | + env.AR_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('ar')) |
| 53 | + env.CC_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang')) |
| 54 | + env.CXX_target = path.resolve(__dirname, '..', process.env.WASI_SDK_PATH, 'bin', executable('clang++')) |
| 55 | + env.CFLAGS = '--target=wasm32' |
| 56 | + } |
| 57 | + return env |
| 58 | +} |
| 59 | + |
| 60 | +describe('wasm', function () { |
| 61 | + it('build simple node-api addon to wasm (wasm32-emscripten)', async function () { |
| 62 | + if (process.platform === 'win32') { |
| 63 | + return this.skip('TODO: require https://github.com/nodejs/node-gyp/pull/2974') |
| 64 | + } |
| 65 | + if (!process.env.EMSDK) { |
| 66 | + return this.skip('emsdk not found') |
| 67 | + } |
| 68 | + this.timeout(platformTimeout(1, { win32: 5 })) |
| 69 | + |
| 70 | + const cmd = [ |
| 71 | + nodeGyp, |
| 72 | + 'rebuild', |
| 73 | + '-C', wasmAddonPath, |
| 74 | + '--loglevel=verbose', |
| 75 | + '--arch=wasm32', |
| 76 | + `--nodedir=${path.dirname(require.resolve('emnapi'))}`, |
| 77 | + '--', '-f', 'make' |
| 78 | + ] |
| 79 | + const [err, logLines] = await execFile(cmd, getWasmEnv('emscripten')) |
| 80 | + const lastLine = logLines[logLines.length - 1] |
| 81 | + assert.strictEqual(err, null) |
| 82 | + assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok') |
| 83 | + assert.strictEqual(runWasm(), 'world') |
| 84 | + }) |
| 85 | + |
| 86 | + it('build simple node-api addon to wasm (wasm32-wasip1)', async function () { |
| 87 | + if (process.platform === 'win32') { |
| 88 | + return this.skip('TODO: require https://github.com/nodejs/node-gyp/pull/2974') |
| 89 | + } |
| 90 | + if (!process.env.WASI_SDK_PATH) { |
| 91 | + return this.skip('wasi-sdk not found') |
| 92 | + } |
| 93 | + this.timeout(platformTimeout(1, { win32: 5 })) |
| 94 | + |
| 95 | + const cmd = [ |
| 96 | + nodeGyp, |
| 97 | + 'rebuild', |
| 98 | + '-C', wasmAddonPath, |
| 99 | + '--loglevel=verbose', |
| 100 | + '--arch=wasm32', |
| 101 | + `--nodedir=${path.dirname(require.resolve('emnapi'))}`, |
| 102 | + '--', '-f', 'make' |
| 103 | + ] |
| 104 | + const [err, logLines] = await execFile(cmd, getWasmEnv('wasi')) |
| 105 | + const lastLine = logLines[logLines.length - 1] |
| 106 | + assert.strictEqual(err, null) |
| 107 | + assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok') |
| 108 | + assert.strictEqual(runWasm(), 'world') |
| 109 | + }) |
| 110 | + |
| 111 | + it('build simple node-api addon to wasm (wasm32-unknown-unknown)', async function () { |
| 112 | + if (process.platform === 'win32') { |
| 113 | + return this.skip('TODO: require https://github.com/nodejs/node-gyp/pull/2974') |
| 114 | + } |
| 115 | + if (!process.env.WASI_SDK_PATH) { |
| 116 | + return this.skip('wasi-sdk not found') |
| 117 | + } |
| 118 | + this.timeout(platformTimeout(1, { win32: 5 })) |
| 119 | + |
| 120 | + const cmd = [ |
| 121 | + nodeGyp, |
| 122 | + 'rebuild', |
| 123 | + '-C', wasmAddonPath, |
| 124 | + '--loglevel=verbose', |
| 125 | + '--arch=wasm32', |
| 126 | + `--nodedir=${path.dirname(require.resolve('emnapi'))}`, |
| 127 | + '--', '-f', 'make' |
| 128 | + ] |
| 129 | + const [err, logLines] = await execFile(cmd, getWasmEnv('wasm')) |
| 130 | + const lastLine = logLines[logLines.length - 1] |
| 131 | + assert.strictEqual(err, null) |
| 132 | + assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok') |
| 133 | + assert.strictEqual(runWasm(), 'world') |
| 134 | + }) |
| 135 | +}) |
0 commit comments