Skip to content

Commit 1b8b407

Browse files
committed
swift: Fix duplicate SDK include paths causing a compile error
Some dependencies can bring include paths pointing to older macOS SDK's. In this case, it was libffi pointing to SDK from 12.0. When the Foundation framework is imported in Swift, swiftc attempts to import the FFI module from the most recent version of the SDK, which causes a compilation error because of a conflicting definition between the two SDK versions. SwiftPM also had this problem: swiftlang/swift-package-manager#6772 The solution on our side is a simplified version of what SwiftPM did. Let's naively look for .sdk paths in the compile args of our dependencies and replace them with the most recent one. I included a test which is confirmed to fail without the workaround added in this patch. This was not tested on anything else than macOS, but I don't expect it to make the situation worse in any case.
1 parent dc1b4be commit 1b8b407

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

mesonbuild/compilers/swift.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .compilers import Compiler, clike_debug_args
1212

1313
if T.TYPE_CHECKING:
14+
from ..dependencies import Dependency
1415
from ..envconfig import MachineInfo
1516
from ..environment import Environment
1617
from ..linkers.linkers import DynamicLinker
@@ -55,6 +56,24 @@ def get_werror_args(self) -> T.List[str]:
5556
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
5657
return ['-emit-dependencies']
5758

59+
def get_dependency_compile_args(self, dep: Dependency) -> T.List[str]:
60+
args = dep.get_compile_args()
61+
# Some deps might sneak in a hardcoded path to an older macOS SDK, which can
62+
# cause compilation errors. Let's replace all .sdk paths with the current one.
63+
# SwiftPM does it this way: https://github.com/swiftlang/swift-package-manager/pull/6772
64+
# Not tested on anything else than macOS for now.
65+
if not self.info.is_darwin():
66+
return args
67+
suffix = '.sdk'
68+
sdk_path = subprocess.check_output(['xcrun', '--show-sdk-path'],
69+
universal_newlines=True, encoding='utf-8').strip()
70+
for idx, arg in enumerate(args):
71+
if suffix not in arg:
72+
continue
73+
sdk_end = arg.index(suffix) + len(suffix)
74+
args[idx] = arg[:2] + sdk_path + arg[sdk_end:]
75+
return args
76+
5877
def depfile_for_object(self, objfile: str) -> T.Optional[str]:
5978
return os.path.splitext(objfile)[0] + '.' + self.get_depfile_suffix()
6079

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// This import is needed for swiftc to implictly import the FFI module
2+
// which will in turn conflict with the dependency's include path and error out
3+
// if we don't manually replace all SDK paths with the newest one.
4+
import Foundation
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
project('swift sdk include dir test', 'swift')
2+
3+
bar_dep = declare_dependency(
4+
# Simulates including 'libffi' from brew as a dep via pkg-config
5+
# Without a workaround that replaces all SDK paths with the most recent one,
6+
# a compile error will occur due to conflicting definitions of the FFI module.
7+
compile_args: '-I/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/usr/include/ffi',
8+
)
9+
10+
foo = static_library('foo', 'foo.swift',
11+
dependencies: [bar_dep],
12+
)

0 commit comments

Comments
 (0)