# Copyright 2017 The Bazel Authors. All rights reserved.
#
# 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.

workspace(name = "bazel_toolchains_client")

local_repository(
    name = "bazel_toolchains",
    path = "../../../..",
)

local_repository(
    name = "toolchain_config_host",
    path = "../toolchain_config_host",
)

load(
    "@bazel_tools//tools/build_defs/repo:http.bzl",
    "http_archive",
)

http_archive(
    name = "java-sample",
    sha256 = "38f0c15f9805e0635acb7df1ac22b82e411e1bf29ddad90aa25f6967897af602",
    strip_prefix = "examples-5d8c8961a2516ebf875787df35e98cadd08d43dc/java-tutorial",
    urls = [("https://github.com/bazelbuild/examples/archive/5d8c8961a2516ebf875787df35e98cadd08d43dc.tar.gz")],
)

load("@bazel_toolchains//rules:rbe_repo.bzl", "rbe_autoconfig")

rbe_autoconfig(
    name = "rbe_default",
    use_checked_in_confs = "Force",
)

rbe_autoconfig(
    name = "rbe_default_generate",
    use_checked_in_confs = "False",
)

rbe_autoconfig(
    name = "rbe_jdk11",
    java_home = "/usr/lib/jvm/11.29.3-ca-jdk11.0.2/reduced",
    use_checked_in_confs = "Force",
)

load(
    "@toolchain_config_host//configs/test_configs:repo.bzl",
    "CUSTOM_BAZEL_DIGEST1",
    "CUSTOM_BAZEL_DIGEST2",
    "CUSTOM_BAZEL_DIGEST3",
    "CUSTOM_BAZEL_VERSION1",
    "CUSTOM_BAZEL_VERSION2",
    "CUSTOM_BAZEL_VERSION3",
    "CUSTOM_ENV1",
    "CUSTOM_ENV2",
    "CUSTOM_ENV3",
    "CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC",
)
load(
    "@toolchain_config_host//configs/test_configs_no_java:repo.bzl",
    NO_JAVA_TOOLCHAIN_CONFIG_SUITE_SPEC = "CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC",
)

# Basic test for rbe_custom, the corresponding GCB build should first
# build the @toolchain_config_host so this one can use those.
rbe_autoconfig(
    name = "rbe_custom",
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

# Should use checked-in configs as, even though container is specified, latest is
# set as tag.
rbe_autoconfig(
    name = "rbe_custom_tag_latest",
    registry = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC["container_registry"],
    repository = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC["container_repo"],
    tag = "latest",
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

# This test should generate configs as we use a custom env.
rbe_autoconfig(
    name = "rbe_custom_generate_env",
    env = {"CUSTOM_KEY": "CUSTOM_VALUE"},
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
)

load(
    "@bazel_toolchains//configs/ubuntu16_04_clang:versions.bzl",
    toolchain_config_suite_autogen_spec = "TOOLCHAIN_CONFIG_AUTOGEN_SPEC",
)
load(
    "@bazel_toolchains//configs/dependency-tracking:ubuntu1604.bzl",
    _ubuntu1604_registry = "registry",
    _ubuntu1604_repository = "repository",
)

# This test uses the custom toolchain_config_suite_spec. It should not generate configs as, even
# though target overrides repository and digest (using rbe-ubuntu16-04
# container), its setting the base container digest to point to a compatible
# bazel one for which configs are pre-generated.

rbe_autoconfig(
    name = "rbe_custom_base_container_digest",
    base_container_digest = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC.get("toolchain_config_suite_autogen_spec").latest_container,
    digest = toolchain_config_suite_autogen_spec.latest_container,
    registry = _ubuntu1604_registry,
    repository = _ubuntu1604_repository,
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

# This one should not generate configs either, as, even though it uses a custom
# toolchain_config_spec_name, the preeceding step which builds @rbe_custom_toolchain_config_spec_name in
# @toolchain_config_host should generate the configs.
rbe_autoconfig(
    name = "rbe_custom_toolchain_config_spec_name",
    toolchain_config_spec_name = "test_toolchain_config_spec_name",
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

# The three tests below should not generate configs as the corresponding
# targets in @toolchain_config_host should pre-generate them.
# These tests validate the versions.bzl file can be modified by
# several configs and remain valid when different environements
# are used
rbe_autoconfig(
    name = "rbe_custom_env1",
    env = CUSTOM_ENV1,
    toolchain_config_spec_name = "test_env1",
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

rbe_autoconfig(
    name = "rbe_custom_env2",
    env = CUSTOM_ENV2,
    toolchain_config_spec_name = "test_env2",
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

rbe_autoconfig(
    name = "rbe_custom_env3",
    env = CUSTOM_ENV3,
    toolchain_config_spec_name = "test_env3",
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

# The three tests below should not generate configs as the corresponding
# targets in @toolchain_config_host should pre-generate them.
# These tests validate the versions.bzl file can be modified by
# several configs and remain valid when different bazel versions
# are used
rbe_autoconfig(
    name = "rbe_custom_bazel_version1",
    bazel_version = CUSTOM_BAZEL_VERSION1,
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

rbe_autoconfig(
    name = "rbe_custom_bazel_version2",
    bazel_version = CUSTOM_BAZEL_VERSION2,
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

rbe_autoconfig(
    name = "rbe_custom_bazel_version3",
    bazel_version = CUSTOM_BAZEL_VERSION3,
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

# The three tests below should not generate configs as the corresponding
# targets in @toolchain_config_host should pre-generate them.
# These tests validate the versions.bzl file can be modified by
# several configs and remain valid when different bazel versions
# and different container tags are used
rbe_autoconfig(
    name = "rbe_custom_containers1",
    bazel_version = CUSTOM_BAZEL_VERSION1,
    digest = CUSTOM_BAZEL_DIGEST1,
    registry = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC["container_registry"],
    repository = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC["container_repo"],
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

rbe_autoconfig(
    name = "rbe_custom_containers2",
    bazel_version = CUSTOM_BAZEL_VERSION2,
    digest = CUSTOM_BAZEL_DIGEST2,
    registry = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC["container_registry"],
    repository = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC["container_repo"],
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

rbe_autoconfig(
    name = "rbe_custom_containers3",
    bazel_version = CUSTOM_BAZEL_VERSION3,
    digest = CUSTOM_BAZEL_DIGEST3,
    registry = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC["container_registry"],
    repository = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC["container_repo"],
    toolchain_config_suite_spec = CUSTOM_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

# Test that validates files in a local repo (should have been extracted
# by build_and_extract_configs_tar from a configs.tar output
# produced by @rbe_custom.
new_local_repository(
    name = "rbe_custom_config_tar",
    build_file = "BUILD.config_tar_test",
    path = "../config_out",
)

rbe_autoconfig(
    name = "rbe_custom_no_java",
    create_java_configs = False,
    toolchain_config_suite_spec = NO_JAVA_TOOLCHAIN_CONFIG_SUITE_SPEC,
    use_checked_in_confs = "Force",
)

load("@bazel_toolchains//rules:environments.bzl", "clang_env")

# Used by GCB tests that run rbe_ubuntu_1804.yaml config
rbe_autoconfig(
    name = "rbe_ubuntu1804",
    env = clang_env(),
    registry = "gcr.io",
    repository = "cloud-marketplace/google/rbe-ubuntu18-04",
    tag = "latest",
)
