| #!/usr/bin/python3 |
| |
| # This script rsyncs generated bp2build BUILD files from out/soong/bp2build to |
| # the top level AOSP source tree. It can also delete these files from the source |
| # tree. |
| # |
| # Write BUILD files in declared packages: ./build/bazel/scripts/bp2build-sync.py write |
| # Remove BUILD files in declared packages: ./build/bazel/scripts/bp2build-sync.py remove |
| |
| import argparse |
| import glob |
| import json |
| import os |
| import subprocess |
| import sys |
| import shutil |
| |
| AOSP_ROOT = os.path.abspath(os.path.dirname(__file__ +"/../../../../")) |
| BP2BUILD_MANIFEST = os.path.join(AOSP_ROOT, "out/soong/bp2build/MANIFEST") |
| BP2BUILD_MANIFEST_PREVIOUS = os.path.join(AOSP_ROOT, "out/soong/.bp2build_files_in_tree") |
| |
| def list_converted_build_files(manifest): |
| '''Loads and parses the bp2build manifest file for the list of converted BUILD files.''' |
| build_files = [] |
| if not os.path.exists(manifest): |
| # Gracefully return if the manifest doesn't exist. |
| print("Warning: " + manifest + " doesn't exist.") |
| return build_files |
| |
| with open(manifest, 'r') as f: |
| for build_file in f: |
| build_file = build_file.strip() |
| if os.path.basename(build_file) != "BUILD": |
| # Not a BUILD file (bp2build generates other things too) |
| continue |
| # out/soong/bp2build/bionic/tests/headers/posix/BUILD |
| # >--------------------------------< |
| build_file = os.path.relpath(build_file, "out/soong/bp2build") |
| build_files += [build_file] |
| return build_files |
| |
| def write_converted_build_files(): |
| '''Write all of the BUILD files from the bp2build manifest into the source tree.''' |
| remove_converted_build_files() # ensure that stale files from a previous run are cleaned up. |
| |
| for f in list_converted_build_files(BP2BUILD_MANIFEST): |
| src_file = os.path.join(AOSP_ROOT, "out/soong/bp2build", f) |
| dest_file = os.path.join(AOSP_ROOT, f) |
| print("Writing " + dest_file) |
| shutil.copyfile(src_file, dest_file) |
| |
| # Copy the current manifest into out/soong to ensure that the stale files |
| # from a previous run are removed. |
| shutil.copyfile(BP2BUILD_MANIFEST, BP2BUILD_MANIFEST_PREVIOUS) |
| |
| def remove_converted_build_files(): |
| # Solution for b/182886212, to ensure that all BUILD files under the |
| # directories we care about are cleaned up. Just removing the BUILD files in |
| # the current bp2build MANIFEST may not be sufficient, since a new bp2build |
| # run can generate fewer files because of a local change, leaving stale |
| # BUILD files behind. |
| for build_file in list_converted_build_files(BP2BUILD_MANIFEST_PREVIOUS): |
| path = os.path.join(AOSP_ROOT, build_file) |
| if os.path.exists(path): |
| with open(path) as f: |
| if "generated by bp2build" in f.readline(): |
| print("Removing " + path) |
| os.remove(path) |
| else: |
| print("Not removing " + path + " because it's not generated by bp2build") |
| |
| def run(action): |
| '''Primary entry point of this script.''' |
| if action == "write": |
| write_converted_build_files() |
| elif action =="remove": |
| remove_converted_build_files() |
| else: |
| # shouldn't happen, based on argparse's choices. |
| sys.exit(1) |
| |
| |
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser() |
| parser.add_argument( |
| "action", |
| choices=["write", "remove"], |
| help="write or remove generated BUILD files") |
| args = parser.parse_args() |
| run(args.action) |