Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / native_client / pnacl / driver / driver_tools.py
index d5bd35e..7d5e9d6 100755 (executable)
@@ -15,13 +15,10 @@ import re
 import shlex
 import signal
 import subprocess
-import struct
 import sys
 import tempfile
 
-import artools
 import elftools
-import ldtools
 # filetype needs to be imported here because pnacl-driver injects calls to
 # filetype.ForceFileType into argument parse actions.
 # TODO(dschuff): That's ugly. Find a better way.
@@ -31,7 +28,8 @@ import pathtools
 from driver_env import env
 # TODO: import driver_log and change these references from 'foo' to
 # 'driver_log.foo', or split driver_log further
-from driver_log import Log, DriverOpen, DriverClose, StringifyCommand, TempFiles, DriverExit, FixArch
+from driver_log import Log, DriverOpen, DriverClose, StringifyCommand, DriverExit, FixArch
+from driver_temps import TempFiles
 from shelltools import shell
 
 def ParseError(s, leftpos, rightpos, msg):
@@ -85,6 +83,31 @@ def ParseTriple(triple):
   Log.Fatal('machine/os ' + '-'.join(tokens[1:]) + ' not supported.')
 
 
+def GetOSName():
+  if sys.platform == 'darwin':
+    os_name = 'mac'
+  elif sys.platform.startswith('linux'):
+    os_name = 'linux'
+  elif sys.platform in ('cygwin', 'win32'):
+    os_name = 'win'
+  else:
+    Log.Fatal('Machine: %s not supported.' % sys.platform)
+
+  return os_name
+
+def GetArchNameShort():
+  machine = platform.machine().lower()
+  if machine.startswith('arm'):
+    return 'arm'
+  elif machine.startswith('mips'):
+    return 'mips'
+  elif (machine.startswith('x86')
+        or machine in ('amd32', 'i386', 'i686', 'ia32', '32', 'amd64', '64')):
+    return 'x86'
+
+  Log.Fatal('Architecture: %s not supported.' % machine)
+  return 'unknown'
+
 def RunDriver(invocation, args, suppress_inherited_arch_args=False):
   """
   RunDriver() is used to invoke "driver" tools, e.g.
@@ -176,11 +199,15 @@ def FindBaseNaCl():
 @env.register
 @memoize
 def FindBaseToolchain():
-  """ Find toolchain/ directory """
-  dir = FindBaseDir(lambda cur: pathtools.basename(cur) == 'toolchain')
-  if dir is None:
+  """ Find toolchain/OS_ARCH directory """
+  base_dir = FindBaseDir(lambda cur: pathtools.basename(cur) == 'toolchain')
+  if base_dir is None:
     Log.Fatal("Unable to find 'toolchain' directory")
-  return shell.escape(dir)
+  toolchain_dir = os.path.join(
+      base_dir,
+      '%s_%s' % (GetOSName(), GetArchNameShort())
+  )
+  return shell.escape(toolchain_dir)
 
 @env.register
 @memoize
@@ -204,7 +231,8 @@ def FindBaseHost(tool):
   """ Find the base directory for host binaries (i.e. llvm/binutils) """
   if env.has('BPREFIXES'):
     for prefix in env.get('BPREFIXES'):
-      if os.path.exists(pathtools.join(prefix, 'bin', tool)):
+      if os.path.exists(pathtools.join(prefix, 'bin',
+                                       tool + env.getone('EXEC_EXT'))):
         return prefix
 
   base_pnacl = FindBasePNaCl()
@@ -259,7 +287,6 @@ DriverArgPatterns = [
   ( '--pnacl-i686-bias',               "env.set('BIAS', 'X8632')"),
   ( '--pnacl-x86_64-bias',             "env.set('BIAS', 'X8664')"),
   ( '--pnacl-bias=(.+)',               "env.set('BIAS', FixArch($0))"),
-  ( '--pnacl-default-command-line',    "env.set('USE_DEFAULT_CMD_LINE', '1')"),
   ( '-save-temps',                     "env.set('SAVE_TEMPS', '1')"),
   ( '-no-save-temps',                  "env.set('SAVE_TEMPS', '0')"),
   ( ('-B', '(.*)'),  AddHostBinarySearchPath),
@@ -430,17 +457,14 @@ def PathSplit(f):
 # add parent directories. Rinse, repeat.
 class TempNameGen(object):
   def __init__(self, inputs, output):
+    self.TempBase = tempfile.mkdtemp()
     inputs = [ pathtools.abspath(i) for i in inputs ]
-    output = pathtools.abspath(output)
+    output = pathtools.basename(output)
+
+    TempFiles.add(self.TempBase)
 
-    self.TempBase = output + '---linked'
+    self.Output = output + '---linked'
 
-    # TODO(pdox): Figure out if there's a less confusing way
-    #             to simplify the intermediate filename in this case.
-    #if len(inputs) == 1:
-    #  # There's only one input file, don't bother adding the source name.
-    #  TempMap[inputs[0]] = output + '---'
-    #  return
 
     # Build the initial mapping
     self.TempMap = dict()
@@ -450,12 +474,15 @@ class TempNameGen(object):
       path = PathSplit(f)
       self.TempMap[f] = [1, path]
 
+    def MangledName(path):
+      return output + '---' + '_'.join(path[-n:]) + '---'
+
     while True:
       # Find conflicts
       ConflictMap = dict()
       Conflicts = set()
       for (f, [n, path]) in self.TempMap.iteritems():
-        candidate = output + '---' + '_'.join(path[-n:]) + '---'
+        candidate = pathtools.abspath(MangledName(path))
         if candidate in ConflictMap:
           Conflicts.add(ConflictMap[candidate])
           Conflicts.add(f)
@@ -472,31 +499,27 @@ class TempNameGen(object):
           Log.Fatal('Unable to resolve naming conflicts')
         self.TempMap[f][0] = n+1
 
-    # Clean up the map
+    # Clean up the map and put the paths in tempdir
     NewMap = dict()
     for (f, [n, path]) in self.TempMap.iteritems():
-      candidate = output + '---' + '_'.join(path[-n:]) + '---'
-      NewMap[f] = candidate
+      NewMap[f] = os.path.join(self.TempBase, MangledName(path))
     self.TempMap = NewMap
     return
 
   def TempNameForOutput(self, imtype):
-    temp = self.TempBase + '.' + imtype
-    if not env.getbool('SAVE_TEMPS'):
-      TempFiles.add(temp)
+    temp = os.path.join(self.TempBase, self.Output + '.' + imtype)
+    TempFiles.add(temp)
     return temp
 
   def TempNameForInput(self, input, imtype):
     fullpath = pathtools.abspath(input)
-    # If input is already a temporary name, just change the extension
+    # If input is already a temporary name, just add an extension
     if fullpath.startswith(self.TempBase):
-      temp = self.TempBase + '.' + imtype
+      temp = fullpath + '.' + imtype
     else:
       # Source file
       temp = self.TempMap[fullpath] + '.' + imtype
-
-    if not env.getbool('SAVE_TEMPS'):
-      TempFiles.add(temp)
+    TempFiles.add(temp)
     return temp
 
 # (Invoked from loader.py)
@@ -685,7 +708,16 @@ def DriverMain(module, argv):
 
 
 def SetArch(arch):
-  env.set('ARCH', FixArch(arch))
+  arch = FixArch(arch)
+  env.set('ARCH', arch)
+
+  nonsfi_nacl = False
+  if arch.endswith('_NONSFI'):
+    arch = arch[:-len('_NONSFI')]
+    nonsfi_nacl = True
+  env.set('BASE_ARCH', arch)
+  env.setbool('NONSFI_NACL', nonsfi_nacl)
+
 
 def GetArch(required = False):
   arch = env.getone('ARCH')