Home | History | Annotate | Download | only in bin
      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 """
      9 Script for generating the Android framework's version of Skia from gyp
     10 files.
     11 """
     12 
     13 import os
     14 import shutil
     15 import sys
     16 import tempfile
     17 
     18 # Find the top of trunk
     19 SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
     20 SKIA_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.
     24 ANDROID_TOOLS = os.path.join(SKIA_DIR, 'platform_tools', 'android')
     25 sys.path.append(ANDROID_TOOLS)
     26 
     27 import gyp_gen.android_framework_gyp as android_framework_gyp
     28 import gyp_gen.gypd_parser as gypd_parser
     29 import gyp_gen.generate_user_config as generate_user_config
     30 import gyp_gen.makefile_writer as makefile_writer
     31 import gyp_gen.tool_makefile_writer as tool_makefile_writer
     32 import gyp_gen.vars_dict_lib as vars_dict_lib
     33 
     34 # Folder containing all gyp files and generated gypd files.
     35 GYP_FOLDER = 'gyp'
     36 
     37 
     38 def 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 
     59 def 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         ordered_set=common.DEFINES)
    133 
    134     tool_makefile_writer.generate_tool(gyp_dir=tmp_folder,
    135                                        target_file='tests.gyp',
    136                                        skia_trunk=target_dir,
    137                                        dest_dir='tests',
    138                                        skia_lib_var_dict=common,
    139                                        local_module_name='skia_test',
    140                                        local_module_tags=['tests'])
    141 
    142     tool_makefile_writer.generate_tool(gyp_dir=tmp_folder,
    143                                        target_file='bench.gyp',
    144                                        skia_trunk=target_dir,
    145                                        dest_dir='bench',
    146                                        skia_lib_var_dict=common,
    147                                        local_module_name='skia_bench',
    148                                        local_module_tags=['tests'],
    149                                        place_in_local_tmp=True)
    150 
    151     tool_makefile_writer.generate_tool(gyp_dir=tmp_folder,
    152                                        target_file='gm.gyp',
    153                                        skia_trunk=target_dir,
    154                                        dest_dir='gm',
    155                                        skia_lib_var_dict=common,
    156                                        local_module_name='skia_gm',
    157                                        local_module_tags=['tests'])
    158 
    159     tool_makefile_writer.generate_tool(gyp_dir=tmp_folder,
    160                                        target_file='dm.gyp',
    161                                        skia_trunk=target_dir,
    162                                        dest_dir='dm',
    163                                        skia_lib_var_dict=common,
    164                                        local_module_name='skia_dm',
    165                                        local_module_tags=['tests'])
    166 
    167     # Now that the defines have been written to SkUserConfig and they've been
    168     # used to skip adding them to the tools makefiles, they are not needed in
    169     # Android.mk. Reset DEFINES.
    170     common.DEFINES.reset()
    171 
    172     # Further trim arm_neon_var_dict with arm_var_dict. After this call,
    173     # arm_var_dict (which will now be the intersection) includes all definitions
    174     # used by both arm and arm + neon, and arm_neon_var_dict will only contain
    175     # those specific to arm + neon.
    176     arm_var_dict = vars_dict_lib.intersect([arm_var_dict, arm_neon_var_dict])
    177 
    178     # Now create a list of VarsDictData holding everything but common.
    179     deviations_from_common = []
    180     deviations_from_common.append(makefile_writer.VarsDictData(
    181         arm_var_dict, 'arm'))
    182     deviations_from_common.append(makefile_writer.VarsDictData(
    183         arm_neon_var_dict, 'arm', 'ARCH_ARM_HAVE_NEON'))
    184     deviations_from_common.append(makefile_writer.VarsDictData(x86_var_dict,
    185                                                                'x86'))
    186     # Currently, x86_64 is identical to x86
    187     deviations_from_common.append(makefile_writer.VarsDictData(x86_var_dict,
    188                                                                'x86_64'))
    189 
    190     deviations_from_common.append(makefile_writer.VarsDictData(mips_var_dict,
    191                                                                'mips'))
    192 
    193     deviations_from_common.append(makefile_writer.VarsDictData(mips64_var_dict,
    194                                                                'mips64'))
    195 
    196     deviations_from_common.append(makefile_writer.VarsDictData(arm64_var_dict,
    197                                                                'arm64'))
    198 
    199     makefile_writer.write_android_mk(target_dir=target_dir,
    200         common=common, deviations_from_common=deviations_from_common)
    201 
    202   finally:
    203     shutil.rmtree(tmp_folder)
    204 
    205 if __name__ == '__main__':
    206   main()
    207