Generalise gen_snapshots.py to strip_bitcode.py (flutter/engine#36496)

The gen_snapshots.py tool is used to copy an input gen_snapshot to an
output path with an architecture-specific suffix. For example, to copy
gen_snapshot to gen_snapshot_arm64. Bitcode segments, if any, are
stripped.

This moves the input/output filename hardcoding into the BUILD.gn file
and generalises the logic to simply copy an input binary to an output
path with bitcode segments stripped. Since the tool is no longer
gen_snapshot specific, we rename it from gen_snapshots.py to
strip_bitcode.py.

This also renames the generalised `macos_gen_snapshots` rule to
`strip_bitcode`.

Since we're working on removing bitcode support from the engine, this
script will eventually serve no purpose other than to copy the input
binary to an output path, at which point this script, and the associated
`strip_bitcode` template in `//flutter/sky/tools/macos_tools.gni` can be
removed.

Along with the TODO, renaming the script and the rule help ensure we'll
spot this and remove it when bitcode support is removed from the engine.

Finally, this fixes a dependency issue in the target
//flutter/lib/snapshot:create_macos_gen_snapshots. Previously, it
dependended on ":generate_snapshot_bin", but in fact, the only file it
touches is gen_snapshot. This was built transitively as part of the
":generate_snapshot_bin" target, but is now depended on directly.

This is pre-factoring for merging the iOS and macOS gen_snapshot
creation build rules in `flutter/lib/snapshot/BUILD.gn`.

Issue: https://github.com/flutter/flutter/issues/103386
Issue: https://github.com/flutter/flutter/issues/101138
Issue: https://github.com/flutter/flutter/issues/107884
This commit is contained in:
Chris Bracken
2022-09-29 18:48:56 +01:00
committed by GitHub
parent 6d76d00c33
commit d6d0909b65
5 changed files with 101 additions and 99 deletions

View File

@@ -6,7 +6,7 @@ import("//build/compiled_action.gni")
import("//build/fuchsia/sdk.gni")
import("//flutter/common/config.gni")
import("//flutter/lib/ui/dart_ui.gni")
import("//flutter/sky/tools/macos_snapshots.gni")
import("//flutter/sky/tools/macos.gni")
import("//third_party/dart/utils/compile_platform.gni")
# Generates the Dart/Flutter core platform files and tools.
@@ -248,7 +248,8 @@ bin_to_linkable("platform_strong_dill_linkable") {
# Builds gen_snapshot using the host toolchain then copies the resulting binary
# to `gen_snapshot_armv7` or `gen_snapshot_arm64` depending on the target
# platform.
if (host_os == "mac" && (target_cpu == "arm" || target_cpu == "arm64")) {
if (host_os == "mac" && target_os != "mac" &&
(target_cpu == "arm" || target_cpu == "arm64")) {
action("create_arm_gen_snapshot") {
host_output_dir = get_label_info(
"//third_party/dart/runtime/bin:gen_snapshot($host_toolchain)",
@@ -280,9 +281,19 @@ if (host_os == "mac" && (target_cpu == "arm" || target_cpu == "arm64")) {
}
if (host_os == "mac" && target_os == "mac") {
macos_gen_snapshots("create_macos_gen_snapshots") {
deps = [ ":generate_snapshot_bin" ]
arch = "$target_cpu"
strip_bitcode("create_macos_gen_snapshots") {
# The toolchain-specific output directory. For cross-compiles, this is a
# clang-x64 or clang-arm64 subdirectory of the top-level build directory.
host_output_dir = get_label_info(
"//third_party/dart/runtime/bin:gen_snapshot($host_toolchain)",
"root_out_dir")
input = "${host_output_dir}/gen_snapshot"
output = "${root_out_dir}/gen_snapshot_${target_cpu}"
deps = [ "//third_party/dart/runtime/bin:gen_snapshot($host_toolchain)" ]
metadata = {
snapshot_entitlement_file_path = [ "gen_snapshot_{$target_cpu}" ]
}
}
}

View File

@@ -1,46 +0,0 @@
#!/usr/bin/env python3
#
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import subprocess
import sys
import os
def main():
parser = argparse.ArgumentParser(
description='Copies architecture-dependent gen_snapshot binaries to output dir'
)
parser.add_argument('--dst', type=str, required=True)
parser.add_argument('--out-dir', type=str)
parser.add_argument('--arch', type=str)
args = parser.parse_args()
subdir = ''
if args.arch != 'x64':
subdir = 'clang_x64'
generate_gen_snapshot(
os.path.join(args.out_dir, subdir),
os.path.join(args.dst, 'gen_snapshot_%s' % args.arch)
)
def generate_gen_snapshot(directory, destination):
gen_snapshot_dir = os.path.join(directory, 'gen_snapshot')
if not os.path.isfile(gen_snapshot_dir):
print('Cannot find gen_snapshot at %s' % gen_snapshot_dir)
sys.exit(1)
subprocess.check_call([
'xcrun', 'bitcode_strip', '-r', gen_snapshot_dir, '-o', destination
])
if __name__ == '__main__':
sys.exit(main())

View File

@@ -0,0 +1,41 @@
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//flutter/common/config.gni")
# Copies an input macOS binary to the specified output path. Bitcode segments,
# if any, are stripped.
#
# Example:
#
# strip_bitcode(
# input = "$root_build_dir/gen_snapshot"
# output = "$root_build_dir/gen_snapshot_arm64"
# deps = [ ":gen_snapshot" ]
# )
#
# TODO(cbracken): https://github.com/flutter/flutter/issues/107884
# When we stop building with bitcode enabled, this template and the
# strip_bitcode.py script can be deleted and users can call copy() instead.
template("strip_bitcode") {
assert(defined(invoker.input), "The input to strip_bitcode must be defined")
assert(defined(invoker.output), "The output to strip_bitcode most be defined")
action(target_name) {
forward_variables_from(invoker,
[
"deps",
"metadata",
"visibility",
])
script = "//flutter/sky/tools/strip_bitcode.py"
args = [
"--input",
rebase_path(invoker.input),
"--output",
rebase_path(invoker.output),
]
inputs = [ invoker.input ]
outputs = [ invoker.output ]
}
}

View File

@@ -1,48 +0,0 @@
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//flutter/common/config.gni")
# Generates macos gen snapshots $root_build_dir/zip_archives folder.
#
# The output variable specifies the name of the zip file to create.
# The files variable is an array of scopes that specify a source file or
# directory and a destination path in the archive to create.
#
# For example, to create a zip file named archive.zip with all files in the
# root directory of the archive:
#
# zip_bundle("sample") {
# output = "archive.zip"
# files = [
# {
# source = "$root_build_dir/some/path/to/lib.so"
# destination = "lib.so"
# },
# {
# source = "$root_build_dir/some/other/path/with/files"
# destination = "other_files"
# },
# ]
# }
template("macos_gen_snapshots") {
action(target_name) {
forward_variables_from(invoker, [ "visibility" ])
deps = invoker.deps
outputs = [ "$root_build_dir/gen_snapshot_${invoker.arch}" ]
script = "//flutter/sky/tools/gen_snapshots.py"
args = [
"--dst",
rebase_path("$root_out_dir/"),
"--out-dir",
rebase_path("$root_out_dir"),
"--arch",
invoker.arch,
]
metadata = {
snapshot_entitlement_file_path = [ "gen_snapshot_${invoker.arch}" ]
}
}
}

View File

@@ -0,0 +1,44 @@
#!/usr/bin/env python3
#
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import os
import subprocess
import sys
def main():
parser = argparse.ArgumentParser(
description='Copies an input macOS binary to the specified output path. '
'Bitcode segments, if any, are stripped.'
)
parser.add_argument(
'--input',
type=str,
required=True,
help='path of input binary to be read'
)
parser.add_argument(
'--output',
type=str,
required=True,
help='path of output binary to be written'
)
args = parser.parse_args()
# Verify input binary exists.
if not os.path.isfile(args.input):
print('Cannot find input binary at %s' % args.input)
sys.exit(1)
# Copy input path to output path. Strip bitcode segments, if any.
subprocess.check_call([
'xcrun', 'bitcode_strip', '-r', args.input, '-o', args.output
])
if __name__ == '__main__':
sys.exit(main())