# 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.

# Build rules for the Windows installer.

load("//:build_defs.bzl", "mozc_py_binary")
load("//:config.bzl", "BRANDING")

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

_TARGET_COMPATIBLE_WITH = [
    "@platforms//os:windows",
]

mozc_py_binary(
    name = "build_installer",
    srcs = ["build_installer.py"],
    test_lib = False,
    deps = [
        "//build_tools:mozc_version_lib",
        "//build_tools:vs_util",
    ],
)

exports_files([
    "installer_64bit.wxs",
    "installer_oss_64bit.wxs",
])

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

_WXS_FILE = ":installer_oss_64bit.wxs" if BRANDING == "Mozc" else ":installer_64bit.wxs"

_MSI_FILE = "Mozc64.msi" if BRANDING == "Mozc" else "GoogleJapaneseInput64.msi"

genrule(
    name = "installer",
    srcs = [
        "//gui/tool:mozc_tool_win",
        "//renderer/win32:win32_renderer_main",
        "//server:mozc_server_win",
        "//win32/broker:mozc_broker_main",
        "//win32/cache_service:mozc_cache_service",
        "//win32/tip:mozc_tip32",
        "//win32/tip:mozc_tip64",
        "//win32/custom_action",
        _WXS_FILE,
        "//base:mozc_version_txt",
        "//data/images/win:product_icon.ico",
        "//data/installer:credits_en.html",
        "@qt_win//:bin/Qt6Core.dll",
        "@wix//:wix.exe",
    ] + select({
        "@platforms//cpu:arm64": [
            "//win32/tip:mozc_tip64arm",
            "//win32/tip:mozc_tip64x",
        ],
        "//conditions:default": [],
    }),
    outs = [_MSI_FILE],
    cmd = " ".join([
        "$(location :build_installer)",
        "--output=$@",
        "--version_file=$(location //base:mozc_version_txt)",
        "--mozc_tool=$(location //gui/tool:mozc_tool_win)",
        "--mozc_renderer=$(location //renderer/win32:win32_renderer_main)",
        "--mozc_server=$(location //server:mozc_server_win)",
        "--mozc_broker=$(location //win32/broker:mozc_broker_main)",
        "--mozc_cache_service=$(location //win32/cache_service:mozc_cache_service)",
        "--mozc_tip32=$(location //win32/tip:mozc_tip32)",
        "--mozc_tip64=$(location //win32/tip:mozc_tip64)",
        "--custom_action=$(location //win32/custom_action)",
        "--icon_path=$(location //data/images/win:product_icon.ico)",
        "--credit_file=$(location //data/installer:credits_en.html)",
        "--qt_core_dll=$(location @qt_win//:bin/Qt6Core.dll)",
        "--wxs_path=$(location " + _WXS_FILE + ")",
        "--wix_path=$(location @wix//:wix.exe)",
        "--branding=" + BRANDING,
    ]) + select({
        "@platforms//cpu:arm64": " " + " ".join([
            "--mozc_tip64arm=$(location //win32/tip:mozc_tip64arm)",
            "--mozc_tip64x=$(location //win32/tip:mozc_tip64x)",
        ]),
        "//conditions:default": "",
    }) + select({
        ":debug_build": " --debug_build",
        "//conditions:default": "",
    }) + select({
        "@platforms//cpu:x86_64": " --arch=x64",
        "@platforms//cpu:arm64": " --arch=arm64",
        "//conditions:default": "",
    }),
    target_compatible_with = _TARGET_COMPATIBLE_WITH,
    tools = [
        ":build_installer",
    ],
    visibility = ["//:__subpackages__"],
)
