Move target definition into individual files.
[riscv-tests.git] / debug / targets.py
1 import importlib
2 import os.path
3 import sys
4 import tempfile
5
6 import testlib
7
8 class Target(object):
9 # pylint: disable=too-many-instance-attributes
10
11 # Name of the target. Defaults to the name of the class.
12 name = None
13
14 # XLEN of the target. May be overridden with --32 or --64 command line
15 # options.
16 xlen = 0
17
18 # GDB remotetimeout setting.
19 timeout_sec = 2
20
21 # Path to OpenOCD configuration file relative to the .py file where the
22 # target is defined. Defaults to <name>.cfg.
23 openocd_config_path = None
24
25 # Path to linker script relative to the .py file where the target is
26 # defined. Defaults to <name>.lds.
27 link_script_path = None
28
29 # Will be autodetected (by running ExamineTarget) if left unset. Set to
30 # save a little time.
31 misa = None
32
33 # Internal variables:
34 directory = None
35 temporary_files = []
36 temporary_binary = None
37
38 def __init__(self, path, parsed):
39 # Path to module.
40 self.path = path
41 self.directory = os.path.dirname(path)
42 self.server_cmd = parsed.server_cmd
43 self.sim_cmd = parsed.sim_cmd
44 self.isolate = parsed.isolate
45 if not self.name:
46 self.name = type(self).__name__
47 # Default OpenOCD config file to <name>.cfg
48 if not self.openocd_config_path:
49 self.openocd_config_path = "%s.cfg" % self.name
50 self.openocd_config_path = os.path.join(self.directory,
51 self.openocd_config_path)
52 # Default link script to <name>.lds
53 if not self.link_script_path:
54 self.link_script_path = "%s.lds" % self.name
55 self.link_script_path = os.path.join(self.directory,
56 self.link_script_path)
57
58 def create(self):
59 """Create the target out of thin air, eg. start a simulator."""
60 pass
61
62 def server(self):
63 """Start the debug server that gdb connects to, eg. OpenOCD."""
64 return testlib.Openocd(server_cmd=self.server_cmd,
65 config=self.openocd_config_path)
66
67 def compile(self, *sources):
68 binary_name = "%s_%s-%d" % (
69 self.name,
70 os.path.basename(os.path.splitext(sources[0])[0]),
71 self.xlen)
72 if self.isolate:
73 self.temporary_binary = tempfile.NamedTemporaryFile(
74 prefix=binary_name + "_")
75 binary_name = self.temporary_binary.name
76 Target.temporary_files.append(self.temporary_binary)
77 march = "rv%dima" % self.xlen
78 for letter in "fdc":
79 if self.extensionSupported(letter):
80 march += letter
81 testlib.compile(sources +
82 ("programs/entry.S", "programs/init.c",
83 "-I", "../env",
84 "-march=%s" % march,
85 "-T", self.link_script_path,
86 "-nostartfiles",
87 "-mcmodel=medany",
88 "-DXLEN=%d" % self.xlen,
89 "-o", binary_name),
90 xlen=self.xlen)
91 return binary_name
92
93 def extensionSupported(self, letter):
94 # target.misa is set by testlib.ExamineTarget
95 if self.misa:
96 return self.misa & (1 << (ord(letter.upper()) - ord('A')))
97 else:
98 return False
99
100 def add_target_options(parser):
101 parser.add_argument("target", help=".py file that contains definition for "
102 "the target to test with.")
103 parser.add_argument("--sim_cmd",
104 help="The command to use to start the actual target (e.g. "
105 "simulation)", default="spike")
106 parser.add_argument("--server_cmd",
107 help="The command to use to start the debug server (e.g. OpenOCD)")
108
109 xlen_group = parser.add_mutually_exclusive_group()
110 xlen_group.add_argument("--32", action="store_const", const=32, dest="xlen",
111 help="Force the target to be 32-bit.")
112 xlen_group.add_argument("--64", action="store_const", const=64, dest="xlen",
113 help="Force the target to be 64-bit.")
114
115 parser.add_argument("--isolate", action="store_true",
116 help="Try to run in such a way that multiple instances can run at "
117 "the same time. This may make it harder to debug a failure if it "
118 "does occur.")
119
120 def target(parsed):
121 directory = os.path.dirname(parsed.target)
122 filename = os.path.basename(parsed.target)
123 module_name = os.path.splitext(filename)[0]
124
125 sys.path.append(directory)
126 module = importlib.import_module(module_name)
127 found = []
128 for name in dir(module):
129 definition = getattr(module, name)
130 if type(definition) == type and issubclass(definition, Target):
131 found.append(definition)
132 assert len(found) == 1, "%s does not define exactly one subclass of " \
133 "targets.Target" % parsed.target
134
135 return found[0](parsed.target, parsed)