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
15import os
16import platform
17import lldbtest
18
19def getArchitecture():
20    """Returns the architecture in effect the test suite is running with."""
21    return os.environ["ARCH"] if "ARCH" in os.environ else ""
22
23def getCompiler():
24    """Returns the compiler in effect the test suite is running with."""
25    return os.environ["CC"] if "CC" in os.environ else "clang"
26
27def getArchFlag():
28    """Returns the flag required to specify the arch"""
29    compiler = getCompiler()
30    if compiler is None:
31      return ""
32    elif "gcc" in compiler:
33      archflag = "-m"
34    elif "clang" in compiler:
35      archflag = "-arch "
36    else:
37      archflag = None
38
39    return (" ARCHFLAG=" + archflag) if archflag else ""
40
41def getMake():
42    """Returns the name for GNU make"""
43    if platform.system() == "FreeBSD":
44      return "gmake "
45    else:
46      return "make "
47
48def getArchSpec(architecture):
49    """
50    Helper function to return the key-value string to specify the architecture
51    used for the make system.
52    """
53    arch = architecture if architecture else None
54    if not arch and "ARCH" in os.environ:
55        arch = os.environ["ARCH"]
56
57    # Note the leading space character.
58    return (" ARCH=" + arch) if arch else ""
59
60def getCCSpec(compiler):
61    """
62    Helper function to return the key-value string to specify the compiler
63    used for the make system.
64    """
65    cc = compiler if compiler else None
66    if not cc and "CC" in os.environ:
67        cc = os.environ["CC"]
68
69    # Note the leading space character.
70    return (" CC=" + cc) if cc else ""
71
72def getCmdLine(d):
73    """
74    Helper function to return a properly formatted command line argument(s)
75    string used for the make system.
76    """
77
78    # If d is None or an empty mapping, just return an empty string.
79    if not d:
80        return ""
81
82    cmdline = " ".join(["%s='%s'" % (k, v) for k, v in d.items()])
83
84    # Note the leading space character.
85    return " " + cmdline
86
87
88def buildDefault(sender=None, architecture=None, compiler=None, dictionary=None, clean=True):
89    """Build the binaries the default way."""
90    if clean:
91        lldbtest.system(["/bin/sh", "-c",
92                         getMake() + "clean" + getCmdLine(dictionary) + ";"
93                         + getMake()
94                         + getArchSpec(architecture) + getCCSpec(compiler)
95                         + getCmdLine(dictionary)],
96                        sender=sender)
97    else:
98        lldbtest.system(["/bin/sh", "-c",
99                         getMake() + getArchSpec(architecture) + getCCSpec(compiler)
100                         + getCmdLine(dictionary)],
101                        sender=sender)
102
103    # True signifies that we can handle building default.
104    return True
105
106def buildDwarf(sender=None, architecture=None, compiler=None, dictionary=None, clean=True):
107    """Build the binaries with dwarf debug info."""
108    if clean:
109        lldbtest.system(["/bin/sh", "-c",
110                         getMake() + "clean" + getCmdLine(dictionary)
111                         + ";" + getMake() + "MAKE_DSYM=NO"
112                         + getArchSpec(architecture) + getCCSpec(compiler)
113                         + getCmdLine(dictionary)],
114                        sender=sender)
115    else:
116        lldbtest.system(["/bin/sh", "-c",
117                         getMake() + "MAKE_DSYM=NO"
118                         + getArchSpec(architecture) + getCCSpec(compiler)
119                         + getCmdLine(dictionary)],
120                        sender=sender)
121
122    # True signifies that we can handle building dwarf.
123    return True
124
125def cleanup(sender=None, dictionary=None):
126    """Perform a platform-specific cleanup after the test."""
127    #import traceback
128    #traceback.print_stack()
129    if os.path.isfile("Makefile"):
130        lldbtest.system(["/bin/sh", "-c",
131                         getMake() + "clean" + getCmdLine(dictionary)],
132                        sender=sender)
133
134    # True signifies that we can handle cleanup.
135    return True
136