2 # Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 # IMPORTANT NOTE: If you make local mods to this file, you must run:
7 # % pnacl/build.sh driver
8 # in order for them to take effect in the scons build. This command
9 # updates the copy in the toolchain/ tree.
11 # Global environment and expression parsing for the PNaCl driver
14 # This dictionary initializes a shell-like environment.
15 # Shell escaping and ${} substitution are provided.
16 # See "class Environment" defined later for the implementation.
18 from driver_log import Log, DriverExit
19 from shelltools import shell
24 'DRIVER_PATH' : '', # Absolute path to this driver invocation
25 'DRIVER_BIN' : '', # PNaCl driver bin/ directory
26 'DRIVER_REV_FILE' : '${DRIVER_BIN}/REV',
28 'BASE_NACL' : '${@FindBaseNaCl}', # Absolute path of native_client/
29 'BASE_TOOLCHAIN' : '${@FindBaseToolchain}', # Absolute path to toolchain/
30 'BASE' : '${@FindBasePNaCl}', # Absolute path to PNaCl
31 'BUILD_OS' : '${@GetBuildOS}', # "linux", "darwin" or "windows"
32 'BUILD_ARCH' : '${@GetBuildArch}', # "x86_64" or "i686" or "i386"
35 'BPREFIXES' : '', # Prefixes specified using the -B flag.
36 'BASE_LLVM' : '${@FindBaseHost:clang}',
37 'BASE_BINUTILS' : '${@FindBaseHost:le32-nacl-ar}',
39 'BASE_LIB_NATIVE' : '${BASE}/lib-',
41 'BASE_USR' : '${BASE}/usr',
42 'BASE_SDK' : '${BASE}/sdk',
43 'BASE_LIB' : '${BASE}/lib',
44 'BASE_USR_ARCH' : '${BASE_USR_%BCLIB_ARCH%}',
45 'BASE_USR_X8664' : '${BASE}/usr-bc-x86-64',
46 'BASE_LIB_ARCH' : '${BASE_LIB_%BCLIB_ARCH%}',
47 'BASE_LIB_X8664' : '${BASE}/lib-bc-x86-64',
49 'BASE_LLVM_BIN' : '${BASE_LLVM}/bin',
51 '${BASE_TOOLCHAIN}/pnacl_translator/${STANDARD_ARCH}/bin',
53 # TODO(pdox): Unify this with ARCH.
54 'STANDARD_ARCH' : '${STANDARD_ARCH_%ARCH%}',
55 'STANDARD_ARCH_X8632' : 'i686',
56 'STANDARD_ARCH_X8664' : 'x86_64',
57 'STANDARD_ARCH_ARM' : 'armv7',
58 'STANDARD_ARCH_MIPS32': 'mips32',
60 'SCONS_OUT' : '${BASE_NACL}/scons-out',
63 'ARCH' : '', # Target architecture
64 'BIAS' : 'NONE', # This can be 'NONE', 'ARM', 'MIPS32', 'X8632' or
66 # When not set to none, this causes the front-end to
67 # act like a target-specific compiler. This bias is
68 # currently needed while compiling newlib,
69 # and some scons tests.
71 'SAVE_TEMPS' : '0', # Do not clean up temporary files
72 'SANDBOXED' : '0', # Use sandboxed toolchain for this arch. (main switch)
73 'HAS_FRONTEND': '', # Set by ReadConfig(). '1' if the driver install
74 # has support for front-end bitcode tools, or '0'
75 # if it only has the backend translator.
78 'USE_BOOTSTRAP' : '${BUILD_OS==linux ? 1 : 0}',
79 # Args passed from one driver invocation to another
80 'INHERITED_DRIVER_ARGS' : '',
84 'LOG_VERBOSE' : '0', # Log to stdout (--pnacl-driver-verbose)
87 'SO_EXT' : '${SO_EXT_%BUILD_OS%}',
88 'SO_EXT_darwin' : '.dylib',
89 'SO_EXT_linux' : '.so',
90 'SO_EXT_windows' : '.dll',
92 'SO_DIR' : '${SO_DIR_%BUILD_OS%}',
93 'SO_DIR_darwin' : 'lib',
94 'SO_DIR_linux' : 'lib',
95 'SO_DIR_windows' : 'bin', # On Windows, DLLs are placed in bin/
96 # because the dynamic loader searches %PATH%
98 'EXEC_EXT' : '${EXEC_EXT_%BUILD_OS%}',
99 'EXEC_EXT_darwin' : '',
100 'EXEC_EXT_linux' : '',
101 'EXEC_EXT_windows': '.exe',
103 'SCONS_OS' : '${SCONS_OS_%BUILD_OS%}',
104 'SCONS_OS_linux' : 'linux',
105 'SCONS_OS_darwin' : 'mac',
106 'SCONS_OS_windows' : 'win',
109 'GOLD_PLUGIN_SO' : '${BASE_LLVM}/${SO_DIR}/LLVMgold${SO_EXT}',
111 'SCONS_STAGING' : '${SCONS_STAGING_%ARCH%}',
112 'SCONS_STAGING_X8632' : '${SCONS_OUT}/opt-${SCONS_OS}-x86-32/staging',
113 'SCONS_STAGING_X8664' : '${SCONS_OUT}/opt-${SCONS_OS}-x86-64/staging',
114 'SCONS_STAGING_ARM' : '${SCONS_OUT}/opt-${SCONS_OS}-arm/staging',
115 'SCONS_STAGING_MIPS32': '${SCONS_OUT}/opt-${SCONS_OS}-mips32/staging',
117 'SEL_UNIVERSAL_PREFIX': '${USE_EMULATOR ? ${EMULATOR}}',
118 'SEL_UNIVERSAL' : '${SCONS_STAGING}/sel_universal${EXEC_EXT}',
119 # NOTE: -Q skips sel_ldr qualification tests, -c -c skips validation
120 # NOTE: We are not using -B to load the IRT, since the translators do not
122 'SEL_UNIVERSAL_FLAGS' : '--abort_on_error ' +
123 '--uses_reverse_service ' +
124 '${USE_EMULATOR ? -Q -c -c --command_prefix ${EMULATOR}}',
126 'EMULATOR' : '${EMULATOR_%ARCH%}',
127 'EMULATOR_X8632' : '',
128 'EMULATOR_X8664' : '',
129 # NOTE: this is currently the only dependency on the arm trusted TC
131 '${BASE_NACL}/toolchain/linux_arm-trusted/run_under_qemu_arm',
133 '${BASE_NACL}/toolchain/linux_mips-trusted/run_under_qemu_mips32',
135 'SEL_LDR' : '${SCONS_STAGING}/sel_ldr${EXEC_EXT}',
136 'BOOTSTRAP_LDR' : '${SCONS_STAGING}/nacl_helper_bootstrap${EXEC_EXT}',
138 # sandboxed llvm backend
139 'LLC_SB' : '${TRANSLATOR_BIN}/pnacl-llc.nexe',
140 # sandboxed linker (gold based)
141 'LD_SB' : '${TRANSLATOR_BIN}/ld.nexe',
144 'CLANG' : '${BASE_LLVM_BIN}/clang${EXEC_EXT}',
145 # 'clang++' doesn't work on Windows (outside of Cygwin),
146 # because it is a symlink. '-ccc-cxx' enables C++ mode.
147 'CLANGXX' : '${BASE_LLVM_BIN}/clang${EXEC_EXT} -ccc-cxx',
148 'LLVM_OPT' : '${BASE_LLVM_BIN}/opt${EXEC_EXT}',
149 'LLVM_DIS' : '${BASE_LLVM_BIN}/llvm-dis${EXEC_EXT}',
150 'LLVM_NM' : '${BASE_LLVM_BIN}/llvm-nm${EXEC_EXT}',
151 # llvm-as compiles llvm assembly (.ll) to bitcode (.bc/.po)
152 'LLVM_AS' : '${BASE_LLVM_BIN}/llvm-as${EXEC_EXT}',
153 'PNACL_ABICHECK': '${BASE_LLVM_BIN}/pnacl-abicheck${EXEC_EXT}',
156 'LLVM_PNACL_LLC': '${BASE_LLVM_BIN}/pnacl-llc${EXEC_EXT}',
157 # llvm-mc is llvm's native assembler
158 'LLVM_MC' : '${BASE_LLVM_BIN}/llvm-mc${EXEC_EXT}',
161 'BINUTILS_BASE' : '${BASE_BINUTILS}/bin/le32-nacl-',
162 'OBJDUMP' : '${BINUTILS_BASE}objdump${EXEC_EXT}',
163 'NM' : '${BINUTILS_BASE}nm${EXEC_EXT}',
164 'AR' : '${BINUTILS_BASE}ar${EXEC_EXT}',
165 'RANLIB' : '${BINUTILS_BASE}ranlib${EXEC_EXT}',
166 'READELF' : '${BINUTILS_BASE}readelf${EXEC_EXT}',
167 'STRIP' : '${BINUTILS_BASE}strip${EXEC_EXT}',
168 # linker (used for both bitcode and ELF linking)
169 'LD' : '${BINUTILS_BASE}ld.gold${EXEC_EXT}',
171 # Use the default command line arguments to the sandboxed translator.
172 'USE_DEFAULT_CMD_LINE': '1',
175 ######################################################################
179 ######################################################################
181 def ParseError(s, leftpos, rightpos, msg):
182 Log.Error("Parse Error: %s", msg)
184 Log.Error(' ' + (' '*leftpos) + ('^'*(rightpos - leftpos + 1)))
187 # Find the leftmost position in "s" which begins a substring
188 # in "strset", starting at "pos".
190 # FindFirst('hello world', 0, ['h','o']) = ('h', 0)
191 # FindFirst('hello world', 1, ['h','o']) = ('o', 4)
192 # FindFirst('hello world', 0, ['x']) = (None,11)
193 def FindFirst(s, pos, strset):
196 m[s.find(ss, pos)] = ss
200 return (None, len(s))
204 class Environment(object):
208 def register(cls, func):
209 """ Register a function for use in the evaluator """
210 cls.functions[func.__name__] = func
218 self.data = dict(INITIAL_ENV)
220 def update(self, extra):
221 self.data.update(extra)
224 for (k,v) in self.data.iteritems():
225 print '%s == %s' % (k,v)
228 self.stack.append(self.data)
229 self.data = dict(self.data) # Make a copy
232 self.data = self.stack.pop()
234 def has(self, varname):
235 return varname in self.data
237 def getraw(self, varname):
238 return self.eval(self.data[varname])
240 # Evaluate a variable from the environment.
241 # Returns a list of terms.
242 def get(self, varname):
243 return shell.split(self.getraw(varname))
245 # Retrieve a variable from the environment which
246 # is a single term. Returns a string.
247 def getone(self, varname):
248 return shell.unescape(self.getraw(varname))
250 def getbool(self, varname):
251 return bool(int(self.getone(varname)))
253 def setbool(self, varname, val):
255 self.set(varname, '1')
257 self.set(varname, '0')
259 # Set a variable in the environment without shell-escape
260 def setraw(self, varname, val):
261 self.data[varname] = val
263 # Set one or more variables using named arguments
264 def setmany(self, **kwargs):
265 for k,v in kwargs.iteritems():
266 if isinstance(v, types.StringTypes):
268 elif isinstance(v, types.ListType):
271 Log.Fatal('env.setmany given a non-string and non-list value')
273 def clear(self, varname):
274 self.data[varname] = ''
276 # Set a variable to one or more terms, applying shell-escape.
277 def set(self, varname, *vals):
279 self.append(varname, *vals)
281 # Append one or more terms to a variable in the
282 # environment, applying shell-escape.
283 def append(self, varname, *vals):
284 escaped = [ shell.escape(v) for v in vals ]
285 if len(self.data[varname]) > 0:
286 self.data[varname] += ' '
287 self.data[varname] += ' '.join(escaped)
289 # Evaluate an expression s
291 (result, i) = self.eval_expr(s, 0, [])
295 ######################################################################
296 # EXPRESSION EVALUATION CODE
297 # Context Free Grammar:
299 # str = empty | string literal
300 # expr = str | expr '$' '{' bracket_expr '}' expr
301 # bracket_expr = varname | boolexpr ? expr | boolexpr ? expr : expr | @call
302 # boolexpr = boolval | boolval '&&' boolexpr | boolval '||' boolexpr
303 # boolval = varname | !varname | #varname | !#varname | varname '==' str
304 # varname = str | varname '%' bracket_expr '%' varname
305 # call = func | func ':' arglist
307 # arglist = empty | arg ':' arglist
309 # Do not call these functions outside of this class.
310 # The env.eval method is the external interface to the evaluator.
311 ######################################################################
313 # Evaluate a string literal
314 def eval_str(self, s, pos, terminators):
315 (_,i) = FindFirst(s, pos, terminators)
318 # Evaluate %var% substitutions inside a variable name.
319 # Returns (the_actual_variable_name, endpos)
320 # Terminated by } character
321 def eval_varname(self, s, pos, terminators):
322 (_,i) = FindFirst(s, pos, ['%'] + terminators)
323 leftpart = s[pos:i].strip(' ')
324 if i == len(s) or s[i] in terminators:
327 (middlepart, j) = self.eval_bracket_expr(s, i+1, ['%'])
328 if j == len(s) or s[j] != '%':
329 ParseError(s, i, j, "Unterminated %")
331 (rightpart, k) = self.eval_varname(s, j+1, terminators)
333 fullname = leftpart + middlepart + rightpart
334 fullname = fullname.strip()
338 def eval_whitespace(self, s, pos):
340 while i < len(s) and s[i] == ' ':
344 def eval_bool_val(self, s, pos, terminators):
345 (_,i) = self.eval_whitespace(s, pos)
353 (_,i) = self.eval_whitespace(s, i)
361 (varname, j) = self.eval_varname(s, i, ['=']+terminators)
363 # This is an error condition one level up. Don't evaluate anything.
366 if varname not in self.data:
367 ParseError(s, i, j, "Undefined variable '%s'" % varname)
368 vardata = self.data[varname]
370 contents = self.eval(vardata)
373 # String equality test
374 if j+1 == len(s) or s[j+1] != '=':
375 ParseError(s, j, j, "Unexpected token")
377 ParseError(s, j, j, "Cannot combine == and #")
378 (_,j) = self.eval_whitespace(s, j+2)
379 (literal_str,j) = self.eval_str(s, j, [' ']+terminators)
380 (_,j) = self.eval_whitespace(s, j)
382 return (False, j) # Error one level up
387 val = (len(contents) != 0)
388 elif literal_str is not None:
389 val = (contents == literal_str)
391 if contents not in ('0','1'):
393 "%s evaluated to %s, which is not a boolean!" % (varname, contents))
394 val = bool(int(contents))
395 return (negated ^ val, j)
397 # Evaluate a boolexpr
398 def eval_bool_expr(self, s, pos, terminators):
399 (boolval1, i) = self.eval_bool_val(s, pos, ['&','|']+terminators)
401 # This is an error condition one level up. Don't evaluate anything.
404 if s[i] in ('&','|'):
406 if i+1 == len(s) or s[i+1] != s[i]:
407 ParseError(s, i, i, "Unexpected token")
408 is_and = (s[i] == '&')
410 (boolval2, j) = self.eval_bool_expr(s, i+2, terminators)
412 # This is an error condition one level up.
416 return (boolval1 and boolval2, j)
418 return (boolval1 or boolval2, j)
422 # Evaluate the inside of a ${} or %%.
423 # Returns the (the_evaluated_string, endpos)
424 def eval_bracket_expr(self, s, pos, terminators):
425 (_,pos) = self.eval_whitespace(s, pos)
428 # Function call: ${@func}
429 # or possibly : ${@func:arg1:arg2...}
430 (_,i) = FindFirst(s, pos, [':']+terminators)
432 return ('', i) # Error one level up
433 funcname = s[pos+1:i]
439 (_,j) = FindFirst(s, i+1, terminators)
441 return ('', j) # Error one level up
442 args = s[i+1:j].split(':')
444 val = self.functions[funcname](*args)
445 contents = self.eval(val)
448 (m,_) = FindFirst(s, pos, ['?']+terminators)
450 # Regular variable substitution
451 (varname,i) = self.eval_varname(s, pos, terminators)
453 return ('', i) # Error one level up
454 if varname not in self.data:
455 ParseError(s, pos, i, "Undefined variable '%s'" % varname)
456 vardata = self.data[varname]
457 contents = self.eval(vardata)
461 (is_cond_true,i) = self.eval_bool_expr(s, pos, ['?']+terminators)
462 assert(i < len(s) and s[i] == '?')
464 (if_true_expr, j) = self.eval_expr(s, i+1, [' : ']+terminators)
466 return ('', j) # Error one level up
468 if s[j:j+3] == ' : ':
469 (if_false_expr,j) = self.eval_expr(s, j+3, terminators)
471 # This is an error condition one level up.
477 contents = if_true_expr.strip()
479 contents = if_false_expr.strip()
483 # Evaluate an expression with ${} in string s, starting at pos.
484 # Returns (the_evaluated_expression, endpos)
485 def eval_expr(self, s, pos, terminators):
486 (m,i) = FindFirst(s, pos, ['${'] + terminators)
488 if i == len(s) or m in terminators:
491 (middlepart, j) = self.eval_bracket_expr(s, i+2, ['}'])
492 if j == len(s) or s[j] != '}':
493 ParseError(s, i, j, 'Unterminated ${')
495 (rightpart, k) = self.eval_expr(s, j+1, terminators)
496 return (leftpart + middlepart + rightpart, k)
501 def override_env(meth_name, func):
502 """Override a method in the global |env|, given the method name
503 and the new function.
506 setattr(env, meth_name, types.MethodType(func, env, Environment))