1# Copyright 2018 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6"""Shared utilities for the build recipe module."""
7
8
9# This lists the products we want to isolate as outputs for future steps.
10DEFAULT_BUILD_PRODUCTS = [
11  'dm',
12  'dm.exe',
13  'dm.app',
14  'fm',
15  'fm.exe',
16  'nanobench.app',
17  'get_images_from_skps',
18  'get_images_from_skps.exe',
19  'nanobench',
20  'nanobench.exe',
21  'skpbench',
22  'skpbench.exe',
23  '*.so',
24  '*.dll',
25  '*.dylib',
26  'skia_launcher',
27  'skottie_tool',
28  'lib/*.so',
29  'run_testlab',
30]
31
32def py_to_gn(val):
33  """Convert val to a string that can be used as GN args."""
34  if isinstance(val, bool):
35    return 'true' if val else 'false'
36  elif '%s' % val == val:
37    # TODO(dogben): Handle quoting "$\
38    return '"%s"' % val
39  elif isinstance(val, (list, tuple)):
40    return '[%s]' % (','.join(py_to_gn(x) for x in val))
41  elif isinstance(val, dict):
42    gn = ' '.join(
43        '%s=%s' % (k, py_to_gn(v)) for (k, v) in sorted(val.items()))
44    return gn
45  else:  # pragma: nocover
46    raise Exception('Converting %s to gn is not implemented.' % type(val))
47
48
49def copy_listed_files(api, src, dst, product_list):
50  """Copy listed files src to dst."""
51  api.python.inline(
52      name='copy build products',
53      program='''import errno
54import glob
55import os
56import shutil
57import sys
58
59src = sys.argv[1]
60dst = sys.argv[2]
61build_products = %s
62
63try:
64  os.makedirs(dst)
65except OSError as e:
66  if e.errno != errno.EEXIST:
67    raise
68
69for pattern in build_products:
70  path = os.path.join(src, pattern)
71  for f in glob.glob(path):
72    dst_path = os.path.join(dst, os.path.relpath(f, src))
73    if not os.path.isdir(os.path.dirname(dst_path)):
74      os.makedirs(os.path.dirname(dst_path))
75    print('Copying build product %%s to %%s' %% (f, dst_path))
76    shutil.move(f, dst_path)
77''' % str(product_list),
78      args=[src, dst],
79      infra_step=True)
80