blob: f0300f94641667b9b6df48d21006678c58330162 [file] [log] [blame]
Brian Silvermana29ebf92014-04-23 13:08:49 -05001#!/usr/bin/python3
2
Brian Silvermana29ebf92014-04-23 13:08:49 -05003import sys
4import subprocess
5import re
6import os
7import os.path
8import string
9import shutil
10import errno
Brian Silvermanc3740c32014-05-04 12:42:47 -070011import queue
12import threading
Brian Silvermanf2bbe092014-05-13 16:55:03 -070013import pty
Brian Silverman6bca4722014-05-20 17:02:49 -070014import signal
Brian Silvermanc3740c32014-05-04 12:42:47 -070015
16class TestThread(threading.Thread):
Brian Silvermane6bada62014-05-04 16:16:54 -070017 """Runs 1 test and keeps track of its current state.
18
19 A TestThread is either waiting to start the test, actually running it, done,
20 running it, or stopped. The first 3 always happen in that order and can
21 change to stopped at any time.
22
23 It will finish (ie join() will return) once the process has exited, at which
24 point accessing process to see the status is OK.
25
26 Attributes:
27 executable: The file path of the executable to run.
Brian Silvermanb9e89602014-06-27 14:21:08 -050028 args: A tuple of arguments to give the executable.
Brian Silvermane6bada62014-05-04 16:16:54 -070029 env: The environment variables to set.
30 done_queue: A queue.Queue to place self on once done running the test.
31 start_semaphore: A threading.Semaphore to wait on before starting.
32 process_lock: A lock around process.
33 process: The currently executing test process or None. Synchronized by
34 process_lock.
35 stopped: True if we're stopped.
Brian Silverman730bb012014-06-08 13:05:20 -070036 output: A queue of lines of output from the test.
Brian Silvermane6bada62014-05-04 16:16:54 -070037 """
Brian Silverman730bb012014-06-08 13:05:20 -070038
39 class OutputCopier(threading.Thread):
40 """Copies the output of a test from its output pty into a queue.
41
42 This is necessary because otherwise everything locks up if the test writes
43 too much output and fills up the pty's buffer.
44 """
45
46 def __init__(self, name, fd, queue):
47 super(TestThread.OutputCopier, self).__init__(
48 name=(name + '.OutputCopier'))
49
50 self.fd = fd
51 self.queue = queue
52
53 def run(self):
54 with os.fdopen(self.fd) as to_read:
55 try:
56 for line in to_read:
57 self.queue.put(line)
58 except IOError as e:
59# An EIO from the master side of the pty means we hit the end.
60 if e.errno == errno.EIO:
61 return
62 else:
63 raise e
64
Brian Silvermanb9e89602014-06-27 14:21:08 -050065 def __init__(self, executable, args, env, done_queue, start_semaphore):
Brian Silvermanc3740c32014-05-04 12:42:47 -070066 super(TestThread, self).__init__(
Brian Silvermanb9e89602014-06-27 14:21:08 -050067 name=os.path.split(executable)[-1])
Brian Silvermanc3740c32014-05-04 12:42:47 -070068
69 self.executable = executable
Brian Silvermanb9e89602014-06-27 14:21:08 -050070 self.args = args
Brian Silvermanc3740c32014-05-04 12:42:47 -070071 self.env = env
72 self.done_queue = done_queue
73 self.start_semaphore = start_semaphore
74
Brian Silverman730bb012014-06-08 13:05:20 -070075 self.output = queue.Queue()
76
Brian Silvermanc3740c32014-05-04 12:42:47 -070077 self.process_lock = threading.Lock()
78 self.process = None
79 self.stopped = False
Brian Silverman452aaec2014-05-05 16:52:18 -070080 self.returncode = None
Brian Silverman730bb012014-06-08 13:05:20 -070081 self.output_copier = None
Brian Silvermanc3740c32014-05-04 12:42:47 -070082
83 def run(self):
84 with self.start_semaphore:
Brian Silverman452aaec2014-05-05 16:52:18 -070085 if self.stopped:
86 return
Brian Silvermanc3740c32014-05-04 12:42:47 -070087 test_output('Starting test %s...' % self.name)
Brian Silverman730bb012014-06-08 13:05:20 -070088 output_to_read, subprocess_output = pty.openpty()
89 self.output_copier = TestThread.OutputCopier(self.name, output_to_read,
90 self.output)
91 self.output_copier.start()
Brian Silvermanf2bbe092014-05-13 16:55:03 -070092 try:
93 with self.process_lock:
Brian Silvermanb9e89602014-06-27 14:21:08 -050094 self.process = subprocess.Popen((self.name,) + self.args,
95 executable=self.executable,
Brian Silvermanf2bbe092014-05-13 16:55:03 -070096 env=self.env,
97 stderr=subprocess.STDOUT,
98 stdout=subprocess_output,
99 stdin=open(os.devnull, 'r'))
100 finally:
101 os.close(subprocess_output)
Brian Silvermanc3740c32014-05-04 12:42:47 -0700102 self.process.wait()
103 with self.process_lock:
104 self.returncode = self.process.returncode
105 self.process = None
106 if not self.stopped:
Brian Silverman730bb012014-06-08 13:05:20 -0700107 self.output_copier.join()
Brian Silvermanc3740c32014-05-04 12:42:47 -0700108 self.done_queue.put(self)
109
110 def terminate_process(self):
Brian Silvermanbf0e1db2014-05-10 22:13:15 -0700111 """Asks any currently running process to stop."""
Brian Silvermanc3740c32014-05-04 12:42:47 -0700112 with self.process_lock:
Brian Silverman452aaec2014-05-05 16:52:18 -0700113 if not self.process:
114 return
Brian Silvermana5826582014-06-03 19:46:35 -0700115 try:
116 self.process.terminate()
117 except OSError as e:
118 if e.errno == errno.ESRCH:
119 # We don't really care if it's already gone.
120 pass
121 else:
122 raise e
Brian Silvermanc3740c32014-05-04 12:42:47 -0700123 def kill_process(self):
Brian Silvermanbf0e1db2014-05-10 22:13:15 -0700124 """Forcibly terminates any running process."""
Brian Silvermanc3740c32014-05-04 12:42:47 -0700125 with self.process_lock:
Brian Silverman452aaec2014-05-05 16:52:18 -0700126 if not self.process:
127 return
Brian Silverman6bca4722014-05-20 17:02:49 -0700128 try:
129 self.process.kill()
130 except OSError as e:
131 if e.errno == errno.ESRCH:
132 # We don't really care if it's already gone.
133 pass
134 else:
135 raise e
Brian Silvermanbf0e1db2014-05-10 22:13:15 -0700136 def stop(self):
137 """Changes self to the stopped state."""
138 with self.process_lock:
139 self.stopped = True
Brian Silvermana29ebf92014-04-23 13:08:49 -0500140
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500141def aos_path():
Brian Silvermane6bada62014-05-04 16:16:54 -0700142 """Returns:
143 A relative path to the aos directory.
144 """
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500145 return os.path.join(os.path.dirname(__file__), '..')
146
147def get_ip(device):
Brian Silvermane6bada62014-05-04 16:16:54 -0700148 """Retrieves the IP address for a given device."""
149 FILENAME = os.path.normpath(os.path.join(aos_path(), '..',
Brian Silverman452aaec2014-05-05 16:52:18 -0700150 'output', 'ip_base.txt'))
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500151 if not os.access(FILENAME, os.R_OK):
152 os.makedirs(os.path.dirname(FILENAME), exist_ok=True)
153 with open(FILENAME, 'w') as f:
154 f.write('10.9.71')
155 with open(FILENAME, 'r') as f:
Brian Silvermand043d472014-06-21 15:32:39 -0700156 base = f.readline().strip()
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500157 if device == 'prime':
158 return base + '.179'
159 elif device == 'robot':
160 return base + '.2'
161 else:
162 raise Exception('Unknown device %s to get an IP address for.' % device)
163
Brian Silvermana9b1e5c2014-04-30 18:08:04 -0700164def user_output(message):
Brian Silvermane6bada62014-05-04 16:16:54 -0700165 """Prints message to the user."""
Brian Silvermana9b1e5c2014-04-30 18:08:04 -0700166 print('build.py: ' + message, file=sys.stderr)
167
Brian Silverman452aaec2014-05-05 16:52:18 -0700168# A lock to avoid making a mess intermingling test-related messages.
Brian Silvermanc3740c32014-05-04 12:42:47 -0700169test_output_lock = threading.RLock()
170def test_output(message):
Brian Silvermane6bada62014-05-04 16:16:54 -0700171 """Prints message to the user. Intended for messages related to tests."""
Brian Silvermanc3740c32014-05-04 12:42:47 -0700172 with test_output_lock:
Brian Silvermane6bada62014-05-04 16:16:54 -0700173 print('tests: ' + message, file=sys.stdout)
174
175def call_download_externals(argument):
176 """Calls download_externals.sh for a given set of externals.
177
178 Args:
179 argument: The argument to pass to the shell script to tell it what to
180 download.
181 """
182 subprocess.check_call(
183 (os.path.join(aos_path(), 'build', 'download_externals.sh'),
184 argument),
185 stdin=open(os.devnull, 'r'))
Brian Silvermanc3740c32014-05-04 12:42:47 -0700186
Brian Silvermana29ebf92014-04-23 13:08:49 -0500187class Processor(object):
Brian Silvermane6bada62014-05-04 16:16:54 -0700188 """Represents a processor architecture we can build for."""
189
Brian Silvermana29ebf92014-04-23 13:08:49 -0500190 class UnknownPlatform(Exception):
191 def __init__(self, message):
Brian Silverman452aaec2014-05-05 16:52:18 -0700192 super(Processor.UnknownPlatform, self).__init__()
Brian Silvermana29ebf92014-04-23 13:08:49 -0500193 self.message = message
194
Brian Silvermanb3d50542014-04-23 14:28:55 -0500195 class Platform(object):
Brian Silvermane6bada62014-05-04 16:16:54 -0700196 """Represents a single way to build the code."""
197
Brian Silvermanb3d50542014-04-23 14:28:55 -0500198 def outdir(self):
Brian Silvermane6bada62014-05-04 16:16:54 -0700199 """Returns:
200 The path of the directory build outputs get put in to.
201 """
Brian Silvermanb3d50542014-04-23 14:28:55 -0500202 return os.path.join(
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500203 aos_path(), '..', 'output', self.outname())
Brian Silvermanb3d50542014-04-23 14:28:55 -0500204 def build_ninja(self):
Brian Silvermane6bada62014-05-04 16:16:54 -0700205 """Returns:
206 The path of the build.ninja file.
207 """
Brian Silvermanb3d50542014-04-23 14:28:55 -0500208 return os.path.join(self.outdir(), 'build.ninja')
209
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500210 def do_deploy(self, dry_run, command):
Brian Silvermane6bada62014-05-04 16:16:54 -0700211 """Helper for subclasses to implement deploy.
212
213 Args:
214 dry_run: If True, prints the command instead of actually running it.
215 command: A tuple of command-line arguments.
216 """
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500217 real_command = (('echo',) + command) if dry_run else command
218 subprocess.check_call(real_command, stdin=open(os.devnull, 'r'))
Brian Silvermana29ebf92014-04-23 13:08:49 -0500219
Brian Silvermane6bada62014-05-04 16:16:54 -0700220 def deploy(self, dry_run):
221 """Downloads the compiled code to the target computer."""
222 raise NotImplementedError('deploy should be overriden')
223 def outname(self):
224 """Returns:
225 The name of the directory the code will be compiled to.
226 """
227 raise NotImplementedError('outname should be overriden')
228 def os(self):
229 """Returns:
230 The name of the operating system this platform is for.
231
232 This will be used as the value of the OS gyp variable.
233 """
234 raise NotImplementedError('os should be overriden')
235 def gyp_platform(self):
236 """Returns:
237 The platform name the .gyp files know.
238
239 This will be used as the value of the PLATFORM gyp variable.
240 """
241 raise NotImplementedError('gyp_platform should be overriden')
242 def architecture(self):
243 """Returns:
244 The processor architecture for this platform.
245
246 This will be used as the value of the ARCHITECTURE gyp variable.
247 """
248 raise NotImplementedError('architecture should be overriden')
249 def compiler(self):
250 """Returns:
251 The compiler used for this platform.
252
253 Everything before the first _ will be used as the value of the
254 COMPILER gyp variable and the whole thing will be used as the value
255 of the FULL_COMPILER gyp variable.
256 """
257 raise NotImplementedError('compiler should be overriden')
Brian Silverman452aaec2014-05-05 16:52:18 -0700258 def sanitizer(self):
259 """Returns:
260 The sanitizer used on this platform.
261
262 This will be used as the value of the SANITIZER gyp variable.
263
264 "none" if there isn't one.
265 """
266 raise NotImplementedError('sanitizer should be overriden')
Brian Silvermane6bada62014-05-04 16:16:54 -0700267 def debug(self):
268 """Returns:
269 Whether or not this platform compiles with debugging information.
270
271 The DEBUG gyp variable will be set to "yes" or "no" based on this.
272 """
273 raise NotImplementedError('debug should be overriden')
274 def build_env(self):
275 """Returns:
276 A map of environment variables to set while building this platform.
277 """
278 raise NotImplementedError('build_env should be overriden')
Brian Silvermanbd380fd2014-05-13 16:55:24 -0700279 def priority(self):
280 """Returns:
281 A relative priority for this platform relative to other ones.
282
283 Higher priority platforms will get built, tested, etc first. Generally,
284 platforms which give higher-quality compiler errors etc should come first.
285 """
286 return 0
Brian Silvermane6bada62014-05-04 16:16:54 -0700287
Brian Silverman9b7a6842014-05-05 16:19:11 -0700288 def check_installed(self, platforms, is_deploy):
289 """Makes sure that everything necessary to build platforms are installed."""
Brian Silvermane6bada62014-05-04 16:16:54 -0700290 raise NotImplementedError('check_installed should be overriden')
Brian Silverman452aaec2014-05-05 16:52:18 -0700291 def parse_platforms(self, platforms_string):
Brian Silvermane6bada62014-05-04 16:16:54 -0700292 """Args:
293 string: A user-supplied string saying which platforms to select.
294
295 Returns:
296 A tuple of Platform objects.
297
298 Raises:
299 Processor.UnknownPlatform: Parsing string didn't work out.
300 """
301 raise NotImplementedError('parse_platforms should be overriden')
302 def extra_gyp_flags(self):
303 """Returns:
304 A tuple of extra flags to pass to gyp (if any).
305 """
306 return ()
307 def modify_ninja_file(self, ninja_file):
308 """Modifies a freshly generated ninja file as necessary.
309
310 Args:
311 ninja_file: Path to the file to modify.
312 """
313 pass
314 def download_externals(self, platforms):
315 """Calls download_externals as appropriate to build platforms.
316
317 Args:
318 platforms: A list of platforms to download external libraries for.
319 """
320 raise NotImplementedError('download_externals should be overriden')
321
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700322 def do_check_installed(self, other_packages):
Brian Silvermane6bada62014-05-04 16:16:54 -0700323 """Helper for subclasses to implement check_installed.
324
325 Args:
326 other_packages: A tuple of platform-specific packages to check for."""
Brian Silverman9b7a6842014-05-05 16:19:11 -0700327 all_packages = other_packages
328 # Necessary to build stuff.
329 all_packages += ('ccache', 'make')
330 # Necessary to download stuff to build.
331 all_packages += ('wget', 'git', 'subversion', 'patch', 'unzip', 'bzip2')
332 # Necessary to build externals stuff.
333 all_packages += ('python', 'gcc', 'g++')
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700334 try:
Brian Silverman452aaec2014-05-05 16:52:18 -0700335 # TODO(brians): Check versions too.
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700336 result = subprocess.check_output(
337 ('dpkg-query', '--show') + all_packages,
338 stdin=open(os.devnull, 'r'),
339 stderr=subprocess.STDOUT)
340 except subprocess.CalledProcessError as e:
Brian Silverman9b7a6842014-05-05 16:19:11 -0700341 output = e.output.decode('utf-8').rstrip()
342 not_found = []
343 for line in output.splitlines(True):
344 match = re.match(r'dpkg-query: no packages found matching (.*)',
345 line)
346 if match:
347 not_found.append(match.group(1))
348 user_output('Some packages not installed: %s.' % ', '.join(not_found))
349 user_output('Try something like `sudo apt-get install %s`.' %
Brian Silverman452aaec2014-05-05 16:52:18 -0700350 ' '.join(not_found))
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700351 exit(1)
352
Brian Silvermana29ebf92014-04-23 13:08:49 -0500353class CRIOProcessor(Processor):
Brian Silvermane6bada62014-05-04 16:16:54 -0700354 """A Processor subclass for building cRIO code."""
355
Brian Silvermanb3d50542014-04-23 14:28:55 -0500356 class Platform(Processor.Platform):
Brian Silvermana4aff562014-05-02 17:43:50 -0700357 def __init__(self, debug, wind_base):
Brian Silvermanb3d50542014-04-23 14:28:55 -0500358 super(CRIOProcessor.Platform, self).__init__()
359
Brian Silvermane6bada62014-05-04 16:16:54 -0700360 self.__debug = debug
361 self.__wind_base = wind_base
Brian Silvermanb3d50542014-04-23 14:28:55 -0500362
363 def __repr__(self):
Brian Silverman9b7a6842014-05-05 16:19:11 -0700364 return 'CRIOProcessor.Platform(debug=%s)' % self.debug()
Brian Silvermanb3d50542014-04-23 14:28:55 -0500365 def __str__(self):
Brian Silverman9b7a6842014-05-05 16:19:11 -0700366 return 'crio%s' % ('-debug' if self.debug() else '')
Brian Silvermanb3d50542014-04-23 14:28:55 -0500367
368 def outname(self):
Brian Silverman9b7a6842014-05-05 16:19:11 -0700369 return 'crio-debug' if self.debug() else 'crio'
Brian Silvermanb3d50542014-04-23 14:28:55 -0500370 def os(self):
371 return 'vxworks'
372 def gyp_platform(self):
373 return 'crio'
374 def architecture(self):
375 return 'ppc'
376 def compiler(self):
377 return 'gcc'
Brian Silverman9b7a6842014-05-05 16:19:11 -0700378 def sanitizer(self):
379 return 'none'
Brian Silvermane6bada62014-05-04 16:16:54 -0700380 def debug(self):
381 return self.__debug
382 def wind_base(self):
383 return self.__wind_base
Brian Silvermanb3d50542014-04-23 14:28:55 -0500384
Brian Silvermane48c09a2014-04-30 18:04:58 -0700385 # TODO(brians): test this
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500386 def deploy(self, dry_run):
387 self.do_deploy(dry_run,
388 ('ncftpput', get_ip('robot'), '/',
Brian Silverman452aaec2014-05-05 16:52:18 -0700389 os.path.join(self.outdir(), 'lib',
390 'FRC_UserProgram.out')))
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500391
Brian Silvermana4aff562014-05-02 17:43:50 -0700392 def build_env(self):
Brian Silvermane6bada62014-05-04 16:16:54 -0700393 return {'WIND_BASE': self.wind_base()}
Brian Silvermana4aff562014-05-02 17:43:50 -0700394
Brian Silvermana29ebf92014-04-23 13:08:49 -0500395 def __init__(self):
396 super(CRIOProcessor, self).__init__()
397
398 if 'WIND_BASE' in os.environ:
Brian Silvermane6bada62014-05-04 16:16:54 -0700399 self.__wind_base = os.environ['WIND_BASE']
Brian Silvermana29ebf92014-04-23 13:08:49 -0500400 else:
Brian Silvermane6bada62014-05-04 16:16:54 -0700401 self.__wind_base = '/usr/local/powerpc-wrs-vxworks/wind_base'
Brian Silvermana29ebf92014-04-23 13:08:49 -0500402
Brian Silverman452aaec2014-05-05 16:52:18 -0700403 def parse_platforms(self, platforms_string):
404 if platforms_string is None or platforms_string == 'crio':
Brian Silvermane6bada62014-05-04 16:16:54 -0700405 return (CRIOProcessor.Platform(False, self.wind_base()),)
Brian Silvermana4aff562014-05-02 17:43:50 -0700406 elif string == 'crio-debug' or string == 'debug':
Brian Silvermane6bada62014-05-04 16:16:54 -0700407 return (CRIOProcessor.Platform(True, self.wind_base()),)
Brian Silvermanb3d50542014-04-23 14:28:55 -0500408 else:
Brian Silverman452aaec2014-05-05 16:52:18 -0700409 raise Processor.UnknownPlatform(
Brian Silvermandf5348a2014-06-12 23:25:08 -0700410 '"%s" not recognized as a cRIO platform.' % platforms_string)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500411
Brian Silvermane6bada62014-05-04 16:16:54 -0700412 def wind_base(self):
413 return self.__wind_base
Brian Silvermanb3d50542014-04-23 14:28:55 -0500414
Brian Silvermane6bada62014-05-04 16:16:54 -0700415 def extra_gyp_flags(self):
416 return ('-DWIND_BASE=%s' % self.wind_base(),)
417
418 def modify_ninja_file(self, ninja_file):
419 subprocess.check_call(
420 ('sed', '-i',
421 's/nm -gD/nm/g', ninja_file),
422 stdin=open(os.devnull, 'r'))
423
424 def download_externals(self, _):
425 call_download_externals('crio')
Brian Silvermana29ebf92014-04-23 13:08:49 -0500426
Brian Silverman9b7a6842014-05-05 16:19:11 -0700427 def check_installed(self, platforms, is_deploy):
428 packages = ('powerpc-wrs-vxworks', 'tcl')
429 if is_deploy:
430 packages += ('ncftp',)
431 self.do_check_installed(packages)
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700432
Brian Silvermana29ebf92014-04-23 13:08:49 -0500433class PrimeProcessor(Processor):
Brian Silvermane6bada62014-05-04 16:16:54 -0700434 """A Processor subclass for building prime code."""
435
Brian Silvermanb3d50542014-04-23 14:28:55 -0500436 class Platform(Processor.Platform):
Brian Silvermanf0d3c782014-05-02 23:56:32 -0700437 def __init__(self, architecture, compiler, debug, sanitizer):
Brian Silvermanb3d50542014-04-23 14:28:55 -0500438 super(PrimeProcessor.Platform, self).__init__()
439
Brian Silvermane6bada62014-05-04 16:16:54 -0700440 self.__architecture = architecture
441 self.__compiler = compiler
442 self.__debug = debug
443 self.__sanitizer = sanitizer
Brian Silvermana29ebf92014-04-23 13:08:49 -0500444
445 def __repr__(self):
Brian Silvermanf0d3c782014-05-02 23:56:32 -0700446 return 'PrimeProcessor.Platform(architecture=%s, compiler=%s, debug=%s' \
447 ', sanitizer=%s)' \
Brian Silvermane6bada62014-05-04 16:16:54 -0700448 % (self.architecture(), self.compiler(), self.debug(),
449 self.sanitizer())
Brian Silvermana29ebf92014-04-23 13:08:49 -0500450 def __str__(self):
Brian Silvermane6bada62014-05-04 16:16:54 -0700451 return '%s-%s%s-%s' % (self.architecture(), self.compiler(),
Brian Silverman452aaec2014-05-05 16:52:18 -0700452 '-debug' if self.debug() else '', self.sanitizer())
Brian Silvermana29ebf92014-04-23 13:08:49 -0500453
454 def os(self):
455 return 'linux'
456 def gyp_platform(self):
Brian Silvermane6bada62014-05-04 16:16:54 -0700457 return '%s-%s-%s' % (self.os(), self.architecture(), self.compiler())
458 def architecture(self):
459 return self.__architecture
460 def compiler(self):
461 return self.__compiler
462 def sanitizer(self):
463 return self.__sanitizer
464 def debug(self):
465 return self.__debug
Brian Silvermana29ebf92014-04-23 13:08:49 -0500466
Brian Silvermana29ebf92014-04-23 13:08:49 -0500467 def outname(self):
468 return str(self)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500469
Brian Silvermanbd380fd2014-05-13 16:55:24 -0700470 def priority(self):
471 r = 0
472 if self.compiler() == 'gcc':
473 r -= 100
474 elif self.compiler() == 'clang':
475 r += 100
476 if self.sanitizer() != 'none':
477 r -= 50
478 elif self.debug():
479 r -= 10
480 if self.architecture() == 'amd64':
481 r += 5
482 return r
483
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500484 def deploy(self, dry_run):
Brian Silvermane6bada62014-05-04 16:16:54 -0700485 # Downloads code to the prime in a way that avoids clashing too badly with
486 # starter (like the naive download everything one at a time).
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500487 SUM = 'md5sum'
488 TARGET_DIR = '/home/driver/robot_code/bin'
489 TEMP_DIR = '/tmp/aos_downloader'
490 TARGET = 'driver@' + get_ip('prime')
491
492 from_dir = os.path.join(self.outdir(), 'outputs')
493 sums = subprocess.check_output((SUM,) + tuple(os.listdir(from_dir)),
494 stdin=open(os.devnull, 'r'),
495 cwd=from_dir)
496 to_download = subprocess.check_output(
497 ('ssh', TARGET,
Brian Silvermanff485782014-06-18 19:59:09 -0700498 """rm -rf {TMPDIR} && mkdir {TMPDIR} && cd {TO_DIR} \\
499 && echo '{SUMS}' | {SUM} --check --quiet \\
500 |& grep -F FAILED | sed 's/^\\(.*\\): FAILED.*$/\\1/g'""".
501 format(TMPDIR=TEMP_DIR, TO_DIR=TARGET_DIR, SUMS=sums.decode('utf-8'),
502 SUM=SUM)))
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500503 if not to_download:
Brian Silvermana9b1e5c2014-04-30 18:08:04 -0700504 user_output("Nothing to download")
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500505 return
506 self.do_deploy(
507 dry_run,
Brian Silvermanff485782014-06-18 19:59:09 -0700508 ('scp', '-o', 'Compression yes')
509 + tuple([os.path.join(from_dir, f) for f in to_download.decode('utf-8').split('\n')[:-1]])
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500510 + (('%s:%s' % (TARGET, TEMP_DIR)),))
511 if not dry_run:
512 subprocess.check_call(
513 ('ssh', TARGET,
Brian Silvermanff485782014-06-18 19:59:09 -0700514 """mv {TMPDIR}/* {TO_DIR} \\
515 && echo 'Done moving new executables into place' \\
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500516 && ionice -c 3 bash -c 'sync && sync && sync'""".format(
517 TMPDIR=TEMP_DIR, TO_DIR=TARGET_DIR)))
518
Brian Silvermana4aff562014-05-02 17:43:50 -0700519 def build_env(self):
Brian Silvermane6bada62014-05-04 16:16:54 -0700520 OTHER_SYSROOT = '/opt/clang-3.5/'
521 SYMBOLIZER_PATH = OTHER_SYSROOT + 'bin/llvm-symbolizer'
Brian Silvermana4aff562014-05-02 17:43:50 -0700522 r = {}
Brian Silvermane6bada62014-05-04 16:16:54 -0700523 if self.compiler() == 'clang' or self.compiler() == 'gcc_4.8':
524 r['LD_LIBRARY_PATH'] = OTHER_SYSROOT + 'lib64'
525 if self.sanitizer() == 'address':
526 r['ASAN_SYMBOLIZER_PATH'] = SYMBOLIZER_PATH
Brian Silverman452aaec2014-05-05 16:52:18 -0700527 r['ASAN_OPTIONS'] = \
Brian Silverman407ca822014-06-05 18:36:41 -0700528 'detect_leaks=1:check_initialization_order=1:strict_init_order=1' \
Brian Silverman415e65d2014-06-21 22:39:28 -0700529 ':detect_stack_use_after_return=1:detect_odr_violation=2' \
530 ':allow_user_segv_handler=1'
Brian Silvermane6bada62014-05-04 16:16:54 -0700531 elif self.sanitizer() == 'memory':
532 r['MSAN_SYMBOLIZER_PATH'] = SYMBOLIZER_PATH
533 elif self.sanitizer() == 'thread':
534 r['TSAN_OPTIONS'] = 'external_symbolizer_path=' + SYMBOLIZER_PATH
Brian Silvermand3fac732014-05-03 16:03:46 -0700535
536 r['CCACHE_COMPRESS'] = 'yes'
Brian Silverman452aaec2014-05-05 16:52:18 -0700537 r['CCACHE_DIR'] = os.path.abspath(os.path.join(aos_path(), '..', 'output',
538 'ccache_dir'))
Brian Silvermand3fac732014-05-03 16:03:46 -0700539 r['CCACHE_HASHDIR'] = 'yes'
Brian Silvermane6bada62014-05-04 16:16:54 -0700540 if self.compiler() == 'clang':
Brian Silvermand3fac732014-05-03 16:03:46 -0700541 # clang doesn't like being run directly on the preprocessed files.
542 r['CCACHE_CPP2'] = 'yes'
543 # Without this, ccache slows down because of the generated header files.
544 # The race condition that this opens up isn't a problem because the build
545 # system finishes modifying header files before compiling anything that
546 # uses them.
547 r['CCACHE_SLOPPINESS'] = 'include_file_mtime'
Brian Silvermand3fac732014-05-03 16:03:46 -0700548
Brian Silvermane6bada62014-05-04 16:16:54 -0700549 if self.architecture() == 'amd64':
Brian Silvermand3fac732014-05-03 16:03:46 -0700550 r['PATH'] = os.path.join(aos_path(), 'build', 'bin-ld.gold') + \
551 ':' + os.environ['PATH']
552
Brian Silvermana4aff562014-05-02 17:43:50 -0700553 return r
554
Brian Silverman47cd6f62014-05-03 10:35:52 -0700555 ARCHITECTURES = ('arm', 'amd64')
556 COMPILERS = ('clang', 'gcc', 'gcc_4.8')
557 SANITIZERS = ('address', 'undefined', 'integer', 'memory', 'thread', 'none')
558 SANITIZER_TEST_WARNINGS = {
559 'memory': (True,
560"""We don't have all of the libraries instrumented which leads to lots of false
Brian Silvermanc3740c32014-05-04 12:42:47 -0700561 errors with msan (especially stdlibc++).
562 TODO(brians): Figure out a way to deal with it."""),
Brian Silverman47cd6f62014-05-03 10:35:52 -0700563 }
Brian Silvermana5301e32014-05-03 10:51:49 -0700564 PIE_SANITIZERS = ('memory', 'thread')
Brian Silvermana29ebf92014-04-23 13:08:49 -0500565
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500566 def __init__(self, is_test, is_deploy):
Brian Silverman452aaec2014-05-05 16:52:18 -0700567 super(PrimeProcessor, self).__init__()
Brian Silvermana29ebf92014-04-23 13:08:49 -0500568
569 platforms = []
570 for architecture in PrimeProcessor.ARCHITECTURES:
571 for compiler in PrimeProcessor.COMPILERS:
572 for debug in [True, False]:
Brian Silverman47cd6f62014-05-03 10:35:52 -0700573 if architecture == 'arm' and compiler == 'gcc_4.8':
574 # We don't have a compiler to use here.
575 continue
Brian Silvermana29ebf92014-04-23 13:08:49 -0500576 platforms.append(
Brian Silvermanf0d3c782014-05-02 23:56:32 -0700577 PrimeProcessor.Platform(architecture, compiler, debug, 'none'))
578 for sanitizer in PrimeProcessor.SANITIZERS:
Brian Silverman47cd6f62014-05-03 10:35:52 -0700579 for compiler in ('gcc_4.8', 'clang'):
580 if compiler == 'gcc_4.8' and (sanitizer == 'undefined' or
581 sanitizer == 'integer' or
582 sanitizer == 'memory'):
583 # GCC 4.8 doesn't support these sanitizers.
584 continue
Brian Silvermane6bada62014-05-04 16:16:54 -0700585 if sanitizer == 'none':
586 # We already added sanitizer == 'none' above.
587 continue
588 platforms.append(
589 PrimeProcessor.Platform('amd64', compiler, True, sanitizer))
590 self.__platforms = frozenset(platforms)
591
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500592 if is_test:
Brian Silvermane6bada62014-05-04 16:16:54 -0700593 default_platforms = self.select_platforms(architecture='amd64',
594 debug=True)
Brian Silvermanc3740c32014-05-04 12:42:47 -0700595 for sanitizer, warning in PrimeProcessor.SANITIZER_TEST_WARNINGS.items():
596 if warning[0]:
Brian Silvermane6bada62014-05-04 16:16:54 -0700597 default_platforms -= self.select_platforms(sanitizer=sanitizer)
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500598 elif is_deploy:
Brian Silvermane6bada62014-05-04 16:16:54 -0700599 default_platforms = self.select_platforms(architecture='arm',
Brian Silvermanff485782014-06-18 19:59:09 -0700600 compiler='clang',
Brian Silvermane6bada62014-05-04 16:16:54 -0700601 debug=False)
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500602 else:
Brian Silvermane6bada62014-05-04 16:16:54 -0700603 default_platforms = self.select_platforms(debug=False)
604 self.__default_platforms = frozenset(default_platforms)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500605
Brian Silvermane6bada62014-05-04 16:16:54 -0700606 def platforms(self):
607 return self.__platforms
608 def default_platforms(self):
609 return self.__default_platforms
610
611 def download_externals(self, platforms):
612 to_download = set()
613 for architecture in PrimeProcessor.ARCHITECTURES:
614 for sanitizer in PrimeProcessor.PIE_SANITIZERS:
615 if platforms & self.select_platforms(architecture=architecture,
616 sanitizer=sanitizer):
617 to_download.add(architecture + '-fPIE')
618 if platforms & self.select_platforms(architecture=architecture,
619 sanitizer='none'):
620 to_download.add(architecture)
621 for download_target in to_download:
622 call_download_externals(download_target)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500623
Brian Silverman452aaec2014-05-05 16:52:18 -0700624 def parse_platforms(self, platform_string):
625 if platform_string is None:
Brian Silvermane6bada62014-05-04 16:16:54 -0700626 return self.default_platforms()
627 r = self.default_platforms()
Brian Silverman452aaec2014-05-05 16:52:18 -0700628 for part in platform_string.split(','):
Brian Silverman1867aae2014-05-05 17:16:34 -0700629 if part == 'all':
630 r = self.platforms()
631 elif part[0] == '+':
Brian Silvermana29ebf92014-04-23 13:08:49 -0500632 r = r | self.select_platforms_string(part[1:])
633 elif part[0] == '-':
634 r = r - self.select_platforms_string(part[1:])
635 elif part[0] == '=':
636 r = self.select_platforms_string(part[1:])
637 else:
Brian Silverman7cd5ad42014-04-27 08:11:30 -0500638 selected = self.select_platforms_string(part)
Brian Silvermane6bada62014-05-04 16:16:54 -0700639 r = r - (self.platforms() - selected)
Brian Silverman7cd5ad42014-04-27 08:11:30 -0500640 if not r:
641 r = selected
Brian Silvermana29ebf92014-04-23 13:08:49 -0500642 return r
643
Brian Silverman452aaec2014-05-05 16:52:18 -0700644 def select_platforms(self, architecture=None, compiler=None, debug=None,
645 sanitizer=None):
Brian Silvermana29ebf92014-04-23 13:08:49 -0500646 r = []
Brian Silvermane6bada62014-05-04 16:16:54 -0700647 for platform in self.platforms():
648 if architecture is None or platform.architecture() == architecture:
649 if compiler is None or platform.compiler() == compiler:
650 if debug is None or platform.debug() == debug:
651 if sanitizer is None or platform.sanitizer() == sanitizer:
Brian Silvermanf0d3c782014-05-02 23:56:32 -0700652 r.append(platform)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500653 return set(r)
654
Brian Silverman452aaec2014-05-05 16:52:18 -0700655 def select_platforms_string(self, platforms_string):
Brian Silvermanf0d3c782014-05-02 23:56:32 -0700656 architecture, compiler, debug, sanitizer = None, None, None, None
Brian Silverman452aaec2014-05-05 16:52:18 -0700657 for part in platforms_string.split('-'):
Brian Silvermana29ebf92014-04-23 13:08:49 -0500658 if part in PrimeProcessor.ARCHITECTURES:
659 architecture = part
660 elif part in PrimeProcessor.COMPILERS:
661 compiler = part
662 elif part in ['debug', 'dbg']:
663 debug = True
664 elif part in ['release', 'nodebug', 'ndb']:
665 debug = False
Brian Silvermanf0d3c782014-05-02 23:56:32 -0700666 elif part in PrimeProcessor.SANITIZERS:
667 sanitizer = part
Brian Silverman415e65d2014-06-21 22:39:28 -0700668 elif part == 'all':
669 architecture = compiler = debug = sanitizer = None
Brian Silvermana29ebf92014-04-23 13:08:49 -0500670 else:
Brian Silverman452aaec2014-05-05 16:52:18 -0700671 raise Processor.UnknownPlatform(
Brian Silvermandf5348a2014-06-12 23:25:08 -0700672 '"%s" not recognized as a platform string component.' % part)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500673 return self.select_platforms(
674 architecture=architecture,
675 compiler=compiler,
Brian Silvermanf0d3c782014-05-02 23:56:32 -0700676 debug=debug,
677 sanitizer=sanitizer)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500678
Brian Silverman9b7a6842014-05-05 16:19:11 -0700679 def check_installed(self, platforms, is_deploy):
680 packages = set(('lzip', 'm4', 'realpath'))
681 packages.add('ruby')
682 # clang-format from here gets used for all versions.
683 packages.add('clang-3.5')
684 packages.add('arm-eabi-gcc')
685 for platform in platforms:
686 if platform.architecture() == 'arm':
687 packages.add('gcc-4.7-arm-linux-gnueabihf')
688 packages.add('g++-4.7-arm-linux-gnueabihf')
689 if platform.compiler() == 'clang' or platform.compiler() == 'gcc_4.8':
690 packages.add('clang-3.5')
Brian Silverman1867aae2014-05-05 17:16:34 -0700691 if platform.compiler() == 'gcc_4.8':
692 packages.add('libcloog-isl3:amd64')
Brian Silverman9b7a6842014-05-05 16:19:11 -0700693 if is_deploy:
694 packages.add('openssh-client')
695 if platform.compiler() == 'gcc' and platform.architecture() == 'amd64':
696 packages.add('gcc-4.7')
697 packages.add('g++-4.7')
698
699 self.do_check_installed(tuple(packages))
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700700
Brian Silverman6bca4722014-05-20 17:02:49 -0700701def strsignal(num):
702 # It ends up with SIGIOT instead otherwise, which is weird.
703 if num == signal.SIGABRT:
704 return 'SIGABRT'
705 # SIGCLD is a weird way to spell it.
706 if num == signal.SIGCHLD:
707 return 'SIGCHLD'
708
709 SIGNALS_TO_NAMES = dict((getattr(signal, n), n)
710 for n in dir(signal) if n.startswith('SIG')
711 and '_' not in n)
712 return SIGNALS_TO_NAMES.get(num, 'Unknown signal %d' % num)
713
Brian Silvermana29ebf92014-04-23 13:08:49 -0500714def main():
Brian Silvermandf5348a2014-06-12 23:25:08 -0700715 sys.argv.pop(0)
716 exec_name = sys.argv.pop(0)
717 def print_help(exit_status=None, message=None):
718 if message:
719 print(message)
720 sys.stdout.write(
Brian Silvermanb9e89602014-06-27 14:21:08 -0500721"""Usage: {name} [-j n] [action] [-n] [platform] [target|extra_flag]...
Brian Silvermandf5348a2014-06-12 23:25:08 -0700722Arguments:
723 -j, --jobs Explicitly specify how many jobs to run at a time.
724 Defaults to the number of processors + 2.
725 -n, --dry-run Don't actually do whatever.
726 Currently only meaningful for deploy.
727 action What to do. Defaults to build.
728 build: Build the code.
729 clean: Remove all the built output.
730 tests: Build and then run tests.
731 deploy: Build and then download.
732 platform What variants of the code to build.
733 Defaults to something reasonable.
734 See below for details.
735 target... Which targets to build/test/etc.
736 Defaults to everything.
Brian Silvermanb9e89602014-06-27 14:21:08 -0500737 extra_flag... Extra flags associated with the targets.
738 --gtest_*: Arguments to pass on to tests.
Brian Silvermana29ebf92014-04-23 13:08:49 -0500739
Brian Silvermandf5348a2014-06-12 23:25:08 -0700740Specifying targets:
741 Targets are combinations of architecture, compiler, and debug flags. Which
742 ones actually get run is built up as a set. It defaults to something
743 reasonable for the action (specified below).
744 The platform specification (the argument given to this script) is a comma-
745 separated sequence of hyphen-separated platforms, each with an optional
746 prefix.
747 Each selector (the things separated by commas) selects all of the platforms
748 which match all of its components. Its effect on the set of current platforms
749 depends on the prefix character.
750 Here are the prefix characters:
751 + Adds the selected platforms.
752 - Removes the selected platforms.
753 = Sets the current set to the selected platforms.
754 [none] Removes all non-selected platforms.
755 If this makes the current set empty, acts like =.
756 There is also the special psuedo-platform "all" which selects all platforms.
757 All of the available platforms:
758 {all_platforms}
759 Default platforms for deploying:
760 {deploy_platforms}
761 Default platforms for testing:
762 {test_platforms}
763 Default platforms for everything else:
764 {default_platforms}
Brian Silvermana29ebf92014-04-23 13:08:49 -0500765
Brian Silvermandf5348a2014-06-12 23:25:08 -0700766Examples of specifying targets:
767 build everything: "all"
768 only build things with clang: "clang"
769 build everything that uses GCC 4.8 (not just the defaults): "=gcc_4.8"
770 build all of the arm targets that use clang: "clang-arm" or "arm-clang"
771""".format(
772 name=exec_name,
773 all_platforms=str_platforms(PrimeProcessor(False, False).platforms()),
774 deploy_platforms=str_platforms(PrimeProcessor(False, True).default_platforms()),
775 test_platforms=str_platforms(PrimeProcessor(True, False).default_platforms()),
776 default_platforms=str_platforms(PrimeProcessor(False, False).default_platforms()),
777 ))
778 if exit_status is not None:
779 sys.exit(exit_status)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500780
Brian Silvermandf5348a2014-06-12 23:25:08 -0700781 def sort_platforms(platforms):
782 return sorted(
783 platforms, key=lambda platform: (-platform.priority(), str(platform)))
Brian Silvermana29ebf92014-04-23 13:08:49 -0500784
Brian Silvermandf5348a2014-06-12 23:25:08 -0700785 def str_platforms(platforms):
786 r = []
787 for platform in sort_platforms(platforms):
788 r.append(str(platform))
789 if len(r) > 1:
790 r[-1] = 'and ' + r[-1]
791 return ', '.join(r)
792
793 class Arguments(object):
794 def __init__(self):
795 self.jobs = os.sysconf('SC_NPROCESSORS_ONLN') + 2
796 self.action_name = 'build'
797 self.dry_run = False
798 self.targets = []
799 self.platform = None
Brian Silvermanb9e89602014-06-27 14:21:08 -0500800 self.extra_flags = []
Brian Silvermana29ebf92014-04-23 13:08:49 -0500801
Brian Silvermandf5348a2014-06-12 23:25:08 -0700802 args = Arguments()
Brian Silvermana29ebf92014-04-23 13:08:49 -0500803
Brian Silvermandf5348a2014-06-12 23:25:08 -0700804 if len(sys.argv) < 2:
805 print_help(1, 'Not enough arguments')
806 args.processor = sys.argv.pop(0)
807 args.main_gyp = sys.argv.pop(0)
808 VALID_ACTIONS = ['build', 'clean', 'deploy', 'tests']
809 while sys.argv:
810 arg = sys.argv.pop(0)
811 if arg == '-j' or arg == '--jobs':
812 args.jobs = int(sys.argv.pop(0))
813 continue
814 if arg in VALID_ACTIONS:
815 args.action_name = arg
816 continue
817 if arg == '-n' or arg == '--dry-run':
818 if args.action_name != 'deploy':
819 print_help(1, '--dry-run is only valid for deploy')
820 args.dry_run = True
821 continue
822 if arg == '-h' or arg == '--help':
823 print_help(0)
Brian Silvermanb9e89602014-06-27 14:21:08 -0500824 if re.match('^--gtest_.*$', arg):
825 if args.action_name == 'tests':
826 args.extra_flags.append(arg)
827 continue
828 else:
829 print_help(1, '--gtest_* is only valid for tests')
Brian Silvermandf5348a2014-06-12 23:25:08 -0700830 if args.platform:
831 args.targets.append(arg)
832 else:
833 args.platform = arg
Brian Silvermana29ebf92014-04-23 13:08:49 -0500834
835 if args.processor == 'crio':
836 processor = CRIOProcessor()
837 elif args.processor == 'prime':
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500838 processor = PrimeProcessor(args.action_name == 'tests',
839 args.action_name == 'deploy')
Brian Silvermana29ebf92014-04-23 13:08:49 -0500840 else:
Brian Silvermandf5348a2014-06-12 23:25:08 -0700841 print_help(1, message='Unknown processor "%s".' % args.processor)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500842
Brian Silvermana29ebf92014-04-23 13:08:49 -0500843 unknown_platform_error = None
844 try:
Brian Silvermandf5348a2014-06-12 23:25:08 -0700845 platforms = processor.parse_platforms(args.platform)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500846 except Processor.UnknownPlatform as e:
847 unknown_platform_error = e.message
Brian Silvermandf5348a2014-06-12 23:25:08 -0700848 args.targets.insert(0, args.platform)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500849 platforms = processor.parse_platforms(None)
850 if not platforms:
Brian Silvermandf5348a2014-06-12 23:25:08 -0700851 print_help(1, 'No platforms selected')
Brian Silvermana29ebf92014-04-23 13:08:49 -0500852
Brian Silverman9b7a6842014-05-05 16:19:11 -0700853 processor.check_installed(platforms, args.action_name == 'deploy')
Brian Silvermane6bada62014-05-04 16:16:54 -0700854 processor.download_externals(platforms)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500855
856 class ToolsConfig(object):
857 def __init__(self):
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500858 self.variables = {'AOS': aos_path()}
859 with open(os.path.join(aos_path(), 'build', 'tools_config'), 'r') as f:
Brian Silvermana29ebf92014-04-23 13:08:49 -0500860 for line in f:
861 if line[0] == '#':
862 pass
863 elif line.isspace():
864 pass
865 else:
866 new_name, new_value = line.rstrip().split('=')
867 for name, value in self.variables.items():
868 new_value = new_value.replace('${%s}' % name, value)
869 self.variables[new_name] = new_value
870 def __getitem__(self, key):
871 return self.variables[key]
872
873 tools_config = ToolsConfig()
874
875 def handle_clean_error(function, path, excinfo):
Brian Silverman452aaec2014-05-05 16:52:18 -0700876 _, _ = function, path
Brian Silvermana29ebf92014-04-23 13:08:49 -0500877 if issubclass(OSError, excinfo[0]):
878 if excinfo[1].errno == errno.ENOENT:
879 # Who cares if the file we're deleting isn't there?
880 return
881 raise excinfo[1]
882
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700883 def need_to_run_gyp(platform):
Brian Silvermane6bada62014-05-04 16:16:54 -0700884 """Determines if we need to run gyp again or not.
885
886 The generated build files are supposed to re-run gyp again themselves, but
887 that doesn't work (or at least it used to not) and we sometimes want to
888 modify the results anyways.
889
890 Args:
891 platform: The platform to check for.
892 """
Brian Silvermanf0d3c782014-05-02 23:56:32 -0700893 if not os.path.exists(platform.build_ninja()):
894 return True
Brian Silvermane6bada62014-05-04 16:16:54 -0700895 if os.path.getmtime(__file__) > os.path.getmtime(platform.build_ninja()):
896 return True
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700897 dirs = os.listdir(os.path.join(aos_path(), '..'))
Brian Silvermana4aff562014-05-02 17:43:50 -0700898 # Looking through these folders takes a long time and isn't useful.
Brian Silverman452aaec2014-05-05 16:52:18 -0700899 if dirs.count('output'):
900 dirs.remove('output')
901 if dirs.count('.git'):
902 dirs.remove('.git')
Brian Silvermana4aff562014-05-02 17:43:50 -0700903 return not not subprocess.check_output(
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700904 ('find',) + tuple(os.path.join(aos_path(), '..', d) for d in dirs)
Brian Silverman452aaec2014-05-05 16:52:18 -0700905 + ('-newer', platform.build_ninja(),
906 '(', '-name', '*.gyp', '-or', '-name', '*.gypi', ')'),
Brian Silvermana4aff562014-05-02 17:43:50 -0700907 stdin=open(os.devnull, 'r'))
908
909 def env(platform):
Brian Silvermane6bada62014-05-04 16:16:54 -0700910 """Makes sure we pass through important environmental variables.
911
912 Returns:
913 An environment suitable for passing to subprocess.Popen and friends.
914 """
Brian Silvermana4aff562014-05-02 17:43:50 -0700915 build_env = dict(platform.build_env())
Brian Silvermand3fac732014-05-03 16:03:46 -0700916 if not 'TERM' in build_env:
917 build_env['TERM'] = os.environ['TERM']
918 if not 'PATH' in build_env:
919 build_env['PATH'] = os.environ['PATH']
Brian Silvermana4aff562014-05-02 17:43:50 -0700920 return build_env
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700921
Brian Silvermandf5348a2014-06-12 23:25:08 -0700922 sorted_platforms = sort_platforms(platforms)
923 user_output('Building %s...' % str_platforms(sorted_platforms))
Brian Silverman47cd6f62014-05-03 10:35:52 -0700924
925 if args.action_name == 'tests':
926 for sanitizer, warning in PrimeProcessor.SANITIZER_TEST_WARNINGS.items():
927 warned_about = platforms & processor.select_platforms(sanitizer=sanitizer)
928 if warned_about:
929 user_output(warning[1])
930 if warning[0]:
Brian Silvermane6bada62014-05-04 16:16:54 -0700931 # TODO(brians): Add a --force flag or something to override this?
Brian Silverman47cd6f62014-05-03 10:35:52 -0700932 user_output('Refusing to run tests for sanitizer %s.' % sanitizer)
933 exit(1)
934
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700935 num = 1
Brian Silvermanbd380fd2014-05-13 16:55:24 -0700936 for platform in sorted_platforms:
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -0700937 user_output('Building %s (%d/%d)...' % (platform, num, len(platforms)))
Brian Silvermana29ebf92014-04-23 13:08:49 -0500938 if args.action_name == 'clean':
939 shutil.rmtree(platform.outdir(), onerror=handle_clean_error)
940 else:
941 if need_to_run_gyp(platform):
Brian Silvermana9b1e5c2014-04-30 18:08:04 -0700942 user_output('Running gyp...')
Brian Silvermana29ebf92014-04-23 13:08:49 -0500943 gyp = subprocess.Popen(
944 (tools_config['GYP'],
945 '--check',
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500946 '--depth=%s' % os.path.join(aos_path(), '..'),
Brian Silvermana29ebf92014-04-23 13:08:49 -0500947 '--no-circular-check',
948 '-f', 'ninja',
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500949 '-I%s' % os.path.join(aos_path(), 'build', 'aos.gypi'),
Brian Silvermana29ebf92014-04-23 13:08:49 -0500950 '-I/dev/stdin', '-Goutput_dir=output',
951 '-DOS=%s' % platform.os(),
952 '-DPLATFORM=%s' % platform.gyp_platform(),
Brian Silvermane6bada62014-05-04 16:16:54 -0700953 '-DARCHITECTURE=%s' % platform.architecture(),
954 '-DCOMPILER=%s' % platform.compiler().split('_')[0],
955 '-DFULL_COMPILER=%s' % platform.compiler(),
956 '-DDEBUG=%s' % ('yes' if platform.debug() else 'no'),
957 '-DSANITIZER=%s' % platform.sanitizer(),
Brian Silverman452aaec2014-05-05 16:52:18 -0700958 '-DSANITIZER_FPIE=%s' %
959 ('-fPIE' if platform.sanitizer() in PrimeProcessor.PIE_SANITIZERS
960 else '')) +
Brian Silvermanb3d50542014-04-23 14:28:55 -0500961 processor.extra_gyp_flags() + (args.main_gyp,),
Brian Silvermana29ebf92014-04-23 13:08:49 -0500962 stdin=subprocess.PIPE)
963 gyp.communicate(("""
964{
965 'target_defaults': {
966 'configurations': {
967 '%s': {}
968 }
969 }
970}""" % platform.outname()).encode())
971 if gyp.returncode:
Brian Silvermana9b1e5c2014-04-30 18:08:04 -0700972 user_output("Running gyp failed!")
Brian Silvermana29ebf92014-04-23 13:08:49 -0500973 exit(1)
Brian Silvermane6bada62014-05-04 16:16:54 -0700974 processor.modify_ninja_file(platform.build_ninja())
Brian Silverman47cd6f62014-05-03 10:35:52 -0700975 user_output('Done running gyp')
Brian Silvermana29ebf92014-04-23 13:08:49 -0500976 else:
Brian Silverman47cd6f62014-05-03 10:35:52 -0700977 user_output("Not running gyp")
Brian Silvermana29ebf92014-04-23 13:08:49 -0500978
979 try:
Brian Silvermanc3740c32014-05-04 12:42:47 -0700980 call = (tools_config['NINJA'],
Brian Silvermandf5348a2014-06-12 23:25:08 -0700981 '-C', platform.outdir()) + tuple(args.targets)
Brian Silvermanc3740c32014-05-04 12:42:47 -0700982 if args.jobs:
983 call += ('-j', str(args.jobs))
984 subprocess.check_call(call,
Brian Silverman452aaec2014-05-05 16:52:18 -0700985 stdin=open(os.devnull, 'r'),
986 env=env(platform))
Brian Silvermana29ebf92014-04-23 13:08:49 -0500987 except subprocess.CalledProcessError as e:
988 if unknown_platform_error is not None:
Brian Silvermana9b1e5c2014-04-30 18:08:04 -0700989 user_output(unknown_platform_error)
Brian Silvermana29ebf92014-04-23 13:08:49 -0500990 raise e
991
Brian Silvermanbe6cfe22014-04-27 08:06:27 -0500992 if args.action_name == 'deploy':
993 platform.deploy(args.dry_run)
Brian Silvermane48c09a2014-04-30 18:04:58 -0700994 elif args.action_name == 'tests':
995 dirname = os.path.join(platform.outdir(), 'tests')
Brian Silvermanc3740c32014-05-04 12:42:47 -0700996 done_queue = queue.Queue()
997 running = []
Brian Silvermandf5348a2014-06-12 23:25:08 -0700998 test_start_semaphore = threading.Semaphore(args.jobs)
999 if args.targets:
Brian Silvermanc3740c32014-05-04 12:42:47 -07001000 to_run = []
Brian Silvermandf5348a2014-06-12 23:25:08 -07001001 for target in args.targets:
Brian Silvermanc3740c32014-05-04 12:42:47 -07001002 if target.endswith('_test'):
1003 to_run.append(target)
1004 else:
1005 to_run = os.listdir(dirname)
1006 for f in to_run:
Brian Silvermanb9e89602014-06-27 14:21:08 -05001007 thread = TestThread(os.path.join(dirname, f), tuple(args.extra_flags),
1008 env(platform), done_queue,
Brian Silvermanc3740c32014-05-04 12:42:47 -07001009 test_start_semaphore)
1010 running.append(thread)
1011 thread.start()
1012 try:
1013 while running:
1014 done = done_queue.get()
1015 running.remove(done)
1016 with test_output_lock:
1017 test_output('Output from test %s:' % done.name)
Brian Silverman730bb012014-06-08 13:05:20 -07001018 try:
1019 while True:
1020 line = done.output.get(False)
1021 if not sys.stdout.isatty():
1022 # Remove color escape codes.
1023 line = re.sub(r'\x1B\[[0-9;]*[a-zA-Z]', '', line)
1024 sys.stdout.write(line)
1025 except queue.Empty:
1026 pass
1027 if not done.returncode:
1028 test_output('Test %s succeeded' % done.name)
1029 else:
1030 if done.returncode < 0:
1031 sig = -done.returncode
1032 test_output('Test %s was killed by signal %d (%s)' % \
1033 (done.name, sig, strsignal(sig)))
1034 elif done.returncode != 1:
1035 test_output('Test %s exited with %d' % \
1036 (done.name, done.returncode))
Brian Silvermanf2bbe092014-05-13 16:55:03 -07001037 else:
Brian Silverman730bb012014-06-08 13:05:20 -07001038 test_output('Test %s failed' % done.name)
1039 user_output('Aborting because of test failure for %s.' % \
1040 platform)
1041 exit(1)
Brian Silvermanc3740c32014-05-04 12:42:47 -07001042 finally:
1043 if running:
1044 test_output('Killing other tests...')
Brian Silvermanbf0e1db2014-05-10 22:13:15 -07001045# Stop all of them before killing processes because otherwise stopping some of
1046# them tends to let other ones that are waiting to start go.
1047 for thread in running:
Brian Silvermana5826582014-06-03 19:46:35 -07001048 test_output('\tKilling %s' % thread.name)
Brian Silvermanbf0e1db2014-05-10 22:13:15 -07001049 thread.stop()
Brian Silvermanc3740c32014-05-04 12:42:47 -07001050 for thread in running:
1051 thread.terminate_process()
1052 to_remove = []
1053 for thread in running:
1054 thread.join(5)
1055 if not thread.is_alive():
Brian Silverman452aaec2014-05-05 16:52:18 -07001056 to_remove.append(thread)
1057 for thread in to_remove:
1058 running.remove(thread)
Brian Silvermanc3740c32014-05-04 12:42:47 -07001059 for thread in running:
1060 test_output(
1061 'Test %s did not terminate. Killing it.' % thread.name)
1062 thread.kill_process()
1063 thread.join()
1064 test_output('Done killing other tests')
Brian Silvermanbe6cfe22014-04-27 08:06:27 -05001065
Brian Silvermanc2d8e5a2014-05-01 18:33:12 -07001066 user_output('Done building %s (%d/%d)' % (platform, num, len(platforms)))
1067 num += 1
Brian Silvermana29ebf92014-04-23 13:08:49 -05001068
1069if __name__ == '__main__':
1070 main()