load("@pico-sdk//bazel:defs.bzl", "incompatible_with_config")
load("@rules_cc//cc:cc_library.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])

# This exists to break a dependency cycle between
# this library and //src/common/pico_sync.
# Application code should always use :pico_time instead.
cc_library(
    name = "pico_time_headers",
    hdrs = [
        "include/pico/time.h",
        "include/pico/timeout_helper.h",
    ],
    includes = ["include"],
    # invalid_params_if() uses Statement Expressions, which aren't supported in MSVC.
    target_compatible_with = incompatible_with_config("@rules_cc//cc/compiler:msvc-cl"),
    visibility = [
        "//src/common/pico_sync:__pkg__",
        "//src/host/pico_time_adapter:__pkg__",
        "//src/rp2_common/tinyusb:__pkg__",
    ],
    deps = select({
        "//bazel/constraint:host": [
            "//src/host/hardware_timer:hardware_timer_headers",
        ],
        "//conditions:default": [
            "//src/rp2_common/hardware_timer:hardware_timer_headers",
        ],
    }),
)

cc_library(
    name = "pico_time",
    srcs = [
        "time.c",
        "timeout_helper.c",
    ],
    hdrs = [
        "include/pico/time.h",
        "include/pico/timeout_helper.h",
    ],
    # macOS defines __weak as part of Xcode, and the semantics are unfortunately
    # different.
    copts = select({
        "@platforms//os:macos": ["-Wno-ignored-attributes"],
        "//conditions:default": [],
    }),
    includes = ["include"],
    deps = [
        "//src/common/pico_base_headers",
        "//src/common/pico_sync",
        "//src/common/pico_util",
    ] + select({
        "//bazel/constraint:host": [
            "//src/host/hardware_gpio",
            "//src/host/hardware_timer",
            "//src/host/pico_runtime",
            "//src/host/pico_time_adapter",
        ],
        "//conditions:default": [
            "//src/rp2_common/hardware_gpio",
            "//src/rp2_common/hardware_timer",
            "//src/rp2_common/pico_runtime_init",
            "//src/rp2_common/pico_time_adapter",
        ],
    }),
)
