//
// 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: "libsnapuserd_client_defaults",
    defaults: [
        "fs_mgr_defaults",
    ],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
    ],
    export_include_dirs: ["include"],
    srcs: [
        "snapuserd_client.cpp",
    ],
}

cc_library_static {
    name: "libsnapuserd_client",
    defaults: [
        "fs_mgr_defaults",
        "libsnapuserd_client_defaults",
    ],
    recovery_available: true,
    static_libs: [
        "libcutils_sockets",
        "libfs_mgr_file_wait",
        "libdm",
    ],
    shared_libs: [
        "libbase",
        "liblog",
    ],
    export_include_dirs: ["include"],
    ramdisk_available: true,
    vendor_ramdisk_available: true,
}

cc_library_static {
    name: "libsnapuserd",
    defaults: [
        "fs_mgr_defaults",
    ],
    local_include_dirs: ["include/"],
    srcs: [
        "dm_user_block_server.cpp",
        "snapuserd_buffer.cpp",
        "user-space-merge/handler_manager.cpp",
        "user-space-merge/merge_worker.cpp",
        "user-space-merge/read_worker.cpp",
        "user-space-merge/snapuserd_core.cpp",
        "user-space-merge/snapuserd_readahead.cpp",
        "user-space-merge/snapuserd_transitions.cpp",
        "user-space-merge/snapuserd_verify.cpp",
        "user-space-merge/worker.cpp",
        "utility.cpp",
    ],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
        "-Wall",
        "-Werror",
    ],
    static_libs: [
        "libbase",
        "libdm",
        "libext2_uuid",
        "libext4_utils",
        "libsnapshot_cow",
        "liburing",
        "libprocessgroup",
        "libjsoncpp",
        "libcgrouprc",
        "libcgrouprc_format",
    ],
    include_dirs: ["bionic/libc/kernel"],
    export_include_dirs: ["include"],
    header_libs: [
        "libcutils_headers",
        "libstorage_literals_headers",
    ],
    ramdisk_available: true,
    vendor_ramdisk_available: true,
    recovery_available: true,
    host_supported: true,
}

cc_defaults {
    name: "snapuserd_defaults",
    defaults: [
        "fs_mgr_defaults",
    ],
    srcs: [
        "snapuserd_daemon.cpp",
        "user-space-merge/snapuserd_server.cpp",
    ],

    cflags: [
        "-D_FILE_OFFSET_BITS=64",
        "-Wall",
        "-Werror",
    ],

    static_libs: [
        "libbase",
        "libbrotli",
        "libcutils_sockets",
        "libdm",
        "libext2_uuid",
        "libfs_mgr_file_wait",
        "libgflags",
        "liblog",
        "libsnapshot_cow",
        "libsnapuserd",
        "libprocessgroup",
        "libjsoncpp",
        "libcgrouprc",
        "libcgrouprc_format",
        "libsnapuserd_client",
        "libz",
        "liblz4",
        "libext4_utils",
        "liburing",
        "libzstd",
    ],

    header_libs: [
        "libcutils_headers",
        "libstorage_literals_headers",
    ],

    include_dirs: ["bionic/libc/kernel"],
    system_shared_libs: [],

    // snapuserd is started during early boot by first-stage init. At that
    // point, /system is mounted using the "dm-user" device-mapper kernel
    // module. dm-user routes all I/O to userspace to be handled by
    // snapuserd, which would lead to deadlock if we had to handle page
    // faults for its code pages.
    static_executable: true,
}

cc_binary {
    name: "snapuserd",
    defaults: ["snapuserd_defaults"],
    init_rc: [
        "snapuserd.rc",
    ],
    static_libs: [
        "libsnapuserd_client",
    ],
    ramdisk_available: false,
    vendor_ramdisk_available: true,
    recovery_available: true,
}

// This target will install to /system/bin/snapuserd_ramdisk 
// It will also create a symblink on /system/bin/snapuserd that point to
// /system/bin/snapuserd_ramdisk .
// This way, init can check if generic ramdisk copy exists.
cc_binary {
    name: "snapuserd_ramdisk",
    defaults: ["snapuserd_defaults"],
    init_rc: [
        "snapuserd.rc",
    ],
    // This target is specifically for generic ramdisk, therefore we set
    // vendor_ramdisk_available to false.
    ramdisk_available: true,
    vendor_ramdisk_available: false,
    ramdisk: true,
    symlinks: ["snapuserd"],
}

cc_defaults {
    name: "snapuserd_test_defaults",
    defaults: [
        "fs_mgr_defaults",
        "libsnapshot_cow_defaults",
    ],
    srcs: [
        "testing/dm_user_harness.cpp",
        "testing/harness.cpp",
        "testing/host_harness.cpp",
        "user-space-merge/snapuserd_test.cpp",
    ],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "liblog",
    ],
    static_libs: [
        "libbrotli",
        "libcutils_sockets",
        "libdm",
        "libext2_uuid",
        "libext4_utils",
        "libfs_mgr_file_wait",
        "libgflags",
        "libgtest",
        "libsnapshot_cow",
        "libsnapuserd",
        "libprocessgroup",
        "libjsoncpp",
        "libcgrouprc",
        "libcgrouprc_format",
        "liburing",
        "libz",
    ],
    include_dirs: [
        "bionic/libc/kernel",
        ".",
    ],
    header_libs: [
        "libstorage_literals_headers",
        "libfiemap_headers",
        "libcutils_headers",
    ],
    test_options: {
        min_shipping_api_level: 30,
    },

    compile_multilib: "both",
    multilib: {
        lib32: {
            suffix: "32",
        },
        lib64: {
            suffix: "64",
        },
    },

    auto_gen_config: true,
    require_root: true,
}

cc_test {
    name: "snapuserd_test",
    defaults: ["snapuserd_test_defaults"],
    host_supported: true,
    test_suites: [
        "general-tests",
    ],
    test_options: {
        test_runner_options: [
            {
                name: "force-no-test-error",
                value: "false",
            },
        ],
    },
}

// vts tests cannot be host_supported.
cc_test {
    name: "vts_snapuserd_test",
    defaults: ["snapuserd_test_defaults"],
    test_suites: [
        "vts",
    ],
    test_options: {
        // VABC mandatory in Android T per VSR.
        min_shipping_api_level: 32,
    },
}

cc_binary_host {
    name: "snapuserd_extractor",
    defaults: [
        "fs_mgr_defaults",
        "libsnapshot_cow_defaults",
    ],
    srcs: [
        "testing/dm_user_harness.cpp",
        "testing/harness.cpp",
        "testing/host_harness.cpp",
        "user-space-merge/extractor.cpp",
        "snapuserd_extractor.cpp",
    ],
    cflags: [
        "-D_FILE_OFFSET_BITS=64",
        "-Wall",
        "-Werror",
    ],
    shared_libs: [
        "libbase",
        "liblog",
    ],
    static_libs: [
        "libbrotli",
        "libcutils_sockets",
        "libdm",
        "libext2_uuid",
        "libext4_utils",
        "libfs_mgr_file_wait",
        "libgflags",
        "libsnapshot_cow",
        "libsnapuserd",
        "libprocessgroup",
        "libjsoncpp",
        "libcgrouprc",
        "libcgrouprc_format",
        "liburing",
        "libz",
    ],
    include_dirs: [
        "bionic/libc/kernel",
        ".",
    ],
    header_libs: [
        "libstorage_literals_headers",
        "libfiemap_headers",
        "libcutils_headers",
    ],
}
