summaryrefslogtreecommitdiffstats
path: root/waftools/detections/compiler_swift.py
blob: 2e24362ecfe278b1f6db1a1cdd50d33bf0a3adda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from waflib import Utils

def __run(cmd):
    try:
        output = Utils.subprocess.check_output(cmd, universal_newlines=True)
        return output.strip()
    except Exception:
        return ""

def __add_swift_flags(ctx):
    ctx.env.SWIFT_FLAGS = ('-frontend -c -sdk %s -enable-objc-interop'
                           ' -emit-objc-header -parse-as-library'
                           ' -target x86_64-apple-macosx10.10') % (ctx.env.MACOS_SDK)
    swift_version = __run([ctx.env.SWIFT, '-version']).split(' ')[3].split('.')[:2]
    major, minor = [int(n) for n in swift_version]

    # the -swift-version parameter is only supported on swift 3.1 and newer
    if major >= 3 and minor >= 1 or major >= 4:
        ctx.env.SWIFT_FLAGS += ' -swift-version 3'

    if ctx.is_optimization():
        ctx.env.SWIFT_FLAGS += ' -O'

def __add_swift_library_linking_flags(ctx, swift_library):
    ctx.env.append_value('LINKFLAGS', [
        '-L%s' % swift_library,
        '-Xlinker', '-force_load_swift_libs', '-lc++',
    ])

def __find_swift_library(ctx):
    swift_library_paths = [
        'Toolchains/XcodeDefault.xctoolchain/usr/lib/swift_static/macosx',
        'usr/lib/swift_static/macosx'
    ]
    dev_path = __run(['xcode-select', '-p'])[1:]

    ctx.start_msg('Checking for Swift Library')
    for path in swift_library_paths:
        swift_library = ctx.root.find_dir([dev_path, path])
        if swift_library is not None:
            ctx.end_msg(swift_library)
            __add_swift_library_linking_flags(ctx, swift_library)
            return
    ctx.end_msg(False)

def __find_macos_sdk(ctx):
    ctx.start_msg('Checking for macOS SDK')
    sdk = __run(['xcrun', '--sdk', 'macosx', '--show-sdk-path'])
    if sdk:
        ctx.end_msg(sdk)
        ctx.env.MACOS_SDK = sdk
    else:
        ctx.end_msg(False)

def __find_swift_compiler(ctx):
    ctx.start_msg('Checking for swift (Swift compiler)')
    swift = __run(['xcrun', '-find', 'swift'])
    if swift:
        ctx.end_msg(swift)
        ctx.env.SWIFT = swift
        __add_swift_flags(ctx)
        __find_swift_library(ctx)
    else:
        ctx.end_msg(False)

def configure(ctx):
    if ctx.env.DEST_OS == "darwin":
        __find_macos_sdk(ctx)
        __find_swift_compiler(ctx)