File size: 3,302 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env node

const { execSync } = require( 'child_process' );
const fs = require( 'fs' );
const path = require( 'path' );
const yaml = require( 'js-yaml' );

const PROJECT_DIR = path.join( __dirname, '..' );
const BUILD_DIR = path.join( PROJECT_DIR, 'release' );
const ELECTRON_BUILDER_ARGS = process.env.ELECTRON_BUILDER_ARGS || '';
const isReleaseBuild = process.env.RELEASE_BUILD === 'true';

// Build Apple Silicon binaries only unless tagged for release.
const arches = isReleaseBuild ? [ 'x64', 'arm64' ] : [ 'arm64' ];

for ( let i = 0; i < arches.length; i++ ) {
	const arch = arches[ i ];
	console.log( `  • Building artifacts for arch ${ arch }...` );

	// Need to use python3 - not python2 - for correct compilation for Apple Silicon.
	// (i.e. want arm64, not arm64e.)
	// https://github.com/apple/tensorflow_macos/issues/28#issuecomment-735368891
	//
	// Use `file $(which <path/to/python/executable>)` to verify.
	const pythonExe = process.env.PYTHON || '/Library/Developer/CommandLineTools/usr/bin/python3';

	try {
		// Manually rebuild native modules for the target architecture (don't rely electron-builder's auto rebuild).
		execSync( `PYTHON="${ pythonExe }" npx electron-rebuild --force --arch=${ arch }`, {
			stdio: 'inherit',
		} );

		// Note 1/30/21: There is a bug in electron-builder (v22.10.4) that rebuilds native dependencies
		// with the host architecture instead of the target architecture.
		//
		// Set `npmRebuild` to false to disable electron-builder's automatic native module rebuild
		// so that binaries manually generated by electron-rebuild are used instead.
		execSync(
			`npx electron-builder build ${ ELECTRON_BUILDER_ARGS } -c.npmRebuild=false --${ arch } --publish never`,
			{
				stdio: 'inherit',
			}
		);

		if ( isReleaseBuild ) {
			// Rename latest-mac.yml so it won't be overwritten by another invocation of electron-builder.
			execSync( `mv latest-mac.yml latest-mac-${ arch }.yml`, { cwd: BUILD_DIR } );
		}
	} catch ( e ) {
		console.error( `Build error: `, e );
		process.exit( 1 );
	}

	console.log( `  • OK built ${ arch } artifacts ${ '\n' }` );
}

if ( isReleaseBuild ) {
	try {
		// We need to build the arm64 and x64 application bundles with two separate invocations of
		// electron-builder so that native modules can be generated correctly for each target
		// architecture. The "files" key for latest-mac.yml of each architecture should be
		// combined into a single file prior to publishing.
		//
		// Use `otool -hv -arch all <path/to/native/module>` to verify.
		mergeYaml();
	} catch ( e ) {
		console.error( `Error generating artifact YML: `, e );
		process.exit( 1 );
	}
}

console.log( '  • OK built all artifacts\n\n' );

function mergeYaml() {
	const x64YAML = path.join( BUILD_DIR, 'latest-mac-x64.yml' );
	const arm64YAML = path.join( BUILD_DIR, 'latest-mac-arm64.yml' );

	const x64 = yaml.load( fs.readFileSync( x64YAML ), 'utf8' );
	const arm64 = yaml.load( fs.readFileSync( arm64YAML ), 'utf8' );

	x64.files = x64.files.concat( arm64.files );

	const merged = yaml.dump( x64, { indent: 2, lineWidth: -1 } );
	fs.writeFileSync( path.join( BUILD_DIR, 'latest-mac.yml' ), merged );

	fs.unlinkSync( x64YAML );
	fs.unlinkSync( arm64YAML );

	console.log( '  • OK updated latest-mac.yml: \n', x64 );
}