Skip to content

Commit ba2d625

Browse files
committed
swift_build_support: unify CMake toolchain code
1 parent 40547a1 commit ba2d625

File tree

6 files changed

+47
-73
lines changed

6 files changed

+47
-73
lines changed

utils/swift_build_support/swift_build_support/products/cmark.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,10 @@ def build(self, host_target):
5454

5555
self.cmake_options.define('CMARK_THREADING', 'ON')
5656

57-
(platform, arch) = host_target.split('-')
58-
59-
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
60-
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
61-
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
62-
63-
if host_target.startswith("macosx") or \
64-
host_target.startswith("iphone") or \
65-
host_target.startswith("appletv") or \
66-
host_target.startswith("watch"):
67-
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
68-
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
69-
elif platform == "linux":
70-
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
71-
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
72-
elif platform == "openbsd":
57+
host_toolchain = self.generate_toolchain_file(host_target)
58+
59+
(platform, _) = host_target.split('-')
60+
if not host_toolchain and platform == "openbsd":
7361
toolchain_file = self.get_openbsd_toolchain_file()
7462
if toolchain_file:
7563
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)

utils/swift_build_support/swift_build_support/products/curl.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,7 @@ def build(self, host_target):
109109
self.cmake_options.define('ENABLE_UNIX_SOCKETS', 'NO')
110110
self.cmake_options.define('ENABLE_THREADED_RESOLVER', 'NO')
111111

112-
(platform, arch) = host_target.split('-')
113-
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
114-
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
115-
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
116-
117-
if host_target.startswith("macosx") or \
118-
host_target.startswith("iphone") or \
119-
host_target.startswith("appletv") or \
120-
host_target.startswith("watch"):
121-
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
122-
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
123-
elif platform == "linux":
124-
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
125-
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
112+
self.generate_toolchain_file(host_target)
126113

127114
if self.args.build_zlib:
128115
# If we're building zlib, make cmake search in the built toolchain

utils/swift_build_support/swift_build_support/products/earlyswiftsyntax.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,7 @@ def build(self, host_target):
5151
self.args.swift_build_variant)
5252
self.cmake_options.define('BUILD_SHARED_LIBS:STRING', 'NO')
5353

54-
(platform, arch) = host_target.split('-')
55-
56-
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
57-
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
58-
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
59-
60-
if host_target.startswith("macosx") or \
61-
host_target.startswith("iphone") or \
62-
host_target.startswith("appletv") or \
63-
host_target.startswith("watch"):
64-
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
65-
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
66-
elif platform == "linux":
67-
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
68-
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
54+
self.generate_toolchain_file(host_target)
6955

7056
self.build_with_cmake(["all"], self.args.swift_build_variant, [])
7157

utils/swift_build_support/swift_build_support/products/libxml2.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,5 @@ def build(self, host_target):
8585
self.cmake_options.define('LIBXML2_WITH_TESTS', 'NO')
8686
self.cmake_options.define('LIBXML2_WITH_ZLIB', 'NO')
8787

88-
(platform, arch) = host_target.split('-')
89-
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
90-
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
91-
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
92-
93-
if host_target.startswith("macosx") or \
94-
host_target.startswith("iphone") or \
95-
host_target.startswith("appletv") or \
96-
host_target.startswith("watch"):
97-
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
98-
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
99-
elif platform == "linux":
100-
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
101-
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
88+
self.generate_toolchain_file(host_target)
10289
self.build_with_cmake(["LibXml2"], self.args.libxml2_build_variant, [])

utils/swift_build_support/swift_build_support/products/product.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ def target_for_platform(self, platform, arch, include_version=True):
291291
return target
292292

293293
def generate_darwin_toolchain_file(self, platform, arch):
294+
"""
295+
Generates a new CMake tolchain file that specifies Darwin as a target
296+
plaftorm.
297+
298+
Returns: path on the filesystem to the newly generated toolchain file.
299+
"""
300+
294301
shell.makedirs(self.build_dir)
295302
toolchain_file = os.path.join(self.build_dir, 'BuildScriptToolchain.cmake')
296303

@@ -359,6 +366,13 @@ def get_linux_target(self, platform, arch):
359366
return '{}-unknown-linux-{}'.format(sysroot_arch, abi)
360367

361368
def generate_linux_toolchain_file(self, platform, arch):
369+
"""
370+
Generates a new CMake tolchain file that specifies Linux as a target
371+
plaftorm.
372+
373+
Returns: path on the filesystem to the newly generated toolchain file.
374+
"""
375+
362376
shell.makedirs(self.build_dir)
363377
toolchain_file = os.path.join(self.build_dir, 'BuildScriptToolchain.cmake')
364378

@@ -397,6 +411,31 @@ def generate_linux_toolchain_file(self, platform, arch):
397411

398412
return toolchain_file
399413

414+
def generate_toolchain_file(self, host_target):
415+
"""
416+
Checks `host_target` platform and generates a new CMake tolchain file
417+
appropriate for that target plaftorm. Defines `CMAKE_C_FLAGS` and
418+
`CMAKE_CXX_FLAGS` as CMake options. Also defines `CMAKE_TOOLCHAIN_FILE`
419+
with the path of the generated toolchain file as a CMake option.
420+
421+
Returns: path to the newly generated toolchain file on the filesystem.
422+
"""
423+
424+
(platform, arch) = host_target.split('-')
425+
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
426+
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
427+
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
428+
429+
toolchain_file = None
430+
if self.is_darwin_host(host_target):
431+
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
432+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
433+
elif platform == "linux":
434+
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
435+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
436+
437+
return toolchain_file
438+
400439
def get_openbsd_toolchain_file(self):
401440
return os.getenv('OPENBSD_USE_TOOLCHAIN_FILE')
402441

utils/swift_build_support/swift_build_support/products/zlib.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,5 @@ def build(self, host_target):
8181
self.cmake_options.define('SKIP_INSTALL_FILES', 'YES')
8282
self.cmake_options.define('CMAKE_INSTALL_PREFIX', '/usr')
8383

84-
(platform, arch) = host_target.split('-')
85-
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
86-
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
87-
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
88-
89-
if host_target.startswith("macosx") or \
90-
host_target.startswith("iphone") or \
91-
host_target.startswith("appletv") or \
92-
host_target.startswith("watch"):
93-
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
94-
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
95-
elif platform == "linux":
96-
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
97-
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
84+
self.generate_toolchain_file(host_target)
9885
self.build_with_cmake(["all"], self.args.zlib_build_variant, [])

0 commit comments

Comments
 (0)