Skip to content

swift_build_support: unify CMake toolchain code #61696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions utils/swift_build_support/swift_build_support/products/cmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,10 @@ def build(self, host_target):

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

(platform, arch) = host_target.split('-')

common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)

if host_target.startswith("macosx") or \
host_target.startswith("iphone") or \
host_target.startswith("appletv") or \
host_target.startswith("watch"):
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
elif platform == "linux":
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
elif platform == "openbsd":
host_toolchain = self.generate_toolchain_file_for_darwin_or_linux(host_target)

(platform, _) = host_target.split('-')
if not host_toolchain and platform == "openbsd":
toolchain_file = self.get_openbsd_toolchain_file()
if toolchain_file:
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
Expand Down
15 changes: 1 addition & 14 deletions utils/swift_build_support/swift_build_support/products/curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,7 @@ def build(self, host_target):
self.cmake_options.define('ENABLE_UNIX_SOCKETS', 'NO')
self.cmake_options.define('ENABLE_THREADED_RESOLVER', 'NO')

(platform, arch) = host_target.split('-')
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)

if host_target.startswith("macosx") or \
host_target.startswith("iphone") or \
host_target.startswith("appletv") or \
host_target.startswith("watch"):
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
elif platform == "linux":
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
self.generate_toolchain_file_for_darwin_or_linux(host_target)

if self.args.build_zlib:
# If we're building zlib, make cmake search in the built toolchain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,7 @@ def build(self, host_target):
self.args.swift_build_variant)
self.cmake_options.define('BUILD_SHARED_LIBS:STRING', 'NO')

(platform, arch) = host_target.split('-')

common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)

if host_target.startswith("macosx") or \
host_target.startswith("iphone") or \
host_target.startswith("appletv") or \
host_target.startswith("watch"):
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
elif platform == "linux":
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
self.generate_toolchain_file_for_darwin_or_linux(host_target)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,5 @@ def build(self, host_target):
self.cmake_options.define('LIBXML2_WITH_TESTS', 'NO')
self.cmake_options.define('LIBXML2_WITH_ZLIB', 'NO')

(platform, arch) = host_target.split('-')
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)

if host_target.startswith("macosx") or \
host_target.startswith("iphone") or \
host_target.startswith("appletv") or \
host_target.startswith("watch"):
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
elif platform == "linux":
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
self.generate_toolchain_file_for_darwin_or_linux(host_target)
self.build_with_cmake(["LibXml2"], self.args.libxml2_build_variant, [])
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ def target_for_platform(self, platform, arch, include_version=True):
return target

def generate_darwin_toolchain_file(self, platform, arch):
"""
Generates a new CMake tolchain file that specifies Darwin as a target
plaftorm.

Returns: path on the filesystem to the newly generated toolchain file.
"""

shell.makedirs(self.build_dir)
toolchain_file = os.path.join(self.build_dir, 'BuildScriptToolchain.cmake')

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

def generate_linux_toolchain_file(self, platform, arch):
"""
Generates a new CMake tolchain file that specifies Linux as a target
plaftorm.

Returns: path on the filesystem to the newly generated toolchain file.
"""

shell.makedirs(self.build_dir)
toolchain_file = os.path.join(self.build_dir, 'BuildScriptToolchain.cmake')

Expand Down Expand Up @@ -397,6 +411,33 @@ def generate_linux_toolchain_file(self, platform, arch):

return toolchain_file

def generate_toolchain_file_for_darwin_or_linux(self, host_target):
"""
Checks `host_target` platform and generates a new CMake tolchain file
appropriate for that target plaftorm (either Darwin or Linux). Defines
`CMAKE_C_FLAGS` and `CMAKE_CXX_FLAGS` as CMake options. Also defines
`CMAKE_TOOLCHAIN_FILE` with the path of the generated toolchain file
as a CMake option.

Returns: path to the newly generated toolchain file on the
filesystem.
"""

(platform, arch) = host_target.split('-')
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)

toolchain_file = None
if self.is_darwin_host(host_target):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this refactoring, nice catch!

toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
elif platform == "linux":
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)

return toolchain_file

def get_openbsd_toolchain_file(self):
return os.getenv('OPENBSD_USE_TOOLCHAIN_FILE')

Expand Down
15 changes: 1 addition & 14 deletions utils/swift_build_support/swift_build_support/products/zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,5 @@ def build(self, host_target):
self.cmake_options.define('SKIP_INSTALL_FILES', 'YES')
self.cmake_options.define('CMAKE_INSTALL_PREFIX', '/usr')

(platform, arch) = host_target.split('-')
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)

if host_target.startswith("macosx") or \
host_target.startswith("iphone") or \
host_target.startswith("appletv") or \
host_target.startswith("watch"):
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
elif platform == "linux":
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
self.generate_toolchain_file_for_darwin_or_linux(host_target)
self.build_with_cmake(["all"], self.args.zlib_build_variant, [])