//
// Copyright (C) 2013 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: "libziparchive_flags",
    cpp_std: "c++2a",
    cflags: [
        // ZLIB_CONST turns on const for input buffers, which is pretty standard.
        "-DZLIB_CONST",
        "-Werror",
        "-Wall",
        "-D_FILE_OFFSET_BITS=64",
    ],
    cppflags: [
        // Incorrectly warns when C++11 empty brace {} initializer is used.
        // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61489
        "-Wno-missing-field-initializers",
        "-Wconversion",
        "-Wno-sign-conversion",
    ],

    // Enable -Wold-style-cast only for non-Windows targets.  _islower_l,
    // _isupper_l etc. in MinGW locale_win32.h (included from
    // libcxx/include/__locale) has an old-style-cast.
    target: {
        not_windows: {
            cppflags: [
                "-Wold-style-cast",
            ],
        },
    },
    sanitize: {
        misc_undefined: [
            "signed-integer-overflow",
            "unsigned-integer-overflow",
            "shift",
            "integer-divide-by-zero",
            "implicit-signed-integer-truncation",
            // TODO: Fix crash when we enable this option
            // "implicit-unsigned-integer-truncation",
            // TODO: not tested yet.
            // "implicit-integer-sign-change",
        ],
    },
}

cc_defaults {
    name: "libziparchive_defaults",
    local_include_dirs: ["incfs_support/include/"],
    srcs: [
        "zip_archive.cc",
        "zip_archive_stream_entry.cc",
        "zip_cd_entry_map.cc",
        "zip_error.cpp",
        "zip_writer.cc",
    ],

    target: {
        windows: {
            cflags: ["-mno-ms-bitfields"],

            enabled: true,
        },
    },

    shared_libs: [
        "libbase",
        "liblog",
    ],

    // for FRIEND_TEST
    header_libs: ["libgtest_prod_headers"],
    export_header_lib_headers: ["libgtest_prod_headers"],

    export_include_dirs: ["include"],
}

cc_defaults {
    name: "incfs_support_defaults",
    cflags: ["-DZIPARCHIVE_DISABLE_CALLBACK_API=1"],
    export_include_dirs: ["incfs_support/include/"],
    tidy: true,
    tidy_checks: [
        "android-*",
        "cert-*",
        "clang-analyzer-security*",
        "-cert-err34-c",
        "clang-analyzer-security*",
        // Disabling due to many unavoidable warnings from POSIX API usage.
        "-google-runtime-int",
        "-google-explicit-constructor",
        // do not call 'longjmp'; consider using exception handling instead - library relies on it
        "-cert-err52-cpp",
        // typedef pointer used with const: all Zip* typedefs generate this when declared as const
        "-misc-misplaced-const",
    ],
}

cc_defaults {
    name: "libziparchive_lib_defaults",
    host_supported: true,
    vendor_available: true,
    product_available: true,
    recovery_available: true,
    vendor_ramdisk_available: true,
    native_bridge_supported: true,
    vndk: {
        enabled: true,
    },
    double_loadable: true,
    export_shared_lib_headers: ["libbase"],

    defaults: [
        "libziparchive_defaults",
        "libziparchive_flags",
    ],
    shared_libs: [
        "liblog",
        "libbase",
        "libz",
    ],
    target: {
        linux_bionic: {
            enabled: true,
        },
    },

    apex_available: [
        "//apex_available:platform",
        "com.android.art",
        "com.android.art.debug",
        "com.android.virt",
    ],
    min_sdk_version: "apex_inherit",
}

cc_library {
    name: "libziparchive",
    defaults: ["libziparchive_lib_defaults"],
    cflags: [
        // Disable incfs hardending code for the default library
        "-DINCFS_SUPPORT_DISABLED=1",
    ],
}

cc_library_static {
    name: "libziparchive_for_incfs",
    defaults: ["libziparchive_lib_defaults", "incfs_support_defaults"],
    srcs: [
        "incfs_support/signal_handling.cpp",
    ],
}

// Tests.
cc_test {
    name: "ziparchive-tests",
    host_supported: true,
    defaults: ["libziparchive_flags"],

    data: [
        "testdata/**/*",
    ],

    srcs: [
        "entry_name_utils_test.cc",
        "zip_archive_test.cc",
        "zip_writer_test.cc",
    ],
    shared_libs: [
        "libbase",
        "liblog",
    ],

    static_libs: [
        "libziparchive",
        "libz",
        "libutils",
    ],

    target: {
        host: {
            cppflags: ["-Wno-unnamed-type-template-args"],
        },
        windows: {
            enabled: true,
        },
    },
    test_suites: ["device-tests"],
}

// Performance benchmarks.
cc_benchmark {
    name: "ziparchive-benchmarks",
    defaults: ["libziparchive_flags"],

    srcs: [
        "zip_archive_benchmark.cpp",
    ],
    shared_libs: [
        "libbase",
        "liblog",
    ],

    static_libs: [
        "libziparchive",
        "libz",
        "libutils",
    ],

    target: {
        host: {
            cppflags: ["-Wno-unnamed-type-template-args"],
        },
    },
}

cc_binary {
    name: "ziptool",
    defaults: ["libziparchive_flags"],
    srcs: ["ziptool.cpp"],
    shared_libs: [
        "libbase",
        "libziparchive",
        "libz",
    ],
    recovery_available: true,
    host_supported: true,
    target: {
        android: {
            symlinks: ["unzip", "zipinfo"],
        },
    },
}

cc_fuzz {
    name: "libziparchive_fuzzer",
    srcs: ["libziparchive_fuzzer.cpp"],
    static_libs: [
        "libziparchive",
        "libbase",
        "libz",
        "liblog"
    ],
    host_supported: true,
    corpus: ["testdata/*"],
}

cc_fuzz {
    name: "libziparchive_for_incfs_fuzzer",
    srcs: ["libziparchive_fuzzer.cpp"],
    static_libs: [
        "libziparchive_for_incfs",
        "libbase",
        "libz",
        "liblog"
    ],
    host_supported: true,
    corpus: ["testdata/*"],
}

cc_fuzz {
    name: "libziparchive_writer_fuzzer",
    srcs: ["libziparchive_writer_fuzzer.cpp"],
    static_libs: ["libziparchive", "libbase", "libz", "liblog"],
    host_supported: true,
    corpus: ["testdata/*"],
}

sh_test {
    name: "ziptool-tests",
    src: "run-ziptool-tests-on-android.sh",
    filename: "run-ziptool-tests-on-android.sh",
    test_suites: ["general-tests"],
    host_supported: true,
    device_supported: false,
    data: ["cli-tests/**/*"],
    target_required: ["cli-test", "ziptool"],
    data_device_bins: ["cli-test"],
}

python_test_host {
    name: "ziparchive_tests_large",
    srcs: ["test_ziparchive_large.py"],
    main: "test_ziparchive_large.py",
    version: {
        py2: {
            enabled: true,
            embedded_launcher: false,
        },
        py3: {
            enabled: false,
            embedded_launcher: false,
        },
    },
    test_suites: ["general-tests"],
    test_options: {
        unit_test: false,
    },
}