Skip to content

Commit c2f7bac

Browse files
committed
swift_build_support: unify CMake toolchain code
1 parent 6cd0327 commit c2f7bac

File tree

6 files changed

+45
-73
lines changed

6 files changed

+45
-73
lines changed

utils/swift_build_support/swift_build_support/products/cmark.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,8 @@ 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+
if not host_toolchain and platform == "openbsd":
7359
toolchain_file = self.get_openbsd_toolchain_file()
7460
if toolchain_file:
7561
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
@@ -248,6 +248,13 @@ def has_cross_compile_hosts(self):
248248
return self.args.cross_compile_hosts
249249

250250
def generate_darwin_toolchain_file(self, platform, arch):
251+
"""
252+
Generates a new CMake tolchain file that specifies Darwin as a target
253+
plaftorm.
254+
255+
Returns: path on the filesystem to the newly generated toolchain file.
256+
"""
257+
251258
shell.makedirs(self.build_dir)
252259
toolchain_file = os.path.join(self.build_dir, 'BuildScriptToolchain.cmake')
253260

@@ -328,6 +335,13 @@ def get_linux_target(self, platform, arch):
328335
return '{}-unknown-linux-{}'.format(sysroot_arch, abi)
329336

330337
def generate_linux_toolchain_file(self, platform, arch):
338+
"""
339+
Generates a new CMake tolchain file that specifies Linux as a target
340+
plaftorm.
341+
342+
Returns: path on the filesystem to the newly generated toolchain file.
343+
"""
344+
331345
shell.makedirs(self.build_dir)
332346
toolchain_file = os.path.join(self.build_dir, 'BuildScriptToolchain.cmake')
333347

@@ -366,6 +380,31 @@ def generate_linux_toolchain_file(self, platform, arch):
366380

367381
return toolchain_file
368382

383+
def generate_toolchain_file(self, host_target):
384+
"""
385+
Checks `host_target` platform and generates a new CMake tolchain file
386+
appropriate for that target plaftorm. Defines `CMAKE_C_FLAGS` and
387+
`CMAKE_CXX_FLAGS` as CMake options. Also defines `CMAKE_TOOLCHAIN_FILE`
388+
with the path of the generated toolchain file as a CMake option.
389+
390+
Returns: path to the newly generated toolchain file on the filesystem.
391+
"""
392+
393+
(platform, arch) = host_target.split('-')
394+
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
395+
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
396+
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
397+
398+
toolchain_file = None
399+
if self.is_darwin_host(host_target):
400+
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
401+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
402+
elif platform == "linux":
403+
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
404+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
405+
406+
return toolchain_file
407+
369408
def get_openbsd_toolchain_file(self):
370409
return os.getenv('OPENBSD_USE_TOOLCHAIN_FILE')
371410

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)