//
// Copyright (C) 2020 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 {
    // See: http://go/android-license-faq
    // A large-scale-change added 'default_applicable_licenses' to import
    // all of the 'license_kinds' from "art_license"
    // to get the below license kinds:
    //   SPDX-license-identifier-Apache-2.0
    default_applicable_licenses: ["art_license"],
}

cc_defaults {
    name: "odrefresh-defaults",
    srcs: [
        "odrefresh.cc",
        "odr_common.cc",
        "odr_compilation_log.cc",
        "odr_fs_utils.cc",
        "odr_metrics.cc",
        "odr_metrics_record.cc",
    ],
    local_include_dirs: ["include"],
    header_libs: ["dexoptanalyzer_headers"],
    generated_sources: [
        "apex-info-list-tinyxml",
        "art-apex-cache-info",
        "art-odrefresh-operator-srcs",
    ],
    shared_libs: [
        "libartpalette",
        "libbase",
        "liblog",
    ],
    static_libs: ["libtinyxml2"],
    tidy: true,
    tidy_flags: [
        "-format-style=file",
        "-header-filter=(art/odrefresh/|system/apex/)",
    ],
}

cc_defaults {
    name: "odrefresh_binary_defaults",
    host_supported: true,
    defaults: [
        "art_defaults",
        "odrefresh-defaults",
    ],
    srcs: ["odrefresh_main.cc"],
    shared_libs: [
        "libdexfile",
    ],
    target: {
        android: {
            compile_multilib: "first",
        },
    },
}

cc_library_headers {
    name: "odrefresh_headers",
    export_include_dirs: ["include"],
    host_supported: true,
    stl: "none",
    system_shared_libs: [],
    min_sdk_version: "29", // As part of mainline modules(APEX), it should support at least 29(Q).
    sdk_version: "minimum", // The minimum sdk version required by users of this module.
    apex_available: [
        "//apex_available:platform", // For odsign.
    ],
    visibility: ["//visibility:public"],
}

gensrcs {
    name: "art-odrefresh-operator-srcs",
    cmd: "$(location generate_operator_out) art/odrefresh $(in) > $(out)",
    tools: ["generate_operator_out"],
    srcs: [
        "odr_metrics.h",
    ],
    output_extension: "operator_out.cc",
}

art_cc_binary {
    name: "odrefresh",
    defaults: ["odrefresh_binary_defaults"],
    required: [
        "dexoptanalyzer",
        "dex2oat",
    ],
    shared_libs: [
        "libart",
        "libartbase",
    ],
    apex_available: [
        "com.android.art",
        "com.android.art.debug",
    ],
}

art_cc_binary {
    name: "odrefreshd",
    defaults: [
        "art_debug_defaults",
        "odrefresh_binary_defaults",
    ],
    required: [
        "dexoptanalyzerd",
        "dex2oatd",
    ],
    shared_libs: [
        "libartd",
        "libartbased",
    ],
    apex_available: [
        "com.android.art.debug",
        // TODO(b/183882457): This binary doesn't go into com.android.art, but
        // apex_available lists need to be the same for internal libs to avoid
        // stubs, and this depends on libartd.
        "com.android.art",
    ],
}

cc_library_static {
    name: "libodrstatslog",
    defaults: ["art_defaults"],
    host_supported: true,
    export_include_dirs: ["include"],
    local_include_dirs: ["include"],
    shared_libs: ["libartbase"],
    target: {
        android: {
            generated_headers: ["statslog_odrefresh.h"],
            generated_sources: ["statslog_odrefresh.cpp"],
            srcs: [
                "odr_metrics_record.cc",
                "odr_statslog_android.cc",
            ],
            shared_libs: ["libstatssocket"],
        },
        host: {
            srcs: ["odr_statslog_host.cc"],
        },
    },
    apex_available: [
        "com.android.art",
        "com.android.art.debug",
    ],
}

art_cc_defaults {
    name: "art_odrefresh_tests_defaults",
    defaults: ["odrefresh-defaults"],
    header_libs: ["odrefresh_headers"],
    srcs: [
        "odr_artifacts_test.cc",
        "odr_compilation_log_test.cc",
        "odr_fs_utils_test.cc",
        "odr_metrics_test.cc",
        "odr_metrics_record_test.cc",
        "odrefresh_test.cc",
    ],
    static_libs: [
        "libgmock",
    ],
}

// Version of ART gtest `art_odrefresh_tests` bundled with the ART APEX on target.
// TODO(b/192274705): Remove this module when the migration to standalone ART gtests is complete.
art_cc_test {
    name: "art_odrefresh_tests",
    defaults: [
        "art_gtest_defaults",
        "art_odrefresh_tests_defaults",
    ],
    shared_libs: [
        "libdexfiled",
    ],
    test_config_template: "art_odrefresh_tests.xml",
}

// Standalone version of ART gtest `art_odrefresh_tests`, not bundled with the ART APEX on target.
art_cc_test {
    name: "art_standalone_odrefresh_tests",
    defaults: [
        "art_standalone_gtest_defaults",
        "art_odrefresh_tests_defaults",
    ],
    shared_libs: [
        "libdexfile",
    ],
    test_config_template: "art_odrefresh_tests.xml",
}

genrule {
    name: "statslog_odrefresh.h",
    tools: ["stats-log-api-gen"],
    cmd: "$(location stats-log-api-gen) --header $(genDir)/statslog_odrefresh.h --module art --namespace art,metrics,statsd",
    out: [
        "statslog_odrefresh.h",
    ],
}

genrule {
    name: "statslog_odrefresh.cpp",
    tools: ["stats-log-api-gen"],
    cmd: "$(location stats-log-api-gen) --cpp $(genDir)/statslog_odrefresh.cpp --module art --namespace art,metrics,statsd --importHeader statslog_odrefresh.h",
    out: [
        "statslog_odrefresh.cpp",
    ],
}

xsd_config {
    name: "art-apex-cache-info",
    srcs: ["CacheInfo.xsd"],
    package_name: "com.android.art",
    api_dir: "schema",
    gen_writer: true,
    tinyxml: true,
}
