Upstream version 7.36.149.0
[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 base64
10 import os
11 import shutil
12 import stat
13 import subprocess
14 import sys
15
16 sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
17 import pynacl.file_tools
18 import pynacl.repo_tools
19
20
21 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
22 NACL_DIR = os.path.dirname(SCRIPT_DIR)
23
24 # User-facing tools
25 DRIVER_TOOLS = ['pnacl-' + tool + '.py' for tool in
26                     ('abicheck', 'ar', 'as', 'clang', 'clang++', 'compress', 'dis',
27                      'driver', 'finalize', 'ld', 'nativeld', 'nm', 'opt',
28                      'ranlib', 'readelf', 'strip', 'translate')]
29 # Utilities used by the driver
30 DRIVER_UTILS = [name + '.py' for name in
31                     ('artools', 'driver_env', 'driver_log', 'driver_temps',
32                      'driver_tools', 'elftools', 'filetype', 'ldtools',
33                      'loader', 'pathtools', 'shelltools')]
34
35 def InstallDriverScripts(subst, srcdir, dstdir, host_windows=False,
36                          host_64bit=False, extra_config=[]):
37   srcdir = subst.SubstituteAbsPaths(srcdir)
38   dstdir = subst.SubstituteAbsPaths(dstdir)
39   pynacl.file_tools.MakeDirectoryIfAbsent(os.path.join(dstdir, 'pydir'))
40   for name in DRIVER_TOOLS + DRIVER_UTILS:
41     shutil.copy(os.path.join(srcdir, name), os.path.join(dstdir, 'pydir'))
42   # Install redirector sh/bat scripts
43   for name in DRIVER_TOOLS:
44     # Chop the .py off the name
45     nopy_name = os.path.join(dstdir, os.path.splitext(name)[0])
46     shutil.copy(os.path.join(srcdir, 'redirect.sh'), nopy_name)
47     os.chmod(nopy_name,
48              stat.S_IRUSR | stat.S_IXUSR | stat.S_IWUSR | stat.S_IRGRP |
49              stat.S_IWGRP | stat.S_IXGRP)
50
51     if host_windows:
52       # Windows gets both sh and bat extensions so it works w/cygwin and without
53       batch_script = nopy_name + '.bat'
54       shutil.copy(os.path.join(srcdir, 'redirect.bat'), batch_script)
55       os.chmod(batch_script,
56                stat.S_IRUSR | stat.S_IXUSR | stat.S_IWUSR | stat.S_IRGRP |
57                stat.S_IWGRP | stat.S_IXGRP)
58   # Install the driver.conf file
59   with open(os.path.join(dstdir, 'driver.conf'), 'w') as f:
60     print >> f, 'HAS_FRONTEND=1'
61     print >> f, 'HOST_ARCH=x86_64' if host_64bit else 'HOST_ARCH=x86_32'
62     for line in extra_config:
63       print >> f, subst.Substitute(line)
64   # Install the REV file
65   with open(os.path.join(dstdir, 'REV'), 'w') as f:
66     try:
67       url, rev = pynacl.repo_tools.GitRevInfo(NACL_DIR)
68       repotype = 'GIT'
69     except subprocess.CalledProcessError:
70       url, rev = pynacl.repo_tools.SvnRevInfo(NACL_DIR)
71       repotype = 'SVN'
72     print >> f, '[%s] %s: %s' % (repotype, url, rev)
73
74
75 def CheckoutGitBundleForTrybot(repo, destination):
76   # For testing LLVM, Clang, etc. changes on the trybots, look for a
77   # Git bundle file created by llvm_change_try_helper.sh.
78   bundle_file = os.path.join(NACL_DIR, 'pnacl', 'not_for_commit',
79                              '%s_bundle' % repo)
80   base64_file = '%s.b64' % bundle_file
81   if os.path.exists(base64_file):
82     input_fh = open(base64_file, 'r')
83     output_fh = open(bundle_file, 'wb')
84     base64.decode(input_fh, output_fh)
85     input_fh.close()
86     output_fh.close()
87     subprocess.check_call(
88         pynacl.repo_tools.GitCmd() + ['fetch'],
89         cwd=destination
90     )
91     subprocess.check_call(
92         pynacl.repo_tools.GitCmd() + ['bundle', 'unbundle', bundle_file],
93         cwd=destination
94     )
95     commit_id_file = os.path.join(NACL_DIR, 'pnacl', 'not_for_commit',
96                                   '%s_commit_id' % repo)
97     commit_id = open(commit_id_file, 'r').readline().strip()
98     subprocess.check_call(
99         pynacl.repo_tools.GitCmd() + ['checkout', commit_id],
100         cwd=destination
101     )
102
103 def CmdCheckoutGitBundleForTrybot(subst, repo, destination):
104   return CheckoutGitBundleForTrybot(repo, subst.SubstituteAbsPaths(destination))