File: gen_buildfiles.py

package info (click to toggle)
android-platform-tools 35.0.2-1~exp6
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,716 kB
  • sloc: cpp: 995,749; java: 290,495; ansic: 145,647; xml: 58,531; python: 39,608; sh: 14,500; javascript: 5,198; asm: 4,866; makefile: 3,115; yacc: 769; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (829 lines) | stat: -rw-r--r-- 34,397 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#!/usr/bin/env python3
#
# Copyright (C) 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import argparse
from collections import defaultdict
import glob
import json
import logging
import os
import sys

import collect_licenses
import utils


class GenBuildFile(object):
    """Generates Android.bp for VNDK snapshot.

    VNDK snapshot directory structure under prebuilts/vndk/v{version}:
        Android.bp
        {SNAPSHOT_ARCH}/
            Android.bp
            arch-{TARGET_ARCH}-{TARGET_ARCH_VARIANT}/
                shared/
                    vndk-core/
                        (VNDK-core libraries, e.g. libbinder.so)
                    vndk-sp/
                        (VNDK-SP libraries, e.g. libc++.so)
            arch-{TARGET_2ND_ARCH}-{TARGET_2ND_ARCH_VARIANT}/
                shared/
                    vndk-core/
                        (VNDK-core libraries, e.g. libbinder.so)
                    vndk-sp/
                        (VNDK-SP libraries, e.g. libc++.so)
            binder32/
                (This directory is newly introduced in v28 (Android P) to hold
                prebuilts built for 32-bit binder interface.)
                Android.bp
                arch-{TARGET_ARCH}-{TARGE_ARCH_VARIANT}/
                    ...
            configs/
                (various *.txt configuration files, e.g. ld.config.*.txt)
        ... (other {SNAPSHOT_ARCH}/ directories)
        common/
            Android.bp
            NOTICE_FILES/
                (license files, e.g. libfoo.so.txt)
    """
    INDENT = '    '
    ETC_MODULES = [
        'llndk.libraries.txt',
        'vndksp.libraries.txt',
        'vndkcore.libraries.txt',
        'vndkprivate.libraries.txt',
        'vndkproduct.libraries.txt',
    ]

    def __init__(self, install_dir, vndk_version):
        """GenBuildFile constructor.

        Args:
          install_dir: string, absolute path to the prebuilts/vndk/v{version}
            directory where the build files will be generated.
          vndk_version: int, VNDK snapshot version (e.g. 30)
        """
        self._install_dir = install_dir
        self._vndk_version = vndk_version
        self._etc_paths = self._get_etc_paths()
        self._snapshot_archs = utils.get_snapshot_archs(install_dir)
        self._root_bpfile = os.path.join(install_dir, utils.ROOT_BP_PATH)
        self._common_bpfile = os.path.join(install_dir, utils.COMMON_BP_PATH)
        self._llndk = self._parse_lib_list(
            os.path.basename(self._etc_paths['llndk.libraries.txt']))
        self._vndk_core = self._parse_lib_list(
            os.path.basename(self._etc_paths['vndkcore.libraries.txt']))
        self._vndk_sp = self._parse_lib_list(
            os.path.basename(self._etc_paths['vndksp.libraries.txt']))
        self._vndk_private = self._parse_lib_list(
            os.path.basename(self._etc_paths['vndkprivate.libraries.txt']))
        self._vndk_product = self._parse_lib_list(
            os.path.basename(self._etc_paths['vndkproduct.libraries.txt']))
        self._modules_with_notice = self._get_modules_with_notice()
        self._license_in_json = not self._modules_with_notice
        self._license_kinds_map = defaultdict(set)
        self._license_texts_map = defaultdict(set)
        self.modules_with_restricted_lic = set()

    def _get_etc_paths(self):
        """Returns a map of relative file paths for each ETC module."""

        etc_paths = dict()
        for etc_module in self.ETC_MODULES:
            etc_pattern = '{}*'.format(os.path.splitext(etc_module)[0])
            globbed = glob.glob(
                os.path.join(self._install_dir, utils.CONFIG_DIR_PATH_PATTERN,
                             etc_pattern))
            if len(globbed) > 0:
                rel_etc_path = globbed[0].replace(self._install_dir, '')[1:]
                etc_paths[etc_module] = rel_etc_path
        return etc_paths

    def _parse_lib_list(self, txt_filename):
        """Returns a map of VNDK library lists per VNDK snapshot arch.

        Args:
          txt_filename: string, name of snapshot config file

        Returns:
          dict, e.g. {'arm64': ['libfoo.so', 'libbar.so', ...], ...}
        """
        lib_map = dict()
        for txt_path in utils.find(self._install_dir, [txt_filename]):
            arch = utils.snapshot_arch_from_path(txt_path)
            abs_path_of_txt = os.path.join(self._install_dir, txt_path)
            with open(abs_path_of_txt, 'r') as f:
                lib_map[arch] = f.read().strip().split('\n')
            if lib_map[arch] == ['']:
                lib_map[arch].clear()
        return lib_map

    def _get_modules_with_notice(self):
        """Returns a list of modules that have associated notice files. """
        notice_paths = glob.glob(
            os.path.join(self._install_dir, utils.NOTICE_FILES_DIR_PATH,
                         '*.txt'))
        return sorted(os.path.splitext(os.path.basename(p))[0] for p in notice_paths)

    def generate_root_android_bp(self):
        """Autogenerates Android.bp."""

        logging.info('Generating Android.bp for snapshot v{}'.format(
            self._vndk_version))
        prebuilt_buildrules = []
        for prebuilt in self.ETC_MODULES:
            prebuilt_buildrules.append(self._gen_etc_prebuilt(prebuilt))

        with open(self._root_bpfile, 'w') as bpfile:
            bpfile.write(self._gen_autogen_msg('/'))
            bpfile.write('\n')
            bpfile.write(self._gen_license_package())
            bpfile.write('\n')
            bpfile.write(self._gen_license())
            bpfile.write('\n')
            bpfile.write('\n'.join(prebuilt_buildrules))
            bpfile.write('\n')

        logging.info('Successfully generated {}'.format(self._root_bpfile))

    def generate_common_android_bp(self):
        """Autogenerates common/Android.bp."""

        logging.info('Generating common/Android.bp for snapshot v{}'.format(
            self._vndk_version))
        with open(self._common_bpfile, 'w') as bpfile:
            bpfile.write(self._gen_autogen_msg('/'))
            bpfile.write('\n')
            bpfile.write(self._gen_license_package())
            if self._license_in_json:
                for name in self._license_kinds_map:
                    bpfile.write('\n')
                    bpfile.write(self._gen_notice_license(name))
            else:
                for module in self._modules_with_notice:
                    bpfile.write('\n')
                    bpfile.write(self._gen_notice_license(module))

    def generate_android_bp(self):
        """Autogenerates Android.bp."""

        def gen_for_variant(arch, is_binder32=False):
            """Generates Android.bp file for specified VNDK snapshot variant.

            A VNDK snapshot variant is defined by the TARGET_ARCH and binder
            bitness. Example snapshot variants:
                vndk_v{ver}_arm:            {arch: arm, binder: 64-bit}
                vndk_v{ver}_arm_binder32:   {arch: arm, binder: 32-bit}

            Args:
              arch: string, VNDK snapshot arch (e.g. 'arm64')
              is_binder32: bool, True if binder interface is 32-bit
            """
            binder32_suffix = '_{}'.format(
                utils.BINDER32) if is_binder32 else ''
            logging.info('Generating Android.bp for vndk_v{}_{}{}'.format(
                self._vndk_version, arch, binder32_suffix))

            src_root = os.path.join(self._install_dir, arch)
            module_names_txt = os.path.join(
                src_root, "configs", "module_names.txt")
            module_names = dict()
            try:
                with open(module_names_txt, 'r') as f:
                    # Remove empty lines from module_names_txt
                    module_list = filter(None, f.read().split('\n'))
                for module in module_list:
                    lib, name = module.split(' ')
                    module_names[lib] = name
            except IOError:
                # If module_names.txt doesn't exist, ignore it and parse
                # module names out from .so filenames. (old snapshot)
                pass

            variant_subpath = arch
            if is_binder32:
                variant_subpath = os.path.join(arch, utils.BINDER32)
            variant_path = os.path.join(self._install_dir, variant_subpath)
            bpfile_path = os.path.join(variant_path, 'Android.bp')

            vndk_core_buildrules = self._gen_vndk_shared_prebuilts(
                self._vndk_core[arch],
                arch,
                is_llndk=False,
                is_vndk_sp=False,
                is_binder32=is_binder32,
                module_names=module_names)
            vndk_sp_buildrules = self._gen_vndk_shared_prebuilts(
                self._vndk_sp[arch],
                arch,
                is_llndk=False,
                is_vndk_sp=True,
                is_binder32=is_binder32,
                module_names=module_names)
            include_llndk = self._vndk_version > 30
            if include_llndk:
                llndk_buildrules = self._gen_vndk_shared_prebuilts(
                    self._llndk[arch],
                    arch,
                    is_llndk=True,
                    is_vndk_sp=False,
                    is_binder32=is_binder32,
                    module_names=module_names)

            with open(bpfile_path, 'w') as bpfile:
                bpfile.write(self._gen_autogen_msg('/'))
                bpfile.write('\n')
                bpfile.write(self._gen_license_package())
                bpfile.write('\n')
                bpfile.write('\n'.join(vndk_core_buildrules))
                bpfile.write('\n')
                bpfile.write('\n'.join(vndk_sp_buildrules))
                if include_llndk:
                    bpfile.write('\n')
                    bpfile.write('\n'.join(llndk_buildrules))

            variant_include_path = os.path.join(variant_path, 'include')
            include_path = os.path.join(self._install_dir, arch, 'include')
            if os.path.isdir(include_path) and variant_include_path != include_path:
                os.symlink(os.path.relpath(include_path, variant_path),
                    variant_include_path)

            logging.info('Successfully generated {}'.format(bpfile_path))

        for arch in self._snapshot_archs:
            if os.path.isdir(
                    os.path.join(self._install_dir, arch, utils.BINDER32)):
                gen_for_variant(arch, is_binder32=True)
            gen_for_variant(arch)

    def _gen_autogen_msg(self, comment_char):
        return ('{0}{0} THIS FILE IS AUTOGENERATED BY '
                'development/vndk/snapshot/gen_buildfiles.py\n'
                '{0}{0} DO NOT EDIT\n'.format(comment_char))

    def _gen_license_package(self):
        """ Generates license package for VNDK snapshot libs """
        return ('package {{\n'
                '{ind}default_applicable_licenses: ["prebuilts_vndk_v{version}_license"],\n'
                '}}\n'.format(
                    ind=self.INDENT,
                    version=self._vndk_version))

    def _get_license_kinds(self, module=''):
        """ Returns a set of license kinds

        Args:
          module: module name to find the license kind.
                  If empty, check all license files.
        """
        if self._license_in_json:
            license_kinds = set()
            if module == '':
                # collect all license kinds
                for kinds in self._license_kinds_map.values():
                    license_kinds.update(kinds)
                return license_kinds
            else:
                return self._license_kinds_map[module]

        license_collector = collect_licenses.LicenseCollector(self._install_dir)
        license_collector.run(module)
        return license_collector.license_kinds

    def _get_license_texts(self, module):
        if self._license_in_json:
            return {'{notice_dir}/{license_text}'.format(
                        notice_dir=utils.NOTICE_FILES_DIR_NAME,
                        license_text=license_text)
                        for license_text in self._license_texts_map[module]}
        else:
            return {'{notice_dir}/{module}.txt'.format(
                        notice_dir=utils.NOTICE_FILES_DIR_NAME,
                        module=module)}

    def _gen_license(self):
        """ Generates license module.

        It uses license files for all VNDK snapshot libraries in common/NOTICE_FILES directory.
        """
        license_kinds = self._get_license_kinds()
        license_kinds_string = ''
        for license_kind in sorted(license_kinds):
            license_kinds_string += '{ind}{ind}"{license_kind}",\n'.format(
                                    ind=self.INDENT, license_kind=license_kind)
        return ('license {{\n'
                '{ind}name: "prebuilts_vndk_v{version}_license",\n'
                '{ind}visibility: [":__subpackages__"],\n'
                '{ind}license_kinds: [\n'
                '{license_kinds}'
                '{ind}],\n'
                '{ind}license_text: ["{notice_files}"],\n'
                '}}\n'.format(
                    ind=self.INDENT,
                    version=self._vndk_version,
                    license_kinds=license_kinds_string,
                    notice_files=os.path.join(utils.NOTICE_FILES_DIR_PATH, '**', '*')))

    def _get_versioned_name(self,
                            prebuilt,
                            arch,
                            is_etc=False,
                            is_binder32=False,
                            module_names=None):
        """Returns the VNDK version-specific module name for a given prebuilt.

        The VNDK version-specific module name is defined as follows:
        For a VNDK shared lib: 'libfoo.so'
            if binder is 32-bit:
                'libfoo.vndk.{version}.{arch}.binder32.vendor'
            else:
                'libfoo.vndk.{version}.{arch}.vendor'
        For an ETC module: 'foo.txt' -> 'foo.{version}.txt'

        Args:
          prebuilt: string, name of the prebuilt object
          arch: string, VNDK snapshot arch (e.g. 'arm64')
          is_etc: bool, True if the LOCAL_MODULE_CLASS of prebuilt is 'ETC'
          is_binder32: bool, True if binder interface is 32-bit
          module_names: dict, module names for given prebuilts
        """
        if is_etc:
            name, ext = os.path.splitext(prebuilt)
            versioned_name = '{}.{}{}'.format(name, self._vndk_version, ext)
        else:
            module_names = module_names or dict()
            if prebuilt in module_names:
                name = module_names[prebuilt]
            else:
                name = os.path.splitext(prebuilt)[0]
            binder_suffix = '.{}'.format(utils.BINDER32) if is_binder32 else ''
            versioned_name = '{}.vndk.{}.{}{}.vendor'.format(
                name, self._vndk_version, arch, binder_suffix)

        return versioned_name

    def _gen_etc_prebuilt(self, prebuilt):
        """Generates build rule for an ETC prebuilt.

        Args:
          prebuilt: string, name of ETC prebuilt object
        """
        etc_path = self._etc_paths[prebuilt]
        etc_sub_path = etc_path[etc_path.index('/') + 1:]

        prebuilt_etc = ('prebuilt_etc {{\n'
                        '{ind}name: "{versioned_name}",\n'
                        '{ind}target: {{\n'.format(
                            ind=self.INDENT,
                            versioned_name=self._get_versioned_name(
                                prebuilt, None, is_etc=True)))
        for arch in self._snapshot_archs:
            prebuilt_etc += ('{ind}{ind}android_{arch}: {{\n'
                             '{ind}{ind}{ind}src: "{arch}/{etc_sub_path}",\n'
                             '{ind}{ind}}},\n'.format(
                                 ind=self.INDENT,
                                 arch=arch,
                                 etc_sub_path=etc_sub_path))
        prebuilt_etc += ('{ind}}},\n'
                         '}}\n'.format(ind=self.INDENT))
        return prebuilt_etc

    def _gen_prebuilt_library_shared(self, prebuilt_lib_info):
        """Generates cc_prebuilt_library_shared modules for the old vendor
        compatibility.

        Some vendor modules still require old version of libraries that is not
        available from the current source tree. To provide the old copy of the
        libraries, use the vndk snapshot.

        Args:
            prebuilt_lib_info: pair of (string, list of strings), name of the
                        prebuilt library and the list of shared libs for it.
        """
        lib_name = prebuilt_lib_info[0]
        shared_libs = prebuilt_lib_info[1]

        shared_libs_prop = ''
        if shared_libs:
            shared_libs_prop = ('{ind}shared_libs: [\n'.format(ind=self.INDENT))
            for lib in shared_libs:
                shared_libs_prop += ('{ind}{ind}"{lib}",\n'.format(
                                        ind=self.INDENT, lib=lib))
            shared_libs_prop += ('{ind}],\n'.format(ind=self.INDENT))

        cc_prebuilt_libraries = ('cc_prebuilt_library_shared {{\n'
                                 '{ind}name: "{name}-vendorcompat",\n'
                                 '{ind}stem: "{name}",\n'
                                 '{ind}vendor: true,\n'
                                 '{ind}// These are already stripped, and '
                                 'restripping them just issues diagnostics.\n'
                                 '{ind}strip: {{\n'
                                 '{ind}{ind}none: true,\n'
                                 '{ind}}},\n'
                                 '{shared_libs}'
                                 '{ind}target: {{\n'.format(
                                    ind=self.INDENT,
                                    name=lib_name,
                                    shared_libs=shared_libs_prop))
        src_paths = utils.find(self._install_dir, [lib_name+'.so'])
        for src in src_paths:
            dirs = src.split(os.path.sep)
            if len(dirs) < 3 or not dirs[1].startswith('arch-{}-'.format(dirs[0])):
                continue
            cc_prebuilt_libraries += ('{ind}{ind}android_{arch}: {{\n'
                                      '{ind}{ind}{ind}srcs: ["{src}"],\n'
                                      '{ind}{ind}}},\n'.format(
                                        ind=self.INDENT, arch=dirs[0], src=src))
        cc_prebuilt_libraries += ('{ind}}},\n'
                                  '}}\n'.format(ind=self.INDENT))
        return cc_prebuilt_libraries

    def _gen_notice_license(self, module):
        """Generates a notice license build rule for a given module.
        When genererating each notice license, collect
        modules_with_restricted_lic, the list of modules that are under the GPL.

        Args:
          module: string, module name
        """
        def has_restricted_license(license_kinds):
            for lic in license_kinds:
                if 'GPL' in lic:
                    return True
            return False

        license_kinds = self._get_license_kinds(module)
        if has_restricted_license(license_kinds):
            self.modules_with_restricted_lic.add(module)
        license_kinds_string = ''
        for license_kind in sorted(license_kinds):
            license_kinds_string += '{ind}{ind}"{license_kind}",\n'.format(
                                    ind=self.INDENT, license_kind=license_kind)
        license_texts = self._get_license_texts(module)
        license_texts_string = ''
        for license_text in sorted(license_texts):
            license_texts_string += '{ind}{ind}"{license_text}",\n'.format(
                                    ind=self.INDENT, license_text=license_text)
        return ('license {{\n'
                '{ind}name: "{license_name}",\n'
                '{ind}license_kinds: [\n'
                '{license_kinds}'
                '{ind}],\n'
                '{ind}license_text: [\n'
                '{license_texts}'
                '{ind}],\n'
                '}}\n'.format(
                    ind=self.INDENT,
                    license_name=self._get_notice_license_name(module),
                    license_kinds=license_kinds_string,
                    license_texts=license_texts_string))

    def _get_notice_license_name(self, module):
        """ Gets a notice license module name for a given module.

        Args:
          notice: string, module name.
        """
        return 'vndk-v{ver}-{module}-license'.format(
            ver=self._vndk_version, module=module)

    def _gen_vndk_shared_prebuilts(self,
                                   prebuilts,
                                   arch,
                                   is_llndk,
                                   is_vndk_sp,
                                   is_binder32,
                                   module_names):
        """Returns list of build rules for given prebuilts.

        Args:
          prebuilts: list of VNDK shared prebuilts
          arch: string, VNDK snapshot arch (e.g. 'arm64')
          is_llndk: bool, True if the prebuilts are LLNDK stubs
          is_vndk_sp: bool, True if prebuilts are VNDK_SP libs
          is_binder32: bool, True if binder interface is 32-bit
          module_names: dict, module names for given prebuilts
        """

        module_prebuilts = dict()
        for prebuilt in prebuilts:
            if prebuilt in module_names:
                name = module_names[prebuilt]
            else:
                name = os.path.splitext(prebuilt)[0]

            if name not in module_prebuilts:
                module_prebuilts[name] = list()
            module_prebuilts[name].append(prebuilt)

        build_rules = []
        for name in module_prebuilts:
            bp_module = self._gen_vndk_shared_prebuilt(
                name,
                arch,
                srcs=module_prebuilts[name],
                is_llndk=is_llndk,
                is_vndk_sp=is_vndk_sp,
                is_binder32=is_binder32)
            if bp_module:
                build_rules.append(bp_module)
        return build_rules

    def _gen_vndk_shared_prebuilt(self,
                                  name,
                                  arch,
                                  srcs,
                                  is_llndk,
                                  is_vndk_sp,
                                  is_binder32):
        """Returns build rule for given prebuilt module, or an empty
        string if the module is invalid (e.g. srcs doesn't exist).

        Args:
          name: string, name of prebuilt module
          arch: string, VNDK snapshot arch (e.g. 'arm64')
          srcs: list, prebuilt source file names of this module
          is_llndk: bool, True if prebuilt is a LLNDK stub
          is_vndk_sp: bool, True if prebuilt is a VNDK_SP lib
          is_binder32: bool, True if binder interface is 32-bit
        """

        def is_prebuilts_in_list(prebuilts, vndk_list):
            for prebuilt in prebuilts:
                if prebuilt in vndk_list:
                    return True
            return False

        def get_license_prop(name):
            """Returns the license prop build rule.

            Args:
              name: string, name of the module
            """
            if name in self._license_kinds_map:
                return '{ind}licenses: ["{license}"],\n'.format(
                        ind=self.INDENT,
                        license=self._get_notice_license_name(name))
            return ''

        def get_notice_file(prebuilts):
            """Returns build rule for notice file (attribute 'licenses').

            Args:
              prebuilts: list, names of prebuilt objects
            """
            notice = ''
            for prebuilt in prebuilts:
                if prebuilt in self._modules_with_notice:
                    notice = '{ind}licenses: ["{notice_license}"],\n'.format(
                        ind=self.INDENT,
                        notice_license=self._get_notice_license_name(prebuilt))
                    break
            return notice

        def get_arch_props(name, arch, srcs_props):
            """Returns build rule for arch specific srcs.

            e.g.,
                arch: {
                    arm: {
                        export_include_dirs: ["..."],
                        export_system_include_dirs: ["..."],
                        export_flags: ["..."],
                        relative_install_path: "...",
                        srcs: ["..."]
                    },
                    arm64: {
                        export_include_dirs: ["..."],
                        export_system_include_dirs: ["..."],
                        export_flags: ["..."],
                        relative_install_path: "...",
                        srcs: ["..."]
                    },
                }

            Args:
              name: string, name of prebuilt module
              arch: string, VNDK snapshot arch (e.g. 'arm64')
              srcs_props: dict, prebuilt source paths and corresponding flags
            """
            arch_props = '{ind}arch: {{\n'.format(ind=self.INDENT)

            def list_to_prop_value(l, name):
                if len(l) == 0:
                    return ''
                dirs=',\n{ind}{ind}{ind}{ind}'.format(
                    ind=self.INDENT).join(['"%s"' % d for d in l])
                return ('{ind}{ind}{ind}{name}: [\n'
                        '{ind}{ind}{ind}{ind}{dirs},\n'
                        '{ind}{ind}{ind}],\n'.format(
                            ind=self.INDENT,
                            dirs=dirs,
                            name=name))

            def rename_generated_dirs(dirs):
                # Rename out/soong/.intermediates to generated-headers for better readability.
                return [d.replace(utils.SOONG_INTERMEDIATES_DIR, utils.GENERATED_HEADERS_DIR, 1) for d in dirs]

            for src in sorted(srcs_props.keys()):
                include_dirs = ''
                system_include_dirs = ''
                flags = ''
                relative_install_path = ''
                props = srcs_props[src]
                if 'ExportedDirs' in props:
                    dirs = rename_generated_dirs(props['ExportedDirs'])
                    l = ['include/%s' % d for d in dirs]
                    include_dirs = list_to_prop_value(l, 'export_include_dirs')
                if 'ExportedSystemDirs' in props:
                    dirs = rename_generated_dirs(props['ExportedSystemDirs'])
                    l = ['include/%s' % d for d in dirs]
                    system_include_dirs = list_to_prop_value(l, 'export_system_include_dirs')
                if 'ExportedFlags' in props:
                    flags = list_to_prop_value(props['ExportedFlags'], 'export_flags')
                if 'RelativeInstallPath' in props:
                    relative_install_path = ('{ind}{ind}{ind}'
                        'relative_install_path: "{path}",\n').format(
                            ind=self.INDENT,
                            path=props['RelativeInstallPath'])
                if 'LicenseKinds' in props:
                    self._license_kinds_map[name].update(props['LicenseKinds'])
                if 'LicenseTexts' in props:
                    self._license_texts_map[name].update(props['LicenseTexts'])

                arch_props += ('{ind}{ind}{arch}: {{\n'
                               '{include_dirs}'
                               '{system_include_dirs}'
                               '{flags}'
                               '{relative_install_path}'
                               '{ind}{ind}{ind}srcs: ["{src}"],\n'
                               '{ind}{ind}}},\n').format(
                                  ind=self.INDENT,
                                  arch=utils.prebuilt_arch_from_path(
                                      os.path.join(arch, src)),
                                  include_dirs=include_dirs,
                                  system_include_dirs=system_include_dirs,
                                  flags=flags,
                                  relative_install_path=relative_install_path,
                                  src=src)
            arch_props += '{ind}}},\n'.format(ind=self.INDENT)
            return arch_props

        src_root = os.path.join(self._install_dir, arch)
        if is_binder32:
            src_root = os.path.join(src_root, utils.BINDER32)

        src_paths = utils.find(src_root, srcs)
        # filter out paths under 'binder32' subdirectory
        src_paths = list(filter(lambda src: not src.startswith(utils.BINDER32),
            src_paths))
        # This module is invalid if no srcs are found.
        if not src_paths:
            logging.info('No srcs found for {}; skipping'.format(name))
            return ""

        product_available = ''
        # if vndkproduct.libraries.txt is empty, make the VNDKs available to product by default.
        if is_llndk or not self._vndk_product[arch] or is_prebuilts_in_list(srcs, self._vndk_product[arch]):
            product_available = '{ind}product_available: true,\n'.format(
                ind=self.INDENT)

        vndk_props = ''
        if not is_llndk:
            vndk_sp = ''
            if is_vndk_sp:
                vndk_sp = '{ind}{ind}support_system_process: true,\n'.format(
                    ind=self.INDENT)

            vndk_private = ''
            if is_prebuilts_in_list(srcs, self._vndk_private[arch]):
                vndk_private = '{ind}{ind}private: true,\n'.format(
                    ind=self.INDENT)

            vndk_props = ('{ind}vndk: {{\n'
                          '{ind}{ind}enabled: true,\n'
                          '{vndk_sp}'
                          '{vndk_private}'
                          '{ind}}},\n'.format(
                              ind=self.INDENT,
                              product_available=product_available,
                              vndk_sp=vndk_sp,
                              vndk_private=vndk_private))

        srcs_props = dict()
        for src in src_paths:
            props = dict()
            prop_path = os.path.join(src_root, src+'.json')
            try:
                with open(prop_path, 'r') as f:
                    props = json.loads(f.read())
                os.unlink(prop_path)
            except:
                # TODO(b/70312118): Parse from soong build system
                if name == 'android.hidl.memory@1.0-impl':
                    props['RelativeInstallPath'] = 'hw'
            srcs_props[src] = props
        arch_props = get_arch_props(name, arch, srcs_props)

        if self._license_in_json:
            license = get_license_prop(name)
        else:
            license = get_notice_file(srcs)

        binder32bit = ''
        if is_binder32:
            binder32bit = '{ind}binder32bit: true,\n'.format(ind=self.INDENT)

        min_sdk_version = ''
        for src, props in srcs_props.items():
            if 'MinSdkVersion' in props:
                min_sdk_version = '{ind}min_sdk_version: "{ver}",\n'.format(
                    ind=self.INDENT,
                    ver=props['MinSdkVersion'])
                break

        return ('vndk_prebuilt_shared {{\n'
                '{ind}name: "{name}",\n'
                '{ind}version: "{ver}",\n'
                '{ind}target_arch: "{target_arch}",\n'
                '{binder32bit}'
                '{ind}vendor_available: true,\n'
                '{product_available}'
                '{vndk_props}'
                '{min_sdk_version}'
                '{license}'
                '{arch_props}'
                '}}\n'.format(
                    ind=self.INDENT,
                    name=name,
                    ver=self._vndk_version,
                    target_arch=arch,
                    binder32bit=binder32bit,
                    product_available=product_available,
                    vndk_props=vndk_props,
                    min_sdk_version=min_sdk_version,
                    license=license,
                    arch_props=arch_props))


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'vndk_version',
        type=utils.vndk_version_int,
        help='VNDK snapshot version to install, e.g. "{}".'.format(
            utils.MINIMUM_VNDK_VERSION))
    parser.add_argument(
        '-v',
        '--verbose',
        action='count',
        default=0,
        help='Increase output verbosity, e.g. "-v", "-vv".')
    return parser.parse_args()


def main():
    """For local testing purposes.

    Note: VNDK snapshot must be already installed under
      prebuilts/vndk/v{version}.
    """
    ANDROID_BUILD_TOP = utils.get_android_build_top()
    PREBUILTS_VNDK_DIR = utils.join_realpath(ANDROID_BUILD_TOP,
                                             'prebuilts/vndk')

    args = get_args()
    vndk_version = args.vndk_version
    install_dir = os.path.join(PREBUILTS_VNDK_DIR, 'v{}'.format(vndk_version))
    if not os.path.isdir(install_dir):
        raise ValueError(
            'Please provide valid VNDK version. {} does not exist.'
            .format(install_dir))
    utils.set_logging_config(args.verbose)

    buildfile_generator = GenBuildFile(install_dir, vndk_version)
    # To parse json information, read and generate arch android.bp using
    # generate_android_bp() first.
    buildfile_generator.generate_android_bp()
    buildfile_generator.generate_root_android_bp()
    buildfile_generator.generate_common_android_bp()

    logging.info('Done.')


if __name__ == '__main__':
    main()