]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/plugins/builder_base.py
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / plugins / builder_base.py
1 """
2 If the build* function is passed the compiler argument, for example, 'llvm-gcc',
3 it is passed as a make variable to the make command.  Otherwise, we check the
4 LLDB_CC environment variable; if it is defined, it is passed as a make variable
5 to the make command.
6
7 If neither the compiler keyword argument nor the LLDB_CC environment variable is
8 specified, no CC make variable is passed to the make command.  The Makefile gets
9 to define the default CC being used.
10
11 Same idea holds for LLDB_ARCH environment variable, which maps to the ARCH make
12 variable.
13 """
14
15 # System imports
16 import os
17 import platform
18 import subprocess
19 import sys
20
21 # Our imports
22 import lldbsuite.test.lldbtest as lldbtest
23 import lldbsuite.test.lldbutil as lldbutil
24 from lldbsuite.test_event import build_exception
25
26 def getArchitecture():
27     """Returns the architecture in effect the test suite is running with."""
28     return os.environ["ARCH"] if "ARCH" in os.environ else ""
29
30 def getCompiler():
31     """Returns the compiler in effect the test suite is running with."""
32     compiler = os.environ.get("CC", "clang")
33     compiler = lldbutil.which(compiler)
34     return os.path.realpath(compiler)
35
36 def getArchFlag():
37     """Returns the flag required to specify the arch"""
38     compiler = getCompiler()
39     if compiler is None:
40       return ""
41     elif "gcc" in compiler:
42       archflag = "-m"
43     elif "clang" in compiler:
44       archflag = "-arch"
45     else:
46       archflag = None
47
48     return ("ARCHFLAG=" + archflag) if archflag else ""
49
50 def getMake():
51     """Returns the name for GNU make"""
52     if platform.system() == "FreeBSD" or platform.system() == "NetBSD":
53       return "gmake"
54     else:
55       return "make"
56
57 def getArchSpec(architecture):
58     """
59     Helper function to return the key-value string to specify the architecture
60     used for the make system.
61     """
62     arch = architecture if architecture else None
63     if not arch and "ARCH" in os.environ:
64         arch = os.environ["ARCH"]
65
66     return ("ARCH=" + arch) if arch else ""
67
68 def getCCSpec(compiler):
69     """
70     Helper function to return the key-value string to specify the compiler
71     used for the make system.
72     """
73     cc = compiler if compiler else None
74     if not cc and "CC" in os.environ:
75         cc = os.environ["CC"]
76     if cc:
77         return "CC=\"%s\"" % cc
78     else:
79         return ""
80
81 def getCmdLine(d):
82     """
83     Helper function to return a properly formatted command line argument(s)
84     string used for the make system.
85     """
86
87     # If d is None or an empty mapping, just return an empty string.
88     if not d:
89         return ""
90     pattern = '%s="%s"' if "win32" in sys.platform else "%s='%s'"
91
92     def setOrAppendVariable(k, v):
93         append_vars = ["CFLAGS_EXTRAS", "LD_EXTRAS"]
94         if k in append_vars and k in os.environ:
95             v = os.environ[k] + " " + v
96         return pattern % (k, v)
97     cmdline = " ".join([setOrAppendVariable(k, v) for k, v in list(d.items())])
98
99     return cmdline
100
101
102 def runBuildCommands(commands, sender):
103     try:
104         lldbtest.system(commands, sender=sender)
105     except subprocess.CalledProcessError as called_process_error:
106         # Convert to a build-specific error.
107         # We don't do that in lldbtest.system() since that
108         # is more general purpose.
109         raise build_exception.BuildError(called_process_error)
110
111
112 def buildDefault(sender=None, architecture=None, compiler=None, dictionary=None, clean=True):
113     """Build the binaries the default way."""
114     commands = []
115     if clean:
116         commands.append([getMake(), "clean", getCmdLine(dictionary)])
117     commands.append([getMake(), getArchSpec(architecture), getCCSpec(compiler), getCmdLine(dictionary)])
118
119     runBuildCommands(commands, sender=sender)
120
121     # True signifies that we can handle building default.
122     return True
123
124 def buildDwarf(sender=None, architecture=None, compiler=None, dictionary=None, clean=True):
125     """Build the binaries with dwarf debug info."""
126     commands = []
127     if clean:
128         commands.append([getMake(), "clean", getCmdLine(dictionary)])
129     commands.append([getMake(), "MAKE_DSYM=NO", getArchSpec(architecture), getCCSpec(compiler), getCmdLine(dictionary)])
130
131     runBuildCommands(commands, sender=sender)
132     # True signifies that we can handle building dwarf.
133     return True
134
135 def buildDwo(sender=None, architecture=None, compiler=None, dictionary=None, clean=True):
136     """Build the binaries with dwarf debug info."""
137     commands = []
138     if clean:
139         commands.append([getMake(), "clean", getCmdLine(dictionary)])
140     commands.append([getMake(), "MAKE_DSYM=NO", "MAKE_DWO=YES", getArchSpec(architecture), getCCSpec(compiler), getCmdLine(dictionary)])
141
142     runBuildCommands(commands, sender=sender)
143     # True signifies that we can handle building dwo.
144     return True
145
146 def buildGModules(sender=None, architecture=None, compiler=None, dictionary=None, clean=True):
147     """Build the binaries with dwarf debug info."""
148     commands = []
149     if clean:
150         commands.append([getMake(), "clean", getCmdLine(dictionary)])
151     commands.append([getMake(), "MAKE_DSYM=NO", "MAKE_GMODULES=YES", getArchSpec(architecture), getCCSpec(compiler), getCmdLine(dictionary)])
152
153     lldbtest.system(commands, sender=sender)
154     # True signifies that we can handle building with gmodules.
155     return True
156
157 def cleanup(sender=None, dictionary=None):
158     """Perform a platform-specific cleanup after the test."""
159     #import traceback
160     #traceback.print_stack()
161     commands = []
162     if os.path.isfile("Makefile"):
163         commands.append([getMake(), "clean", getCmdLine(dictionary)])
164
165     runBuildCommands(commands, sender=sender)
166     # True signifies that we can handle cleanup.
167     return True