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

load("//rules:docker_config.bzl", "docker_toolchain_autoconfig")
load(
    "//rules:environments.bzl",
    "clang_env",
    "gcc_env",
    "ubuntu16_04_clang_default_keys",
    "ubuntu16_04_clang_default_packages",
    "ubuntu16_04_clang_default_repos",
)
load("//configs/dependency-tracking:ubuntu1604.bzl", _latest_bazel = "bazel")

licenses(["notice"])  # Apache 2.0

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

bazel_latest_with_rc = "0.25.0"

bazel_rc = "1"

docker_toolchain_autoconfig(
    name = "ubuntu-xenial-autoconfig",
    base = "@io_bazel_rules_docker//tests/docker/toolchain_container:rbe-test-xenial-with-pkgs.tar",
    env = gcc_env(),
    tags = ["manual"],
    test = True,
)

docker_toolchain_autoconfig(
    name = "external-repo-autoconfig",
    base = "@io_bazel_rules_docker//tests/docker/toolchain_container:rbe-test-xenial-with-pkgs.tar",
    config_repos = [
        "local_config_sh",
        "local_config_cc",
    ],
    env = gcc_env(),
    git_repo = "https://github.com/bazelbuild/bazel",
    tags = ["manual"],
    test = True,
)

# To build this target run:
# bazel build //tests/config:this-project-repo-autoconfig --define mount_project=$(realpath .)
# To test it run:
# bazel test //tests/config:this-project-repo-autoconfig_test --define mount_project=$(realpath .)
docker_toolchain_autoconfig(
    name = "this-project-repo-autoconfig",
    base = "@io_bazel_rules_docker//tests/docker/toolchain_container:rbe-test-xenial-with-pkgs.tar",
    config_repos = [
        "local_config_sh",
        "local_config_cc",
    ],
    env = gcc_env(),
    mount_project = "$(mount_project)",
    tags = ["manual"],
    test = True,
)

docker_toolchain_autoconfig(
    name = "ubuntu-xenial-bazel-head-autoconfig",
    base = "@io_bazel_rules_docker//tests/docker/toolchain_container:rbe-test-xenial-with-pkgs.tar",
    env = gcc_env(),
    tags = ["manual"],
    test = True,
    use_bazel_head = True,
)

# Note that we only support Bazel rc versions at 0.10.0 or above.
docker_toolchain_autoconfig(
    name = "ubuntu-xenial-custom-bazel-rc-version-autoconfig",
    base = "@io_bazel_rules_docker//tests/docker/toolchain_container:rbe-test-xenial-with-pkgs.tar",
    bazel_rc_version = bazel_rc,
    bazel_version = bazel_latest_with_rc,
    env = gcc_env(),
    tags = ["manual"],
    test = True,
)

docker_toolchain_autoconfig(
    name = "ubuntu-xenial-build-bazel-src",
    base = "@io_bazel_rules_docker//tests/docker/toolchain_container:rbe-test-no-bazel.tar",
    bazel_version = _latest_bazel,
    build_bazel_src = True,
    env = gcc_env(),
    tags = ["manual"],
    test = True,
)

# For test purpose only. Used by //tests/config:ubuntu16_04_clang_autoconfig_test.
# ubuntu16_04_clang_autoconfig_test compares configs generated by this target and those
# already published in the repo to make sure the current docker_toolchain_autoconfig
# can generate the exact same configs as before.
# Container: marketplace.gcr.io/google/clang-ubuntu@sha256:ab3f65314c94279e415926948f8428428e3b6057003f15197ffeae0b1b5a2386
docker_toolchain_autoconfig(
    name = "ubuntu16_04-clang-1.2-bazel_0.24.0-autoconfig",
    additional_repos = ubuntu16_04_clang_default_repos(),
    base = "@ubuntu16_04-clang-test//image",
    bazel_version = "0.24.0",
    env = clang_env(),
    keys = ubuntu16_04_clang_default_keys(),
    packages = ubuntu16_04_clang_default_packages(),
    tags = ["manual"],
    test = True,
)

sh_test(
    name = "ubuntu16_04_clang_autoconfig_test",
    size = "medium",
    timeout = "long",
    srcs = ["ubuntu16_04_clang_autoconfig_test.sh"],
    data = [
        ":ubuntu16_04-clang-1.2-bazel_0.24.0-autoconfig_outputs.tar",
        "@bazel_toolchains_test//file",
    ],
)
