Home | History | Annotate | Download | only in build
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2016 the V8 project authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 
      7 import os
      8 import os.path as path
      9 import generate_protocol_externs
     10 import re
     11 import subprocess
     12 import sys
     13 
     14 if len(sys.argv) == 2 and sys.argv[1] == '--help':
     15   print("Usage: %s" % path.basename(sys.argv[0]))
     16   sys.exit(0)
     17 
     18 java_required_major = 1
     19 java_required_minor = 7
     20 
     21 v8_inspector_path = path.dirname(path.dirname(path.abspath(__file__)))
     22 
     23 protocol_externs_file = path.join(v8_inspector_path, 'protocol_externs.js')
     24 injected_script_source_name = path.join(v8_inspector_path,
     25   'injected-script-source.js')
     26 injected_script_externs_file = path.join(v8_inspector_path,
     27   'injected_script_externs.js')
     28 
     29 generate_protocol_externs.generate_protocol_externs(protocol_externs_file,
     30   path.join(v8_inspector_path, 'js_protocol.json'))
     31 
     32 error_warning_regex = re.compile(r'WARNING|ERROR')
     33 
     34 closure_compiler_jar = path.join(v8_inspector_path, 'build',
     35   'closure-compiler', 'closure-compiler.jar')
     36 
     37 common_closure_args = [
     38   '--checks_only',
     39   '--warning_level', 'VERBOSE'
     40 ]
     41 
     42 # Error reporting and checking.
     43 errors_found = False
     44 
     45 def popen(arguments):
     46   return subprocess.Popen(arguments, stdout=subprocess.PIPE,
     47     stderr=subprocess.STDOUT)
     48 
     49 def error_excepthook(exctype, value, traceback):
     50   print 'ERROR:'
     51   sys.__excepthook__(exctype, value, traceback)
     52 sys.excepthook = error_excepthook
     53 
     54 def has_errors(output):
     55   return re.search(error_warning_regex, output) != None
     56 
     57 # Find java. Based on
     58 # http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python.
     59 def which(program):
     60   def is_exe(fpath):
     61     return path.isfile(fpath) and os.access(fpath, os.X_OK)
     62 
     63   fpath, fname = path.split(program)
     64   if fpath:
     65     if is_exe(program):
     66       return program
     67   else:
     68     for part in os.environ['PATH'].split(os.pathsep):
     69       part = part.strip('"')
     70       exe_file = path.join(part, program)
     71       if is_exe(exe_file):
     72         return exe_file
     73   return None
     74 
     75 def find_java():
     76   exec_command = None
     77   has_server_jvm = True
     78   java_path = which('java')
     79   if not java_path:
     80     java_path = which('java.exe')
     81 
     82   if not java_path:
     83     print 'NOTE: No Java executable found in $PATH.'
     84     sys.exit(0)
     85 
     86   is_ok = False
     87   java_version_out, _ = popen([java_path, '-version']).communicate()
     88   java_build_regex = re.compile(r'^\w+ version "(\d+)\.(\d+)')
     89   # pylint: disable=E1103
     90   match = re.search(java_build_regex, java_version_out)
     91   if match:
     92     major = int(match.group(1))
     93     minor = int(match.group(2))
     94     is_ok = major >= java_required_major and minor >= java_required_minor
     95   if is_ok:
     96     exec_command = [java_path, '-Xms1024m', '-server',
     97       '-XX:+TieredCompilation']
     98     check_server_proc = popen(exec_command + ['-version'])
     99     check_server_proc.communicate()
    100     if check_server_proc.returncode != 0:
    101       # Not all Java installs have server JVMs.
    102       exec_command = exec_command.remove('-server')
    103       has_server_jvm = False
    104 
    105   if not is_ok:
    106     print 'NOTE: Java executable version %d.%d or above not found in $PATH.' % (java_required_major, java_required_minor)
    107     sys.exit(0)
    108   print 'Java executable: %s%s' % (java_path, '' if has_server_jvm else ' (no server JVM)')
    109   return exec_command
    110 
    111 java_exec = find_java()
    112 
    113 spawned_compiler_command = java_exec + [
    114   '-jar',
    115   closure_compiler_jar
    116 ] + common_closure_args
    117 
    118 print 'Compiling injected-script-source.js...'
    119 
    120 command = spawned_compiler_command + [
    121   '--externs', injected_script_externs_file,
    122   '--externs', protocol_externs_file,
    123   '--js', injected_script_source_name
    124 ]
    125 
    126 injected_script_compile_proc = popen(command)
    127 
    128 print 'Validating injected-script-source.js...'
    129 injectedscript_check_script_path = path.join(v8_inspector_path, 'build',
    130   'check_injected_script_source.py')
    131 validate_injected_script_proc = popen([sys.executable,
    132   injectedscript_check_script_path, injected_script_source_name])
    133 
    134 print
    135 
    136 (injected_script_compile_out, _) = injected_script_compile_proc.communicate()
    137 print 'injected-script-source.js compilation output:%s' % os.linesep
    138 print injected_script_compile_out
    139 errors_found |= has_errors(injected_script_compile_out)
    140 
    141 (validate_injected_script_out, _) = validate_injected_script_proc.communicate()
    142 print 'Validate injected-script-source.js output:%s' % os.linesep
    143 print validate_injected_script_out if validate_injected_script_out else '<empty>'
    144 errors_found |= has_errors(validate_injected_script_out)
    145 
    146 os.remove(protocol_externs_file)
    147 
    148 if errors_found:
    149   print 'ERRORS DETECTED'
    150   sys.exit(1)
    151