Files
sdk/pkg/front_end/tool/trim.dart
T
Sigmund Cherem 44f8e21c82 [kernel] Adds helper to trim dill files for modular dependencies.
Introduce a helper library to trim components based on what we believe
it is needed for modular bytecode compilation. The script is configured
to accept a set of entry points, so unreachable libraries can be removed
entirely. The contents of the retained libraries is trimmed to remove
method bodies, constructor bodies, and initializers, except for where
they may be needed.


In the near future, this should be expanded to:
* include proper unit testing in the CFE
* review whether additional trimming operations can be made
* consider an explicit representation of trimmed content, to help the
  CFE recover when assumptions are not met (e.g. sentinel markers
  to establish whether a value has been trimmed)
* CFE produces trimmed data directly if needed, without having to first
  produce the full dill.

Tests that specifically stress that we don't over-trim include:
apply_mixin (requires preserving method bodies), const_body (requires
preserving initializers).

TEST=existing and new e2e dynamic module aot tests.

b/394936876

Change-Id: I26db8385bdfe1664b2aea234ec8bb896c7c21230
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/418702
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Commit-Queue: Sigmund Cherem <sigmund@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
2025-04-01 08:49:11 -07:00

82 lines
3.1 KiB
Dart

#!/usr/bin/env dart
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:args/args.dart';
import 'package:front_end/src/util/trim.dart';
import 'package:kernel/target/targets.dart';
import 'package:vm/modular/target/flutter.dart';
/// Helper script to invoke [createTrimmedCopy] as a command-line tool given a
/// `dynamic_interface.yaml` input file.
Future<void> main(List<String> args) async {
ArgResults argResults;
try {
argResults = _argParser.parse(args);
} on ArgParserException catch (e) {
print(e.message);
print(_argParser.usage);
exit(1);
}
if (args.length < 2) {
print(_argParser.usage);
exit(1);
}
String? dynamicInterfacePath = argResults['dynamic-interface'] as String?;
File? dynamicInterface =
dynamicInterfacePath != null ? File(dynamicInterfacePath) : null;
await createTrimmedCopy(TrimOptions(
inputAppPath: argResults['input'] as String,
inputPlatformPath: argResults['platform'] as String,
outputAppPath: argResults['output'] as String,
outputPlatformPath: argResults['output-platform'] as String?,
dynamicInterfaceUri: dynamicInterface?.uri,
dynamicInterfaceContents: dynamicInterface?.readAsStringSync(),
requiredUserLibraries:
(argResults['required-user-libraries'] as List<String>).toSet(),
requiredDartLibraries:
FlutterTarget(TargetFlags()).extraRequiredLibraries.toSet(),
librariesToClear:
(argResults['clear-dart-library-body'] as List<String>).toSet()));
}
final ArgParser _argParser = ArgParser()
..addOption('input',
help: 'Input application dill file path', mandatory: true)
..addOption('platform',
help: 'Input platform dill file path', mandatory: true)
..addOption('output',
help: 'Output application dill file path', mandatory: true)
..addOption('output-platform', help: 'Output platform dill file path')
..addOption('dynamic-interface',
help: 'Path to the dynamic_interface.yaml file')
..addMultiOption('clear-dart-library-body',
abbr: 'c',
help: 'List of `dart:` that, even though are required, can be cleared '
'internally since they are only included for compatibility with '
'"extraRequiredLibraries", but are not needed for compilation',
defaultsTo: defaultNonProductionLibraries)
..addMultiOption('required-user-libraries',
abbr: 'u',
help: 'Alternative to providing a dynamic_interface.yaml input. '
'Specifies the list of necessary user written '
'libraries. Can be a full `package:` URI or a prefix pattern, '
'like `package:foo/*`.',
defaultsTo: const []);
/// Libraries that are not needed for production builds and that should
/// be possible to clear when trimming .dill files.
const List<String> defaultNonProductionLibraries = [
'dart:mirrors',
'dart:developer',
'dart:ffi',
'dart:vmservice_io',
'dart:isolate ',
'dart:_vmservice',
'dart:cli',
];