- add sources.
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / build_tools / nacl-mono-builder.py
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium 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 optparse
7 import os
8 import sys
9 import tarfile
10
11 import buildbot_common
12 from build_paths import SCRIPT_DIR
13
14 SDK_BUILD_DIR = SCRIPT_DIR
15 MONO_BUILD_DIR = os.path.join(SDK_BUILD_DIR, 'mono_build')
16 MONO_DIR = os.path.join(MONO_BUILD_DIR, 'nacl-mono')
17
18
19 def main(args):
20   parser = optparse.OptionParser()
21   parser.add_option('--arch',
22                     help='Target architecture',
23                     dest='arch',
24                     default='x86-32')
25   parser.add_option('--sdk-revision',
26                     help='SDK Revision'
27                          ' (default=buildbot revision)',
28                     dest='sdk_revision',
29                     default=None)
30   parser.add_option('--sdk-url',
31                     help='SDK Download URL',
32                     dest='sdk_url',
33                     default=None)
34   parser.add_option('--install-dir',
35                     help='Install Directory',
36                     dest='install_dir',
37                     default='naclmono')
38   (options, args) = parser.parse_args(args[1:])
39
40   assert sys.platform.find('linux') != -1
41
42   buildbot_revision = os.environ.get('BUILDBOT_REVISION', '')
43
44   build_prefix = options.arch + ' '
45
46   buildbot_common.BuildStep(build_prefix + 'Clean Old SDK')
47   buildbot_common.MakeDir(MONO_BUILD_DIR)
48   buildbot_common.RemoveDir(os.path.join(MONO_BUILD_DIR, 'pepper_*'))
49
50   buildbot_common.BuildStep(build_prefix + 'Setup New SDK')
51   sdk_dir = None
52   sdk_revision = options.sdk_revision
53   sdk_url = options.sdk_url
54   if not sdk_url:
55     if not sdk_revision:
56       assert buildbot_revision
57       sdk_revision = buildbot_revision.split(':')[0]
58     sdk_url = 'gs://nativeclient-mirror/nacl/nacl_sdk/'\
59               'trunk.%s/naclsdk_linux.tar.bz2' % sdk_revision
60
61   sdk_url = sdk_url.replace('https://commondatastorage.googleapis.com/',
62                             'gs://')
63
64   sdk_file = sdk_url.split('/')[-1]
65
66   buildbot_common.Run([buildbot_common.GetGsutil(), 'cp', sdk_url, sdk_file],
67                       cwd=MONO_BUILD_DIR)
68   tar_file = None
69   try:
70     tar_file = tarfile.open(os.path.join(MONO_BUILD_DIR, sdk_file))
71     pepper_dir = os.path.commonprefix(tar_file.getnames())
72     tar_file.extractall(path=MONO_BUILD_DIR)
73     sdk_dir = os.path.join(MONO_BUILD_DIR, pepper_dir)
74   finally:
75     if tar_file:
76       tar_file.close()
77
78   assert sdk_dir
79
80   buildbot_common.BuildStep(build_prefix + 'Checkout Mono')
81   # TODO(elijahtaylor): Get git URL from master/trigger to make this
82   # more flexible for building from upstream and release branches.
83   if options.arch == 'arm':
84     git_url = 'git://github.com/igotti-google/mono.git'
85     git_rev = 'arm_nacl'
86   else:
87     git_url = 'git://github.com/elijahtaylor/mono.git'
88     git_rev = 'HEAD'
89   if buildbot_revision:
90     # Unfortunately, we use different git branches/revisions
91     # for ARM and x86 now, so ignore buildbot_revision variable for ARM.
92     # Need to rethink this approach, if we'll plan to support
93     # more flexible repo selection mechanism.
94     if options.arch != 'arm':
95       git_rev = buildbot_revision.split(':')[1]
96   # ARM and x86 is built out of different git trees, so distinguish
97   # them by appending the arch. It also makes 32 and 64 bit x86 separated,
98   # which is good.
99   # TODO(olonho): maybe we need to avoid modifications of global.
100   global MONO_DIR
101   tag = options.arch
102   MONO_DIR = "%s-%s" % (MONO_DIR, tag)
103   if not os.path.exists(MONO_DIR):
104     buildbot_common.MakeDir(MONO_DIR)
105     buildbot_common.Run(['git', 'clone', git_url, MONO_DIR])
106   else:
107     buildbot_common.Run(['git', 'fetch'], cwd=MONO_DIR)
108   if git_rev:
109     buildbot_common.Run(['git', 'checkout', git_rev], cwd=MONO_DIR)
110
111   arch_to_bitsize = {'x86-32': '32',
112                      'x86-64': '64',
113                      'arm':    'arm'}
114   arch_to_output_folder = {'x86-32': 'runtime-x86-32-build',
115                            'x86-64': 'runtime-x86-64-build',
116                            'arm':    'runtime-arm-build'}
117
118   buildbot_common.BuildStep(build_prefix + 'Configure Mono')
119   os.environ['NACL_SDK_ROOT'] = sdk_dir
120   os.environ['TARGET_ARCH'] = options.arch
121   os.environ['TARGET_BITSIZE'] = arch_to_bitsize[options.arch]
122   buildbot_common.Run(['./autogen.sh'], cwd=MONO_DIR)
123   buildbot_common.Run(['make', 'distclean'], cwd=MONO_DIR)
124
125   buildbot_common.BuildStep(build_prefix + 'Build and Install Mono')
126   nacl_interp_script = os.path.join(SDK_BUILD_DIR, 'nacl_interp_loader_mono.sh')
127   os.environ['NACL_INTERP_LOADER'] = nacl_interp_script
128   buildbot_common.Run(['./nacl-mono-runtime.sh',
129                       MONO_DIR, # Mono directory with 'configure'
130                       arch_to_output_folder[options.arch], # Build dir
131                       options.install_dir],
132                       cwd=SDK_BUILD_DIR)
133
134   # TODO(elijahtaylor,olonho): Re-enable tests on arm when they compile/run.
135   if options.arch != 'arm':
136     buildbot_common.BuildStep(build_prefix + 'Test Mono')
137     buildbot_common.Run(['make', 'check', '-j8'],
138         cwd=os.path.join(SDK_BUILD_DIR, arch_to_output_folder[options.arch]))
139
140   return 0
141
142
143 if __name__ == '__main__':
144   sys.exit(main(sys.argv))