// Copyright (C) 2019 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.

//
// Module-specific defaults.
//
// For module X, if we need to build it both as a library and an executable:
//  - A default rule `releasetools_X_defaults` is created, which lists `srcs`, `libs` and
//    `required` properties.
//  - `python_library_host` and `python_binary_host` are created by listing
//    `releasetools_X_defaults` in their defaults.
//

python_defaults {
    name: "releasetools_add_img_to_target_files_defaults",
    srcs: [
        "add_img_to_target_files.py",
    ],
    libs: [
        "releasetools_build_image",
        "releasetools_build_super_image",
        "releasetools_common",
    ],
    required: [
        "care_map_generator",
    ],
}

python_defaults {
    name: "releasetools_build_image_defaults",
    srcs: [
        "build_image.py",
    ],
    libs: [
        "releasetools_common",
        "releasetools_verity_utils",
    ],
    required: [
        "blk_alloc_to_base_fs",
        "e2fsck",
        "simg2img",
        "tune2fs",
    ],
}

python_defaults {
    name: "releasetools_build_super_image_defaults",
    srcs: [
        "build_super_image.py",
    ],
    libs: [
        "releasetools_common",
    ],
}

python_defaults {
    name: "releasetools_img_from_target_files_defaults",
    srcs: [
        "img_from_target_files.py",
    ],
    libs: [
        "releasetools_build_super_image",
        "releasetools_common",
    ],
    required: [
        "zip2zip",
    ],
}

python_defaults {
    name: "releasetools_check_target_files_vintf_defaults",
    srcs: [
        "check_target_files_vintf.py",
    ],
    libs: [
        "releasetools_common",
    ],
    required: [
        "checkvintf",
    ],
}

python_defaults {
    name: "releasetools_ota_from_target_files_defaults",
    srcs: [
        "edify_generator.py",
        "ota_from_target_files.py",
        "target_files_diff.py",
    ],
    libs: [
        "releasetools_check_target_files_vintf",
        "releasetools_common",
        "releasetools_verity_utils",
    ],
    required: [
        "brillo_update_payload",
        "checkvintf",
    ],
}

//
// Host libraries.
//

python_defaults {
    name: "releasetools_library_defaults",
    version: {
        py2: {
            enabled: true,
        },
        py3: {
            enabled: true,
        },
    },
}

python_library_host {
    name: "releasetools_add_img_to_target_files",
    defaults: [
        "releasetools_library_defaults",
        "releasetools_add_img_to_target_files_defaults",
    ],
}

python_library_host {
    name: "releasetools_apex_utils",
    defaults: ["releasetools_library_defaults"],
    srcs: [
        "apex_utils.py",
    ],
    libs: [
        "releasetools_common",
    ],
}

python_library_host {
    name: "releasetools_build_image",
    defaults: [
        "releasetools_library_defaults",
        "releasetools_build_image_defaults",
    ],
}

python_library_host {
    name: "releasetools_build_super_image",
    defaults: [
        "releasetools_library_defaults",
        "releasetools_build_super_image_defaults",
    ],
}

python_library_host {
    name: "releasetools_check_target_files_vintf",
    defaults: [
        "releasetools_library_defaults",
        "releasetools_check_target_files_vintf_defaults",
    ],
}

python_library_host {
    name: "releasetools_common",
    defaults: ["releasetools_library_defaults"],
    srcs: [
        "blockimgdiff.py",
        "common.py",
        "images.py",
        "rangelib.py",
        "sparse_img.py",
    ],
    // Only the tools that are referenced directly are listed as required modules. For example,
    // `avbtool` is not here, as the script always uses the one from info_dict['avb_avbtool'].
    required: [
        "aapt2",
        "boot_signer",
        "brotli",
        "bsdiff",
        "imgdiff",
        "minigzip",
        "lz4",
        "mkbootfs",
        "signapk",
    ],
}

python_library_host {
    name: "releasetools_img_from_target_files",
    defaults: [
        "releasetools_library_defaults",
        "releasetools_img_from_target_files_defaults",
    ],
}

python_library_host {
    name: "releasetools_ota_from_target_files",
    defaults: [
        "releasetools_library_defaults",
        "releasetools_ota_from_target_files_defaults",
    ],
}

python_library_host {
    name: "releasetools_verity_utils",
    defaults: ["releasetools_library_defaults"],
    srcs: [
        "verity_utils.py",
    ],
    required: [
        "append2simg",
        "build_verity_metadata",
        "build_verity_tree",
        "fec",
    ],
}

//
// Host binaries.
//

python_defaults {
    name: "releasetools_binary_defaults",
    version: {
        py2: {
            enabled: true,
            embedded_launcher: true,
        },
        py3: {
            enabled: false,
            embedded_launcher: false,
        },
    },
    // TODO (b/140144201) Build imgdiff from releasetools_common
    required: [
        "aapt2",
        "boot_signer",
        "brotli",
        "bsdiff",
        "imgdiff",
        "minigzip",
        "lz4",
        "mkbootfs",
        "signapk",
    ],
}

python_binary_host {
    name: "add_img_to_target_files",
    defaults: [
        "releasetools_binary_defaults",
        "releasetools_add_img_to_target_files_defaults",
    ],
}

python_binary_host {
    name: "build_image",
    defaults: [
        "releasetools_binary_defaults",
        "releasetools_build_image_defaults",
    ],
}

python_binary_host {
    name: "build_super_image",
    defaults: [
        "releasetools_binary_defaults",
        "releasetools_build_super_image_defaults",
    ],
}

python_binary_host {
    name: "check_partition_sizes",
    srcs: [
        "check_partition_sizes.py",
    ],
    libs: [
        "releasetools_common",
    ],
    defaults: [
        "releasetools_binary_defaults",
    ],
}

python_binary_host {
    name: "check_ota_package_signature",
    defaults: ["releasetools_binary_defaults"],
    srcs: [
        "check_ota_package_signature.py",
    ],
    libs: [
        "releasetools_common",
    ],
    required: [
        "delta_generator",
    ],
}

python_binary_host {
    name: "check_target_files_signatures",
    defaults: ["releasetools_binary_defaults"],
    srcs: [
        "check_target_files_signatures.py",
    ],
    libs: [
        "releasetools_common",
    ],
    required: [
        "aapt",
    ],
}

python_binary_host {
    name: "check_target_files_vintf",
    defaults: [
        "releasetools_binary_defaults",
        "releasetools_check_target_files_vintf_defaults"
    ],
}

python_binary_host {
    name: "img_from_target_files",
    defaults: [
        "releasetools_binary_defaults",
        "releasetools_img_from_target_files_defaults",
    ],
}

python_binary_host {
    name: "make_recovery_patch",
    defaults: ["releasetools_binary_defaults"],
    srcs: [
        "make_recovery_patch.py",
    ],
    libs: [
        "releasetools_common",
    ],
}

python_binary_host {
    name: "merge_builds",
    defaults: ["releasetools_binary_defaults"],
    srcs: [
        "merge_builds.py",
    ],
    libs: [
        "releasetools_build_super_image",
        "releasetools_common",
    ],
}

python_binary_host {
    name: "merge_target_files",
    defaults: ["releasetools_binary_defaults"],
    srcs: [
        "merge_target_files.py",
    ],
    libs: [
        "releasetools_add_img_to_target_files",
        "releasetools_build_super_image",
        "releasetools_check_target_files_vintf",
        "releasetools_common",
        "releasetools_img_from_target_files",
        "releasetools_ota_from_target_files",
    ],
    required: [
        "checkvintf",
    ],
}

python_binary_host {
    name: "ota_from_target_files",
    defaults: [
        "releasetools_binary_defaults",
        "releasetools_ota_from_target_files_defaults",
    ],
}

python_binary_host {
    name: "ota_package_parser",
    defaults: ["releasetools_binary_defaults"],
    srcs: [
        "ota_package_parser.py",
        "rangelib.py",
    ],
}

python_binary_host {
    name: "sparse_img",
    defaults: ["releasetools_binary_defaults"],
    srcs: [
        "rangelib.py",
        "sparse_img.py",
    ],
}

python_binary_host {
    name: "sign_apex",
    defaults: ["releasetools_binary_defaults"],
    srcs: [
        "sign_apex.py",
    ],
    libs: [
        "releasetools_apex_utils",
        "releasetools_common",
    ],
}

python_binary_host {
    name: "sign_target_files_apks",
    defaults: ["releasetools_binary_defaults"],
    srcs: [
        "sign_target_files_apks.py",
    ],
    libs: [
        "releasetools_add_img_to_target_files",
        "releasetools_apex_utils",
        "releasetools_common",
    ],
}

python_binary_host {
    name: "validate_target_files",
    defaults: ["releasetools_binary_defaults"],
    srcs: [
        "validate_target_files.py",
    ],
    libs: [
        "releasetools_common",
    ],
}

//
// Tests.
//

python_defaults {
    name: "releasetools_test_defaults",
    srcs: [
        "check_ota_package_signature.py",
        "check_partition_sizes.py",
        "check_target_files_signatures.py",
        "make_recovery_patch.py",
        "merge_target_files.py",
        "ota_package_parser.py",
        "sign_apex.py",
        "sign_target_files_apks.py",
        "validate_target_files.py",

        "test_*.py",
    ],
    libs: [
        "releasetools_add_img_to_target_files",
        "releasetools_apex_utils",
        "releasetools_build_image",
        "releasetools_build_super_image",
        "releasetools_check_target_files_vintf",
        "releasetools_common",
        "releasetools_img_from_target_files",
        "releasetools_ota_from_target_files",
        "releasetools_verity_utils",
    ],
    data: [
        "testdata/**/*",
    ],
    required: [
        "otatools",
    ],
}

python_test_host {
    name: "releasetools_test",
    defaults: ["releasetools_test_defaults"],
    main: "test_utils.py",
    version: {
        py2: {
            enabled: true,
            // When using embedded launcher, atest will try (but may fail) to load libc++.so from
            // host, because the test executable won't be able to find the needed libs via its
            // runpath.
            embedded_launcher: false,
        },
        py3: {
            enabled: false,
            embedded_launcher: false,
        },
    },
    test_suites: ["general-tests"],
}

python_test_host {
    name: "releasetools_py3_test",
    defaults: ["releasetools_test_defaults"],
    main: "test_utils.py",
    version: {
        py2: {
            enabled: false,
            embedded_launcher: false,
        },
        py3: {
            enabled: true,
            embedded_launcher: false,
        },
    },
    test_suites: ["general-tests"],
}
