//
// Copyright (C) 2011 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: "libartbase_defaults",
    defaults: ["art_defaults"],
    host_supported: true,
    srcs: [
        "arch/instruction_set.cc",
        "base/allocator.cc",
        "base/arena_allocator.cc",
        "base/arena_bit_vector.cc",
        "base/bit_vector.cc",
        "base/compiler_filter.cc",
        "base/enums.cc",
        "base/file_magic.cc",
        "base/file_utils.cc",
        "base/flags.cc",
        "base/hex_dump.cc",
        "base/hiddenapi_flags.cc",
        "base/logging.cc",
        "base/malloc_arena_pool.cc",
        "base/membarrier.cc",
        "base/memfd.cc",
        "base/memory_region.cc",
        "base/mem_map.cc",
        // "base/mem_map_fuchsia.cc", put in target when fuchsia supported by soong
        "base/metrics/metrics_common.cc",
        "base/os_linux.cc",
        "base/runtime_debug.cc",
        "base/safe_copy.cc",
        "base/scoped_arena_allocator.cc",
        "base/scoped_flock.cc",
        "base/socket_peer_is_trusted.cc",
        "base/time_utils.cc",
        "base/unix_file/fd_file.cc",
        "base/unix_file/random_access_file_utils.cc",
        "base/utils.cc",
        "base/zip_archive.cc",
    ],
    target: {
        android: {
            srcs: [
                "base/globals_unix.cc",
                "base/mem_map_unix.cc",
            ],
            static_libs: [
                // ZipArchive support, the order matters here to get all symbols.
                "libziparchive",
            ],
            shared_libs: [
                "libz",
                "liblog",
                // For ashmem.
                "libartpalette",
                // For common macros.
                "libbase",
            ],
            export_shared_lib_headers: ["libbase"], // ART's macros.h depends on libbase's macros.h.
        },
        not_windows: {
            srcs: [
                "base/globals_unix.cc",
                "base/mem_map_unix.cc",
            ],
            static: {
                cflags: ["-DART_STATIC_LIBARTBASE"],
            },
            shared_libs: [
                "libziparchive",
                "libz",
                "liblog",
                // For ashmem.
                "libartpalette",
                // For common macros.
                "libbase",
            ],
            export_shared_lib_headers: ["libbase"], // ART's macros.h depends on libbase's macros.h.
        },
        windows: {
            srcs: [
                "base/mem_map_windows.cc",
            ],
            static_libs: [
                "libziparchive",
                "libz",
                "liblog",
                // For ashmem.
                "libartpalette",
                // For common macros.
                "libbase",
            ],
            export_static_lib_headers: ["libbase"], // ART's macros.h depends on libbase's macros.h.

            cflags: ["-Wno-thread-safety"],
        },
        darwin: {
            enabled: true, // for libdexfile.
        },
    },
    generated_sources: ["art_libartbase_operator_srcs"],

    export_include_dirs: ["."],
}

cc_defaults {
    name: "libartbase_static_base_defaults",
    whole_static_libs: [
        "libbase",
        "libartpalette",
        "liblog",
        "libz",
        "libziparchive",
    ],
}

cc_defaults {
    name: "libartbase_static_defaults",
    defaults: ["libartbase_static_base_defaults"],
    whole_static_libs: ["libartbase"],
}

cc_defaults {
    name: "libartbased_static_defaults",
    defaults: ["libartbase_static_base_defaults"],
    whole_static_libs: ["libartbased"],
}

gensrcs {
    name: "art_libartbase_operator_srcs",
    cmd: "$(location generate_operator_out) art/libartbase $(in) > $(out)",
    tools: ["generate_operator_out"],
    srcs: [
        "arch/instruction_set.h",
        "base/allocator.h",
        "base/unix_file/fd_file.h",
    ],
    output_extension: "operator_out.cc",
}

art_cc_library {
    name: "libartbase",
    defaults: [
        "libartbase_defaults",
        "libart_nativeunwind_defaults",
    ],
    visibility: [
        // TODO(b/183483755): Please visibility checks when the prebuilt
        // libartbase is present but not preferred, and the prebuilt libdexfile
        // hence depends on the source instead.
        // TODO(b/172480617): Alternatively, clean up when we no longer need to
        // support both prebuilts and sources present simultaneously.
        "//prebuilts/module_sdk/art/current/host-exports",
        // TODO(b/133140750): Clean this up.
        "//packages/modules/NetworkStack/tests:__subpackages__",
    ],
    apex_available: [
        "com.android.art",
        "com.android.art.debug",
    ],

    shared_libs: [
        "libbase",
        "libziparchive",
    ],
    export_shared_lib_headers: ["libbase"],
    target: {
        windows: {
            enabled: true,
            shared: {
                enabled: false,
            },
        },
    },
    afdo: true,
}

art_cc_library {
    name: "libartbased",
    defaults: [
        "art_debug_defaults",
        "libartbase_defaults",
    ],
    apex_available: [
        "com.android.art.debug",
    ],
    shared_libs: [
        "libbase",
        "libziparchive",
    ],
    export_shared_lib_headers: ["libbase"],
    target: {
        windows: {
            enabled: true,
            shared: {
                enabled: false,
            },
        },
    },
}

art_cc_defaults {
    name: "libartbase-art-gtest-defaults",
    srcs: [
        "base/common_art_test.cc",
    ],
    shared_libs: [
        "libbase",
        "libbacktrace",
    ],
    header_libs: [
        "libnativehelper_header_only",
    ],
    static: {
        whole_static_libs: [
            "libc++fs",
        ],
    },
    shared: {
        static_libs: [
            "libc++fs",
        ],
    },
}

art_cc_library {
    name: "libartbase-art-gtest",
    defaults: [
        "libart-gtest-defaults",
        "libartbase-art-gtest-defaults",
    ],
    shared_libs: [
        "libartbase",
        "libartbase-testing",
        "libdexfile",
    ],
}

art_cc_library {
    name: "libartbased-art-gtest",
    defaults: [
        "libartd-gtest-defaults",
        "libartbase-art-gtest-defaults",
    ],
    shared_libs: [
        "libartbased",
        "libartbased-testing",
        "libdexfiled",
    ],
}

art_cc_defaults {
    name: "libartbase-testing-defaults",
    defaults: [
        "art_defaults",
    ],
    host_supported: true,
    srcs: [
        "base/testing.cc",
    ],
    shared_libs: [
        "libbase",
    ],
    apex_available: [
        "com.android.art.debug",
        // TODO(b/183882457): This lib 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 libdexfiled and others.
        "com.android.art",
    ],
}

art_cc_library {
    name: "libartbase-testing",
    defaults: [
        "libartbase-testing-defaults",
    ],
    shared_libs: [
        "libartbase",
    ],
}

art_cc_library {
    name: "libartbased-testing",
    defaults: [
        "libartbase-testing-defaults",
    ],
    shared_libs: [
        "libartbased",
    ],
}

art_cc_defaults {
    name: "art_libartbase_tests_defaults",
    tidy_timeout_srcs: [
        "base/bit_utils_test.cc",
        "base/intrusive_forward_list_test.cc",
    ],
    srcs: [
        "arch/instruction_set_test.cc",
        "base/arena_allocator_test.cc",
        "base/bit_field_test.cc",
        "base/bit_memory_region_test.cc",
        "base/bit_string_test.cc",
        "base/bit_struct_test.cc",
        "base/bit_table_test.cc",
        "base/bit_utils_test.cc",
        "base/bit_vector_test.cc",
        "base/compiler_filter_test.cc",
        "base/file_utils_test.cc",
        "base/flags_test.cc",
        "base/hash_map_test.cc",
        "base/hash_set_test.cc",
        "base/hex_dump_test.cc",
        "base/histogram_test.cc",
        "base/indenter_test.cc",
        "base/intrusive_forward_list_test.cc",
        "base/leb128_test.cc",
        "base/logging_test.cc",
        "base/memfd_test.cc",
        "base/membarrier_test.cc",
        "base/memory_region_test.cc",
        "base/mem_map_test.cc",
        "base/metrics/metrics_test.cc",
        "base/safe_copy_test.cc",
        "base/scoped_flock_test.cc",
        "base/time_utils_test.cc",
        "base/transform_array_ref_test.cc",
        "base/transform_iterator_test.cc",
        "base/unix_file/fd_file_test.cc",
        "base/utils_test.cc",
        "base/variant_map_test.cc",
        "base/zip_archive_test.cc",
    ],
    shared_libs: [
        "libbase",
    ],
    static_libs: [
        "libgmock",
    ],
}

// Version of ART gtest `art_libartbase_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_libartbase_tests",
    defaults: [
        "art_gtest_defaults",
        "art_libartbase_tests_defaults",
    ],
}

// Standalone version of ART gtest `art_libartbase_tests`, not bundled with the ART APEX on target.
art_cc_test {
    name: "art_standalone_libartbase_tests",
    defaults: [
        "art_standalone_gtest_defaults",
        "art_libartbase_tests_defaults",
    ],
    data: [":generate-boot-image"],
    test_config_template: ":art-gtests-target-standalone-with-boot-image-template",
}

cc_library_headers {
    name: "art_libartbase_headers",
    defaults: ["art_defaults"],
    host_supported: true,
    export_include_dirs: ["."],
    shared_libs: ["libbase"],
    export_shared_lib_headers: ["libbase"],

    apex_available: [
        "com.android.art",
        "com.android.art.debug",
    ],
}