from conans import ConanFile, CMake, tools
import os

STATIC_LIBS = ["nvtt", "squish", "rg_etc1", "nvimage", "bc6h", "posh",
               "bc7", "nvmath", "nvthread", "nvcore"]
SHARED_LIBS = ["nvtt", "nvimage", "nvthread", "nvmath", "nvcore"]

class NvidiatexturetoolsConan(ConanFile):
    name = "nvidia-texture-tools"
    version = "662d223626185f7c6c7e0d822a4796a691acc05a"
    license = "MIT"
    author = "koeleck"
    url = "<Package recipe repository url here, for issues about the package>"
    description = "The NVIDIA Texture Tools is a collection of image processing and texture manipulation tools, designed to be integrated in game tools and asset processing pipelines."
    settings = "os", "compiler", "build_type", "arch"
    source_subfolder = "nvtt"
    no_copy_source = True
    options = {"shared": [True, False],
               "fPIC": [True, False],
               "use_OpenMP": [True, False]
    }
    default_options = "shared=False", "fPIC=True", "use_OpenMP=True"
    generators = "cmake"

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

    def source(self):
        url = "https://github.com/castano/nvidia-texture-tools/archive/{}.zip".format(self.version)
        tools.get(url)
        os.rename('nvidia-texture-tools-{}'.format(self.version), self.source_subfolder)
        tools.replace_in_file(os.path.join(self.source_subfolder, "CMakeLists.txt"), "PROJECT(NV)",
                              '''PROJECT(NV)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()''')

    def build(self):
        cmake = CMake(self)
        cmake.definitions["HAVE_CUDA"] = False
        cmake.definitions["HAVE_OPENMP"] = self.options.use_OpenMP
        cmake.configure(source_folder=self.source_subfolder)
        cmake.build()

    def package(self):
        self.copy("license*", src=self.source_subfolder, ignore_case=True, keep_path=False)
        self.copy("nvtt.h", dst="include/nvtt", src=os.path.join(self.source_subfolder, "src", "nvtt"), keep_path=False)
        self.copy("nvtt_wrapper.h", dst="include/nvtt", src=os.path.join(self.source_subfolder, "src", "nvtt"), keep_path=False)
        if self.options.shared:
            for libname in SHARED_LIBS:
                self.copy("*{}*.dll".format(libname), dst="bin", src=os.path.join(self.build_folder, "bin"), keep_path=False)
                self.copy("*{}*.lib".format(libname), dst="lib", src=os.path.join(self.build_folder, "lib"), keep_path=False)
                self.copy("*{}*.so*".format(libname), dst="lib", src=os.path.join(self.build_folder, "lib"), keep_path=False)
        else:
            for libname in STATIC_LIBS:
                self.copy("*{}*.a".format(libname), dst="lib", src=os.path.join(self.build_folder, "lib"), keep_path=False)
                self.copy("*{}*.lib".format(libname), dst="lib", src=os.path.join(self.build_folder, "lib"), keep_path=False)

    def package_info(self):
        all_libs = tools.collect_libs(self)
        if self.options.shared:
            libs = all_libs
        else:
            libs = []
            for libname in STATIC_LIBS:
                libs += [lib for lib in all_libs if libname in lib]
        self.cpp_info.libs = libs
        if self.settings.os == "Linux":
            self.cpp_info.libs.extend(["dl", "pthread"])

        if self.options.shared:
            self.cpp_info.defines = ["NVTT_SHARED=1"]

[((31, 8, 31, 22), 'conans.tools.get', 'tools.get', ({(31, 18, 31, 21): 'url'}, {}), '(url)', False, 'from conans import ConanFile, CMake, tools\n'), ((39, 16, 39, 27), 'conans.CMake', 'CMake', ({(39, 22, 39, 26): 'self'}, {}), '(self)', False, 'from conans import ConanFile, CMake, tools\n'), ((60, 19, 60, 43), 'conans.tools.collect_libs', 'tools.collect_libs', ({(60, 38, 60, 42): 'self'}, {}), '(self)', False, 'from conans import ConanFile, CMake, tools\n'), ((33, 30, 33, 83), 'os.path.join', 'os.path.join', ({(33, 43, 33, 64): 'self.source_subfolder', (33, 66, 33, 82): '"""CMakeLists.txt"""'}, {}), "(self.source_subfolder, 'CMakeLists.txt')", False, 'import os\n'), ((47, 52, 47, 102), 'os.path.join', 'os.path.join', ({(47, 65, 47, 86): 'self.source_subfolder', (47, 88, 47, 93): '"""src"""', (47, 95, 47, 101): '"""nvtt"""'}, {}), "(self.source_subfolder, 'src', 'nvtt')", False, 'import os\n'), ((48, 60, 48, 110), 'os.path.join', 'os.path.join', ({(48, 73, 48, 94): 'self.source_subfolder', (48, 96, 48, 101): '"""src"""', (48, 103, 48, 109): '"""nvtt"""'}, {}), "(self.source_subfolder, 'src', 'nvtt')", False, 'import os\n'), ((51, 69, 51, 107), 'os.path.join', 'os.path.join', ({(51, 82, 51, 99): 'self.build_folder', (51, 101, 51, 106): '"""bin"""'}, {}), "(self.build_folder, 'bin')", False, 'import os\n'), ((52, 69, 52, 107), 'os.path.join', 'os.path.join', ({(52, 82, 52, 99): 'self.build_folder', (52, 101, 52, 106): '"""lib"""'}, {}), "(self.build_folder, 'lib')", False, 'import os\n'), ((53, 69, 53, 107), 'os.path.join', 'os.path.join', ({(53, 82, 53, 99): 'self.build_folder', (53, 101, 53, 106): '"""lib"""'}, {}), "(self.build_folder, 'lib')", False, 'import os\n'), ((56, 67, 56, 105), 'os.path.join', 'os.path.join', ({(56, 80, 56, 97): 'self.build_folder', (56, 99, 56, 104): '"""lib"""'}, {}), "(self.build_folder, 'lib')", False, 'import os\n'), ((57, 69, 57, 107), 'os.path.join', 'os.path.join', ({(57, 82, 57, 99): 'self.build_folder', (57, 101, 57, 106): '"""lib"""'}, {}), "(self.build_folder, 'lib')", False, 'import os\n')]