@@ -11,6 +11,7 @@ import argparse
11
11
import subprocess
12
12
import sys
13
13
import os
14
+ import platform
14
15
15
16
SRC_ROOT = os .path .dirname (os .path .dirname (os .path .dirname (os .path .abspath (__file__ ))))
16
17
@@ -89,8 +90,6 @@ def is_host_build(args):
89
90
# If target_os == None, then this is a host build.
90
91
# However, for linux arm64 builds, we cross compile from x64 hosts, so the
91
92
# target_os='linux' and linux-cpu='arm64'
92
- # TODO(fujino): make host platform explicit
93
- # https://github.com/flutter/flutter/issues/79403
94
93
return args .target_os is None or (args .target_os == 'linux' and args .linux_cpu == 'arm64' )
95
94
96
95
# Determines whether a prebuilt Dart SDK can be used instead of building one.
@@ -121,6 +120,47 @@ def can_use_prebuilt_dart(args):
121
120
return prebuilts_dir != None and os .path .isdir (prebuilts_dir )
122
121
123
122
123
+ # Returns the host machine operating system.
124
+ def get_host_os ():
125
+ if sys .platform .startswith (('cygwin' , 'win' )):
126
+ return 'win'
127
+ if sys .platform == 'darwin' :
128
+ return 'mac'
129
+ return 'linux'
130
+
131
+
132
+ # Runs true if the currently executing python interpreter is running under
133
+ # Rosetta. I.e., python3 is an x64 executable and we're on an arm64 Mac.
134
+ def is_rosetta ():
135
+ if platform .system () == 'Darwin' :
136
+ p = subprocess .Popen (['sysctl' , '-in' , 'sysctl.proc_translated' ],
137
+ stdout = subprocess .PIPE ,
138
+ stderr = subprocess .STDOUT )
139
+ output , _ = p .communicate ()
140
+ return output .decode ('utf-8' ).strip () == '1'
141
+ return False
142
+
143
+
144
+ # Returns the host machine CPU architecture.
145
+ def get_host_cpu (args ):
146
+ # If gn itself is running under Rosetta on an arm64 Mac, platform.machine()
147
+ # will return x86_64; instead return the underlying host architecture.
148
+ if is_rosetta ():
149
+ return 'arm64'
150
+ m = platform .machine ()
151
+ if m in ['aarch64' , 'arm64' ]:
152
+ return 'arm64'
153
+ if m in ['x86_64' , 'AMD64' , 'x64' ]:
154
+ return 'x64'
155
+ if m in ['i686' , 'i386' , 'x86' ]:
156
+ return 'x86'
157
+ raise Exception ('Unknown CPU architecture: %s' % m )
158
+
159
+
160
+ # Returns the target CPU architecture.
161
+ #
162
+ # For macOS host builds where --mac-cpu is specified, returns that value.
163
+ # For all other host builds, assumes 'x64'.
124
164
def get_target_cpu (args ):
125
165
if args .target_os == 'android' :
126
166
return args .android_cpu
@@ -139,7 +179,10 @@ def get_target_cpu(args):
139
179
return 'wasm'
140
180
if args .target_os == 'win' :
141
181
return args .windows_cpu
142
- # Building host artifacts. Default to x64.
182
+
183
+ # Host build. Default to x64 unless overridden.
184
+ if get_host_os () == 'mac' and args .mac_cpu :
185
+ return args .mac_cpu
143
186
return 'x64'
144
187
145
188
@@ -212,19 +255,39 @@ def to_gn_args(args):
212
255
# The GN arg is not available in the windows toolchain.
213
256
gn_args ['enable_lto' ] = enable_lto
214
257
215
- if args .target_os :
258
+ # Set OS, CPU arch for host or target build.
259
+ if is_host_build (args ):
260
+ gn_args ['host_os' ] = get_host_os ()
261
+ gn_args ['host_cpu' ] = get_host_cpu (args )
262
+ gn_args ['target_os' ] = gn_args ['host_os' ]
263
+ gn_args ['target_cpu' ] = get_target_cpu (args )
264
+ gn_args ['dart_target_arch' ] = gn_args ['target_cpu' ]
265
+ else :
216
266
gn_args ['target_os' ] = args .target_os
217
- gn_args ['target_cpu' ] = get_target_cpu (args )
218
- gn_args ['dart_target_arch' ] = gn_args ['target_cpu' ]
267
+ gn_args ['target_cpu' ] = get_target_cpu (args )
268
+ gn_args ['dart_target_arch' ] = gn_args ['target_cpu' ]
219
269
220
- # No cross-compilation on Windows (for now).
221
- # See: https://github.com/flutter/engine/pull/3883
270
+ # No cross-compilation on Windows (for now). Use host toolchain that
271
+ # matches the bit-width of the target architecture.
222
272
if sys .platform .startswith (('cygwin' , 'win' )) and args .target_os != 'win' :
273
+ gn_args ['host_cpu' ] = cpu_for_target_arch (gn_args ['target_cpu' ])
223
274
gn_args ['target_cpu' ] = cpu_for_target_arch (gn_args ['target_cpu' ])
224
275
225
- if args .target_os == 'ios' :
276
+ # macOS host builds (whether x64 or arm64) must currently be built under
277
+ # Rosetta on Apple Silicon Macs.
278
+ # TODO(cbracken): https://github.com/flutter/flutter/issues/103386
279
+ if is_host_build (args ) and gn_args ['host_os' ] == 'mac' :
280
+ gn_args ['host_cpu' ] = 'x64'
281
+
282
+ # macOS target builds (whether x64 or arm64) must currently be built under
283
+ # Rosetta on Apple Silicon Macs.
284
+ # TODO(cbracken): https://github.com/flutter/flutter/issues/103386
285
+ if 'target_os' in gn_args and gn_args ['target_os' ] == 'mac' :
286
+ gn_args ['host_cpu' ] = 'x64'
287
+
288
+ if gn_args ['target_os' ] == 'ios' :
226
289
gn_args ['use_ios_simulator' ] = args .simulator
227
- elif args . target_os == 'mac' :
290
+ elif gn_args [ ' target_os' ] == 'mac' :
228
291
gn_args ['use_ios_simulator' ] = False
229
292
230
293
if args .dart_debug :
@@ -275,10 +338,6 @@ def to_gn_args(args):
275
338
if sys .platform .startswith (('cygwin' , 'win' )):
276
339
gn_args ['dart_use_fallback_root_certificates' ] = True
277
340
278
- # Make sure host_cpu matches the bit width of target_cpu on x86.
279
- if gn_args ['target_cpu' ] == 'x86' :
280
- gn_args ['host_cpu' ] = 'x86'
281
-
282
341
if args .target_sysroot :
283
342
gn_args ['target_sysroot' ] = args .target_sysroot
284
343
gn_args ['custom_sysroot' ] = args .target_sysroot
0 commit comments