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

package {
    default_applicable_licenses: ["Android-Apache-2.0"],
}

cc_defaults {
    name: "libsnapshot_defaults",
    defaults: ["fs_mgr_defaults"],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "libchrome",
        "libcutils",
        "liblog",
    ],
    static_libs: [
        "libbrotli",
        "libdm",
        "libfstab",
        "update_metadata-protos",
    ],
    whole_static_libs: [
        "libbrotli",
        "libcutils",
        "libext2_uuid",
        "libext4_utils",
        "libfstab",
        "libsnapuserd_client",
        "libz",
    ],
    header_libs: [
        "libfiemap_headers",
        "libstorage_literals_headers",
        "libupdate_engine_headers",
    ],
    export_static_lib_headers: [
        "update_metadata-protos",
    ],
    export_header_lib_headers: [
        "libfiemap_headers",
    ],
    export_include_dirs: ["include"],
    proto: {
        type: "lite",
        export_proto_headers: true,
        canonical_path_from_root: false,
    },
}

cc_defaults {
    name: "libsnapshot_hal_deps",
    cflags: [
        "-DLIBSNAPSHOT_USE_HAL",
    ],
    shared_libs: [
        "android.hardware.boot@1.0",
        "android.hardware.boot@1.1",
        "android.hardware.boot-V1-ndk",
        "libboot_control_client",
    ],
}

filegroup {
    name: "libsnapshot_sources",
    srcs: [
        "android/snapshot/snapshot.proto",
        "device_info.cpp",
        "snapshot.cpp",
        "snapshot_stats.cpp",
        "snapshot_stub.cpp",
        "snapshot_metadata_updater.cpp",
        "partition_cow_creator.cpp",
        "return.cpp",
        "utility.cpp",
        "scratch_super.cpp",
    ],
}

cc_library_headers {
    name: "libsnapshot_headers",
    recovery_available: true,
    defaults: ["libsnapshot_defaults"],
}

cc_library_static {
    name: "libsnapshot_static",
    defaults: [
        "libsnapshot_defaults",
        "libsnapshot_hal_deps",
    ],
    srcs: [":libsnapshot_sources"],
    static_libs: [
        "libfs_mgr_binder",
    ],
    whole_static_libs: [
        "libselinux",
    ],
}

cc_library {
    name: "libsnapshot",
    defaults: [
        "libsnapshot_defaults",
        "libsnapshot_cow_defaults",
        "libsnapshot_hal_deps",
    ],
    srcs: [":libsnapshot_sources"],
    shared_libs: [
        "libfs_mgr_binder",
        "liblp",
        "libprotobuf-cpp-lite",
    ],
    static_libs: [
        "libsnapshot_cow",
    ],
    whole_static_libs: [
        "libselinux",
    ],
}

cc_library_static {
    name: "libsnapshot_init",
    native_coverage: true,
    defaults: ["libsnapshot_defaults"],
    srcs: [":libsnapshot_sources"],
    ramdisk_available: true,
    recovery_available: true,
    cflags: [
        "-DLIBSNAPSHOT_NO_COW_WRITE",
    ],
    static_libs: [
        "libfs_mgr",
        "libselinux",
    ],
}

cc_library_static {
    name: "libsnapshot_nobinder",
    defaults: [
        "libsnapshot_defaults",
        "libsnapshot_hal_deps",
    ],
    srcs: [":libsnapshot_sources"],
    recovery_available: true,
    cflags: [
        "-DLIBSNAPSHOT_NO_COW_WRITE",
    ],
    static_libs: [
        "libfs_mgr",
    ],
    whole_static_libs: [
        "libselinux",
    ],
}

cc_defaults {
    name: "libsnapshot_cow_defaults",
    defaults: [
        "fs_mgr_defaults",
    ],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "liblog",
    ],
    static_libs: [
        "libbrotli",
        "libz",
        "liblz4",
        "libzstd",
    ],
    header_libs: [
        "libupdate_engine_headers",
    ],
}

cc_library_static {
    name: "libsnapshot_cow",
    defaults: [
        "libsnapshot_cow_defaults",
    ],
    srcs: [
        "libsnapshot_cow/cow_compress.cpp",
        "libsnapshot_cow/cow_decompress.cpp",
        "libsnapshot_cow/cow_format.cpp",
        "libsnapshot_cow/cow_reader.cpp",
        "libsnapshot_cow/parser_v2.cpp",
        "libsnapshot_cow/parser_v3.cpp",
        "libsnapshot_cow/snapshot_reader.cpp",
        "libsnapshot_cow/writer_base.cpp",
        "libsnapshot_cow/writer_v2.cpp",
        "libsnapshot_cow/writer_v3.cpp",
    ],

    header_libs: [
        "libstorage_literals_headers",
    ],
    export_include_dirs: ["include"],
    host_supported: true,
    recovery_available: true,
    ramdisk_available: true,
    vendor_ramdisk_available: true,
}

cc_library_static {
    name: "libsnapshot_test_helpers",
    defaults: ["libsnapshot_defaults"],
    export_include_dirs: [
        "include_test",
    ],
    srcs: [
        "android/snapshot/snapshot.proto",
        "test_helpers.cpp",
    ],
    shared_libs: [
        "android.hardware.boot@1.1",
        "libcrypto",
    ],
    export_shared_lib_headers: [
        "android.hardware.boot@1.1",
    ],
    header_libs: [
        "libstorage_literals_headers",
    ],
    export_header_lib_headers: [
        "libstorage_literals_headers",
    ],
    static_libs: [
        "libfs_mgr",
        "libgmock",
        "libgtest",
        "libselinux",
    ],
}

cc_defaults {
    name: "libsnapshot_test_defaults",
    defaults: [
        "libsnapshot_defaults",
        "libsnapshot_cow_defaults",
    ],
    srcs: [
        "partition_cow_creator_test.cpp",
        "snapshot_metadata_updater_test.cpp",
        "snapshot_test.cpp",
    ],
    shared_libs: [
        "libbinder",
        "libcrypto",
        "libhidlbase",
        "libprotobuf-cpp-lite",
        "libutils",
        "libz",
    ],
    static_libs: [
        "android.hardware.boot@1.0",
        "android.hardware.boot@1.1",
        "android.hardware.boot-V1-ndk",
        "libbrotli",
        "libfs_mgr_binder",
        "libgflags",
        "libgsi",
        "libgmock",
        "liblp",
        "libsnapshot_static",
        "libsnapshot_cow",
        "libsnapshot_test_helpers",
        "libsparse",
    ],
    header_libs: [
        "libstorage_literals_headers",
    ],
    auto_gen_config: true,
    require_root: true,
}

cc_test {
    name: "vts_libsnapshot_test",
    defaults: [
        "libsnapshot_test_defaults",
        "libsnapshot_hal_deps",
    ],
    test_suites: [
        "vts",
        "general-tests",
    ],
    compile_multilib: "both",
    multilib: {
        lib32: {
            suffix: "32",
        },
        lib64: {
            suffix: "64",
        },
    },
    test_options: {
        min_shipping_api_level: 30,
    },
}

cc_test {
    name: "vab_legacy_tests",
    defaults: [
        "libsnapshot_test_defaults",
        "libsnapshot_hal_deps",
    ],
    cppflags: [
        "-DLIBSNAPSHOT_TEST_VAB_LEGACY",
    ],
    test_suites: [
        "general-tests",
    ],
    compile_multilib: "64",
    test_options: {
        // Legacy VAB launched in Android R.
        min_shipping_api_level: 30,
        test_runner_options: [
            {
                name: "force-no-test-error",
                value: "false",
            },
        ],
    },
}

cc_test {
    name: "vts_ota_config_test",
    srcs: [
        "vts_ota_config_test.cpp",
    ],
    shared_libs: [
        "libbase",
    ],
    test_suites: [
        "vts",
    ],
    test_options: {
        min_shipping_api_level: 33,
    },
    auto_gen_config: true,
    require_root: true,
}

cc_binary {
    name: "snapshotctl",
    defaults: [
        "libsnapshot_cow_defaults",
        "libsnapshot_hal_deps",
    ],
    srcs: [
        "snapshotctl.cpp",
        "scratch_super.cpp",
    ],
    static_libs: [
        "libbrotli",
        "libfstab",
        "libz",
        "update_metadata-protos",
    ],
    shared_libs: [
        "libbase",
        "libext2_uuid",
        "libext4_utils",
        "libfs_mgr_binder",
        "libhidlbase",
        "liblog",
        "liblp",
        "libprotobuf-cpp-lite",
        "libsnapshot",
        "libstatslog",
        "libutils",
    ],
    header_libs: [
        "libstorage_literals_headers",
    ],
    product_variables: {
        debuggable: {
            cppflags: [
                "-DSNAPSHOTCTL_USERDEBUG_OR_ENG",
            ],
            shared_libs: [
                "android.hardware.boot@1.0",
                "android.hardware.boot@1.1",
                "android.hardware.boot-V1-ndk",
                "libboot_control_client",
            ],
        },
    },
}

cc_test {
    name: "cow_api_test",
    defaults: [
        "fs_mgr_defaults",
        "libsnapshot_cow_defaults",
    ],
    srcs: [
        "libsnapshot_cow/snapshot_reader_test.cpp",
        "libsnapshot_cow/test_v2.cpp",
        "libsnapshot_cow/test_v3.cpp",
    ],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "libcrypto",
        "liblog",
        "libz",
    ],
    static_libs: [
        "libbrotli",
        "libgtest",
        "libsnapshot_cow",
    ],
    header_libs: [
        "libstorage_literals_headers",
    ],
    test_suites: [
        "general-tests",
    ],
    test_options: {
        min_shipping_api_level: 30,
    },
    data: [
        "tools/testdata/cow_v2",
        "tools/testdata/incompressible_block",
    ],
    auto_gen_config: true,
    require_root: false,
    host_supported: true,
}

cc_binary {
    name: "inspect_cow",
    host_supported: true,
    device_supported: true,
    defaults: ["libsnapshot_cow_defaults"],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
        "-Wall",
        "-Werror",
    ],
    static_libs: [
        "libbase",
        "libbrotli",
        "libcrypto_static",
        "liblog",
        "libgflags",
        "libsnapshot_cow",
        "libz",
    ],
    shared_libs: [
    ],
    srcs: [
        "libsnapshot_cow/inspect_cow.cpp",
    ],
}

cc_binary {
    name: "create_snapshot",
    host_supported: true,
    device_supported: false,

    srcs: ["libsnapshot_cow/create_cow.cpp"],

    cflags: [
        "-Wall",
        "-Werror",
    ],

    static_libs: [
        "liblog",
        "libbase",
        "libext4_utils",
        "libsnapshot_cow",
        "libcrypto",
        "libbrotli",
        "libz",
        "liblz4",
        "libzstd",
        "libgflags",
    ],
    shared_libs: [
    ],

    header_libs: [
        "libstorage_literals_headers",
    ],

    dist: {
        targets: [
            "sdk",
            "sdk-repo-platform-tools",
            "sdk_repo",
        ],
    },
    target: {
        darwin: {
            enabled: false,
        },
        windows: {
            enabled: false,
        },
    },
    stl: "libc++_static",
    static_executable: true,
}

python_library_host {
    name: "snapshot_proto_python",
    srcs: [
        "android/snapshot/snapshot.proto",
    ],
    proto: {
        canonical_path_from_root: false,
    },
}
