blob: cfa3ade7fed55120bf20dfdbbcaf6d1da0c508b3 [file] [log] [blame]
Dan Albert3bc20082017-02-23 14:45:30 -08001#!/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"""Generates the Android.bp file for prebuilts/ndk."""
17import os
18
19
20def local_path(path):
21 """Returns an abspath to the given path from this file's directory."""
22 return os.path.normpath(os.path.join(os.path.dirname(__file__), path))
23
24
25def find(path, names):
26 """Finds a list of files in a directory that match the given names."""
27 found = []
28 for root, _, files in os.walk(path):
29 for file_name in sorted(files):
30 if file_name in names:
31 abspath = os.path.abspath(os.path.join(root, file_name))
32 rel_to_root = abspath.replace(os.path.abspath(path), '')
33 found.append(rel_to_root[1:]) # strip leading /
34 return found
35
36
37def sdk_version_from_path(path):
38 """Returns the integer SDK version for the given path."""
39 return int(path.split('/')[0].split('-')[1])
40
41
42def get_prebuilts(names):
43 """Returns a list of prebuilt objects that match the given names."""
44 prebuilts_path = local_path('current/platforms')
45 prebuilts = find(prebuilts_path, names)
46 prebuilts = [p for p in prebuilts if 'arch-arm/' in p]
47 prebuilts.sort(key=sdk_version_from_path)
48 return prebuilts
49
50
51def gen_crt_prebuilt(_, name, version):
52 """Generate a module for a CRT prebuilt object."""
53 return ('ndk_prebuilt_object {{\n'
54 ' name: "ndk_{name}.{version}",\n'
55 ' sdk_version: "{version}",\n'
56 '}}'.format(name=name, version=version))
57
58
59def gen_prebuilts(module_generator, names):
60 """Generate blueprints for the given modules."""
61 prebuilts = []
62 for prebuilt in get_prebuilts(names):
63 name = os.path.splitext(os.path.basename(prebuilt))[0]
64 version = sdk_version_from_path(prebuilt)
65 if version < 9:
66 # We don't support anything before Gingerbread any more.
67 continue
68 prebuilts.append(module_generator(prebuilt, name, version))
69 return prebuilts
70
71
72def main():
73 """Program entry point."""
74 blueprints = gen_prebuilts(gen_crt_prebuilt, (
75 'crtbegin_so.o',
76 'crtend_so.o',
77 'crtbegin_dynamic.o',
78 'crtbegin_static.o',
79 'crtend_android.o'))
80
81 with open(local_path('Android.bp'), 'w') as bpfile:
Jeff Gaston92b4f6f2017-09-11 16:59:55 -070082 bpfile.write('// THIS FILE IS AUTOGENERATED BY gen_blueprints.py\n')
Dan Albert3bc20082017-02-23 14:45:30 -080083 bpfile.write('// DO NOT EDIT\n')
84 bpfile.write('\n')
85 bpfile.write('\n\n'.join(blueprints))
86 bpfile.write('\n\n')
Dan Willemsen1d642582017-04-13 10:28:56 -070087 bpfile.write('build = ["cpufeatures.bp", "stl.bp"]')
Dan Albert3bc20082017-02-23 14:45:30 -080088
89
90if __name__ == '__main__':
91 main()