Skip to content

Commit e0ff789

Browse files
add gn flag to optimize builds for size (#176835)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> Adds a `gn` flag to optimize builds for size over speed when optimization is requested. The existing behaviour is that Windows, Android and iOS builds are always optimized for size. This has not changed. The flag allows other builds to prefer size over speed. Closes #176685 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent eb713a5 commit e0ff789

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

engine/src/build/config/BUILDCONFIG.gn

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ declare_args() {
132132
# Debug build.
133133
is_debug = true
134134

135+
# Optimize for code size rather than speed.
136+
# Optimized builds for Windows, iOS, and Android are
137+
# always optimized for size regardless of the value of this flag.
138+
optimize_for_size = false
139+
135140
# Whether this is a minimal linux instance without x11, etc.
136141
is_minimal_linux = false
137142

engine/src/build/config/compiler/BUILD.gn

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,11 @@ config("optimize") {
10151015
cflags = [ "-O3" ]
10161016
}
10171017
} else {
1018-
cflags = [ "-O2" ] + common_optimize_on_cflags
1018+
if (optimize_for_size) {
1019+
cflags = [ "-Os" ] + common_optimize_on_cflags
1020+
} else {
1021+
cflags = [ "-O2" ] + common_optimize_on_cflags
1022+
}
10191023
}
10201024

10211025
lto_flags = []

engine/src/flutter/tools/gn

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,13 @@ def to_gn_args(args):
439439

440440
gn_args['buildtools_path'] = '//flutter/buildtools'
441441

442+
if args.unoptimized and args.optimize_for_size:
443+
raise Exception('--unoptimized and --optimize-for-size are mutually exclusive')
444+
442445
gn_args['is_debug'] = args.unoptimized
443446

447+
gn_args['optimize_for_size'] = args.optimize_for_size
448+
444449
gn_args.update(setup_git_versions())
445450

446451
gn_args.update(setup_rbe(args))
@@ -843,6 +848,7 @@ def to_gn_wasm_args(args, gn_args):
843848
gn_args['target_os'] = 'wasm'
844849
gn_args['target_cpu'] = 'wasm'
845850
gn_args['wasm_use_dwarf'] = args.wasm_use_dwarf
851+
gn_args['wasm_prioritize_size'] = args.optimize_for_size
846852
gn_args['skia_use_angle'] = False
847853
gn_args['skia_use_dng_sdk'] = False
848854
gn_args['skia_use_expat'] = False
@@ -899,7 +905,14 @@ def parse_args(args):
899905
parser = argparse.ArgumentParser(description='A script to run `gn gen`.')
900906

901907
parser.add_argument('--unoptimized', default=False, action='store_true')
902-
908+
parser.add_argument(
909+
'--optimize-for-size',
910+
default=False,
911+
action='store_true',
912+
help='Optimize for code size rather than speed. '
913+
'Optimized builds for Windows, iOS, and Android are '
914+
'always optimized for size regardless of the value of this flag.'
915+
)
903916
parser.add_argument(
904917
'--enable-unittests',
905918
action='store_true',

0 commit comments

Comments
 (0)