
from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import copy, load
import os
import re

class DarraEtherCATConan(ConanFile):
    """DarraEtherCAT C SDK - EtherCAT 主站控制库"""

    name = "darra-ethercat"
    version = "2.4.0"
    license = "Proprietary"
    author = "Hangzhou Darra Technology Co., Ltd."
    url = "https://www.darra.com"
    description = "DarraEtherCAT C SDK - EtherCAT 主站控制库 (头文件 + 动态加载)"
    topics = ("ethercat", "industrial", "automation", "plc", "motion-control")

    settings = "os", "compiler", "build_type", "arch"
    options = {
        "header_only": [True, False],
        "build_examples": [True, False],
    }
    default_options = {
        "header_only": True,
        "build_examples": False,
    }

    exports_sources = (
        "CMakeLists.txt",
        "cmake/*",
        "include/*",
        "src/*",
        "examples/*",
        "LICENSE",
    )

    def _extract_version_from_header(self):
        """从头文件中提取版本号"""
        types_h = os.path.join(self.source_folder, "include", "ethercat_types.h")
        if os.path.exists(types_h):
            content = load(self, types_h)
            major = re.search(r"#define\s+VERSION_MAJOR\s+(\d+)", content)
            minor = re.search(r"#define\s+VERSION_MINOR\s+(\d+)", content)
            patch = re.search(r"#define\s+VERSION_PATCH\s+(\d+)", content)
            if major and minor and patch:
                return f"{major.group(1)}.{minor.group(1)}.{patch.group(1)}"
        return None

    def set_version(self):
        """尝试从头文件自动获取版本号"""
        ver = self._extract_version_from_header()
        if ver:
            self.version = ver

    def config_options(self):
        """根据平台调整选项"""
        pass

    def layout(self):
        """设置构建目录布局"""
        cmake_layout(self)

    def generate(self):
        """生成 CMake 工具链"""
        tc = CMakeToolchain(self)
        tc.variables["HEADER_ONLY"] = self.options.header_only
        tc.variables["BUILD_EXAMPLES"] = self.options.build_examples
        tc.generate()

    def build(self):
        """构建项目"""
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        """打包文件"""
        cmake = CMake(self)
        cmake.install()

        copy(self, "LICENSE", src=self.source_folder,
             dst=os.path.join(self.package_folder, "licenses"))

    def package_info(self):
        """声明包信息 (供消费者使用)"""
        self.cpp_info.set_property("cmake_file_name", "DarraEtherCAT")
        self.cpp_info.set_property("cmake_target_name", "darra::ethercat")

        self.cpp_info.includedirs = ["include/darra-ethercat"]

        if self.options.header_only:

            self.cpp_info.defines = ["DYNAMIC_LOAD"]
            self.cpp_info.bindirs = []
            self.cpp_info.libdirs = []
        else:

            self.cpp_info.libs = ["Darra.Core"]

        if self.settings.os == "Windows":
            self.cpp_info.system_libs = ["kernel32"]
        elif self.settings.os == "Linux":
            self.cpp_info.system_libs = ["dl", "pthread"]

    def package_id(self):
        """头文件模式下, 包 ID 与设置无关"""
        if self.options.header_only:
            self.info.clear()
