Skip to content

Commit d680f1d

Browse files
gottesmmDougGregor
authored andcommitted
[build-script] Use a toolchain file and always cross compile cmark.
NOTE: We still build for the host platform always, but even in that case within cmark itself, we are cross compiling it. (cherry picked from commit d829de1)
1 parent 4039218 commit d680f1d

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

1715

@@ -59,20 +57,12 @@ def build(self, host_target):
5957
host_target.startswith("appletv") or \
6058
host_target.startswith("watch"):
6159

62-
cmake_os_sysroot = xcrun.sdk_path(platform)
63-
64-
cmake_osx_deployment_target = ''
65-
if platform == "macosx":
66-
cmake_osx_deployment_target = self.args.darwin_deployment_version_osx
67-
6860
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
6961

7062
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
7163
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
72-
self.cmake_options.define('CMAKE_OSX_SYSROOT:PATH', cmake_os_sysroot)
73-
self.cmake_options.define('CMAKE_OSX_DEPLOYMENT_TARGET',
74-
cmake_osx_deployment_target)
75-
self.cmake_options.define('CMAKE_OSX_ARCHITECTURES', arch)
64+
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
65+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
7666

7767
self.build_with_cmake(["all"], self.args.cmark_build_variant, [])
7868

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

@@ -193,14 +196,15 @@ def install_toolchain_path(self, host_target):
193196
return targets.toolchain_path(install_destdir,
194197
self.args.install_prefix)
195198

199+
def is_darwin_host(self, host_target):
200+
return host_target.startswith("macosx") or \
201+
host_target.startswith("iphone") or \
202+
host_target.startswith("appletv") or \
203+
host_target.startswith("watch")
204+
196205
def should_include_host_in_lipo(self, host_target):
197-
if self.args.cross_compile_hosts:
198-
if host_target.startswith("macosx") or \
199-
host_target.startswith("iphone") or \
200-
host_target.startswith("appletv") or \
201-
host_target.startswith("watch"):
202-
return True
203-
return False
206+
return self.args.cross_compile_hosts and \
207+
self.is_darwin_host(host_target)
204208

205209
def host_install_destdir(self, host_target):
206210
if self.args.cross_compile_hosts:
@@ -222,38 +226,62 @@ def is_cross_compile_target(self, host_target):
222226
return self.args.cross_compile_hosts and \
223227
host_target in self.args.cross_compile_hosts
224228

225-
# TODO: Remove once we've moved over to cmake toolchains
226-
def common_cross_c_flags(self, platform, arch):
227-
cross_flags = []
229+
def generate_darwin_toolchain_file(self, platform, arch):
230+
shell.makedirs(self.build_dir)
231+
toolchain_file = os.path.join(self.build_dir, 'BuildScriptToolchain.cmake')
228232

233+
cmake_osx_sysroot = xcrun.sdk_path(platform)
234+
235+
target = None
229236
if platform == 'macosx':
230237
target = '{}-apple-macosx{}'.format(
231238
arch, self.args.darwin_deployment_version_osx)
232-
cross_flags.extend(['-arch', arch, '-target', target])
233239
elif platform == 'iphonesimulator':
234240
target = '{}-apple-ios{}'.format(
235241
arch, self.args.darwin_deployment_version_ios)
236-
cross_flags.extend(['-arch', arch, '-target', target])
237242
elif platform == 'iphoneos':
238243
target = '{}-apple-ios{}'.format(
239244
arch, self.args.darwin_deployment_version_ios)
240-
cross_flags.extend(['-arch', arch, '-target', target])
241245
elif platform == 'appletvsimulator':
242246
target = '{}-apple-tvos{}'.format(
243247
arch, self.args.darwin_deployment_version_tvos)
244-
cross_flags.extend(['-arch', arch, '-target', target])
245248
elif platform == 'appletvos':
246249
target = '{}-apple-tvos{}'.format(
247250
arch, self.args.darwin_deployment_version_tvos)
248-
cross_flags.extend(['-arch', arch, '-target', target])
249251
elif platform == 'watchsimulator':
250252
target = '{}-apple-watchos{}'.format(
251253
arch, self.args.darwin_deployment_version_watchos)
252-
cross_flags.extend(['-arch', arch, '-target', target])
253254
elif platform == 'watchos':
254255
target = '{}-apple-watchos{}'.format(
255256
arch, self.args.darwin_deployment_version_watchos)
256-
cross_flags.extend(['-arch', arch, '-target', target])
257+
else:
258+
raise RuntimeError("Unhandled platform?!")
259+
260+
toolchain_args = {}
261+
262+
toolchain_args['CMAKE_SYSTEM_NAME'] = 'Darwin'
263+
toolchain_args['CMAKE_OSX_SYSROOT'] = cmake_osx_sysroot
264+
toolchain_args['CMAKE_OSX_ARCHITECTURES'] = arch
265+
266+
if self.toolchain.cc.endswith('clang'):
267+
toolchain_args['CMAKE_C_COMPILER_TARGET'] = target
268+
if self.toolchain.cxx.endswith('clang++'):
269+
toolchain_args['CMAKE_CXX_COMPILER_TARGET'] = target
270+
# Swift always supports cross compiling.
271+
toolchain_args['CMAKE_Swift_COMPILER_TARGET'] = target
272+
273+
# Sort by the key so that we always produce the same toolchain file
274+
data = sorted(toolchain_args.items(), key=lambda x: x[0])
275+
if not self.args.dry_run:
276+
with open(toolchain_file, 'w') as f:
277+
f.writelines("set({} {})\n".format(k, v) for k, v in data)
278+
else:
279+
print("DRY_RUN! Writing Toolchain file to path: {}".format(toolchain_file))
280+
281+
return toolchain_file
282+
283+
def common_cross_c_flags(self, platform, arch):
284+
cross_flags = []
257285

258286
if self.is_release():
259287
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)