9fbd68e20bc023df46aba6649a36b2142ba20b08
[platform/framework/web/crosswalk.git] / src / native_client / toolchain_build / pnacl_commands.py
1 #!/usr/bin/python
2 # Copyright (c) 2013 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.
5
6 """Runnables for toolchain_build_pnacl.py
7 """
8
9 import os
10 import shutil
11 import stat
12 import subprocess
13 import sys
14
15 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
16 import pynacl.file_tools
17 import pynacl.repo_tools
18
19
20 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
21 NACL_DIR = os.path.dirname(SCRIPT_DIR)
22
23 # User-facing tools
24 DRIVER_TOOLS = ['pnacl-' + tool + '.py' for tool in
25                     ('abicheck', 'ar', 'as', 'clang', 'clang++', 'compress', 'dis',
26                      'driver', 'finalize', 'ld', 'nativeld', 'nm', 'opt',
27                      'ranlib', 'readelf', 'strip', 'translate')]
28 # Utilities used by the driver
29 DRIVER_UTILS = [name + '.py' for name in
30                     ('artools', 'driver_env', 'driver_log', 'driver_temps',
31                      'driver_tools', 'elftools', 'filetype', 'ldtools',
32                      'loader', 'pathtools', 'shelltools')]
33
34 def InstallDriverScripts(subst, srcdir, dstdir, host_windows=False,
35                          host_64bit=False, extra_config=[]):
36   srcdir = subst.SubstituteAbsPaths(srcdir)
37   dstdir = subst.SubstituteAbsPaths(dstdir)
38   pynacl.file_tools.MakeDirectoryIfAbsent(os.path.join(dstdir, 'pydir'))
39   for name in DRIVER_TOOLS + DRIVER_UTILS:
40     shutil.copy(os.path.join(srcdir, name), os.path.join(dstdir, 'pydir'))
41   # Install redirector sh/bat scripts
42   for name in DRIVER_TOOLS:
43     # Chop the .py off the name
44     nopy_name = os.path.join(dstdir, os.path.splitext(name)[0])
45     shutil.copy(os.path.join(srcdir, 'redirect.sh'), nopy_name)
46     os.chmod(nopy_name,
47              stat.S_IRUSR | stat.S_IXUSR | stat.S_IWUSR | stat.S_IRGRP |
48              stat.S_IWGRP | stat.S_IXGRP)
49
50     if host_windows:
51       # Windows gets both sh and bat extensions so it works w/cygwin and without
52       batch_script = nopy_name + '.bat'
53       shutil.copy(os.path.join(srcdir, 'redirect.bat'), batch_script)
54       os.chmod(batch_script,
55                stat.S_IRUSR | stat.S_IXUSR | stat.S_IWUSR | stat.S_IRGRP |
56                stat.S_IWGRP | stat.S_IXGRP)
57   # Install the driver.conf file
58   with open(os.path.join(dstdir, 'driver.conf'), 'w') as f:
59     print >> f, 'HAS_FRONTEND=1'
60     print >> f, 'HOST_ARCH=x86_64' if host_64bit else 'HOST_ARCH=x86_32'
61     for line in extra_config:
62       print >> f, subst.Substitute(line)
63   # Install the REV file
64   with open(os.path.join(dstdir, 'REV'), 'w') as f:
65     try:
66       url, rev = pynacl.repo_tools.GitRevInfo(NACL_DIR)
67       repotype = 'GIT'
68     except subprocess.CalledProcessError:
69       url, rev = pynacl.repo_tools.SvnRevInfo(NACL_DIR)
70       repotype = 'SVN'
71     print >> f, '[%s] %s: %s' % (repotype, url, rev)