Upstream version 9.38.198.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',
27                      'dis', 'driver', 'finalize', 'ld', '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', 'nativeld', 'pathtools', 'shelltools')]
34
35 def InstallDriverScripts(logger, subst, srcdir, dstdir, host_windows=False,
36                          host_64bit=False, extra_config=[]):
37   srcdir = subst.SubstituteAbsPaths(srcdir)
38   dstdir = subst.SubstituteAbsPaths(dstdir)
39   logger.debug('Installing Driver Scripts: %s -> %s', srcdir, dstdir)
40
41   pynacl.file_tools.MakeDirectoryIfAbsent(os.path.join(dstdir, 'pydir'))
42   for name in DRIVER_TOOLS + DRIVER_UTILS:
43     source = os.path.join(srcdir, name)
44     destination = os.path.join(dstdir, 'pydir')
45     logger.debug('  Installing: %s -> %s', source, destination)
46     shutil.copy(source, destination)
47   # Install redirector sh/bat scripts
48   for name in DRIVER_TOOLS:
49     # Chop the .py off the name
50     source = os.path.join(srcdir, 'redirect.sh')
51     destination = os.path.join(dstdir, os.path.splitext(name)[0])
52     logger.debug('  Installing: %s -> %s', source, destination)
53     shutil.copy(source, destination)
54     os.chmod(destination,
55              stat.S_IRUSR | stat.S_IXUSR | stat.S_IWUSR | stat.S_IRGRP |
56              stat.S_IWGRP | stat.S_IXGRP)
57
58     if host_windows:
59       # Windows gets both sh and bat extensions so it works w/cygwin and without
60       win_source = os.path.join(srcdir, 'redirect.bat')
61       win_dest = destination + '.bat'
62       logger.debug('  Installing: %s -> %s', win_source, win_dest)
63       shutil.copy(win_source, win_dest)
64       os.chmod(win_dest,
65                stat.S_IRUSR | stat.S_IXUSR | stat.S_IWUSR | stat.S_IRGRP |
66                stat.S_IWGRP | stat.S_IXGRP)
67   # Install the driver.conf file
68   driver_conf = os.path.join(dstdir, 'driver.conf')
69   logger.debug('  Installing: %s', driver_conf)
70   with open(driver_conf, 'w') as f:
71     print >> f, 'HAS_FRONTEND=1'
72     print >> f, 'HOST_ARCH=x86_64' if host_64bit else 'HOST_ARCH=x86_32'
73     for line in extra_config:
74       print >> f, subst.Substitute(line)
75   # Install the REV file
76   rev_file = os.path.join(dstdir, 'REV')
77   logger.debug('  Installing: %s', rev_file)
78   with open(rev_file, 'w') as f:
79     try:
80       url, rev = pynacl.repo_tools.GitRevInfo(NACL_DIR)
81       repotype = 'GIT'
82     except subprocess.CalledProcessError:
83       url, rev = pynacl.repo_tools.SvnRevInfo(NACL_DIR)
84       repotype = 'SVN'
85     print >> f, '[%s] %s: %s' % (repotype, url, rev)
86
87
88 def CheckoutGitBundleForTrybot(repo, destination):
89   # For testing LLVM, Clang, etc. changes on the trybots, look for a
90   # Git bundle file created by llvm_change_try_helper.sh.
91   bundle_file = os.path.join(NACL_DIR, 'pnacl', 'not_for_commit',
92                              '%s_bundle' % repo)
93   base64_file = '%s.b64' % bundle_file
94   if os.path.exists(base64_file):
95     input_fh = open(base64_file, 'r')
96     output_fh = open(bundle_file, 'wb')
97     base64.decode(input_fh, output_fh)
98     input_fh.close()
99     output_fh.close()
100     subprocess.check_call(
101         pynacl.repo_tools.GitCmd() + ['fetch'],
102         cwd=destination
103     )
104     subprocess.check_call(
105         pynacl.repo_tools.GitCmd() + ['bundle', 'unbundle', bundle_file],
106         cwd=destination
107     )
108     commit_id_file = os.path.join(NACL_DIR, 'pnacl', 'not_for_commit',
109                                   '%s_commit_id' % repo)
110     commit_id = open(commit_id_file, 'r').readline().strip()
111     subprocess.check_call(
112         pynacl.repo_tools.GitCmd() + ['checkout', commit_id],
113         cwd=destination
114     )
115
116 def CmdCheckoutGitBundleForTrybot(logger, subst, repo, destination):
117   destination = subst.SubstituteAbsPaths(destination)
118   logger.debug('Checking out Git Bundle for Trybot: %s [%s]', destination, repo)
119   return CheckoutGitBundleForTrybot(repo, destination)