xref: /llvm-project/lldb/packages/Python/lldbsuite/test/builders/darwin.py (revision 0f12cf7ebaaf9f2a52be3baef7d25562a5ac15cb)
1import re
2import os
3import subprocess
4
5from .builder import Builder
6from lldbsuite.test import configuration
7import lldbsuite.test.lldbutil as lldbutil
8
9REMOTE_PLATFORM_NAME_RE = re.compile(r"^remote-(.+)$")
10SIMULATOR_PLATFORM_RE = re.compile(r"^(.+)-simulator$")
11
12
13def get_os_env_from_platform(platform):
14    match = REMOTE_PLATFORM_NAME_RE.match(platform)
15    if match:
16        return match.group(1), ""
17    match = SIMULATOR_PLATFORM_RE.match(platform)
18    if match:
19        return match.group(1), "simulator"
20    return None, None
21
22
23def get_os_from_sdk(sdk):
24    return sdk[:sdk.find('.')], ""
25
26
27def get_os_and_env():
28    if configuration.lldb_platform_name:
29        return get_os_env_from_platform(configuration.lldb_platform_name)
30    if configuration.apple_sdk:
31        return get_os_from_sdk(configuration.apple_sdk)
32    return None, None
33
34
35def get_triple():
36    # Construct the vendor component.
37    vendor = "apple"
38
39    # Construct the os component.
40    os, env = get_os_and_env()
41    if os is None or env is None:
42        return None, None, None, None
43
44    # Get the SDK from the os and env.
45    sdk = lldbutil.get_xcode_sdk(os, env)
46    if sdk is None:
47        return None, None, None, None
48
49    # Get the version from the SDK.
50    version = lldbutil.get_xcode_sdk_version(sdk)
51    if version is None:
52        return None, None, None, None
53
54    return vendor, os, version, env
55
56
57class BuilderDarwin(Builder):
58    def getTriple(self, arch):
59        vendor, os, version, env = get_triple()
60        components = [arch, vendor, os, version, env]
61        if None in components:
62            return None
63        return '-'.join(components)
64
65    def getExtraMakeArgs(self):
66        """
67        Helper function to return extra argumentsfor the make system. This
68        method is meant to be overridden by platform specific builders.
69        """
70        args = dict()
71
72        if configuration.dsymutil:
73            args['DSYMUTIL'] = configuration.dsymutil
74
75        operating_system, env = get_os_and_env()
76        if operating_system and operating_system != "macosx":
77            builder_dir = os.path.dirname(os.path.abspath(__file__))
78            test_dir = os.path.dirname(builder_dir)
79            if env == "simulator":
80              entitlements_file = 'entitlements-simulator.plist'
81            else:
82              entitlements_file = 'entitlements.plist'
83            entitlements = os.path.join(test_dir, 'make', entitlements_file)
84            args['CODESIGN'] = 'codesign --entitlements {}'.format(
85                entitlements)
86        else:
87            args['CODESIGN'] = 'codesign'
88
89        # Return extra args as a formatted string.
90        return ['{}={}'.format(key, value) for key, value in args.items()]
91
92    def getArchCFlags(self, arch):
93        """Returns the ARCH_CFLAGS for the make system."""
94        # Get the triple components.
95        vendor, os, version, env = get_triple()
96        if vendor is None or os is None or version is None or env is None:
97            return []
98
99        # Construct the triple from its components.
100        triple = '-'.join([arch, vendor, os, version, env])
101
102        # Construct min version argument
103        version_min = ""
104        if env == "simulator":
105            version_min = "-m{}-simulator-version-min={}".format(os, version)
106        else:
107            version_min = "-m{}-version-min={}".format(os, version)
108
109        return ["ARCH_CFLAGS=-target {} {}".format(triple, version_min)]
110
111    def _getDebugInfoArgs(self, debug_info):
112        if debug_info == "dsym":
113            return ["MAKE_DSYM=YES"]
114        return super(BuilderDarwin, self)._getDebugInfoArgs(debug_info)
115