Skip to content

Commit ec6fe27

Browse files
author
John Kleinschmidt
authored
build: add v8_context_generator to mksnapshot zip (3-0-x) (electron#15505)
* build: add v8_context_generator to mksnapshot Starting with 3-0-x, in order to use custom snapshots the v8_context_snapshot_generator binary is also needed. Also, add tests for mksnapshot. * Actually run verify-mksnapshot
1 parent 8566f87 commit ec6fe27

File tree

5 files changed

+188
-24
lines changed

5 files changed

+188
-24
lines changed

.circleci/config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ build-steps: &build-steps
7070
else
7171
echo 'Headless testing not needed'
7272
fi
73+
74+
- run:
75+
name: Verify mksnapshot
76+
command: |
77+
if [ "$RUN_TESTS" == "true" ] && [ "$ELECTRON_RELEASE" == "1" ]; then
78+
echo 'Verifying mksnapshot on release build'
79+
script/verify-mksnapshot.py
80+
else
81+
echo 'Skipping mksnapshot tests due to configuration'
82+
fi
83+
7384
- run:
7485
name: Test
7586
environment:

script/create-dist.py

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@
7979
],
8080
}
8181

82+
V8_SNAPSHOT_BINARIES = {
83+
'darwin': [
84+
'libffmpeg.dylib',
85+
'libicui18n.dylib',
86+
'libicuuc.dylib',
87+
'libv8.dylib',
88+
'v8_context_snapshot_generator',
89+
],
90+
'linux': [
91+
'libffmpeg.so',
92+
'libicui18n.so',
93+
'libicuuc.so',
94+
'libv8.so',
95+
'v8_context_snapshot_generator',
96+
],
97+
'win32': [
98+
'ffmpeg.dll',
99+
'icui18n.dll',
100+
'icuuc.dll',
101+
'v8.dll',
102+
'v8_context_snapshot_generator.exe',
103+
],
104+
}
82105

83106
def main():
84107
args = parse_args()
@@ -95,8 +118,8 @@ def main():
95118
force_build()
96119
create_symbols()
97120
copy_binaries()
98-
copy_chrome_binary('chromedriver')
99-
copy_chrome_binary('mksnapshot')
121+
copy_chrome_binary('chromedriver', CHROMIUM_DIR, DIST_DIR)
122+
copy_chrome_binary('mksnapshot', CHROMIUM_DIR, DIST_DIR)
100123
copy_license()
101124
if PLATFORM == 'win32':
102125
copy_vcruntime_binaries()
@@ -136,15 +159,21 @@ def copy_binaries():
136159
symlinks=True)
137160

138161

139-
def copy_chrome_binary(binary):
162+
def copy_chrome_binary(binary, src_dir, dest_dir, is_native_mksnapshot = False):
140163
if PLATFORM == 'win32':
141164
binary += '.exe'
142-
src = os.path.join(CHROMIUM_DIR, binary)
143-
dest = os.path.join(DIST_DIR, binary)
165+
src = os.path.join(src_dir, binary)
166+
dest = os.path.join(dest_dir, binary)
144167

145168
# Copy file and keep the executable bit.
146169
shutil.copyfile(src, dest)
147170
os.chmod(dest, os.stat(dest).st_mode | stat.S_IEXEC)
171+
if binary.startswith('mksnapshot') and not is_native_mksnapshot:
172+
snapshot_gen_path = os.path.join(src_dir, 'snapshot_gen', '*')
173+
snapshot_gen_files = glob.glob(snapshot_gen_path)
174+
snapshot_gen_files += [ os.path.join(src_dir, get_ffmpeg_name()) ]
175+
for gen_file in snapshot_gen_files:
176+
shutil.copy2(gen_file, dest_dir)
148177

149178
def copy_vcruntime_binaries():
150179
arch = get_target_arch()
@@ -272,9 +301,16 @@ def create_dist_zip():
272301

273302

274303
def create_chrome_binary_zip(binary, version):
304+
files = ['LICENSE', 'LICENSES.chromium.html']
305+
if PLATFORM == 'win32':
306+
files += [binary + '.exe']
307+
else:
308+
files += [binary]
309+
275310
file_suffix = ''
276311
create_native_mksnapshot = False
277312
if binary == 'mksnapshot':
313+
files += V8_SNAPSHOT_BINARIES[PLATFORM]
278314
arch = get_target_arch()
279315
if arch.startswith('arm'):
280316
# if the arch is arm/arm64 the mksnapshot executable is an x64 binary,
@@ -284,23 +320,14 @@ def create_chrome_binary_zip(binary, version):
284320
dist_name = get_zip_name(binary, version, file_suffix)
285321
zip_file = os.path.join(SOURCE_ROOT, 'dist', dist_name)
286322

287-
files = ['LICENSE', 'LICENSES.chromium.html']
288-
if PLATFORM == 'win32':
289-
files += [binary + '.exe']
290-
else:
291-
files += [binary]
292-
293323
with scoped_cwd(DIST_DIR):
294324
make_zip(zip_file, files, [])
295325

296326
if create_native_mksnapshot == True:
297327
# Create a zip with the native version of the mksnapshot binary.
298-
src = os.path.join(NATIVE_MKSNAPSHOT_DIR, binary)
299-
dest = os.path.join(DIST_DIR, binary)
300-
# Copy file and keep the executable bit.
301-
shutil.copyfile(src, dest)
302-
os.chmod(dest, os.stat(dest).st_mode | stat.S_IEXEC)
303-
328+
copy_chrome_binary('mksnapshot', NATIVE_MKSNAPSHOT_DIR, DIST_DIR, True)
329+
copy_chrome_binary('v8_context_snapshot_generator', NATIVE_MKSNAPSHOT_DIR, \
330+
DIST_DIR)
304331
dist_name = get_zip_name(binary, version)
305332
zip_file = os.path.join(SOURCE_ROOT, 'dist', dist_name)
306333
with scoped_cwd(DIST_DIR):
@@ -310,13 +337,7 @@ def create_ffmpeg_zip():
310337
dist_name = get_zip_name('ffmpeg', ELECTRON_VERSION)
311338
zip_file = os.path.join(SOURCE_ROOT, 'dist', dist_name)
312339

313-
if PLATFORM == 'darwin':
314-
ffmpeg_name = 'libffmpeg.dylib'
315-
elif PLATFORM == 'linux':
316-
ffmpeg_name = 'libffmpeg.so'
317-
elif PLATFORM == 'win32':
318-
ffmpeg_name = 'ffmpeg.dll'
319-
340+
ffmpeg_name = get_ffmpeg_name()
320341
shutil.copy2(os.path.join(CHROMIUM_DIR, '..', 'ffmpeg', ffmpeg_name),
321342
DIST_DIR)
322343

@@ -326,6 +347,14 @@ def create_ffmpeg_zip():
326347
with scoped_cwd(DIST_DIR):
327348
make_zip(zip_file, [ffmpeg_name, 'LICENSE', 'LICENSES.chromium.html'], [])
328349

350+
def get_ffmpeg_name():
351+
if PLATFORM == 'darwin':
352+
ffmpeg_name = 'libffmpeg.dylib'
353+
elif PLATFORM == 'linux':
354+
ffmpeg_name = 'libffmpeg.so'
355+
elif PLATFORM == 'win32':
356+
ffmpeg_name = 'ffmpeg.dll'
357+
return ffmpeg_name
329358

330359
def create_symbols_zip():
331360
if get_target_arch() == 'mips64el':

script/verify-mksnapshot.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python
2+
3+
import glob
4+
import os
5+
import shutil
6+
import subprocess
7+
import sys
8+
9+
from lib.util import electron_gyp, rm_rf, scoped_cwd
10+
11+
12+
SOURCE_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
13+
SNAPSHOT_DIR = os.path.join(SOURCE_ROOT, 'vendor', 'download',
14+
'libchromiumcontent', 'static_library')
15+
PROJECT_NAME = electron_gyp()['project_name%']
16+
PRODUCT_NAME = electron_gyp()['product_name%']
17+
SNAPSHOT_SOURCE = os.path.join(SOURCE_ROOT, 'spec', 'fixtures', 'testsnap.js')
18+
19+
def main():
20+
os.chdir(SOURCE_ROOT)
21+
app_path = create_app_copy()
22+
blob_out_path = app_path
23+
snapshot_gen_path = os.path.join(SNAPSHOT_DIR, 'snapshot_gen', '*')
24+
snapshot_gen_files = glob.glob(snapshot_gen_path)
25+
if sys.platform == 'darwin':
26+
electron = os.path.join(app_path, 'Contents', 'MacOS', PRODUCT_NAME)
27+
blob_out_path = os.path.join(app_path, 'Contents', 'Frameworks',
28+
'{0} Framework.framework'.format(PROJECT_NAME),
29+
'Resources')
30+
snapshot_gen_files += [ os.path.join(SNAPSHOT_DIR, 'libffmpeg.dylib') ]
31+
elif sys.platform == 'win32':
32+
electron = os.path.join(app_path, '{0}.exe'.format(PROJECT_NAME))
33+
snapshot_gen_files += [
34+
os.path.join(SNAPSHOT_DIR, 'ffmpeg.dll'),
35+
os.path.join(SNAPSHOT_DIR, 'ffmpeg.dll.lib'),
36+
]
37+
else:
38+
electron = os.path.join(app_path, PROJECT_NAME)
39+
snapshot_gen_files += [ os.path.join(SNAPSHOT_DIR, 'libffmpeg.so') ]
40+
41+
# Copy mksnapshot and friends to the directory the snapshot_blob should be
42+
# generated in.
43+
mksnapshot_binary = get_binary_path('mksnapshot', SNAPSHOT_DIR)
44+
shutil.copy2(mksnapshot_binary, blob_out_path)
45+
for gen_file in snapshot_gen_files:
46+
shutil.copy2(gen_file, blob_out_path)
47+
48+
returncode = 0
49+
try:
50+
with scoped_cwd(blob_out_path):
51+
mkargs = [ get_binary_path('mksnapshot', blob_out_path), \
52+
SNAPSHOT_SOURCE, '--startup_blob', 'snapshot_blob.bin' ]
53+
subprocess.check_call(mkargs)
54+
print 'ok mksnapshot successfully created snapshot_blob.bin.'
55+
context_snapshot = 'v8_context_snapshot.bin'
56+
context_snapshot_path = os.path.join(blob_out_path, context_snapshot)
57+
gen_binary = get_binary_path('v8_context_snapshot_generator', \
58+
blob_out_path)
59+
genargs = [ gen_binary, \
60+
'--output_file={0}'.format(context_snapshot_path) ]
61+
subprocess.check_call(genargs)
62+
print 'ok v8_context_snapshot_generator successfully created ' \
63+
+ context_snapshot
64+
65+
test_path = os.path.join('spec', 'fixtures', 'snapshot-items-available.js')
66+
subprocess.check_call([electron, test_path])
67+
print 'ok successfully used custom snapshot.'
68+
except subprocess.CalledProcessError as e:
69+
print 'not ok an error was encountered while testing mksnapshot.'
70+
print e
71+
returncode = e.returncode
72+
except KeyboardInterrupt:
73+
print 'Other error'
74+
returncode = 0
75+
76+
return returncode
77+
78+
79+
# Create copy of app to create custom snapshot
80+
def create_app_copy():
81+
initial_app_path = os.path.join(SOURCE_ROOT, 'out', 'R')
82+
app_path = os.path.join(SOURCE_ROOT, 'out', 'R-mksnapshot-test')
83+
84+
if sys.platform == 'darwin':
85+
app_name = '{0}.app'.format(PRODUCT_NAME)
86+
initial_app_path = os.path.join(initial_app_path, app_name)
87+
app_path = os.path.join(app_path, app_name)
88+
89+
rm_rf(app_path)
90+
shutil.copytree(initial_app_path, app_path, symlinks=True)
91+
return app_path
92+
93+
def get_binary_path(binary_name, root_path):
94+
if sys.platform == 'win32':
95+
binary_path = os.path.join(root_path, '{0}.exe'.format(binary_name))
96+
else:
97+
binary_path = os.path.join(root_path, binary_name)
98+
return binary_path
99+
100+
if __name__ == '__main__':
101+
sys.exit(main())
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Verifies that objects contained in custom snapshot are accessible in Electron.
2+
3+
const {app} = require('electron')
4+
5+
app.once('ready', () => {
6+
try {
7+
const testValue = f() // eslint-disable-line no-undef
8+
if (testValue === 86) {
9+
console.log('ok test snapshot successfully loaded.')
10+
app.exit(0)
11+
} else {
12+
console.log('not ok test snapshot could not be successfully loaded.')
13+
app.exit(1)
14+
}
15+
return
16+
} catch (ex) {
17+
console.log('Error running custom snapshot', ex)
18+
app.exit(1)
19+
}
20+
})

spec/fixtures/testsnap.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// taken from https://chromium.googlesource.com/v8/v8.git/+/HEAD/test/cctest/test-serialize.cc#1127
2+
function f () { return g() * 2 } // eslint-disable-line no-unused-vars
3+
function g () { return 43 }

0 commit comments

Comments
 (0)