|
| 1 | +import path from 'path'; |
| 2 | +import fs from 'fs'; |
| 3 | + |
| 4 | +import {execute} from './execute.js'; |
| 5 | +import {addElemIf, appendPathIfItExists, prependPathIfItExists} from './utils.js'; |
| 6 | + |
| 7 | +//const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 8 | +const cwd = process.cwd(); |
| 9 | +const depotToolsPath = path.join(cwd, 'third_party', 'depot_tools'); |
| 10 | +const dawnPath = `${cwd}/third_party/dawn`; |
| 11 | +const buildPath = `${dawnPath}/out/cmake-release` |
| 12 | + |
| 13 | +const isMac = process.platform === 'darwin'; |
| 14 | +const isWin = process.platform === 'win32'; |
| 15 | + |
| 16 | +prependPathIfItExists(depotToolsPath); |
| 17 | +appendPathIfItExists('/Applications/CMake.app/Contents/bin'); |
| 18 | +appendPathIfItExists('C:\\Program Files\\CMake\\bin'); |
| 19 | + |
| 20 | +function fixupPackageJson(filename) { |
| 21 | + const pkg = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf8'})); |
| 22 | + const vsPkg = JSON.parse(fs.readFileSync(filename, {encoding: 'utf8'})); |
| 23 | + const newPkg = { |
| 24 | + ...pkg, |
| 25 | + ...vsPkg, |
| 26 | + type: "commonjs", |
| 27 | + scripts: {}, |
| 28 | + version: pkg.version, |
| 29 | + }; |
| 30 | + fs.writeFileSync(filename, JSON.stringify(newPkg, null, 2)); |
| 31 | +} |
| 32 | + |
| 33 | +async function buildDawnNode() { |
| 34 | + try { |
| 35 | + process.env.DEPOT_TOOLS_WIN_TOOLCHAIN = '0' |
| 36 | + process.chdir('third_party/dawn'); |
| 37 | + fs.copyFileSync('scripts/standalone-with-node.gclient', '.gclient'); |
| 38 | + await execute('gclient', ['metrics', '--opt-out']); |
| 39 | + await execute('gclient', ['sync']); |
| 40 | + fs.mkdirSync('out/cmake-release', {recursive: true}); |
| 41 | + process.chdir('out/cmake-release'); |
| 42 | + |
| 43 | + await execute('cmake', [ |
| 44 | + dawnPath, |
| 45 | + ...addElemIf(!isWin, '-GNinja'), |
| 46 | + '-DDAWN_BUILD_NODE_BINDINGS=1', |
| 47 | + ...addElemIf(isMac, '-DCMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'), |
| 48 | + ]); |
| 49 | + if (isWin) { |
| 50 | + await execute('cmake', ['--build', '.', '-target', 'dawn.node']) |
| 51 | + } else { |
| 52 | + await execute('ninja', ['dawn.node']); |
| 53 | + } |
| 54 | + } finally { |
| 55 | + process.chdir(cwd); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +async function packageExtension(target) { |
| 60 | + try { |
| 61 | + process.chdir(buildPath); |
| 62 | + await execute('npm', ['install']); |
| 63 | + await execute(`${cwd}/node_modules/.bin/vsce`, [ |
| 64 | + 'package', |
| 65 | + '--allow-star-activation', |
| 66 | + '--target', target, |
| 67 | + ]); |
| 68 | + } finally { |
| 69 | + process.chdir(cwd); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +async function copyResult(filepath, target) { |
| 74 | + const srcFilename = path.join(filepath, 'dawn.node'); |
| 75 | + const dstFilename = path.join('dist', `${target}.dawn.node`); |
| 76 | + fs.mkdirSync(path.dirname(dstFilename), {recursive: true}); |
| 77 | + fs.copyFileSync(srcFilename, dstFilename); |
| 78 | + return dstFilename; |
| 79 | +} |
| 80 | + |
| 81 | +async function main() { |
| 82 | + try { |
| 83 | + const target = `${process.platform}-${process.arch}`; |
| 84 | + console.log('building for:', target); |
| 85 | + await execute('git', ['submodule', 'update', '--init']); |
| 86 | + await buildDawnNode(); |
| 87 | + //fixupPackageJson(`${buildPath}/package.json`); |
| 88 | + //fs.copyFileSync('third_party/dawn/LICENSE', `${buildPath}/LICENSE`); |
| 89 | + //await packageExtension(target); |
| 90 | + const packageName = await copyResult(buildPath, target); |
| 91 | + console.log('created:', packageName); |
| 92 | + } catch (e) { |
| 93 | + console.error(e); |
| 94 | + console.error(e.stack); |
| 95 | + process.exit(1); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +main(); |
0 commit comments