Skip to content

Commit

Permalink
Generalise gen_snapshots.py to strip_bitcode.py (flutter#36496)
Browse files Browse the repository at this point in the history
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: flutter/flutter#103386
Issue: flutter/flutter#101138
Issue: flutter/flutter#107884
  • Loading branch information
cbracken authored Sep 29, 2022
1 parent 493ccb8 commit 88d523c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 99 deletions.
21 changes: 16 additions & 5 deletions lib/snapshot/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)",
Expand Down Expand Up @@ -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}" ]
}
}
}

Expand Down
46 changes: 0 additions & 46 deletions sky/tools/gen_snapshots.py

This file was deleted.

41 changes: 41 additions & 0 deletions sky/tools/macos.gni
Original file line number Diff line number Diff line change
@@ -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 ]
}
}
48 changes: 0 additions & 48 deletions sky/tools/macos_snapshots.gni

This file was deleted.

44 changes: 44 additions & 0 deletions sky/tools/strip_bitcode.py
Original file line number Diff line number Diff line change
@@ -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())

0 comments on commit 88d523c

Please sign in to comment.