Home | History | Annotate | Download | only in ndk
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2015 Google Inc. All rights reserved.
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #     http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 import os
     18 
     19 
     20 def local_path(path):
     21     return os.path.normpath(os.path.join(os.path.dirname(__file__), path))
     22 
     23 
     24 def find(path, names):
     25     found = []
     26     for root, _, files in os.walk(path):
     27         for file_name in sorted(files):
     28             if file_name in names:
     29                 abspath = os.path.abspath(os.path.join(root, file_name))
     30                 rel_to_root = abspath.replace(os.path.abspath(path), '')
     31                 found.append(rel_to_root[1:])  # strip leading /
     32     return found
     33 
     34 
     35 def sdk_version_from_path(path):
     36     return int(path.split('/')[0].split('-')[1])
     37 
     38 
     39 def sdk_versions():
     40     versions = []
     41     for sdk in os.listdir(local_path('current/platforms')):
     42         if sdk.startswith('android-'):
     43             versions.append(sdk_version_from_path(sdk))
     44     return sorted(versions)
     45 
     46 
     47 def gen_defaults():
     48     defaults = []
     49     for sdk in sdk_versions():
     50         default = []
     51         arch_flags = []
     52 
     53         for arch in ['arm', 'arm64', 'mips', 'mips64', 'x86', 'x86_64']:
     54             arch_path = local_path(
     55                 'current/platforms/android-{sdk}/arch-{arch}/usr/include'.format(sdk=sdk, arch=arch))
     56             if os.path.exists(arch_path):
     57                 arch_flags.append(
     58                     '        {arch}: {{\n'
     59                     '            export_include_dirs: ["{includes}"],\n'
     60                     '        }},'.format(arch=arch, includes=arch_path))
     61 
     62         default.append('cc_defaults {{\n'
     63                        '    name: "ndk_{version}_defaults",\n'
     64                        '    sdk_version: "{version}",'.format(version=sdk))
     65         if len(arch_flags) > 0:
     66             default.append('    arch: {{\n{arch_flags}\n'
     67                            '    }},'.format(arch_flags='\n'.join(arch_flags)))
     68         default.append('}')
     69         defaults.append('\n'.join(default))
     70     return defaults
     71 
     72 
     73 def get_prebuilts(names):
     74     prebuilts_path = local_path('current/platforms')
     75     prebuilts = find(prebuilts_path, names)
     76     prebuilts = [p for p in prebuilts if 'arch-arm/' in p]
     77     prebuilts.sort(key=sdk_version_from_path)
     78     return prebuilts
     79 
     80 
     81 def gen_lib_prebuilt(prebuilt, name, version):
     82     return ('ndk_prebuilt_library {{\n'
     83             '    name: "{name}.ndk.{version}",\n'
     84             '    defaults: ["ndk_{version}_defaults"],\n'
     85             '}}'.format(name=name, version=version))
     86 
     87 
     88 def gen_crt_prebuilt(_, name, version):
     89     return ('ndk_prebuilt_object {{\n'
     90             '    name: "ndk_{name}.{version}",\n'
     91             '    sdk_version: "{version}",\n'
     92             '}}'.format(name=name, version=version))
     93 
     94 
     95 def gen_prebuilts(fn, names):
     96     prebuilts = []
     97     for prebuilt in get_prebuilts(names):
     98         name = os.path.splitext(os.path.basename(prebuilt))[0]
     99         version = sdk_version_from_path(prebuilt)
    100         if version < 9:
    101             # We don't support anything before Gingerbread any more.
    102             continue
    103         prebuilts.append(fn(prebuilt, name, version))
    104     return prebuilts
    105 
    106 
    107 def main():
    108     blueprints = gen_defaults()
    109     blueprints.extend(gen_prebuilts(gen_crt_prebuilt, (
    110         'crtbegin_so.o',
    111         'crtend_so.o',
    112         'crtbegin_dynamic.o',
    113         'crtbegin_static.o',
    114         'crtend_android.o')))
    115 
    116     with open(local_path('Android.bp'), 'w') as f:
    117         f.write('// THIS FILE IS AUTOGENERATED BY gen-blueprints.py\n')
    118         f.write('// DO NOT EDIT\n')
    119         f.write('\n')
    120         f.write('\n\n'.join(blueprints))
    121         f.write('\n\n')
    122         f.write('build = ["stl.bp"]\n')
    123 
    124 
    125 if __name__ == '__main__':
    126     main()
    127