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 debugger_script_source_name = path.join(v8_inspector_path, 29 'debugger-script.js') 30 debugger_script_externs_file = path.join(v8_inspector_path, 31 'debugger_script_externs.js') 32 33 generate_protocol_externs.generate_protocol_externs(protocol_externs_file, 34 path.join(v8_inspector_path, 'js_protocol.json')) 35 36 error_warning_regex = re.compile(r'WARNING|ERROR') 37 38 closure_compiler_jar = path.join(v8_inspector_path, 'build', 39 'closure-compiler', 'closure-compiler.jar') 40 41 common_closure_args = [ 42 '--checks_only', 43 '--warning_level', 'VERBOSE' 44 ] 45 46 # Error reporting and checking. 47 errors_found = False 48 49 def popen(arguments): 50 return subprocess.Popen(arguments, stdout=subprocess.PIPE, 51 stderr=subprocess.STDOUT) 52 53 def error_excepthook(exctype, value, traceback): 54 print 'ERROR:' 55 sys.__excepthook__(exctype, value, traceback) 56 sys.excepthook = error_excepthook 57 58 def has_errors(output): 59 return re.search(error_warning_regex, output) != None 60 61 # Find java. Based on 62 # http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python. 63 def which(program): 64 def is_exe(fpath): 65 return path.isfile(fpath) and os.access(fpath, os.X_OK) 66 67 fpath, fname = path.split(program) 68 if fpath: 69 if is_exe(program): 70 return program 71 else: 72 for part in os.environ['PATH'].split(os.pathsep): 73 part = part.strip('"') 74 exe_file = path.join(part, program) 75 if is_exe(exe_file): 76 return exe_file 77 return None 78 79 def find_java(): 80 exec_command = None 81 has_server_jvm = True 82 java_path = which('java') 83 if not java_path: 84 java_path = which('java.exe') 85 86 if not java_path: 87 print 'NOTE: No Java executable found in $PATH.' 88 sys.exit(0) 89 90 is_ok = False 91 java_version_out, _ = popen([java_path, '-version']).communicate() 92 java_build_regex = re.compile(r'^\w+ version "(\d+)\.(\d+)') 93 # pylint: disable=E1103 94 match = re.search(java_build_regex, java_version_out) 95 if match: 96 major = int(match.group(1)) 97 minor = int(match.group(2)) 98 is_ok = major >= java_required_major and minor >= java_required_minor 99 if is_ok: 100 exec_command = [java_path, '-Xms1024m', '-server', 101 '-XX:+TieredCompilation'] 102 check_server_proc = popen(exec_command + ['-version']) 103 check_server_proc.communicate() 104 if check_server_proc.returncode != 0: 105 # Not all Java installs have server JVMs. 106 exec_command = exec_command.remove('-server') 107 has_server_jvm = False 108 109 if not is_ok: 110 print 'NOTE: Java executable version %d.%d or above not found in $PATH.' % (java_required_major, java_required_minor) 111 sys.exit(0) 112 print 'Java executable: %s%s' % (java_path, '' if has_server_jvm else ' (no server JVM)') 113 return exec_command 114 115 java_exec = find_java() 116 117 spawned_compiler_command = java_exec + [ 118 '-jar', 119 closure_compiler_jar 120 ] + common_closure_args 121 122 print 'Compiling injected-script-source.js...' 123 124 command = spawned_compiler_command + [ 125 '--externs', injected_script_externs_file, 126 '--externs', protocol_externs_file, 127 '--js', injected_script_source_name 128 ] 129 130 injected_script_compile_proc = popen(command) 131 132 print 'Compiling debugger-script.js...' 133 134 command = spawned_compiler_command + [ 135 '--externs', debugger_script_externs_file, 136 '--js', debugger_script_source_name, 137 '--new_type_inf' 138 ] 139 140 debugger_script_compile_proc = popen(command) 141 142 print 'Validating injected-script-source.js...' 143 injectedscript_check_script_path = path.join(v8_inspector_path, 'build', 144 'check_injected_script_source.py') 145 validate_injected_script_proc = popen([sys.executable, 146 injectedscript_check_script_path, injected_script_source_name]) 147 148 print 149 150 (injected_script_compile_out, _) = injected_script_compile_proc.communicate() 151 print 'injected-script-source.js compilation output:%s' % os.linesep 152 print injected_script_compile_out 153 errors_found |= has_errors(injected_script_compile_out) 154 155 (debugger_script_compiler_out, _) = debugger_script_compile_proc.communicate() 156 print 'debugger-script.js compilation output:%s' % os.linesep 157 print debugger_script_compiler_out 158 errors_found |= has_errors(debugger_script_compiler_out) 159 160 (validate_injected_script_out, _) = validate_injected_script_proc.communicate() 161 print 'Validate injected-script-source.js output:%s' % os.linesep 162 print validate_injected_script_out if validate_injected_script_out else '<empty>' 163 errors_found |= has_errors(validate_injected_script_out) 164 165 os.remove(protocol_externs_file) 166 167 if errors_found: 168 print 'ERRORS DETECTED' 169 sys.exit(1) 170