# Copyright 2010-2021, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Description:
#   Declaration of package_group for visibility managing.

load("@bazel_skylib//lib:selects.bzl", "selects")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load(
    "//:build_defs.bzl",
    "mozc_select",
    "mozc_select_enable_session_watchdog",
    "mozc_select_enable_supplemental_model",
    "mozc_select_enable_usage_rewriter",
)
load("//:config.bzl", "BAZEL_TOOLS_PREFIX")
load("//bazel:stubs.bzl", "bzl_library")

package(default_visibility = ["//:__subpackages__"])

package_group(
    name = "mozc",
    packages = ["//..."],
)

config_setting(
    name = "opt",
    values = {
        "compilation_mode": "opt",
    },
    visibility = ["//visibility:private"],
)

# CHANNEL=dev is used to build Dev channel binaries.
# Dev channel is a kind of beta release that can be used by external users.
# The version number of Dev channel should end with 10x (e.g. 100 for Windows, 101 for macOS).
config_setting(
    name = "dev_channel",
    values = {
        "define": "CHANNEL=dev",
    },
)

# CHANNEL=stable is used to build Stable channel binaries.
# Stable channel is a main release.
# The version number of Stable channel is a single digit (e.g. 0 for Windows, 1 for macOS).
config_setting(
    name = "stable_channel",
    values = {
        "define": "CHANNEL=stable",
    },
)

# CODESIGN=release is used to build binaries that are signed for external product release.
config_setting(
    name = "codesign_release",
    values = {
        "define": "CODESIGN=release",
    },
)

# CODESIGN=testing is used to build binaries that are signed for internal testing release.
config_setting(
    name = "codesign_testing",
    values = {
        "define": "CODESIGN=testing",
    },
)

alias(
    name = "compiler",
    actual = BAZEL_TOOLS_PREFIX + "//tools/cpp:compiler",
    visibility = ["//:__subpackages__"],
)

config_setting(
    name = "compiler_msvc_cl",
    flag_values = {
        ":compiler": "msvc-cl",
    },
    visibility = ["//:__subpackages__"],
)

config_setting(
    name = "compiler_clang_cl",
    flag_values = {
        ":compiler": "clang-cl",
    },
    visibility = ["//:__subpackages__"],
)

selects.config_setting_group(
    name = "compiler_msvc_like",
    match_any = [
        # clang-cl.exe is a drop-in replacement for cl.exe.
        # Command line options are mostly compatible.
        # https://clang.llvm.org/docs/MSVCCompatibility.html
        ":compiler_clang_cl",
        ":compiler_msvc_cl",
    ],
    visibility = ["//:__subpackages__"],
)

# Generic Windows platform with clang-cl that automatically matches host CPU.
platform(
    name = "host-windows-clang-cl",
    constraint_values = [
        "@platforms//os:windows",
        "@bazel_tools//tools/cpp:clang-cl",
    ],
    parents = ["@local_config_platform//:host"],
)

platform(
    name = "windows-x86_32",
    constraint_values = [
        "@platforms//cpu:x86_32",
        "@platforms//os:windows",
    ],
)

platform(
    name = "windows-x86_64",
    constraint_values = [
        "@platforms//cpu:x86_64",
        "@platforms//os:windows",
    ],
)

platform(
    name = "windows-arm64",
    constraint_values = [
        "@platforms//cpu:arm64",
        "@platforms//os:windows",
    ],
)

# Special target so as to define special macros for each platforms.
# Don't depend on this directly. Use mozc_cc_(library|binary|test) rule instead.
cc_library(
    name = "macro",
    defines = mozc_select(
        android = [
        ],
        chromiumos = [
            "MOZC_USE_MOZC_TESTING",
        ],
        default = [
        ],
        ios = [
            "MOZC_USE_MOZC_TESTING",
        ],
        linux = [
            "GOOGLE_JAPANESE_INPUT_BUILD",
        ],
        macos = [
            "GOOGLE_JAPANESE_INPUT_BUILD",
        ],
        oss_android = [
        ],
        oss_linux = [
            "MOZC_BUILD",
        ],
        oss_macos = [
            "MOZC_BUILD",
        ],
        oss_windows = [
            "MOZC_BUILD",
        ],
        prod_linux = [
            "GOOGLE_JAPANESE_INPUT_BUILD",
        ],
        prod_macos = [
            "GOOGLE_JAPANESE_INPUT_BUILD",
        ],
        prod_windows = [
            "GOOGLE_JAPANESE_INPUT_BUILD",
        ],
        wasm = [
            "GOOGLE_JAPANESE_INPUT_BUILD",
        ],
        windows = [
            "GOOGLE_JAPANESE_INPUT_BUILD",
        ],
    ) + mozc_select(
        windows = [
            "WINVER=0x0A00",
            "_WIN32_WINNT=0x0A00",
            "_ATL_ALL_WARNINGS",
            "_ATL_ALLOW_CHAR_UNSIGNED",
            "_ATL_CSTRING_EXPLICIT_CONSTRUCTORS",
            "_ATL_NO_AUTOMATIC_NAMESPACE",
            "_ATL_NO_HOSTING",
            "ID_TRACE_LEVEL=1",
            "_MIDL_USE_GUIDDEF_",
            "PSAPI_VERSION=2",
            "UNICODE",
            "_UNICODE",
        ],
    ) + mozc_select_enable_session_watchdog(
        off = ["MOZC_DISABLE_SESSION_WATCHDOG"],
    ) + mozc_select_enable_supplemental_model([
    ]) + mozc_select_enable_usage_rewriter(
        off = ["NO_USAGE_REWRITER"],
    ) + select({
        ":dev_channel": ["CHANNEL_DEV=1"],
        "//conditions:default": [],
    }),
)

bzl_library(
    name = "build_defs_bzl",
    srcs = ["build_defs.bzl"],
    test_size = "small",
    visibility = ["//:__subpackages__"],
    deps = [
        ":config_bzl",
        "//bazel:run_build_tool_bzl",
        "//bazel:stubs.bzl",
        "//devtools/build_cleaner/skylark:build_defs_lib",
        "@bazel_skylib//rules:select_file",
        "@build_bazel_rules_apple//apple:macos",
        "@rules_cc//cc:core_rules",
    ],
)

bzl_library(
    name = "config_bzl",
    srcs = ["config.bzl"],
    test_size = "small",
    visibility = ["//:__subpackages__"],
)

filegroup(
    name = "package",
    srcs = mozc_select(
        android = ["//android/jni:native_libs"],
        default = [],
        linux = ["//unix:package"],
        macos = ["//mac:package"],
        windows = ["//win32/installer"],
    ),
)

selects.config_setting_group(
    name = "enable_session_watchdog",
    match_any = [
        # TODO(yuryu): define platforms and use bazel_platforms.
        "//bazel/cc_target_os:linux",
        "//bazel/cc_target_os:oss_linux",
        "//bazel/cc_target_os:prod_linux",
        "//bazel/cc_target_os:darwin",
        "//bazel/cc_target_os:oss_macos",
        "//bazel/cc_target_os:prod_macos",
        "//bazel/cc_target_os:windows",
        "//bazel/cc_target_os:oss_windows",
        "//bazel/cc_target_os:prod_windows",
    ],
)

selects.config_setting_group(
    name = "enable_spellchecker",
    match_any = [
        "@platforms//:incompatible",  # placeholder to avoid empty match.
    ],
)

selects.config_setting_group(
    name = "enable_usage_rewriter",
    match_any = [
        # TODO(yuryu): define platforms and use bazel_platforms.
        "//bazel/cc_target_os:linux",
        "//bazel/cc_target_os:oss_linux",
        "//bazel/cc_target_os:prod_linux",
        "//bazel/cc_target_os:darwin",
        "//bazel/cc_target_os:oss_macos",
        "//bazel/cc_target_os:prod_macos",
        "//bazel/cc_target_os:windows",
        "//bazel/cc_target_os:oss_windows",
        "//bazel/cc_target_os:prod_windows",
    ],
)
