Skip to content

Commit f1ebf6c

Browse files
authored
Merge pull request #38124 from gottesmm/pr-b1cd58be660
[build-script] Use a toolchain file and always cross compile cmark.
2 parents e5eb892 + d829de1 commit f1ebf6c

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

utils/swift_build_support/swift_build_support/products/cmark.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#
1111
# ----------------------------------------------------------------------------
1212

13-
from build_swift.build_swift.wrappers import xcrun
14-
1513
from . import cmake_product
1614
from . import earlyswiftdriver
1715

@@ -61,20 +59,12 @@ def build(self, host_target):
6159
host_target.startswith("appletv") or \
6260
host_target.startswith("watch"):
6361

64-
cmake_os_sysroot = xcrun.sdk_path(platform)
65-
66-
cmake_osx_deployment_target = ''
67-
if platform == "macosx":
68-
cmake_osx_deployment_target = self.args.darwin_deployment_version_osx
69-
7062
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
7163

7264
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
7365
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
74-
self.cmake_options.define('CMAKE_OSX_SYSROOT:PATH', cmake_os_sysroot)
75-
self.cmake_options.define('CMAKE_OSX_DEPLOYMENT_TARGET',
76-
cmake_osx_deployment_target)
77-
self.cmake_options.define('CMAKE_OSX_ARCHITECTURES', arch)
66+
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
67+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
7868

7969
self.build_with_cmake(["all"], self.args.cmark_build_variant, [])
8070

utils/swift_build_support/swift_build_support/products/product.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
import abc
1414
import os
1515

16+
from build_swift.build_swift.wrappers import xcrun
17+
1618
from .. import cmake
19+
from .. import shell
1720
from .. import targets
1821

1922

@@ -204,14 +207,15 @@ def install_toolchain_path(self, host_target):
204207
return targets.toolchain_path(install_destdir,
205208
self.args.install_prefix)
206209

210+
def is_darwin_host(self, host_target):
211+
return host_target.startswith("macosx") or \
212+
host_target.startswith("iphone") or \
213+
host_target.startswith("appletv") or \
214+
host_target.startswith("watch")
215+
207216
def should_include_host_in_lipo(self, host_target):
208-
if self.args.cross_compile_hosts:
209-
if host_target.startswith("macosx") or \
210-
host_target.startswith("iphone") or \
211-
host_target.startswith("appletv") or \
212-
host_target.startswith("watch"):
213-
return True
214-
return False
217+
return self.args.cross_compile_hosts and \
218+
self.is_darwin_host(host_target)
215219

216220
def host_install_destdir(self, host_target):
217221
if self.args.cross_compile_hosts:
@@ -233,38 +237,62 @@ def is_cross_compile_target(self, host_target):
233237
return self.args.cross_compile_hosts and \
234238
host_target in self.args.cross_compile_hosts
235239

236-
# TODO: Remove once we've moved over to cmake toolchains
237-
def common_cross_c_flags(self, platform, arch):
238-
cross_flags = []
240+
def generate_darwin_toolchain_file(self, platform, arch):
241+
shell.makedirs(self.build_dir)
242+
toolchain_file = os.path.join(self.build_dir, 'BuildScriptToolchain.cmake')
239243

244+
cmake_osx_sysroot = xcrun.sdk_path(platform)
245+
246+
target = None
240247
if platform == 'macosx':
241248
target = '{}-apple-macosx{}'.format(
242249
arch, self.args.darwin_deployment_version_osx)
243-
cross_flags.extend(['-arch', arch, '-target', target])
244250
elif platform == 'iphonesimulator':
245251
target = '{}-apple-ios{}'.format(
246252
arch, self.args.darwin_deployment_version_ios)
247-
cross_flags.extend(['-arch', arch, '-target', target])
248253
elif platform == 'iphoneos':
249254
target = '{}-apple-ios{}'.format(
250255
arch, self.args.darwin_deployment_version_ios)
251-
cross_flags.extend(['-arch', arch, '-target', target])
252256
elif platform == 'appletvsimulator':
253257
target = '{}-apple-tvos{}'.format(
254258
arch, self.args.darwin_deployment_version_tvos)
255-
cross_flags.extend(['-arch', arch, '-target', target])
256259
elif platform == 'appletvos':
257260
target = '{}-apple-tvos{}'.format(
258261
arch, self.args.darwin_deployment_version_tvos)
259-
cross_flags.extend(['-arch', arch, '-target', target])
260262
elif platform == 'watchsimulator':
261263
target = '{}-apple-watchos{}'.format(
262264
arch, self.args.darwin_deployment_version_watchos)
263-
cross_flags.extend(['-arch', arch, '-target', target])
264265
elif platform == 'watchos':
265266
target = '{}-apple-watchos{}'.format(
266267
arch, self.args.darwin_deployment_version_watchos)
267-
cross_flags.extend(['-arch', arch, '-target', target])
268+
else:
269+
raise RuntimeError("Unhandled platform?!")
270+
271+
toolchain_args = {}
272+
273+
toolchain_args['CMAKE_SYSTEM_NAME'] = 'Darwin'
274+
toolchain_args['CMAKE_OSX_SYSROOT'] = cmake_osx_sysroot
275+
toolchain_args['CMAKE_OSX_ARCHITECTURES'] = arch
276+
277+
if self.toolchain.cc.endswith('clang'):
278+
toolchain_args['CMAKE_C_COMPILER_TARGET'] = target
279+
if self.toolchain.cxx.endswith('clang++'):
280+
toolchain_args['CMAKE_CXX_COMPILER_TARGET'] = target
281+
# Swift always supports cross compiling.
282+
toolchain_args['CMAKE_Swift_COMPILER_TARGET'] = target
283+
284+
# Sort by the key so that we always produce the same toolchain file
285+
data = sorted(toolchain_args.items(), key=lambda x: x[0])
286+
if not self.args.dry_run:
287+
with open(toolchain_file, 'w') as f:
288+
f.writelines("set({} {})\n".format(k, v) for k, v in data)
289+
else:
290+
print("DRY_RUN! Writing Toolchain file to path: {}".format(toolchain_file))
291+
292+
return toolchain_file
293+
294+
def common_cross_c_flags(self, platform, arch):
295+
cross_flags = []
268296

269297
if self.is_release():
270298
cross_flags.append('-fno-stack-protector')
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# REQUIRES: standalone_build
2+
# REQUIRES: OS=macosx
3+
4+
# RUN: %empty-directory(%t)
5+
# RUN: mkdir -p %t
6+
# RUN: SKIP_XCODE_VERSION_CHECK=1 SWIFT_BUILD_ROOT=%t %swift_src_root/utils/build-script --dry-run --install-all --cmake %cmake --skip-build-llvm --skip-build-swift 2>&1 | %FileCheck %s
7+
8+
# CHECK: DRY_RUN! Writing Toolchain file to path:{{.*}}BuildScriptToolchain.cmake
9+
# CHECK: cmake {{.*}}-DCMAKE_TOOLCHAIN_FILE:PATH={{.*}}BuildScriptToolchain.cmake {{.*}}cmark

0 commit comments

Comments
 (0)