Skip to content

Commit 4f9d718

Browse files
committed
Support building this repo for more platforms, by checking the build triple
This replaces all but one prior check of the host system instead, plus removed unnecessary regex checking.
1 parent af9fce4 commit 4f9d718

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

Utilities/build-script-helper.py

+27-25
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import subprocess
1010
import sys
1111
import errno
12-
import re
1312

1413
if platform.system() == 'Darwin':
1514
shared_lib_ext = '.dylib'
@@ -80,7 +79,8 @@ def get_swiftpm_options(args):
8079
if args.verbose:
8180
swiftpm_args += ['--verbose']
8281

83-
if platform.system() == 'Darwin':
82+
build_os = args.build_target.split('-')[2]
83+
if build_os.startswith('macosx'):
8484
swiftpm_args += [
8585
# Relative library rpath for swift; will only be used when /usr/lib/swift
8686
# is not available.
@@ -99,8 +99,7 @@ def get_swiftpm_options(args):
9999
if args.cross_compile_hosts:
100100
swiftpm_args += ['--destination', args.cross_compile_config]
101101

102-
if 'ANDROID_DATA' in os.environ or (args.cross_compile_hosts and re.match(
103-
'android-', args.cross_compile_hosts[0])):
102+
if '-android' in args.build_target:
104103
swiftpm_args += [
105104
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/android',
106105
# SwiftPM will otherwise try to compile against GNU strerror_r on
@@ -110,7 +109,7 @@ def get_swiftpm_options(args):
110109
else:
111110
# Library rpath for swift, dispatch, Foundation, etc. when installing
112111
swiftpm_args += [
113-
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/linux',
112+
'-Xlinker', '-rpath', '-Xlinker', '$ORIGIN/../lib/swift/' + build_os,
114113
]
115114

116115
if args.action == 'install':
@@ -158,15 +157,14 @@ def handle_invocation(args):
158157
swiftpm_args = get_swiftpm_options(args)
159158
toolchain_bin = os.path.join(args.toolchain, 'bin')
160159
swift_exec = os.path.join(toolchain_bin, 'swift')
161-
swiftc_exec = os.path.join(toolchain_bin, 'swiftc')
162160

163161
# Platform-specific targets for which we must build swift-driver
164162
if args.cross_compile_hosts:
165163
targets = args.cross_compile_hosts
166-
elif platform.system() == 'Darwin':
167-
targets = [get_build_target(swiftc_exec, args) + macos_deployment_target]
164+
elif '-apple-macosx' in args.build_target:
165+
targets = [args.build_target + macos_deployment_target]
168166
else:
169-
targets = [get_build_target(swiftc_exec, args)]
167+
targets = [args.build_target]
170168

171169
env = os.environ
172170
# Use local dependencies (i.e. checked out next to swift-driver).
@@ -182,7 +180,7 @@ def handle_invocation(args):
182180
env['SWIFT_EXEC'] = '%sc' % (swift_exec)
183181

184182
if args.action == 'build':
185-
if args.cross_compile_hosts and not re.match('-macosx', args.cross_compile_hosts[0]):
183+
if args.cross_compile_hosts and not '-macosx' in args.cross_compile_hosts[0]:
186184
swiftpm('build', swift_exec, swiftpm_args, env)
187185
else:
188186
build_using_cmake(args, toolchain_bin, args.build_path, targets)
@@ -205,7 +203,7 @@ def handle_invocation(args):
205203
env['SWIFT_DRIVER_LIT_DIR'] = args.lit_test_dir
206204
swiftpm('test', swift_exec, test_args, env)
207205
elif args.action == 'install':
208-
if platform.system() == 'Darwin':
206+
if '-apple-macosx' in args.build_target:
209207
build_using_cmake(args, toolchain_bin, args.build_path, targets)
210208
install(args, args.build_path, targets)
211209
else:
@@ -428,7 +426,7 @@ def build_using_cmake(args, toolchain_bin, build_dir, targets):
428426
base_cmake_flags = []
429427
swift_flags = base_swift_flags.copy()
430428
swift_flags.append('-target %s' % target)
431-
if platform.system() == 'Darwin':
429+
if '-apple-macosx' in args.build_target:
432430
base_cmake_flags.append('-DCMAKE_OSX_DEPLOYMENT_TARGET=%s' % macos_deployment_target)
433431
base_cmake_flags.append('-DCMAKE_OSX_ARCHITECTURES=%s' % target.split('-')[0])
434432

@@ -480,7 +478,7 @@ def build_llbuild_using_cmake(args, target, swiftc_exec, build_dir, base_cmake_f
480478
llbuild_cmake_flags.append('-DSQLite3_INCLUDE_DIR=%s/usr/include' % args.sysroot)
481479
# FIXME: This may be particularly hacky but CMake finds a different version of libsqlite3
482480
# on some machines. This is also Darwin-specific...
483-
if platform.system() == 'Darwin':
481+
if '-apple-macosx' in args.build_target:
484482
llbuild_cmake_flags.append('-DSQLite3_LIBRARY=%s/usr/lib/libsqlite3.tbd' % args.sysroot)
485483
llbuild_swift_flags = swift_flags[:]
486484

@@ -520,7 +518,7 @@ def build_yams_using_cmake(args, target, swiftc_exec, build_dir, base_cmake_flag
520518
'-DCMAKE_C_COMPILER:=clang',
521519
'-DBUILD_SHARED_LIBS=OFF']
522520

523-
if platform.system() == 'Darwin':
521+
if '-apple-macosx' in args.build_target:
524522
yams_cmake_flags.append('-DCMAKE_OSX_DEPLOYMENT_TARGET=%s' % macos_deployment_target)
525523
yams_cmake_flags.append('-DCMAKE_C_FLAGS=-target %s' % target)
526524
else:
@@ -598,16 +596,20 @@ def cmake_build(args, swiftc_exec, cmake_args, swift_flags, source_path,
598596
if args.verbose:
599597
print(stdout)
600598

601-
def get_build_target(swiftc_path, args):
599+
def get_build_target(swiftc_path, args, cross_compile=False):
602600
"""Returns the target-triple of the current machine."""
603601
try:
604-
target_info_json = subprocess.check_output([swiftc_path, '-print-target-info'],
602+
command = [swiftc_path, '-print-target-info']
603+
if cross_compile:
604+
cross_compile_json = json.load(open(args.cross_compile_config))
605+
command += ['-target', cross_compile_json["target"]]
606+
target_info_json = subprocess.check_output(command,
605607
stderr=subprocess.PIPE,
606608
universal_newlines=True).strip()
607609
args.target_info = json.loads(target_info_json)
608610
triple = args.target_info['target']['triple']
609611
# Windows also wants unversionedTriple, but does not use this.
610-
if platform.system() == 'Darwin':
612+
if '-apple-macosx' in args.target_info["target"]["unversionedTriple"]:
611613
triple = args.target_info['target']['unversionedTriple']
612614
return triple
613615
except Exception as e:
@@ -666,18 +668,18 @@ def add_common_args(parser):
666668
args.build_path = os.path.abspath(args.build_path)
667669
args.toolchain = os.path.abspath(args.toolchain)
668670

669-
if platform.system() == 'Darwin':
671+
swift_exec = os.path.join(os.path.join(args.toolchain, 'bin'), 'swiftc')
672+
args.build_target = get_build_target(swift_exec, args, cross_compile=(True if args.cross_compile_config else False))
673+
if '-apple-macosx' in args.build_target:
670674
args.sysroot = call_output(["xcrun", "--sdk", "macosx", "--show-sdk-path"], verbose=args.verbose)
671675
else:
672676
args.sysroot = None
673677

674-
swift_exec = os.path.join(os.path.join(args.toolchain, 'bin'), 'swiftc')
675-
build_target = get_build_target(swift_exec, args)
676-
if (build_target == 'x86_64-apple-macosx' and 'macosx-arm64' in args.cross_compile_hosts):
677-
args.cross_compile_hosts = [build_target + macos_deployment_target, 'arm64-apple-macosx%s' % macos_deployment_target]
678-
elif (build_target == 'arm64-apple-macosx' and 'macosx-x86_64' in args.cross_compile_hosts):
679-
args.cross_compile_hosts = [build_target + macos_deployment_target, 'x86_64-apple-macosx%s' % macos_deployment_target]
680-
elif args.cross_compile_hosts and re.match('android-', args.cross_compile_hosts[0]):
678+
if (args.build_target == 'x86_64-apple-macosx' and 'macosx-arm64' in args.cross_compile_hosts):
679+
args.cross_compile_hosts = [args.build_target + macos_deployment_target, 'arm64-apple-macosx%s' % macos_deployment_target]
680+
elif (args.build_target == 'arm64-apple-macosx' and 'macosx-x86_64' in args.cross_compile_hosts):
681+
args.cross_compile_hosts = [args.build_target + macos_deployment_target, 'x86_64-apple-macosx%s' % macos_deployment_target]
682+
elif args.cross_compile_hosts and 'android-' in args.cross_compile_hosts[0]:
681683
print('Cross-compiling for %s' % args.cross_compile_hosts[0])
682684
elif args.cross_compile_hosts:
683685
error("cannot cross-compile for %s" % cross_compile_hosts)

0 commit comments

Comments
 (0)