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

required_conan_version = ">=2.0.0"

class GTestConan(ConanFile):
    name = "xgoogletest"
    version = "1.17.1"
    description = "Google's C++ test framework with color output support"
    url = "https://github.com/google/googletest"
    license = "BSD-3-Clause"
    author = "Google & xdl"
    topics = ("testing", "unit-testing", "mocking", "google", "gtest", "gmock")
    settings = "os", "compiler", "build_type", "arch"
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
        "build_gmock": [True, False],
        "gtest_force_shared_crt": [True, False],
        "gtest_hide_internal_symbols": [True, False],
    }
    default_options = {
        "shared": False,
        "fPIC": True,
        "build_gmock": True,
        "gtest_force_shared_crt": False,
        "gtest_hide_internal_symbols": False,
    }

    exports_sources = "CMakeLists.txt", "googletest/*", "googlemock/*", "ci/*"

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")

    def layout(self):
        cmake_layout(self)

    def generate(self):
        tc = CMakeToolchain(self)
        tc.variables["BUILD_GMOCK"] = self.options.build_gmock
        tc.variables["INSTALL_GTEST"] = True
        tc.variables["gtest_force_shared_crt"] = self.options.gtest_force_shared_crt
        tc.variables["gtest_hide_internal_symbols"] = self.options.gtest_hide_internal_symbols
        tc.variables["BUILD_SHARED_LIBS"] = self.options.shared
        tc.generate()
        
        deps = CMakeDeps(self)
        deps.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        copy(self, "LICENSE", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
        copy(self, "*.h", dst=os.path.join(self.package_folder, "include"), src=os.path.join(self.source_folder, "googletest", "include"))
        copy(self, "*.h", dst=os.path.join(self.package_folder, "include"), src=os.path.join(self.source_folder, "googlemock", "include"))
        
        copy(self, "*.lib", dst=os.path.join(self.package_folder, "lib"), src=self.build_folder, keep_path=False)
        copy(self, "*.a", dst=os.path.join(self.package_folder, "lib"), src=self.build_folder, keep_path=False)
        copy(self, "*.so*", dst=os.path.join(self.package_folder, "lib"), src=self.build_folder, keep_path=False)
        copy(self, "*.dylib*", dst=os.path.join(self.package_folder, "lib"), src=self.build_folder, keep_path=False)
        copy(self, "*.dll", dst=os.path.join(self.package_folder, "bin"), src=self.build_folder, keep_path=False)

    def package_info(self):
        self.cpp_info.libs = ["gtest", "gtest_main"]
        if self.options.build_gmock:
            self.cpp_info.libs.extend(["gmock", "gmock_main"])
        
        if self.settings.os == "Windows":
            self.cpp_info.system_libs = ["ws2_32"]
