# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.

"""Hexagon environment checks for CI usage

These are required by tvm.testing and live here to avoid a circular dependency.
"""

import os

import tvm

ANDROID_SERIAL_NUMBER = "ANDROID_SERIAL_NUMBER"
HEXAGON_TOOLCHAIN = "HEXAGON_TOOLCHAIN"


def _compile_time_check():
    """Return True if compile-time support for Hexagon is present, otherwise
    error string.

    Backs :func:`tvm.testing.env.has_hexagon_toolchain`.
    """
    if tvm.runtime.enabled("llvm") and tvm.target.codegen.llvm_version_major() < 7:
        return "Hexagon requires LLVM 7 or later"

    if "HEXAGON_TOOLCHAIN" not in os.environ:
        return f"Missing environment variable {HEXAGON_TOOLCHAIN}."

    return True


def _run_time_check():
    """Return True if run-time support for Hexagon is present, otherwise
    error string.

    Backs :func:`tvm.testing.env.has_hexagon`.
    """
    if ANDROID_SERIAL_NUMBER not in os.environ:
        return f"Missing environment variable {ANDROID_SERIAL_NUMBER}."

    return True
