# Copyright (C) 2025 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
#
#     https://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.

"""Perfetto Bazel module configuration for bzlmod."""

module(
    name = "perfetto",
    version = "0.0.0",
)

bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "protobuf", version = "31.1", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_python", version = "1.0.0")
bazel_dep(name = "rules_android", version = "0.6.6")

remote_android_extensions = use_extension(
    "@rules_android//bzlmod_extensions:android_extensions.bzl",
    "remote_android_tools_extensions",
)
use_repo(remote_android_extensions, "android_tools")

android_sdk_repository_extension = use_extension(
    "@rules_android//rules/android_sdk_repository:rule.bzl",
    "android_sdk_repository_extension",
)

# When built using 'tools/bazel', 'ANDROID_HOME' environment variable points
# to the hermetic Android SDK installation, that should be downloaded first
# with 'tools/install-build-deps --android'
android_sdk_repository_extension.configure(
    api_level = 35,
    build_tools_version = "35.0.1",
)
use_repo(android_sdk_repository_extension, "androidsdk")

register_toolchains("@androidsdk//:sdk-toolchain", "@androidsdk//:all")

bazel_dep(name = "rules_jvm_external", version = "6.9")
bazel_dep(name = "rules_android_ndk", version = "0.1.3")

android_ndk_repository_extension = use_extension(
    "@rules_android_ndk//:extension.bzl",
    "android_ndk_repository_extension",
)

# When built using 'tools/bazel', 'ANDROID_NDK_HOME' environment variable points
# to the hermetic Android NDK installation, that should be downloaded first
# with 'tools/install-build-deps --android'
android_ndk_repository_extension.configure(
    api_level = 26,
)
use_repo(android_ndk_repository_extension, "androidndk")

register_toolchains("@androidndk//:all")

# Perfetto configuration repository extension.
# This creates @perfetto_cfg which provides PERFETTO_CONFIG struct.
perfetto_cfg_ext = use_extension(
    "//bazel:perfetto_cfg_ext.bzl",
    "perfetto_cfg_ext",
)
use_repo(perfetto_cfg_ext, "perfetto_cfg")

# Perfetto third-party dependencies extension.
# These are the same deps defined in //bazel:deps.bzl for WORKSPACE users.
perfetto_deps = use_extension(
    "//bazel:perfetto_deps.bzl",
    "perfetto_deps",
)
use_repo(
    perfetto_deps,
    "perfetto_dep_expat",
    "perfetto_dep_linenoise",
    "perfetto_dep_llvm_demangle",
    "perfetto_dep_open_csd",
    "perfetto_dep_sqlite",
    "perfetto_dep_sqlite_src",
    "perfetto_dep_zlib",
)

# Maven dependencies for Android instrumentation tests.
# Use a unique name to avoid conflicts with other modules (e.g., bazel_worker_java).
maven = use_extension(
    "@rules_jvm_external//:extensions.bzl",
    "maven",
)
maven.install(
    name = "perfetto_maven",
    artifacts = [
        "androidx.test:runner:1.6.2",
        "androidx.test:monitor:1.7.2",
        "com.google.truth:truth:1.4.4",
        "junit:junit:4.13.2",
        "androidx.test.ext:junit:1.2.1",
    ],
    repositories = [
        "https://maven.google.com",
        "https://repo1.maven.org/maven2",
    ],
    # Use rules_android's aar_import to avoid toolchain type mismatch.
    aar_import_bzl_label = "@rules_android//rules:rules.bzl",
)
use_repo(maven, "perfetto_maven")
