@@ -5,49 +5,62 @@ import {execute} from './execute.js';
55import { addElemIf , appendPathIfItExists , exists , prependPathIfItExists } from './utils.js' ;
66
77//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`
8+ const kCwd = process . cwd ( ) ;
9+ const kDepotToolsPath = path . join ( kCwd , 'third_party' , 'depot_tools' ) ;
10+ const kDawnPath = `${ kCwd } /third_party/dawn` ;
11+ const kOutDir = 'out/cmake-release' ;
12+ const kBuildPath = `${ kDawnPath } /${ kOutDir } `
1213
1314const isMac = process . platform === 'darwin' ;
1415const isWin = process . platform === 'win32' ;
1516
16- prependPathIfItExists ( depotToolsPath ) ;
17+ prependPathIfItExists ( kDepotToolsPath ) ;
1718appendPathIfItExists ( '/Applications/CMake.app/Contents/bin' ) ;
1819appendPathIfItExists ( 'C:\\Program Files\\CMake\\bin' ) ;
1920
20- async function buildDawnNode ( ) {
21+ async function processThenRestoreCWD ( fn ) {
22+ const cwd = process . cwd ( ) ;
2123 try {
24+ await fn ( ) ;
25+ } finally {
26+ process . chdir ( cwd ) ;
27+ }
28+ }
29+
30+ async function compile ( ) {
31+ await processThenRestoreCWD ( async ( ) => {
32+ process . chdir ( kBuildPath ) ;
33+ if ( isWin ) {
34+ await execute ( 'cmake' , [ '--build' , '.' , '--target' , 'dawn_node' ] )
35+ } else {
36+ await execute ( 'ninja' , [ 'dawn.node' ] ) ;
37+ }
38+ } ) ;
39+ }
40+
41+ async function createProject ( ) {
42+ await processThenRestoreCWD ( async ( ) => {
2243 process . env . DEPOT_TOOLS_WIN_TOOLCHAIN = '0'
23- process . chdir ( 'third_party/dawn' ) ;
44+ process . chdir ( kDawnPath ) ;
2445 fs . copyFileSync ( 'scripts/standalone-with-node.gclient' , '.gclient' ) ;
2546 await execute ( 'gclient' , [ 'metrics' , '--opt-out' ] ) ;
2647 await execute ( 'gclient' , [ 'sync' ] ) ;
27- const outDir = 'out/cmake-release' ;
28- if ( exists ( outDir ) ) {
29- fs . rmSync ( outDir , { recursive : true } ) ;
48+ if ( exists ( kOutDir ) ) {
49+ fs . rmSync ( kOutDir , { recursive : true } ) ;
3050 }
31- fs . mkdirSync ( outDir , { recursive : true } ) ;
32- process . chdir ( outDir ) ;
51+ fs . mkdirSync ( kOutDir , { recursive : true } ) ;
52+ process . chdir ( kOutDir ) ;
3353
3454 await execute ( 'cmake' , [
35- dawnPath ,
55+ kDawnPath ,
3656 ...addElemIf ( ! isWin , '-GNinja' ) ,
3757 '-DDAWN_BUILD_NODE_BINDINGS=1' ,
3858 '-DDAWN_USE_X11=OFF' ,
3959 `-DCMAKE_BUILD_TYPE=${ process . env . CMAKE_BUILD_TYPE ?? 'Release' } ` ,
4060 ...addElemIf ( isWin , '-DCMAKE_SYSTEM_VERSION=10.0.26100.0' ) ,
4161 ...addElemIf ( isMac , '-DCMAKE_OSX_SYSROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk' ) ,
4262 ] ) ;
43- if ( isWin ) {
44- await execute ( 'cmake' , [ '--build' , '.' , '--target' , 'dawn_node' ] )
45- } else {
46- await execute ( 'ninja' , [ 'dawn.node' ] ) ;
47- }
48- } finally {
49- process . chdir ( cwd ) ;
50- }
63+ } ) ;
5164}
5265
5366async function copyResult ( filepath , target ) {
@@ -59,12 +72,15 @@ async function copyResult(filepath, target) {
5972}
6073
6174async function main ( ) {
75+ const compileOnly = process . argv [ 2 ] === '--compile-only' ;
6276 try {
6377 const target = `${ process . platform } -${ process . arch } ` ;
6478 console . log ( 'building for:' , target ) ;
65- await execute ( 'git' , [ 'submodule' , 'update' , '--init' ] ) ;
66- await buildDawnNode ( ) ;
67- const packageName = await copyResult ( buildPath , target ) ;
79+ if ( ! compileOnly ) {
80+ await createProject ( ) ;
81+ }
82+ await compile ( ) ;
83+ const packageName = await copyResult ( kBuildPath , target ) ;
6884 console . log ( 'created:' , packageName ) ;
6985 } catch ( e ) {
7086 console . error ( e ) ;
0 commit comments