1#!/usr/bin/env python3
2
3"""
4The complete list of the remaining Make files in each partition for all lunch targets
5
6How to run?
7python3 $(path-to-file)/mk2bp_partition.py
8"""
9
10from pathlib import Path
11
12import csv
13import datetime
14import os
15import shutil
16import subprocess
17import sys
18import time
19
20def get_top():
21  path = '.'
22  while not os.path.isfile(os.path.join(path, 'build/soong/soong_ui.bash')):
23    if os.path.abspath(path) == '/':
24      sys.exit('Could not find android source tree root.')
25    path = os.path.join(path, '..')
26  return os.path.abspath(path)
27
28# get the values of a build variable
29def get_build_var(variable, product, build_variant):
30  """Returns the result of the shell command get_build_var."""
31  env = {
32      **os.environ,
33      'TARGET_PRODUCT': product if product else '',
34      'TARGET_BUILD_VARIANT': build_variant if build_variant else '',
35  }
36  return subprocess.run([
37      'build/soong/soong_ui.bash',
38      '--dumpvar-mode',
39      variable
40  ], check=True, capture_output=True, env=env, text=True).stdout.strip()
41
42def get_make_file_partitions():
43    lunch_targets = set(get_build_var("all_named_products", "", "").split())
44    total_lunch_targets = len(lunch_targets)
45    makefile_by_partition = dict()
46    partitions = set()
47    current_count = 0
48    start_time = time.time()
49    # cannot run command `m lunch_target`
50    broken_targets = {"mainline_sdk", "ndk"}
51    for lunch_target in sorted(lunch_targets):
52        current_count += 1
53        current_time = time.time()
54        print (current_count, "/", total_lunch_targets, lunch_target, datetime.timedelta(seconds=current_time - start_time))
55        if lunch_target in broken_targets:
56            continue
57        installed_product_out = get_build_var("PRODUCT_OUT", lunch_target, "userdebug")
58        filename = os.path.join(installed_product_out, "mk2bp_remaining.csv")
59        copy_filename = os.path.join(installed_product_out, lunch_target + "_mk2bp_remaining.csv")
60        # only generate if not exists
61        if not os.path.exists(copy_filename):
62            bash_cmd = "bash build/soong/soong_ui.bash --make-mode TARGET_PRODUCT=" + lunch_target
63            bash_cmd += " TARGET_BUILD_VARIANT=userdebug " + filename
64            subprocess.run(bash_cmd, shell=True, text=True, check=True, stdout=subprocess.DEVNULL)
65            # generate a copied .csv file, to avoid possible overwritings
66            with open(copy_filename, "w") as file:
67                shutil.copyfile(filename, copy_filename)
68
69        # open mk2bp_remaining.csv file
70        with open(copy_filename, "r") as csvfile:
71            reader = csv.reader(csvfile, delimiter=",", quotechar='"')
72            # bypass the header row
73            next(reader, None)
74            for row in reader:
75                # read partition information
76                partition = row[2]
77                makefile_by_partition.setdefault(partition, set()).add(row[0])
78                partitions.add(partition)
79
80    # write merged make file list for each partition into a csv file
81    installed_path = Path(installed_product_out).parents[0].as_posix()
82    csv_path = installed_path + "/mk2bp_partition.csv"
83    with open(csv_path, "wt") as csvfile:
84        writer = csv.writer(csvfile, delimiter=",")
85        count_makefile = 0
86        for partition in sorted(partitions):
87            number_file = len(makefile_by_partition[partition])
88            count_makefile += number_file
89            writer.writerow([partition, number_file])
90            for makefile in sorted(makefile_by_partition[partition]):
91                writer.writerow([makefile])
92        row = ["The total count of make files is ", count_makefile]
93        writer.writerow(row)
94
95def main():
96    os.chdir(get_top())
97    get_make_file_partitions()
98
99if __name__ == "__main__":
100    main()
101