1#!/usr/bin/python
2
3# Copyright 2016 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8
9"""
10Script to build the command buffer shared library and copy it to Skia tree
11"""
12
13
14import argparse
15import os
16import shlex
17import shutil
18import subprocess
19import sys
20
21
22def main():
23  parser = argparse.ArgumentParser(description=('Builds command_buffer_gles2 '
24                                                'library and copies it'))
25  parser.add_argument('-c', '--chrome-dir', required=True, help=
26      'path to Chromium checkout (directory containing .gclient)')
27  parser.add_argument('-o', '--output-dir', required=True,
28      help='path to copy the command buffer shared library to. Typically this '
29           'is out/Debug or out/Release in a Skia repository')
30  parser.add_argument('--make-output-dir', default=False, action='store_true',
31      help='Makes the output directory if it does not already exist.')
32  parser.add_argument('--chrome-out-dir', default='CommandBufferForSkia',
33      help='Type of name of the gn output directory (e.g. Debug or Release). '
34           'This is relative to the Chromium src/out directory. Note that this '
35           'script will reset the gn args in this directory on each run.')
36  parser.add_argument('--extra-gn-args', default='',
37      help=('Extra GN arguments to use for the output directory used to build'
38            'the command buffer'))
39  parser.add_argument('--extra-ninja-args', default='',
40      help=('Extra arguments to pass to ninja when building the command '
41            'buffer shared library'))
42  parser.add_argument('--chrome-revision', default='origin/lkgr',
43      help='Revision (hash, branch, tag) of Chromium to use.')
44  parser.add_argument('--no-sync', action='store_true', default=False,
45      help='Don\'t run git fetch or gclient sync in the Chromium tree')
46  parser.add_argument('--no-hooks', action='store_true', default=False,
47      help='Don\'t run gclient runhooks in the Chromium tree. Implies '
48           '--no-sync')
49  args = parser.parse_args()
50
51  args.chrome_dir = os.path.abspath(args.chrome_dir)
52  args.output_dir = os.path.abspath(args.output_dir)
53
54  if args.no_hooks:
55     args.no_sync = True
56
57  if os.path.isfile(args.chrome_dir):
58    sys.exit(args.chrome_dir + ' exists but is a file.')
59
60  if os.path.isfile(args.output_dir):
61    sys.exit(args.output_dir + ' exists but is a file.')
62
63  chrome_src_dir = os.path.join(args.chrome_dir, 'src')
64
65  if not os.path.isdir(chrome_src_dir):
66    sys.exit(chrome_src_dir + ' is not a directory.')
67
68  if os.path.isfile(args.output_dir):
69    sys.exit(args.output_dir + ' exists but is a file.')
70  elif not os.path.isdir(args.output_dir):
71    if args.make_output_dir:
72      os.makedirs(args.output_dir)
73    else:
74      sys.exit(args.output_dir + ' does not exist (specify --make-output-dir '
75          'to create).')
76
77  chrome_target_dir_rel = os.path.join('out', args.chrome_out_dir)
78
79  # The command buffer shared library will have a different name on Linux,
80  # Mac, and Windows. Also, the name of the gclient executable we call out to
81  # has a .bat file extension on Windows.
82  platform = sys.platform
83  if platform == 'cygwin':
84    platform = 'win32'
85
86  shared_lib_name = 'libcommand_buffer_gles2.so'
87  gclient = 'gclient'
88  if platform == 'darwin':
89    shared_lib_name = 'libcommand_buffer_gles2.dylib'
90  elif platform == 'win32':
91    shared_lib_name = 'command_buffer_gles2.dll'
92    gclient = 'gclient.bat'
93
94  if not args.no_sync:
95    try:
96      subprocess.check_call(['git', 'fetch'], cwd=chrome_src_dir)
97    except subprocess.CalledProcessError as error:
98      sys.exit('Error (ret code: %s) calling "%s" in %s' % (error.returncode,
99          error.cmd, chrome_src_dir))
100
101    try:
102      subprocess.check_call(['git', 'checkout', args.chrome_revision],
103          cwd=chrome_src_dir)
104    except subprocess.CalledProcessError as error:
105      sys.exit('Error (ret code: %s) calling "%s" in %s' % (error.returncode,
106          error.cmd, chrome_src_dir))
107
108    try:
109      os.environ['GYP_GENERATORS'] = 'ninja'
110      subprocess.check_call([gclient, 'sync', '--reset', '--force',
111                             '--nohooks'],
112          cwd=chrome_src_dir)
113    except subprocess.CalledProcessError as error:
114      sys.exit('Error (ret code: %s) calling "%s" in %s' % (error.returncode,
115          error.cmd, chrome_src_dir))
116
117  if not args.no_hooks:
118    try:
119      subprocess.check_call([gclient, 'runhooks'], cwd=chrome_src_dir)
120    except subprocess.CalledProcessError as error:
121      sys.exit('Error (ret code: %s) calling "%s" in %s' % (
122          error.returncode, error.cmd, chrome_src_dir))
123
124  gn = 'gn'
125  platform = 'linux64'
126  if sys.platform == 'darwin':
127    platform = 'mac'
128  elif sys.platform == 'win32':
129    platform = 'win'
130    gn = 'gn.exe'
131  gn = os.path.join(chrome_src_dir, 'buildtools', platform, gn)
132  try:
133    gnargs = 'is_component_build=false is_debug=false ' + args.extra_gn_args
134    subprocess.check_call([gn, 'gen', chrome_target_dir_rel, '--args='+gnargs],
135                          cwd=chrome_src_dir)
136  except subprocess.CalledProcessError as error:
137    sys.exit('Error (ret code: %s) calling "%s" in %s' % (
138        error.returncode, error.cmd, chrome_src_dir))
139
140  try:
141    subprocess.check_call(['ninja'] + shlex.split(args.extra_ninja_args) +
142        ['-C', chrome_target_dir_rel, 'command_buffer_gles2'],
143        cwd=chrome_src_dir)
144  except subprocess.CalledProcessError as error:
145    sys.exit('Error (ret code: %s) calling "%s" in %s' % (error.returncode,
146        error.cmd, chrome_src_dir))
147
148  shared_lib_src_dir = os.path.join(chrome_src_dir, chrome_target_dir_rel)
149
150  shared_lib_src = os.path.join(shared_lib_src_dir, shared_lib_name)
151  shared_lib_dst = os.path.join(args.output_dir, shared_lib_name)
152
153  if not os.path.isfile(shared_lib_src):
154    sys.exit('Command buffer shared library not at expected location: ' +
155        shared_lib_src)
156
157  shutil.copy2(shared_lib_src, shared_lib_dst)
158
159  if not os.path.isfile(shared_lib_dst):
160    sys.exit('Command buffer library not copied to ' + shared_lib_dst)
161
162  print('Command buffer library copied to ' + shared_lib_dst)
163
164
165if __name__ == '__main__':
166  main()
167
168