Skip to content

Commit 26c62b6

Browse files
committed
build: enable handling of alpine-linux-musl triple
Currently, when building LLVM/clang on Alpine Linux, CMake toolchain file specifies incorrect `<cpu_arch>-unknown-linux-musl` target, which makes the build immediately fail. Correct target that allows building on Alpine should be specified as `<cpu_arch>-alpine-linux-musl`, with `<cpu_arch>` replaced with respective CPU platform, e.g. `aarch64`.
1 parent e5e9827 commit 26c62b6

File tree

1 file changed

+22
-7
lines changed
  • utils/swift_build_support/swift_build_support/products

1 file changed

+22
-7
lines changed

utils/swift_build_support/swift_build_support/products/product.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def generate_darwin_toolchain_file(self, platform, arch):
339339

340340
return toolchain_file
341341

342-
def get_linux_abi(self, arch):
342+
def get_linux_target_components(self, arch):
343343
# Map tuples of (platform, arch) to ABI
344344
#
345345
# E.x.: Hard ABI or Soft ABI for Linux map to gnueabihf
@@ -348,22 +348,37 @@ def get_linux_abi(self, arch):
348348
'armv7': ('arm', 'gnueabihf')
349349
}
350350

351-
# Default is just arch, gnu
352-
sysroot_arch, abi = arch_platform_to_abi.get(arch, (arch, 'gnu'))
353-
return sysroot_arch, abi
351+
abi = 'gnu'
352+
vendor = 'unknown'
353+
354+
try:
355+
output = shell.capture(["clang", "--version"])
356+
357+
# clang can't handle default `*-unknown-linux-*` components on Alpine,
358+
# it needs special handling to propagate `vendor` and `abi` intact
359+
if 'alpine-linux-musl' in output:
360+
vendor = 'alpine'
361+
abi = 'musl'
362+
363+
sysroot_arch, abi = arch_platform_to_abi.get(arch, (arch, abi))
364+
365+
except BaseException:
366+
# Default is just arch, gnu
367+
sysroot_arch, abi = arch_platform_to_abi.get(arch, (arch, abi))
368+
return sysroot_arch, vendor, abi
354369

355370
def get_linux_sysroot(self, platform, arch):
356371
if not self.is_cross_compile_target('{}-{}'.format(platform, arch)):
357372
return None
358-
sysroot_arch, abi = self.get_linux_abi(arch)
373+
sysroot_arch, abi = self.get_linux_target_components(arch)
359374
# $ARCH-$PLATFORM-$ABI
360375
# E.x.: aarch64-linux-gnu
361376
sysroot_dirname = '{}-{}-{}'.format(sysroot_arch, platform, abi)
362377
return os.path.join(os.sep, 'usr', sysroot_dirname)
363378

364379
def get_linux_target(self, platform, arch):
365-
sysroot_arch, abi = self.get_linux_abi(arch)
366-
return '{}-unknown-linux-{}'.format(sysroot_arch, abi)
380+
sysroot_arch, vendor, abi = self.get_linux_target_components(arch)
381+
return '{}-{}-linux-{}'.format(sysroot_arch, vendor, abi)
367382

368383
def generate_linux_toolchain_file(self, platform, arch):
369384
"""

0 commit comments

Comments
 (0)