Skip to content

Commit

Permalink
Add swift.get_library_path
Browse files Browse the repository at this point in the history
  • Loading branch information
2xsaiko committed Feb 11, 2025
1 parent 8cbe989 commit 6630971
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
25 changes: 25 additions & 0 deletions mesonbuild/compilers/swift.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import annotations

import json
import re
import subprocess, os.path
import typing as T
Expand All @@ -29,6 +30,27 @@
's': ['-O'],
}


def _get_swift_library_paths(exelist: T.List[str], info: MachineInfo, static: bool) -> T.List[str]:
try:
command = [*exelist, '-print-target-info']
if static:
command += ['-static-stdlib']
s = subprocess.check_output(command, universal_newlines=True,
encoding='utf-8', stderr=subprocess.STDOUT)
data = json.loads(s)
paths = [
*data['paths']['runtimeLibraryPaths'],
*data['paths']['runtimeLibraryImportPaths'],
*([data['paths']['sdkPath']] if info.is_darwin() else []),
]
return paths
except subprocess.CalledProcessError as e:
raise MesonException(f'Failed to get Swift target info: {e.output}')
except KeyError:
raise MesonException('Failed to extract Swift library path')


class SwiftCompiler(Compiler):

LINKER_PREFIX = ['-Xlinker']
Expand All @@ -54,6 +76,9 @@ def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoic
mlog.error('xcrun not found. Install Xcode to compile Swift code.')
raise MesonException('Could not detect Xcode. Please install it to compile Swift code.')

self.dynamic_library_path: T.List[str] = _get_swift_library_paths(exelist, info, False)
self.static_library_path: T.List[str] = _get_swift_library_paths(exelist, info, True)

def get_pic_args(self) -> T.List[str]:
return []

Expand Down
7 changes: 7 additions & 0 deletions mesonbuild/modules/swift.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ def __init__(self, interpreter: Interpreter):
self.swiftc = None

self.methods.update({
'get_library_path': self.get_library_path,
'generate_cpp_header': self.generate_cpp_header,
})

@typed_pos_args('swift.get_library_path')
@typed_kwargs('swift.get_library_path')
def get_library_path(self, state: ModuleState, args, kwargs) -> ModuleReturnValue:
paths = [*self.swiftc.dynamic_library_path, *self.swiftc.static_library_path]
return ModuleReturnValue(paths, [])

@typed_pos_args('swift.generate_cpp_header', build.BuildTarget)
@typed_kwargs('swift.generate_cpp_header')
def generate_cpp_header(self, state: ModuleState, args: T.Tuple[build.BuildTarget], kwargs) -> ModuleReturnValue:
Expand Down

0 comments on commit 6630971

Please sign in to comment.