1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
#!/usr/bin/python
# Copyright (c) 2011 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from optparse import OptionParser
import os
import subprocess
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import pynacl.platform
NATIVE_CLIENT_DIR = os.path.dirname(os.path.dirname(__file__))
TOOLCHAIN_DIR = os.path.join(NATIVE_CLIENT_DIR, 'toolchain')
"""Building verification script
This module will take the output directory path, base nexe name and
architecture and combine them to generate a sel_ldr execution command
line using irt_core of the appropriate architecture.
"""
def RelativePath(filepath):
"""Return a POSIX style relative path."""
cwdpath = os.path.abspath(os.getcwd())
cwdparts = cwdpath.split(os.path.sep)
filepath = os.path.abspath(filepath)
# For Windows, convert to POSIX style path
if sys.platform == 'win32':
filepath = filepath.replace('\\', '/')
fileparts = filepath.split('/')
for index in range(len(fileparts)):
if index >= len(cwdparts):
break
if cwdparts[index] != fileparts[index]:
break
# Add 'cd ..' the rest of cwd that doesn't match
out = '../' * (len(cwdparts) - index)
out += '/'.join(fileparts[index:])
return out
def UseWin64():
"""Check if we are on 64 bit windows."""
if sys.platform != 'win32':
return False
arch32 = os.environ.get('PROCESSOR_ARCHITECTURE', 'unk')
arch64 = os.environ.get('PROCESSOR_ARCHITEW6432', 'unk')
if arch32 == 'AMD64' or arch64 == 'AMD64':
return True
return False
def ErrOut(text):
"""ErrOut prints an error message and the command-line that caused it.
Prints to standard err, both the command-line normally, and separated by
>>...<< to make it easier to copy and paste the command, or to
find command formating issues.
"""
sys.stderr.write('\n\n')
sys.stderr.write( '>>>' + '>> <<'.join(sys.argv) + '<<\n\n')
sys.stderr.write(' '.join(sys.argv) + '<<\n\n')
sys.stderr.write(text + '\n')
sys.exit(1)
def Run(cmd_line):
"""Run the provided command-line returning the error code.
Uses supbrocess to execute the command line, printing debugging information
as well as error messages as appropriate.
"""
sys.stdout.write('Run: %s\n' % ' '.join(cmd_line))
sys.stdout.flush()
try:
ecode = subprocess.call(cmd_line)
except Exception, err:
ErrOut('\nFAILED: %s\n' % str(err))
if ecode:
ErrOut('FAILED with return value: %d\n' % ecode)
else:
sys.stdout.write('Success\n')
sys.stdout.flush()
return ecode
def GetToolchainPath(tool):
"""Given the requested tool, compute the path to that toolchain."""
os_name = pynacl.platform.GetOS()
if tool == 'newlib':
lib_name = 'newlib'
else:
lib_name = 'glibc'
return os.path.join(
TOOLCHAIN_DIR,
'%s_x86' % (os_name),
'nacl_x86_%s_raw' % lib_name
)
def GetLibPath(tool, arch):
"""For a given tool and architecture, determine the library path."""
arch_map = {
'ia32': 'lib32',
'x64': 'lib',
}
lib = arch_map[arch]
# For 64bit win, the arch is incorrectly reported as 32bit, so override it.
if UseWin64():
lib = 'lib'
return os.path.join(GetToolchainPath(tool), 'x86_64-nacl', lib)
def GetNexe(path, name, arch, tool):
"""For a given nexe name, architecture, and tool generate a path to it."""
arch_map = {
'ia32': 'x32',
'x64': 'x64',
}
# For 64bit win, the arch is incorrectly reported as 32bit, so override it.
if UseWin64():
arch = 'x64'
return os.path.join(path, '%s_%s_%s.nexe' % (name, tool, arch_map[arch]))
def GetLdrIrtNexe(path, nexe, arch, tool):
sel_ldr = os.path.join(path, 'sel_ldr')
# If incorrectly reported use sel_ldr64 otherwise assume sel_ldr is already
# the correct architecture.
if UseWin64() and arch == 'ia32':
sel_ldr = os.path.join(path, 'sel_ldr64')
# Get IRT_CORE which is built with newlib
irt_core = GetNexe(path, 'irt_core', arch, 'newlib')
nexe = GetNexe(path, nexe, arch, tool)
return (sel_ldr, irt_core, nexe)
def BuildCmdLine(path, nexe, arch, tool):
(sel_ldr, irt_core, nexe) = GetLdrIrtNexe(path, nexe, arch, tool)
if tool in ('newlib', 'pnacl_newlib'):
return [sel_ldr, '-B', irt_core, '--', nexe]
# For GLIBC we need to add 'runnable-ld.so' and allow sel_ldr access to
# the file system.
libpath = GetLibPath(tool, arch)
ldso = os.path.join(libpath, 'runnable-ld.so')
# Force POSIX style paths required by runnable-ld
nexe = nexe.replace('\\', '/')
return [sel_ldr, '-a', '-B', irt_core, '--', ldso, '--library-path',
libpath, nexe]
def RunLoader(path, nexe, arch, tools, out):
fail = 0
for tool in tools:
print '\n\nRunning: %s %s %s' % (nexe, arch, tool)
cmd_line = BuildCmdLine(path, nexe, arch, tool)
err = Run(cmd_line)
if err:
fail = fail + 1
if out:
out.write('%s on %s built with %s, failed returning %s.\n' %
(nexe, arch, tool, err))
else:
if out:
out.write('SUCCESS: %s on %s built with %s.\n' % (nexe, arch, tool))
return fail
def LogLoader(path, nexe, arch, tools):
for tool in tools:
cmd_line = BuildCmdLine(path, nexe, arch, tool)
print ' '.join(cmd_line)
return 0
def LogDeps(path, nexe, arch, tools):
out_set = set()
out_set.add(RelativePath(__file__))
for tool in tools:
out_set |= set(GetLdrIrtNexe(path, nexe, arch, tool))
for out in out_set:
# Emit forward slashes here as that's what gyp expects.
print out.replace('\\', '/')
def Main(argv):
parser = OptionParser()
# Test build modes
parser.add_option('-i', '--input', help='Generate input list.',
action='store_const', const='i', dest='mode', default='')
parser.add_option('-r', '--run', help='Run the nexe.',
action='store_const', const='r', dest='mode', default='')
parser.add_option('-l', '--log', help='Log the command-lines to run.',
action='store_const', const='l', dest='mode', default='')
# Options
parser.add_option('-v', '--verbose', dest='verbose', default=False,
help='Enable verbosity', action='store_true')
parser.add_option('-o', '--output', dest='output', default='',
help='Set output file name.', action='store')
parser.add_option('-p', '--path', dest='path', default='.',
help='Set path to sel_ldr and NEXEs.', action='store')
parser.add_option('-n', '--name', dest='name', action='store', default='',
help='Base name of the NEXE.')
parser.add_option('-x', '--arch', dest='arch', action='store', default='',
help='Run architecture.')
parser.add_option('-t', '--tools', dest='tools', action='append', default=[],
help='Select which toolchains versions to run.')
options, nexe_args = parser.parse_args(argv[1:])
if not options.name:
parser.error('Missing NEXE name.')
if not options.mode or options.mode not in ['i', 'r', 'l']:
parser.error('Missing run mode.')
if not options.arch:
parser.error('Missing run architecture.')
if not options.tools:
parser.error('Missing toolchains.')
# TODO(bradnelson): Hack to prevent gyp generated path ending with \" from
# being interpreted as escaped quote. Here we strip the extra
# '/hack' we added in the gyp file.
# http://code.google.com/p/chromium/issues/detail?id=141463
if options.mode is 'r':
options.path = os.path.dirname(options.path)
if options.output:
try:
out = open(options.output, 'w')
except EnvironmentError:
print 'Failed to open %s.\n' % options.output
return 1
else:
out = None
tools = ','.join(options.tools)
tools = tools.split(',')
fail = 0
for tool in tools:
if tool not in ['newlib', 'glibc', 'pnacl_newlib']:
parser.error('Unknown tool: %s\n' % tool)
path = options.path
nexe = options.name
arch = options.arch
if options.mode == 'r':
fail = RunLoader(path, nexe, arch, tools, out)
if options.mode == 'l':
fail = LogLoader(path, nexe, arch, tools)
if options.mode == 'i':
fail = LogDeps('<(PRODUCT_DIR)', nexe, arch, tools)
if out:
out.close()
return fail
if __name__ == '__main__':
sys.exit(Main(sys.argv))
|