1"""
2If the build* function is passed the compiler argument, for example, 'llvm-gcc',
3it is passed as a make variable to the make command.  Otherwise, we check the
4LLDB_CC environment variable; if it is defined, it is passed as a make variable
5to the make command.
6
7If neither the compiler keyword argument nor the LLDB_CC environment variable is
8specified, no CC make variable is passed to the make command.  The Makefile gets
9to define the default CC being used.
10
11Same idea holds for LLDB_ARCH environment variable, which maps to the ARCH make
12variable.
13"""
14
15# System imports
16import os
17import platform
18import subprocess
19import sys
20
21# Our imports
22import lldbsuite.test.lldbtest as lldbtest
23import lldbsuite.test.lldbutil as lldbutil
24from lldbsuite.test_event import build_exception
25
26
27def getArchitecture():
28    """Returns the architecture in effect the test suite is running with."""
29    return os.environ["ARCH"] if "ARCH" in os.environ else ""
30
31
32def getCompiler():
33    """Returns the compiler in effect the test suite is running with."""
34    compiler = os.environ.get("CC", "clang")
35    compiler = lldbutil.which(compiler)
36    return os.path.realpath(compiler)
37
38
39def getArchFlag():
40    """Returns the flag required to specify the arch"""
41    compiler = getCompiler()
42    if compiler is None:
43        return ""
44    elif "gcc" in compiler:
45        archflag = "-m"
46    elif "clang" in compiler:
47        archflag = "-arch"
48    else:
49        archflag = None
50
51    return ("ARCHFLAG=" + archflag) if archflag else ""
52
53def getMake(test_subdir, test_name):
54    """Returns the invocation for GNU make.
55       The first argument is a tuple of the relative path to the testcase
56       and its filename stem."""
57    if platform.system() == "FreeBSD" or platform.system() == "NetBSD":
58        make = "gmake"
59    else:
60        make = "make"
61
62    # Construct the base make invocation.
63    lldb_test = os.environ["LLDB_TEST"]
64    lldb_build = os.environ["LLDB_BUILD"]
65    if not (lldb_test and lldb_build and test_subdir and test_name and
66            (not os.path.isabs(test_subdir))):
67        raise Exception("Could not derive test directories")
68    build_dir = os.path.join(lldb_build, test_subdir, test_name)
69    src_dir = os.path.join(lldb_test, test_subdir)
70    # This is a bit of a hack to make inline testcases work.
71    makefile = os.path.join(src_dir, "Makefile")
72    if not os.path.isfile(makefile):
73        makefile = os.path.join(build_dir, "Makefile")
74    return [make,
75            "VPATH="+src_dir,
76            "-C", build_dir,
77            "-I", src_dir,
78            "-I", os.path.join(lldb_test, "make"),
79            "-f", makefile]
80
81
82def getArchSpec(architecture):
83    """
84    Helper function to return the key-value string to specify the architecture
85    used for the make system.
86    """
87    arch = architecture if architecture else None
88    if not arch and "ARCH" in os.environ:
89        arch = os.environ["ARCH"]
90
91    return ("ARCH=" + arch) if arch else ""
92
93
94def getCCSpec(compiler):
95    """
96    Helper function to return the key-value string to specify the compiler
97    used for the make system.
98    """
99    cc = compiler if compiler else None
100    if not cc and "CC" in os.environ:
101        cc = os.environ["CC"]
102    if cc:
103        return "CC=\"%s\"" % cc
104    else:
105        return ""
106
107def getDsymutilSpec():
108    """
109    Helper function to return the key-value string to specify the dsymutil
110    used for the make system.
111    """
112    if "DSYMUTIL" in os.environ:
113        return "DSYMUTIL={}".format(os.environ["DSYMUTIL"])
114    return "";
115
116def getSDKRootSpec():
117    """
118    Helper function to return the key-value string to specify the SDK root
119    used for the make system.
120    """
121    if "SDKROOT" in os.environ:
122        return "SDKROOT={}".format(os.environ["SDKROOT"])
123    return "";
124
125def getModuleCacheSpec():
126    """
127    Helper function to return the key-value string to specify the clang
128    module cache used for the make system.
129    """
130    if "CLANG_MODULE_CACHE_DIR" in os.environ:
131        return "CLANG_MODULE_CACHE_DIR={}".format(
132            os.environ["CLANG_MODULE_CACHE_DIR"])
133    return "";
134
135def getCmdLine(d):
136    """
137    Helper function to return a properly formatted command line argument(s)
138    string used for the make system.
139    """
140
141    # If d is None or an empty mapping, just return an empty string.
142    if not d:
143        return ""
144    pattern = '%s="%s"' if "win32" in sys.platform else "%s='%s'"
145
146    def setOrAppendVariable(k, v):
147        append_vars = ["CFLAGS", "CFLAGS_EXTRAS", "LD_EXTRAS"]
148        if k in append_vars and k in os.environ:
149            v = os.environ[k] + " " + v
150        return pattern % (k, v)
151    cmdline = " ".join([setOrAppendVariable(k, v) for k, v in list(d.items())])
152
153    return cmdline
154
155
156def runBuildCommands(commands, sender):
157    try:
158        lldbtest.system(commands, sender=sender)
159    except subprocess.CalledProcessError as called_process_error:
160        # Convert to a build-specific error.
161        # We don't do that in lldbtest.system() since that
162        # is more general purpose.
163        raise build_exception.BuildError(called_process_error)
164
165
166def buildDefault(
167        sender=None,
168        architecture=None,
169        compiler=None,
170        dictionary=None,
171        testdir=None,
172        testname=None):
173    """Build the binaries the default way."""
174    commands = []
175    commands.append(getMake(testdir, testname) +
176                    ["all",
177                     getArchSpec(architecture),
178                     getCCSpec(compiler),
179                     getDsymutilSpec(),
180                     getSDKRootSpec(),
181                     getModuleCacheSpec(),
182                     getCmdLine(dictionary)])
183
184    runBuildCommands(commands, sender=sender)
185
186    # True signifies that we can handle building default.
187    return True
188
189
190def buildDwarf(
191        sender=None,
192        architecture=None,
193        compiler=None,
194        dictionary=None,
195        testdir=None,
196        testname=None):
197    """Build the binaries with dwarf debug info."""
198    commands = []
199    commands.append(getMake(testdir, testname) +
200                    ["MAKE_DSYM=NO",
201                     getArchSpec(architecture),
202                     getCCSpec(compiler),
203                     getDsymutilSpec(),
204                     getSDKRootSpec(),
205                     getModuleCacheSpec(),
206                     getCmdLine(dictionary)])
207
208    runBuildCommands(commands, sender=sender)
209    # True signifies that we can handle building dwarf.
210    return True
211
212
213def buildDwo(
214        sender=None,
215        architecture=None,
216        compiler=None,
217        dictionary=None,
218        testdir=None,
219        testname=None):
220    """Build the binaries with dwarf debug info."""
221    commands = []
222    commands.append(getMake(testdir, testname) +
223                    ["MAKE_DSYM=NO",
224                     "MAKE_DWO=YES",
225                     getArchSpec(architecture),
226                     getCCSpec(compiler),
227                     getDsymutilSpec(),
228                     getSDKRootSpec(),
229                     getModuleCacheSpec(),
230                     getCmdLine(dictionary)])
231
232    runBuildCommands(commands, sender=sender)
233    # True signifies that we can handle building dwo.
234    return True
235
236
237def buildGModules(
238        sender=None,
239        architecture=None,
240        compiler=None,
241        dictionary=None,
242        testdir=None,
243        testname=None):
244    """Build the binaries with dwarf debug info."""
245    commands = []
246    commands.append(getMake(testdir, testname) +
247                    ["MAKE_DSYM=NO",
248                     "MAKE_GMODULES=YES",
249                     getArchSpec(architecture),
250                     getCCSpec(compiler),
251                     getDsymutilSpec(),
252                     getSDKRootSpec(),
253                     getModuleCacheSpec(),
254                     getCmdLine(dictionary)])
255
256    lldbtest.system(commands, sender=sender)
257    # True signifies that we can handle building with gmodules.
258    return True
259
260
261def cleanup(sender=None, dictionary=None):
262    """Perform a platform-specific cleanup after the test."""
263    return True
264