Skip to content

Commit 4cf93ef

Browse files
authored
Allow LTO enabled builds on Darwin platforms. (flutter#86)
1 parent 26a5151 commit 4cf93ef

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

build/toolchain/clang.gni

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ declare_args() {
66
# Enable the optional type profiler in Clang, which will tag heap allocations
77
# with the allocation type.
88
use_clang_type_profiler = false
9+
10+
# Enable Link Time Optimzations. This significantly slows down linking but
11+
# results in a smaller binary.
12+
enable_lto = true
913
}

build/toolchain/mac/BUILD.gn

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ template("mac_toolchain") {
3535
assert(defined(invoker.cc), "mac_toolchain() must specify a \"cc\" value")
3636
assert(defined(invoker.cxx), "mac_toolchain() must specify a \"cxx\" value")
3737
assert(defined(invoker.ld), "mac_toolchain() must specify a \"ld\" value")
38+
assert(defined(invoker.ar), "mac_toolchain() must specify a \"ar\" value")
3839
assert(defined(invoker.toolchain_cpu),
3940
"mac_toolchain() must specify a \"toolchain_cpu\"")
4041
assert(defined(invoker.toolchain_os),
@@ -46,6 +47,7 @@ template("mac_toolchain") {
4647
cc = invoker.cc
4748
cxx = invoker.cxx
4849
ld = invoker.ld
50+
ar = invoker.ar
4951

5052
# Make these apply to all tools below.
5153
lib_switch = "-l"
@@ -62,9 +64,14 @@ template("mac_toolchain") {
6264
toolchain_flags = "-m32"
6365
}
6466

67+
lto_flags = ""
68+
if (enable_lto) {
69+
lto_flags = "-flto"
70+
}
71+
6572
tool("cc") {
6673
depfile = "{{output}}.d"
67-
command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} $sysroot_flags $toolchain_flags {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
74+
command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} $sysroot_flags $toolchain_flags $lto_flags {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
6875
depsformat = "gcc"
6976
description = "CC {{output}}"
7077
outputs = [
@@ -74,7 +81,7 @@ template("mac_toolchain") {
7481

7582
tool("cxx") {
7683
depfile = "{{output}}.d"
77-
command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} $sysroot_flags $toolchain_flags {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
84+
command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} $sysroot_flags $toolchain_flags $lto_flags {{cflags}} {{cflags_cc}} -c {{source}} -o {{output}}"
7885
depsformat = "gcc"
7986
description = "CXX {{output}}"
8087
outputs = [
@@ -85,7 +92,7 @@ template("mac_toolchain") {
8592
tool("asm") {
8693
# For GCC we can just use the C compiler to compile assembly.
8794
depfile = "{{output}}.d"
88-
command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} $sysroot_flags $toolchain_flags {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
95+
command = "$cc -MMD -MF $depfile {{defines}} {{include_dirs}} {{asmflags}} $sysroot_flags $toolchain_flags $lto_flags {{cflags}} {{cflags_c}} -c {{source}} -o {{output}}"
8996
depsformat = "gcc"
9097
description = "ASM {{output}}"
9198
outputs = [
@@ -95,7 +102,7 @@ template("mac_toolchain") {
95102

96103
tool("objc") {
97104
depfile = "{{output}}.d"
98-
command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} $sysroot_flags $toolchain_flags {{cflags}} {{cflags_c}} {{cflags_objc}} -c {{source}} -o {{output}}"
105+
command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} $sysroot_flags $toolchain_flags $lto_flags {{cflags}} {{cflags_c}} {{cflags_objc}} -c {{source}} -o {{output}}"
99106
depsformat = "gcc"
100107
description = "OBJC {{output}}"
101108
outputs = [
@@ -105,7 +112,7 @@ template("mac_toolchain") {
105112

106113
tool("objcxx") {
107114
depfile = "{{output}}.d"
108-
command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} $sysroot_flags $toolchain_flags {{cflags}} {{cflags_cc}} {{cflags_objcc}} -c {{source}} -o {{output}}"
115+
command = "$cxx -MMD -MF $depfile {{defines}} {{include_dirs}} $sysroot_flags $toolchain_flags $lto_flags {{cflags}} {{cflags_cc}} {{cflags_objcc}} -c {{source}} -o {{output}}"
109116
depsformat = "gcc"
110117
description = "OBJCXX {{output}}"
111118
outputs = [
@@ -114,8 +121,8 @@ template("mac_toolchain") {
114121
}
115122

116123
tool("alink") {
117-
command = "rm -f {{output}} && ./gyp-mac-tool filter-libtool libtool -static -o {{output}} {{inputs}}"
118-
description = "LIBTOOL-STATIC {{output}}"
124+
command = "rm -f {{output}} && $ar rcs {{output}} {{inputs}}"
125+
description = "CREATE ARCHIVE {{output}}"
119126
outputs = [
120127
"{{target_out_dir}}/{{target_output_name}}{{output_extension}}",
121128
]
@@ -139,7 +146,7 @@ template("mac_toolchain") {
139146
temporary_tocname = dylib + ".tmp"
140147

141148
does_reexport_command = "[ ! -e $dylib -o ! -e $tocname ] || otool -l $dylib | grep -q LC_REEXPORT_DYLIB"
142-
link_command = "$ld -shared $sysroot_flags $toolchain_flags {{ldflags}} -o $dylib -Wl,-filelist,$rspfile {{solibs}} {{libs}}"
149+
link_command = "$ld -shared $sysroot_flags $toolchain_flags $lto_flags {{ldflags}} -o $dylib -Wl,-filelist,$rspfile {{solibs}} {{libs}}"
143150
replace_command = "if ! cmp -s $temporary_tocname $tocname; then mv $temporary_tocname $tocname"
144151
extract_toc_command = "{ otool -l $dylib | grep LC_ID_DYLIB -A 5; nm -gP $dylib | cut -f1-2 -d' ' | grep -v U\$\$; true; }"
145152

@@ -175,7 +182,7 @@ template("mac_toolchain") {
175182
outfile = "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"
176183
rspfile = "$outfile.rsp"
177184

178-
command = "$ld $sysroot_flags $toolchain_flags {{ldflags}} -Xlinker -rpath -Xlinker @executable_path/Frameworks -o $outfile -Wl,-filelist,$rspfile {{solibs}} {{libs}}"
185+
command = "$ld $sysroot_flags $toolchain_flags $lto_flags {{ldflags}} -Xlinker -rpath -Xlinker @executable_path/Frameworks -o $outfile -Wl,-filelist,$rspfile {{solibs}} {{libs}}"
179186
description = "LINK $outfile"
180187
rspfile_content = "{{inputs_newline}}"
181188
outputs = [
@@ -193,7 +200,7 @@ template("mac_toolchain") {
193200
description = "COPY {{source}} {{output}}"
194201
}
195202

196-
toolchain_args = {
203+
toolchain_args = {
197204
current_cpu = invoker.toolchain_cpu
198205
current_os = invoker.toolchain_os
199206

@@ -215,6 +222,7 @@ mac_toolchain("ios_clang_arm") {
215222
toolchain_cpu = "arm"
216223
toolchain_os = "mac"
217224
prefix = rebase_path(llvm_bin_path, root_build_dir)
225+
ar = "${goma_prefix}$prefix/llvm-ar"
218226
cc = "${goma_prefix}$prefix/clang"
219227
cxx = "${goma_prefix}$prefix/clang++"
220228
ld = cxx
@@ -227,6 +235,7 @@ mac_toolchain("ios_clang_x64") {
227235
toolchain_cpu = "x64"
228236
toolchain_os = "mac"
229237
prefix = rebase_path(llvm_bin_path, root_build_dir)
238+
ar = "${goma_prefix}$prefix/llvm-ar"
230239
cc = "${goma_prefix}$prefix/clang"
231240
cxx = "${goma_prefix}$prefix/clang++"
232241
ld = cxx
@@ -239,6 +248,7 @@ mac_toolchain("clang_x64") {
239248
toolchain_cpu = "x64"
240249
toolchain_os = "mac"
241250
prefix = rebase_path(llvm_bin_path, root_build_dir)
251+
ar = "${goma_prefix}$prefix/llvm-ar"
242252
cc = "${goma_prefix}$prefix/clang"
243253
cxx = "${goma_prefix}$prefix/clang++"
244254
ld = cxx
@@ -251,6 +261,7 @@ mac_toolchain("clang_i386") {
251261
toolchain_cpu = "i386"
252262
toolchain_os = "mac"
253263
prefix = rebase_path(llvm_bin_path, root_build_dir)
264+
ar = "${goma_prefix}$prefix/llvm-ar"
254265
cc = "${goma_prefix}$prefix/clang"
255266
cxx = "${goma_prefix}$prefix/clang++"
256267
ld = cxx

0 commit comments

Comments
 (0)