from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain, cmake_layout
from conan.tools.files import copy, get, rmdir
import os

required_conan_version = ">=1.60.0 <2.0 || >=2.0.5"


class xxhashRecipe(ConanFile):
    name = "xxhash"
    package_type = "library"
    user = "aurora"

    # Optional metadata
    homepage = "https://xxhash.com/"
    author = "Daniil Markevich <d.markevich@omp.ru>"
    url = "https://gitverse.ru/auroraos/mirror_xxHash"
    license = "BSD License"
    description = "xxHash - Extremely fast hash algorithm"
    topics = ("xxhash", "hash")

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {
        "shared": [True, False]
    }
    default_options = {
        "shared": True
    }

    def layout(self):
        cmake_layout(self)

    def source(self):
        get(self, **self.conan_data["sources"][self.version], strip_root=True)

    def generate(self):
        deps = CMakeDeps(self)
        deps.generate()
        tc = CMakeToolchain(self)
        tc.cache_variables["BUILD_SHARED_LIBS"] = self.options.shared
        tc.variables["XXHASH_BUILD_XXHSUM"] = False
        tc.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure(build_script_folder=os.path.join(self.source_folder, "cmake_unofficial"))
        cmake.build()

    def package(self):
        copy(self, "LICENSE", src=self.source_folder,
             dst=os.path.join(self.package_folder, "licenses"))
        cmake = CMake(self)
        cmake.install()
        rmdir(self, os.path.join(self.package_folder, "lib/cmake"))

    def package_info(self):
        self.cpp_info.libs = ["xxhash"]
