16
16
from __future__ import print_function
17
17
18
18
import argparse
19
+ import json
19
20
import sys
20
21
import os , platform
22
+ import re
21
23
import subprocess
22
24
23
25
def printerr (message ):
@@ -43,6 +45,7 @@ def parse_args(args):
43
45
parser .add_argument ('--install-dir' , default = None , help = 'The location to install the docc executable to.' )
44
46
parser .add_argument ('--copy-doccrender-from' , default = None , help = 'The location to copy an existing Swift-DocC-Render template from.' )
45
47
parser .add_argument ('--copy-doccrender-to' , default = None , help = 'The location to install an existing Swift-DocC-Render template to.' )
48
+ parser .add_argument ("--cross-compile-hosts" , dest = "cross_compile_hosts" , help = "List of cross compile hosts targets." , default = [])
46
49
47
50
parsed = parser .parse_args (args )
48
51
@@ -167,8 +170,16 @@ def get_swiftpm_options(action, args):
167
170
# Library rpath for swift, dispatch, Foundation, etc. when installing
168
171
'-Xlinker' , '-rpath' , '-Xlinker' , '$ORIGIN/../lib/swift/linux' ,
169
172
]
170
-
171
- if action == 'install' :
173
+
174
+ build_target = get_build_target (args )
175
+ cross_compile_hosts = args .cross_compile_hosts
176
+ if cross_compile_hosts :
177
+ if re .search ('-apple-macosx' , build_target ) and re .match ('macosx-' , cross_compile_hosts ):
178
+ swiftpm_args += ["--arch" , "x86_64" , "--arch" , "arm64" ]
179
+ else :
180
+ printerr ("cannot cross-compile for %s" % cross_compile_hosts )
181
+
182
+ if action == 'install' or action == 'show-bin-path' :
172
183
# When tests are run on the host machine, `docc` is located in the build directory; to find
173
184
# its linked libraries (Swift runtime dependencies), `docc` needs to link against the host
174
185
# machine's toolchain libraries. When installing docc on the target machine, the `docc`
@@ -178,6 +189,9 @@ def get_swiftpm_options(action, args):
178
189
if action == 'test' :
179
190
swiftpm_args += ['--parallel' ]
180
191
192
+ if action == 'show-bin-path' :
193
+ swiftpm_args += ['--show-bin-path' ]
194
+
181
195
return swiftpm_args
182
196
183
197
def invoke_swift (action , products , env , args , swiftpm_args ):
@@ -186,7 +200,7 @@ def invoke_swift(action, products, env, args, swiftpm_args):
186
200
for product in products :
187
201
invoke_swift_single_product (action , product , env , args , swiftpm_args )
188
202
189
- def invoke_swift_single_product (action , product , env , args , swiftpm_args ):
203
+ def get_call_to_invoke_swift_single_product (action , product , args , swiftpm_args ):
190
204
call = [args .swift_exec , action ] + swiftpm_args
191
205
192
206
if platform .system () != 'Darwin' :
@@ -197,6 +211,16 @@ def invoke_swift_single_product(action, product, env, args, swiftpm_args):
197
211
call .extend (['--test-product' , product ])
198
212
else :
199
213
call .extend (['--product' , product ])
214
+
215
+ return call
216
+
217
+ def invoke_swift_single_product (action , product , env , args , swiftpm_args ):
218
+ call = get_call_to_invoke_swift_single_product (
219
+ action = action ,
220
+ product = product ,
221
+ args = args ,
222
+ swiftpm_args = swiftpm_args
223
+ )
200
224
201
225
# Tell Swift-DocC that we are building in a build-script environment so that
202
226
# it does not need to be rebuilt if it has already been built before.
@@ -217,7 +241,6 @@ def install(args, env):
217
241
verbose = args .verbose
218
242
# Find the docc executable location
219
243
docc_path = docc_bin_path (
220
- swift_exec = os .path .join (os .path .join (args .toolchain , 'bin' ), 'swift' ),
221
244
args = args ,
222
245
env = env ,
223
246
verbose = verbose
@@ -245,17 +268,29 @@ def install(args, env):
245
268
install_path = copy_render_to ,
246
269
verbose = verbose
247
270
)
271
+
272
+ def get_build_target (args ):
273
+ """Returns the target-triple of the current machine or for cross-compilation."""
274
+ # Adapted from https://github.com/apple/swift-package-manager/blob/fde9916d/Utilities/bootstrap#L296
275
+ command = [args .swift_exec , '-print-target-info' ]
276
+ target_info_json = subprocess .check_output (command , stderr = subprocess .PIPE , universal_newlines = True ).strip ()
277
+ args .target_info = json .loads (target_info_json )
278
+ if platform .system () == 'Darwin' :
279
+ return args .target_info ["target" ]["unversionedTriple" ]
280
+
281
+ return args .target_info ["target" ]["triple" ]
282
+
283
+ def docc_bin_path (args , env , verbose ):
284
+ cmd = get_call_to_invoke_swift_single_product (
285
+ action = 'build' ,
286
+ product = 'docc' ,
287
+ args = args ,
288
+ swiftpm_args = get_swiftpm_options (
289
+ action = 'show-bin-path' ,
290
+ args = args
291
+ )
292
+ )
248
293
249
- def docc_bin_path (swift_exec , args , env , verbose ):
250
- cmd = [
251
- swift_exec ,
252
- 'build' ,
253
- '--show-bin-path' ,
254
- '--package-path' , args .package_path ,
255
- '--scratch-path' , args .build_dir ,
256
- '--configuration' , args .configuration ,
257
- '--product' , 'docc'
258
- ]
259
294
if verbose :
260
295
print (' ' .join ([escape_cmd_arg (arg ) for arg in cmd ]))
261
296
return os .path .join (
0 commit comments