Skip to content

Commit a6f8ba8

Browse files
jesecrichardlau
authored andcommittedJul 20, 2021
build: allow LTO with Clang 3.9.1+
Bug: #38568 Test: manual, - arm64-apple-darwin20.5.0 / Apple clang version 12.0.5 (clang-1205.0.22.9) - x86_64-pc-linux-gnu / Ubuntu clang version 13.0.0-++20210520052624+48780527dd68-1~exp1~20210520153429.417 Signed-off-by: Jesse Chan <jc@linux.com> PR-URL: #38751 Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 58154ce commit a6f8ba8

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed
 

‎common.gypi

+12-5
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,28 @@
164164
'v8_enable_handle_zapping': 0,
165165
'pgo_generate': ' -fprofile-generate ',
166166
'pgo_use': ' -fprofile-use -fprofile-correction ',
167-
'lto': ' -flto=4 -fuse-linker-plugin -ffat-lto-objects ',
168167
'conditions': [
169168
['node_shared != "true"', {
170169
'MSVC_runtimeType': 0 # MultiThreaded (/MT)
171170
}, {
172171
'MSVC_runtimeType': 2 # MultiThreadedDLL (/MD)
173172
}],
173+
['llvm_version=="0.0"', {
174+
'lto': ' -flto=4 -fuse-linker-plugin -ffat-lto-objects ', # GCC
175+
}, {
176+
'lto': ' -flto ', # Clang
177+
}],
174178
],
175179
},
176180
'cflags': [ '-O3' ],
177181
'conditions': [
182+
['enable_lto=="true"', {
183+
'cflags': ['<(lto)'],
184+
'ldflags': ['<(lto)'],
185+
'xcode_settings': {
186+
'LLVM_LTO': 'YES',
187+
},
188+
}],
178189
['OS=="linux"', {
179190
'conditions': [
180191
['node_section_ordering_info!=""', {
@@ -206,10 +217,6 @@
206217
'cflags': ['<(pgo_use)'],
207218
'ldflags': ['<(pgo_use)'],
208219
},],
209-
['enable_lto=="true"', {
210-
'cflags': ['<(lto)'],
211-
'ldflags': ['<(lto)'],
212-
},],
213220
],
214221
},],
215222
['OS == "android"', {

‎configure.py

+23-14
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
action="store_true",
166166
dest="enable_lto",
167167
help="Enable compiling with lto of a binary. This feature is only available "
168-
"on linux with gcc and g++ 5.4.1 or newer.")
168+
"with gcc 5.4.1+ or clang 3.9.1+.")
169169

170170
parser.add_option("--link-module",
171171
action="append",
@@ -866,6 +866,7 @@ def get_gas_version(cc):
866866
# quite prepared to go that far yet.
867867
def check_compiler(o):
868868
if sys.platform == 'win32':
869+
o['variables']['llvm_version'] = '0.0'
869870
if not options.openssl_no_asm and options.dest_cpu in ('x86', 'x64'):
870871
nasm_version = get_nasm_version('nasm')
871872
o['variables']['nasm_version'] = nasm_version
@@ -1045,12 +1046,19 @@ def configure_mips(o, target_arch):
10451046
host_byteorder = 'little' if target_arch in ('mipsel', 'mips64el') else 'big'
10461047
o['variables']['v8_host_byteorder'] = host_byteorder
10471048

1049+
def clang_version_ge(version_checked):
1050+
for compiler in [(CC, 'c'), (CXX, 'c++')]:
1051+
ok, is_clang, clang_version, gcc_version = \
1052+
try_check_compiler(compiler[0], compiler[1])
1053+
if is_clang and clang_version >= version_checked:
1054+
return True
1055+
return False
10481056

10491057
def gcc_version_ge(version_checked):
10501058
for compiler in [(CC, 'c'), (CXX, 'c++')]:
1051-
ok, is_clang, clang_version, compiler_version = \
1059+
ok, is_clang, clang_version, gcc_version = \
10521060
try_check_compiler(compiler[0], compiler[1])
1053-
if is_clang or compiler_version < version_checked:
1061+
if is_clang or gcc_version < version_checked:
10541062
return False
10551063
return True
10561064

@@ -1131,18 +1139,19 @@ def configure_node(o):
11311139
o['variables']['enable_pgo_generate'] = b(options.enable_pgo_generate)
11321140
o['variables']['enable_pgo_use'] = b(options.enable_pgo_use)
11331141

1134-
if flavor != 'linux' and (options.enable_lto):
1142+
if flavor == 'win' and (options.enable_lto):
11351143
raise Exception(
1136-
'The lto option is supported only on linux.')
1137-
1138-
if flavor == 'linux':
1139-
if options.enable_lto:
1140-
version_checked = (5, 4, 1)
1141-
if not gcc_version_ge(version_checked):
1142-
version_checked_str = ".".join(map(str, version_checked))
1143-
raise Exception(
1144-
'The option --enable-lto is supported for gcc and gxx %s'
1145-
' or newer only.' % (version_checked_str))
1144+
'Use Link Time Code Generation instead.')
1145+
1146+
if options.enable_lto:
1147+
gcc_version_checked = (5, 4, 1)
1148+
clang_version_checked = (3, 9, 1)
1149+
if not gcc_version_ge(gcc_version_checked) and not clang_version_ge(clang_version_checked):
1150+
gcc_version_checked_str = ".".join(map(str, gcc_version_checked))
1151+
clang_version_checked_str = ".".join(map(str, clang_version_checked))
1152+
raise Exception(
1153+
'The option --enable-lto is supported for gcc %s+'
1154+
'or clang %s+ only.' % (gcc_version_checked_str, clang_version_checked_str))
11461155

11471156
o['variables']['enable_lto'] = b(options.enable_lto)
11481157

0 commit comments

Comments
 (0)