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"""
9Script for generating the Android framework's version of Skia from gyp
10files.
11"""
12
13import argparse
14import os
15import shutil
16import sys
17import tempfile
18
19# Find the top of trunk
20SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
21SKIA_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir,
22                                         os.pardir))
23
24# Find the directory with our helper files, and add it to the path.
25ANDROID_TOOLS = os.path.join(SKIA_DIR, 'platform_tools', 'android')
26sys.path.append(ANDROID_TOOLS)
27
28import gyp_gen.android_framework_gyp as android_framework_gyp
29import gyp_gen.gypd_parser as gypd_parser
30import gyp_gen.generate_user_config as generate_user_config
31import gyp_gen.makefile_writer as makefile_writer
32import gyp_gen.tool_makefile_writer as tool_makefile_writer
33import gyp_gen.vars_dict_lib as vars_dict_lib
34
35# Folder containing all gyp files and generated gypd files.
36GYP_FOLDER = 'gyp'
37
38
39def generate_var_dict(target_dir, target_file, skia_arch_type, have_neon,
40                      gyp_source_dir):
41  """Create a VarsDict for a particular arch type.
42
43  Each paramater is passed directly to android_framework_gyp.main().
44
45  Args:
46    target_dir: Directory containing gyp files.
47    target_file: Target gyp file.
48    skia_arch_type: Target architecture.
49    have_neon: Whether the target should build for neon.
50    gyp_source_dir: Directory for gyp source.
51  Returns:
52    A VarsDict containing the variable definitions determined by gyp.
53  """
54  result_file = android_framework_gyp.main(target_dir, target_file,
55                                           skia_arch_type, have_neon,
56                                           gyp_source_dir)
57  var_dict = vars_dict_lib.VarsDict()
58  gypd_parser.parse_gypd(var_dict, result_file, '.')
59  android_framework_gyp.clean_gypd_files(target_dir)
60  print '.',
61  return var_dict
62
63def main(target_dir=None, require_sk_user_config=False, gyp_source_dir=None):
64  """Create Android.mk for the Android framework's external/skia.
65
66  Builds Android.mk using Skia's gyp files.
67
68  Args:
69    target_dir: Directory in which to place 'Android.mk'. If None, the file
70      will be placed in skia's root directory.
71    require_sk_user_config: If True, raise an AssertionError if
72      SkUserConfig.h does not exist.
73    gyp_source_dir: Source directory for gyp.
74  """
75  # Create a temporary folder to hold gyp and gypd files. Create it in SKIA_DIR
76  # so that it is a sibling of gyp/, so the relationships between gyp files and
77  # other files (e.g. platform_tools/android/gyp/dependencies.gypi, referenced
78  # by android_deps.gyp as a relative path) is unchanged.
79  # Use mkdtemp to find an unused folder name, but then delete it so copytree
80  # can be called with a non-existent directory.
81  tmp_folder = tempfile.mkdtemp(dir=SKIA_DIR)
82  os.rmdir(tmp_folder)
83  shutil.copytree(os.path.join(SKIA_DIR, GYP_FOLDER), tmp_folder)
84
85  try:
86    main_gyp_file = 'android_framework_lib.gyp'
87
88    print 'Creating Android.mk',
89
90    # Generate a separate VarsDict for each architecture type.  For each
91    # archtype:
92    # 1. call android_framework_gyp.main() to generate gypd files
93    # 2. call parse_gypd to read those gypd files into the VarsDict
94    # 3. delete the gypd files
95    #
96    # Once we have the VarsDict for each architecture type, we combine them all
97    # into a single Android.mk file, which can build targets of any
98    # architecture type.
99
100    # The default uses a non-existant archtype, to find all the general
101    # variable definitions.
102    default_var_dict = generate_var_dict(tmp_folder, main_gyp_file, 'other',
103                                         False, gyp_source_dir)
104    arm_var_dict = generate_var_dict(tmp_folder, main_gyp_file, 'arm', False,
105                                     gyp_source_dir)
106    arm_neon_var_dict = generate_var_dict(tmp_folder, main_gyp_file, 'arm',
107                                          True, gyp_source_dir)
108    x86_var_dict = generate_var_dict(tmp_folder, main_gyp_file, 'x86', False,
109                                     gyp_source_dir)
110    x86_64_var_dict = generate_var_dict(tmp_folder, main_gyp_file, 'x86_64',
111                                        False, gyp_source_dir)
112
113    mips_var_dict = generate_var_dict(tmp_folder, main_gyp_file, 'mips', False,
114                                      gyp_source_dir)
115
116    mips64_var_dict = generate_var_dict(tmp_folder, main_gyp_file, 'mips64',
117                                        False, gyp_source_dir)
118
119    arm64_var_dict = generate_var_dict(tmp_folder, main_gyp_file, 'arm64',
120                                       False, gyp_source_dir)
121
122    # Compute the intersection of all targets. All the files in the intersection
123    # should be part of the makefile always. Each dict will now contain trimmed
124    # lists containing only variable definitions specific to that configuration.
125    var_dict_list = [default_var_dict, arm_var_dict, arm_neon_var_dict,
126                     x86_var_dict, x86_64_var_dict, mips_var_dict,
127                     mips64_var_dict, arm64_var_dict]
128    common = vars_dict_lib.intersect(var_dict_list)
129
130    common.LOCAL_MODULE.add('libskia')
131
132    # Create SkUserConfig
133    user_config = os.path.join(SKIA_DIR, 'include', 'config', 'SkUserConfig.h')
134    if target_dir:
135      dst_dir = target_dir
136    else:
137      dst_dir = os.path.join(SKIA_DIR, 'include', 'core')
138
139    generate_user_config.generate_user_config(
140        original_sk_user_config=user_config,
141        require_sk_user_config=require_sk_user_config, target_dir=dst_dir,
142        defines=common.DEFINES)
143
144    tool_makefile_writer.generate_tool(gyp_dir=tmp_folder,
145                                       target_file='bench.gyp',
146                                       skia_trunk=target_dir,
147                                       dest_dir='bench',
148                                       skia_lib_var_dict=common,
149                                       local_module_name='skia_nanobench',
150                                       local_module_tags=['tests'],
151                                       desired_targets=['nanobench'],
152                                       gyp_source_dir=gyp_source_dir)
153
154    tool_makefile_writer.generate_tool(gyp_dir=tmp_folder,
155                                       target_file='dm.gyp',
156                                       skia_trunk=target_dir,
157                                       dest_dir='dm',
158                                       skia_lib_var_dict=common,
159                                       local_module_name='skia_dm',
160                                       local_module_tags=['tests'],
161                                       desired_targets=['dm'],
162                                       gyp_source_dir=gyp_source_dir)
163
164    # Now that the defines have been written to SkUserConfig and they've been
165    # used to skip adding them to the tools makefiles, they are not needed in
166    # Android.mk. Reset DEFINES.
167    common.DEFINES.reset()
168
169    # Further trim arm_neon_var_dict with arm_var_dict. After this call,
170    # arm_var_dict (which will now be the intersection) includes all definitions
171    # used by both arm and arm + neon, and arm_neon_var_dict will only contain
172    # those specific to arm + neon.
173    arm_var_dict = vars_dict_lib.intersect([arm_var_dict, arm_neon_var_dict])
174
175    # Now create a list of VarsDictData holding everything but common.
176    deviations_from_common = []
177    deviations_from_common.append(makefile_writer.VarsDictData(
178        arm_var_dict, 'arm'))
179    deviations_from_common.append(makefile_writer.VarsDictData(
180        arm_neon_var_dict, 'arm', 'ARCH_ARM_HAVE_NEON'))
181    deviations_from_common.append(makefile_writer.VarsDictData(x86_var_dict,
182                                                               'x86'))
183    deviations_from_common.append(makefile_writer.VarsDictData(x86_64_var_dict,
184                                                               'x86_64'))
185
186    deviations_from_common.append(makefile_writer.VarsDictData(mips_var_dict,
187                                                               'mips'))
188
189    deviations_from_common.append(makefile_writer.VarsDictData(mips64_var_dict,
190                                                               'mips64'))
191
192    deviations_from_common.append(makefile_writer.VarsDictData(arm64_var_dict,
193                                                               'arm64'))
194
195    makefile_writer.write_android_mk(target_dir=target_dir,
196        common=common, deviations_from_common=deviations_from_common)
197
198  finally:
199    shutil.rmtree(tmp_folder)
200
201if __name__ == '__main__':
202  parser = argparse.ArgumentParser()
203  parser.add_argument('--gyp_source_dir', help='Source of gyp program. '
204                      'e.g. <path_to_skia>/third_party/externals/gyp')
205  args = parser.parse_args()
206
207  main(gyp_source_dir=args.gyp_source_dir)
208