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