Skip to content

Commit

Permalink
distrib: Add new tools distribution for mksnapshot (see #3734)
Browse files Browse the repository at this point in the history
  • Loading branch information
magreenblatt committed Jul 16, 2024
1 parent 8f4a474 commit 5607a4d
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 11 deletions.
33 changes: 26 additions & 7 deletions tools/automate/automate-git.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ def check_pattern_matches(output_file=None):
# Don't continue when we know the build will be wrong.
sys.exit(1)


def invalid_options_combination(a, b):
print("Invalid combination of options: '%s' and '%s'" % (a, b))
parser.print_help(sys.stderr)
Expand Down Expand Up @@ -767,6 +768,18 @@ def invalid_options_combination(a, b):
dest='sandboxdistribonly',
default=False,
help='Create a cef_sandbox static library distribution only.')
parser.add_option(
'--tools-distrib',
action='store_true',
dest='toolsdistrib',
default=False,
help='Create a tools distribution.')
parser.add_option(
'--tools-distrib-only',
action='store_true',
dest='toolsdistribonly',
default=False,
help='Create a tools distribution only.')
parser.add_option(
'--no-distrib-docs',
action='store_true',
Expand Down Expand Up @@ -814,22 +827,22 @@ def invalid_options_combination(a, b):
options.buildtests = True

if (options.nochromiumupdate and options.forceupdate):
invalid_options_combination('--no-chromium-update', '--force-update')
invalid_options_combination('--no-chromium-update', '--force-update')
if (options.nocefupdate and options.forceupdate):
invalid_options_combination('--no-cef-update', '--force-update')
invalid_options_combination('--no-cef-update', '--force-update')
if (options.nobuild and options.forcebuild):
invalid_options_combination('--no-build', '--force-build')
invalid_options_combination('--no-build', '--force-build')
if (options.nodistrib and options.forcedistrib):
invalid_options_combination('--no-distrib', '--force-distrib')
invalid_options_combination('--no-distrib', '--force-distrib')
if (options.forceclean and options.fastupdate):
invalid_options_combination('--force-clean', '--fast-update')
invalid_options_combination('--force-clean', '--fast-update')
if (options.forcecleandeps and options.fastupdate):
invalid_options_combination('--force-clean-deps', '--fast-update')
invalid_options_combination('--force-clean-deps', '--fast-update')

if (options.noreleasebuild and \
(options.minimaldistrib or options.minimaldistribonly or \
options.clientdistrib or options.clientdistribonly)) or \
(options.minimaldistribonly + options.clientdistribonly + options.sandboxdistribonly > 1):
(options.minimaldistribonly + options.clientdistribonly + options.sandboxdistribonly + options.toolsdistribonly > 1):
print('Invalid combination of options.')
parser.print_help(sys.stderr)
sys.exit(1)
Expand Down Expand Up @@ -1450,6 +1463,8 @@ def invalid_options_combination(a, b):
distrib_types.append('client')
elif options.sandboxdistribonly:
distrib_types.append('sandbox')
elif options.toolsdistribonly:
distrib_types.append('tools')
else:
distrib_types.append('standard')
if options.minimaldistrib:
Expand All @@ -1458,6 +1473,8 @@ def invalid_options_combination(a, b):
distrib_types.append('client')
if options.sandboxdistrib:
distrib_types.append('sandbox')
if options.toolsdistrib:
distrib_types.append('tools')

cef_tools_dir = os.path.join(cef_src_dir, 'tools')

Expand All @@ -1482,6 +1499,8 @@ def invalid_options_combination(a, b):
path += ' --client'
elif type == 'sandbox':
path += ' --sandbox'
elif type == 'tools':
path += ' --tools'

if first_type:
if options.nodistribdocs:
Expand Down
58 changes: 58 additions & 0 deletions tools/distrib/README.tools.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
CONTENTS
--------

Debug Contains the Debug build of tools.

Release Contains the Release build of tools.


IMPORTANT NOTE
--------------

CEF/Chromium builds are created using the following host architectures:

- Linux: x86-64 (Intel/AMD)
- Windows: x86-64 (Intel/AMD)
- MacOS: ARM64 (Apple Silicon)

Binaries in this tools package must be run on the supported host OS/architecture
even in cases where the output targets a different architecture.

For example, files targeting a MacOS 64-bit (Intel) application must be created
on a MacOS ARM64 (Apple Silicon) host system using the MacOS 64-bit (Intel)
tools distribution.


USAGE
-----

Start with the required host system and the tools distribution that matches your
application's target OS/architecture and CEF version.

Custom V8 Snapshots

Custom startup snapshots [https://v8.dev/blog/custom-startup-snapshots] can be
used to speed up V8/JavaScript load time in the renderer process. With CEF this
works as follows:

1. Generate a single JavaScript file that contains custom startup data. For
example, using https://github.com/atom/electron-link.

2. Execute the `run_mksnapshot` script to create a `v8_context_snapshot.bin`
file containing the custom data in addition to the default V8 data.

Example:
% run_mksnapshot Release /path/to/snapshot.js

Note that bin file names include an architecture component on MacOS
(e.g. `v8_context_snapshot.[arm64|x86_64].bin`)

3. Replace the existing `v8_context_snapshot.bin` file in the installation
folder or app bundle.

4. Run the application and verify in DevTools that the custom startup data
exists. For example, electron-link adds a global `snapshotResult` object.

Please visit the CEF Website for additional usage information.

https://bitbucket.org/chromiumembedded/cef/
66 changes: 66 additions & 0 deletions tools/distrib/tools/run_mksnapshot.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
@echo off
:: Copyright (c) 2024 The Chromium Embedded Framework Authors. All rights
:: reserved. Use of this source code is governed by a BSD-style license
:: that can be found in the LICENSE file.

set RC=

setlocal

if not "%1" == "Debug" (
if not "%1" == "Release" (
echo Usage: run_mksnapshot.bat [Debug^|Release] path\to\snapshot.js
set ERRORLEVEL=1
goto end
)
)

set SCRIPT_DIR=%~dp0
set BIN_DIR=%SCRIPT_DIR%%~1

if not exist "%BIN_DIR%" (
echo %BIN_DIR% directory not found
set ERRORLEVEL=1
goto end
)

set CMD_FILE=mksnapshot_cmd.txt

if not exist "%BIN_DIR%\%CMD_FILE%" (
echo %BIN_DIR%\%CMD_FILE% file not found
set ERRORLEVEL=1
goto end
)

cd "%BIN_DIR%"

:: Read %CMD_FILE% into a local variable.
set /p CMDLINE=<%CMD_FILE%

:: Generate snapshot_blob.bin.
echo Running mksnapshot...
call mksnapshot %CMDLINE% %2

set OUT_FILE=v8_context_snapshot.bin

:: Generate v8_context_snapshot.bin.
echo Running v8_context_snapshot_generator...
call v8_context_snapshot_generator --output_file=%OUT_FILE%

if not exist "%BIN_DIR%\%OUT_FILE%" (
echo Failed
set ERRORLEVEL=1
goto end
)

echo Success! Created %BIN_DIR%\%OUT_FILE%

:end
endlocal & set RC=%ERRORLEVEL%
goto omega

:returncode
exit /B %RC%

:omega
call :returncode %RC%
68 changes: 68 additions & 0 deletions tools/distrib/tools/run_mksnapshot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash
# Copyright (c) 2024 The Chromium Embedded Framework Authors. All rights
# reserved. Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file.

if [ "$1" != "Debug" ] && [ "$1" != "Release" ]; then
echo 'Usage: run_mksnapshot.sh [Debug|Release] /path/to/snapshot.js'
exit 1
fi

SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )"
BIN_DIR=$SCRIPT_DIR/$1

if [ ! -d "$BIN_DIR" ]; then
echo "$BIN_DIR directory not found."
exit 1
fi

CMD_FILE=mksnapshot_cmd.txt

if [ ! -f "$BIN_DIR/$CMD_FILE" ]; then
echo "$BIN_DIR/$CMD_FILE file not found."
exit 1
fi

pushd "$BIN_DIR" > /dev/null

# Read $CMD_FILE into an argument array.
IFS=' ' read -r -a args < $CMD_FILE

if [ "$(uname)" == "Darwin" ]; then
# Execution of tools binaries downloaded on MacOS may be blocked
# by Apple Security settings with a message like "<binary> can't
# be opened because Apple cannot check it for malicious software."
# Remove that block here.
xattr -c ./mksnapshot
xattr -c ./v8_context_snapshot_generator
fi

# Generate snapshot_blob.bin.
echo 'Running mksnapshot...'
./mksnapshot "${args[@]}" "${@:2}"

# Determine the architecture suffix, if any.
suffix=''
if [ "$(uname)" == "Darwin" ]; then
value='--target_arch=arm64'
if [[ " ${args[*]} " =~ [[:space:]]${value}[[:space:]] ]]; then
suffix='.arm64'
else
suffix='.x86_64'
fi
fi

OUT_FILE=v8_context_snapshot${suffix}.bin

# Generate v8_context_snapshot.bin.
echo 'Running v8_context_snapshot_generator...'
./v8_context_snapshot_generator --output_file=$OUT_FILE

popd > /dev/null

if [ -f "$BIN_DIR/$OUT_FILE" ]; then
echo "Success! Created $BIN_DIR/$OUT_FILE"
else
echo "Failed"
exit 1
fi
Loading

0 comments on commit 5607a4d

Please sign in to comment.