1#!/usr/bin/python
2
3# Copyright 2014 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"""
9Modified version of gyp_skia, used by gyp_to_android.py to generate Android.mk
10"""
11
12import os
13import sys
14
15SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
16
17# Unlike gyp_skia, this file is nested deep inside Skia. Find Skia's trunk dir.
18# This line depends on the fact that the script is three levels deep
19# (specifically, it is in platform_tools/android/gyp_gen).
20SKIA_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir,
21                                         os.pardir))
22DIR_CONTENTS = os.listdir(SKIA_DIR)
23assert 'gyp' in DIR_CONTENTS
24
25DEBUG_FAILURE = True
26
27def main(target_dir, target_file, skia_arch_type, have_neon,
28         gyp_source_dir=None):
29  """Create gypd files based on target_file.
30
31  Args:
32    target_dir: Directory containing all gyp files, including common.gypi
33    target_file: Gyp file to start on. Other files within target_dir will
34      be read if target_file depends on them.
35    skia_arch_type: Target architecture to pass to gyp.
36    have_neon: Whether to generate files including neon optimizations.
37      Only meaningful if skia_arch_type is 'arm'.
38    gyp_source_dir: Directory of the gyp source code. The default is in
39      third_party/externals/gyp.
40
41  Returns:
42    path: Path to root gypd file created by running gyp.
43  """
44  # Ensure we import our current gyp source's module, not any version
45  # pre-installed in your PYTHONPATH.
46  if not gyp_source_dir:
47    if DEBUG_FAILURE:
48      print 'gyp_source_dir not provided. using the default!'
49    gyp_source_dir = os.path.join(SKIA_DIR, 'third_party', 'externals', 'gyp')
50
51  if DEBUG_FAILURE:
52    print 'gyp_source_dir is "%s"' % gyp_source_dir
53    if not os.path.exists(gyp_source_dir):
54      print 'and it does not exist!'
55
56  assert os.path.exists(gyp_source_dir)
57
58  sys.path.insert(0, os.path.join(gyp_source_dir, 'pylib'))
59
60  import gyp
61
62  # Set GYP_DEFINES for building for the android framework.
63  gyp_defines = ('skia_android_framework=1 OS=android skia_arch_type=%s '
64                 % skia_arch_type)
65  if skia_arch_type == 'arm':
66    # Always version 7 (which implies thumb) for arm
67    gyp_defines += 'arm_version=7 '
68    if have_neon:
69      gyp_defines += 'arm_neon=1 '
70    else:
71      gyp_defines += 'arm_neon=0 '
72
73  os.environ['GYP_DEFINES'] = gyp_defines
74
75  args = []
76  args.extend(['--depth', '.'])
77  full_path = os.path.join(target_dir, target_file)
78  args.extend([full_path])
79  # Common conditions
80  args.extend(['-I', os.path.join(target_dir, 'common.gypi')])
81  # Use the debugging format. We'll use these to create one master make file.
82  args.extend(['-f', 'gypd'])
83
84  # Off we go...
85  ret = gyp.main(args)
86
87  if ret != 0:
88    raise Exception("gyp failed!")
89
90  # Running gyp should have created a gypd file, with the same name as
91  # full_path but with a 'd' on the end.
92  gypd_file = full_path + 'd'
93  if not os.path.exists(gypd_file):
94    raise Exception("gyp failed to produce gypd file!")
95
96  return gypd_file
97
98
99def clean_gypd_files(folder):
100  """Remove the gypd files generated by main().
101
102  Args:
103    folder: Folder in which to delete all files ending with 'gypd'.
104  """
105  assert os.path.isdir(folder)
106  files = os.listdir(folder)
107  for f in files:
108    if f.endswith('gypd'):
109      os.remove(os.path.join(folder, f))
110