Skip to content

Commit fe15893

Browse files
gottesmmDougGregor
authored andcommitted
[cmark] Start always cross compiling cmark on Linux.
(cherry picked from commit 2333cbc)
1 parent cfd87f4 commit fe15893

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

utils/swift_build_support/swift_build_support/products/cmark.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,19 @@ def build(self, host_target):
5252

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

55+
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
56+
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
57+
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
58+
5559
if host_target.startswith("macosx") or \
5660
host_target.startswith("iphone") or \
5761
host_target.startswith("appletv") or \
5862
host_target.startswith("watch"):
59-
60-
common_c_flags = ' '.join(self.common_cross_c_flags(platform, arch))
61-
62-
self.cmake_options.define('CMAKE_C_FLAGS', common_c_flags)
63-
self.cmake_options.define('CMAKE_CXX_FLAGS', common_c_flags)
6463
toolchain_file = self.generate_darwin_toolchain_file(platform, arch)
6564
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
65+
elif platform == "linux":
66+
toolchain_file = self.generate_linux_toolchain_file(platform, arch)
67+
self.cmake_options.define('CMAKE_TOOLCHAIN_FILE:PATH', toolchain_file)
6668

6769
self.build_with_cmake(["all"], self.args.cmark_build_variant, [])
6870

utils/swift_build_support/swift_build_support/products/product.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,71 @@ def generate_darwin_toolchain_file(self, platform, arch):
280280

281281
return toolchain_file
282282

283+
def get_linux_abi(self, arch):
284+
# Map tuples of (platform, arch) to ABI
285+
#
286+
# E.x.: Hard ABI or Soft ABI for Linux map to gnueabihf
287+
arch_platform_to_abi = {
288+
# For now always map to hard float ABI.
289+
'armv7': ('arm', 'gnueabihf')
290+
}
291+
292+
# Default is just arch, gnu
293+
sysroot_arch, abi = arch_platform_to_abi.get(arch, (arch, 'gnu'))
294+
return sysroot_arch, abi
295+
296+
def get_linux_sysroot(self, platform, arch):
297+
if not self.is_cross_compile_target('{}-{}'.format(platform, arch)):
298+
return None
299+
sysroot_arch, abi = self.get_linux_abi(arch)
300+
# $ARCH-$PLATFORM-$ABI
301+
# E.x.: aarch64-linux-gnu
302+
sysroot_dirname = '{}-{}-{}'.format(sysroot_arch, platform, abi)
303+
return os.path.join(os.sep, 'usr', sysroot_dirname)
304+
305+
def get_linux_target(self, platform, arch):
306+
sysroot_arch, abi = self.get_linux_abi(arch)
307+
return '{}-unknown-linux-{}'.format(sysroot_arch, abi)
308+
309+
def generate_linux_toolchain_file(self, platform, arch):
310+
shell.makedirs(self.build_dir)
311+
toolchain_file = os.path.join(self.build_dir, 'BuildScriptToolchain.cmake')
312+
313+
toolchain_args = {}
314+
315+
toolchain_args['CMAKE_SYSTEM_NAME'] = 'Linux'
316+
toolchain_args['CMAKE_SYSTEM_PROCESSOR'] = arch
317+
318+
# We only set the actual sysroot if we are actually cross
319+
# compiling. This is important since otherwise cmake seems to change the
320+
# RUNPATH to be a relative rather than an absolute path, breaking
321+
# certain cmark tests (and maybe others).
322+
maybe_sysroot = self.get_linux_sysroot(platform, arch)
323+
if maybe_sysroot is not None:
324+
toolchain_args['CMAKE_SYSROOT'] = maybe_sysroot
325+
326+
target = self.get_linux_target(platform, arch)
327+
if self.toolchain.cc.endswith('clang'):
328+
toolchain_args['CMAKE_C_COMPILER_TARGET'] = target
329+
if self.toolchain.cxx.endswith('clang++'):
330+
toolchain_args['CMAKE_CXX_COMPILER_TARGET'] = target
331+
# Swift always supports cross compiling.
332+
toolchain_args['CMAKE_Swift_COMPILER_TARGET'] = target
333+
toolchain_args['CMAKE_FIND_ROOT_PATH_MODE_PROGRAM'] = 'NEVER'
334+
toolchain_args['CMAKE_FIND_ROOT_PATH_MODE_LIBRARY'] = 'ONLY'
335+
toolchain_args['CMAKE_FIND_ROOT_PATH_MODE_INCLUDE'] = 'ONLY'
336+
toolchain_args['CMAKE_FIND_ROOT_PATH_MODE_PACKAGE'] = 'ONLY'
337+
338+
# Sort by the key so that we always produce the same toolchain file
339+
data = sorted(toolchain_args.items(), key=lambda x: x[0])
340+
if not self.args.dry_run:
341+
with open(toolchain_file, 'w') as f:
342+
f.writelines("set({} {})\n".format(k, v) for k, v in data)
343+
else:
344+
print("DRY_RUN! Writing Toolchain file to path: {}".format(toolchain_file))
345+
346+
return toolchain_file
347+
283348
def common_cross_c_flags(self, platform, arch):
284349
cross_flags = []
285350

0 commit comments

Comments
 (0)