Commit 68efe873 authored by jan.koester's avatar jan.koester
Browse files

test

parent 28e1ac9f
Loading
Loading
Loading
Loading
+57 −14
Original line number Diff line number Diff line
# libyaml (C) finden – mehrere Strategien
# 1) CONFIG-Paket (z.B. vcpkg) -> liefert Ziel yaml::yaml
# CMake strategy to find the C library libyaml using multiple approaches:
# 1. Config/Package Manager (Modern)
# 2. pkg-config (System Tooling)
# 3. Classic CMake Search (Manual)

# The goal is to set the final target to 'yaml::yaml' in all successful cases.

# 1. CONFIG Package Search (e.g., vcpkg, Conan)
# This is the most robust method if a config file is available.
find_package(yaml CONFIG QUIET)

if(yaml_FOUND)
    message(STATUS "Found libyaml via CONFIG: yaml::yaml")
    set(LIBYAML_TARGET yaml::yaml)
    # If found via CONFIG, the target yaml::yaml already exists and is configured.
    
else()
    # 2) pkg-config (linux/msys, evtl. auch Windows mit pkg-config)
    # 2. pkg-config Search (Common on Linux/MSYS)
    find_package(PkgConfig QUIET)
    if(PkgConfig_FOUND)
        # Check for the common pkg-config name 'yaml-0.1'
        pkg_check_modules(LIBYAML QUIET yaml-0.1)
    endif()

    if(LIBYAML_FOUND)
        message(STATUS "Found libyaml via pkg-config: ${LIBYAML_VERSION}")
        add_library(libyaml_pkg INTERFACE)
        target_include_directories(libyaml_pkg INTERFACE ${LIBYAML_INCLUDE_DIRS})
        target_link_libraries(libyaml_pkg INTERFACE ${LIBYAML_LIBRARIES})
        set(LIBYAML_TARGET libyaml_pkg)

    else()
        # 3. Classic CMake Search (Manual Fallback)
        find_path(LIBYAML_INCLUDE_DIR yaml.h
                     HINTS $ENV{LIBYAML_ROOT} ${CMAKE_PREFIX_PATH})
        
        # Search for library files (e.g., yaml.lib, libyaml.a, libyaml.so)
        find_library(LIBYAML_LIBRARY NAMES yaml libyaml
                     HINTS $ENV{LIBYAML_ROOT} ${CMAKE_PREFIX_PATH})
                     
        # 4. Check results from the classic search
        if(LIBYAML_INCLUDE_DIR AND LIBYAML_LIBRARY)
            message(STATUS "Found libyaml via classic search: ${LIBYAML_LIBRARY}")
            add_library(libyaml_manual INTERFACE)
            target_include_directories(libyaml_manual INTERFACE ${LIBYAML_INCLUDE_DIR})
            target_link_libraries(libyaml_manual INTERFACE ${LIBYAML_LIBRARY})
            set(LIBYAML_TARGET libyaml_manual)
        
        else()
            message(FATAL_ERROR "libyaml (C) not found)
            # If all methods fail, stop the configuration process.
            message(FATAL_ERROR "libyaml (C) not found. Please set LIBYAML_ROOT or install it.")
        endif()
    endif()
    
    # --- UNIFIED TARGET CREATION ---
    
    # 5. Create the final modern target 'yaml::yaml' 
    #    This block executes only if either pkg-config or the manual search succeeded.
    if(LIBYAML_FOUND OR (LIBYAML_INCLUDE_DIR AND LIBYAML_LIBRARY))
        
        # Define the target as INTERFACE IMPORTED, as it only transfers link/include information.
        add_library(yaml::yaml INTERFACE IMPORTED) 
        
        if(LIBYAML_FOUND)
            # Apply configuration from pkg-config variables (LIBYAML_INCLUDE_DIRS, etc.)
            target_include_directories(yaml::yaml INTERFACE ${LIBYAML_INCLUDE_DIRS})
            target_link_libraries(yaml::yaml INTERFACE ${LIBYAML_LIBRARIES})
            
        elseif(LIBYAML_INCLUDE_DIR AND LIBYAML_LIBRARY)
            # Apply configuration from classic search variables
            target_include_directories(yaml::yaml INTERFACE ${LIBYAML_INCLUDE_DIR})
            target_link_libraries(yaml::yaml INTERFACE ${LIBYAML_LIBRARY})
            
        endif()
    endif()
endif()

---

## ✅ Final Status Check

```cmake
# Final check to ensure the consumer target can link successfully.
if(TARGET yaml::yaml)
    # The variable YAML_FOUND might not be set in this file, but the target exists.
    message(STATUS "libyaml configuration complete. Use target yaml::yaml to link.")
else()
    # This should never be reached if the
 No newline at end of file