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