f217d3204e38c0971d03f947624e498e30c83bb4
[platform/framework/web/crosswalk.git] / src / native_client / buildbot / buildbot_pnacl_toolchain.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 import argparse
7 import logging
8 import os
9 import platform
10 import subprocess
11 import sys
12
13 import buildbot_lib
14
15 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
16 NACL_DIR = os.path.dirname(SCRIPT_DIR)
17
18 # As this is a buildbot script, we want verbose logging. Note however, that
19 # toolchain_build has its own log settings, controlled by its CLI flags.
20 logging.getLogger().setLevel(logging.DEBUG)
21
22 parser = argparse.ArgumentParser(description='PNaCl toolchain buildbot script')
23 group = parser.add_mutually_exclusive_group()
24 group.add_argument('--buildbot', action='store_true',
25                  help='Buildbot mode (build and archive the toolchain)')
26 group.add_argument('--trybot', action='store_true',
27                  help='Trybot mode (build but do not archove the toolchain)')
28 args = parser.parse_args()
29
30 host_os = buildbot_lib.GetHostPlatform()
31
32 # This is a minimal context, not useful for running tests yet, but enough for
33 # basic Step handling.
34 context = buildbot_lib.BuildContext()
35 status = buildbot_lib.BuildStatus(context)
36
37 with buildbot_lib.Step('Update cygwin/check bash', status, halt_on_fail=True):
38   # Update cygwin if necessary.
39   if host_os == 'win':
40     if sys.platform == 'cygwin':
41       print 'This script does not support running from inside cygwin!'
42       sys.exit(1)
43     subprocess.check_call(os.path.join(SCRIPT_DIR, 'cygwin_env.bat'))
44     print os.environ['PATH']
45     paths = os.environ['PATH'].split(os.pathsep)
46     # Put path to cygwin tools at the beginning, so cygwin tools like python
47     # and cmake will supercede others (which do not understand cygwin paths)
48     paths = [os.path.join(NACL_DIR, 'cygwin', 'bin')] + paths
49     print paths
50     os.environ['PATH'] = os.pathsep.join(paths)
51     print os.environ['PATH']
52     bash = os.path.join(NACL_DIR, 'cygwin', 'bin', 'bash')
53   else:
54     # Assume bash is in the path
55     bash = 'bash'
56
57   try:
58     print 'Bash version:'
59     sys.stdout.flush()
60     subprocess.check_call([bash , '--version'])
61   except subprocess.CalledProcessError:
62     print 'Bash not found in path!'
63     raise buildbot_lib.StepFailed()
64
65 toolchain_build_cmd = [
66     sys.executable,
67     os.path.join(
68         NACL_DIR, 'toolchain_build', 'toolchain_build_pnacl.py'),
69     '--verbose', '--sync', '--clobber']
70
71 # Sync the git repos used by build.sh
72 with buildbot_lib.Step('Sync build.sh repos', status, halt_on_fail=True):
73   buildbot_lib.Command(context, toolchain_build_cmd + ['--legacy-repo-sync'])
74
75 # Run toolchain_build.py first. Its outputs are not actually being used yet.
76 # toolchain_build outputs its own buildbot annotations, so don't use
77 # buildbot_lib.Step to run it here.
78 try:
79   gsd_arg = []
80   if args.buildbot:
81     gsd_arg = ['--buildbot']
82   elif args.trybot:
83     gsd_arg = ['--trybot']
84
85   cmd = toolchain_build_cmd + gsd_arg
86   logging.info('Running: ' + ' '.join(cmd))
87   subprocess.check_call(cmd)
88 except subprocess.CalledProcessError:
89   # Ignore any failures and keep going (but make the bot stage red).
90   if host_os == 'win':
91     print '@@@STEP_WARNINGS@@@'
92   else:
93     print '@@@STEP_FAILURE@@@'
94   sys.stdout.flush()
95
96
97 # Now we run the PNaCl buildbot script. It in turn runs the PNaCl build.sh
98 # script and runs scons tests.
99 # TODO(dschuff): re-implement the test-running portion of buildbot_pnacl.sh
100 # using buildbot_lib, and use them here and in the non-toolchain builder.
101 buildbot_shell = os.path.join(NACL_DIR, 'buildbot', 'buildbot_pnacl.sh')
102
103 # Because patching mangles the shell script on the trybots, fix it up here
104 # so we can have working windows trybots.
105 def FixCRLF(f):
106   with open(f, 'rb') as script:
107     data = script.read().replace('\r\n', '\n')
108   with open(f, 'wb') as script:
109     script.write(data)
110
111 if host_os == 'win':
112   FixCRLF(buildbot_shell)
113   FixCRLF(os.path.join(NACL_DIR, 'pnacl', 'build.sh'))
114   FixCRLF(os.path.join(NACL_DIR, 'pnacl', 'scripts', 'common-tools.sh'))
115
116 # Generate flags for buildbot_pnacl.sh
117 if host_os == 'linux':
118   arg_os = 'linux'
119   # TODO(dschuff): Figure out if it makes sense to import the utilities from
120   # build/ into scripts from buildbot/ or only use things from buildbot_lib,
121   # or unify them in some way.
122   arch = 'x8664' if platform.machine() == 'x86_64' else 'x8632'
123 elif host_os == 'mac':
124   arg_os = 'mac'
125   arch = 'x8632'
126 elif host_os == 'win':
127   arg_os = 'win'
128   arch = 'x8664'
129 else:
130   print 'Unrecognized platform: ', host_os
131   sys.exit(1)
132
133 if args.buildbot:
134   trybot_mode = 'false'
135 elif args.trybot:
136   trybot_mode = 'true'
137
138 platform_arg = 'mode-buildbot-tc-' + arch + '-' + arg_os
139
140 command = [bash,
141            buildbot_shell,
142            platform_arg,
143            trybot_mode]
144 logging.info('Running: ' + ' '.join(command))
145 subprocess.check_call(command)