Commit 79cd687a authored by jan.koester's avatar jan.koester
Browse files

test

parent 19ca800e
Loading
Loading
Loading
Loading
+51 −92
Original line number Diff line number Diff line
# CMake strategy to find the C library libyaml using multiple approaches:
# The goal is to set the final target to 'YAML::YAML' in all successful cases.
# FindYAMLCompat.cmake
# Ensures a modern imported target YAML::YAML exists.
# Tries config packages (yaml / LibYAML), then pkg-config, then manual.

# 1. CONFIG Package Search (e.g., vcpkg, Conan)
# Check for both 'yaml' (common vcpkg) and 'LibYAML' (sometimes provided by system)
# 1) Try config packages (vcpkg/Conan/etc.)
set(_yaml_config_found FALSE)
find_package(yaml CONFIG QUIET)
find_package(LibYAML CONFIG QUIET)

# Determine if the CONFIG search succeeded
if(YAML_FOUND OR LibYAML_FOUND)
    message(STATUS "Found libyaml via CONFIG.")
    
    # --- ENSURE UPPERCASE ALIAS ---
    # Most vcpkg/Conan config files define 'yaml::yaml' (lowercase).
    # We must ensure the required 'YAML::YAML' (uppercase) target exists.
    
    # 1. If the lowercase target exists, create the uppercase alias
if(yaml_FOUND)
  if(TARGET yaml::yaml AND NOT TARGET YAML::YAML)
    add_library(YAML::YAML ALIAS yaml::yaml)
        message(STATUS "Created ALIAS target YAML::YAML -> yaml::yaml.")
  endif()
  set(_yaml_config_found TRUE)
endif()

    # 2. Check if the uppercase target already exists (e.g., from LibYAML config)
    elseif(TARGET LibYAML::LibYAML AND NOT TARGET YAML::YAML)
if(NOT _yaml_config_found)
  find_package(LibYAML CONFIG QUIET)
  if(LibYAML_FOUND)
    if(TARGET LibYAML::LibYAML AND NOT TARGET YAML::YAML)
      add_library(YAML::YAML ALIAS LibYAML::LibYAML)
        message(STATUS "Created ALIAS target YAML::YAML -> LibYAML::LibYAML.")
    
    # 3. Handle the case where the CONFIG script already defined YAML::YAML
    elseif(TARGET YAML::YAML)
        # Target already exists, no action needed.
    
    else()
        message(FATAL_ERROR "libyaml found via CONFIG, but no recognizable target (yaml::yaml or YAML::YAML) was created.")
    endif()
    set(_yaml_config_found TRUE)
  endif()
endif()

# Fallback to Manual Search
else() 
    # 2. pkg-config Search (Common on Linux/MSYS)
if(NOT TARGET YAML::YAML)
  # 2) Try pkg-config
  find_package(PkgConfig QUIET)
  if(PkgConfig_FOUND)
    pkg_check_modules(LIBYAML QUIET yaml-0.1)
  endif()

    # Determine if a manual search is necessary
  # 3) Manual fallback if pkg-config failed or incomplete
  if(NOT LIBYAML_FOUND)
        # 3. Classic CMake Search (Manual Fallback)
    find_path(LIBYAML_INCLUDE_DIR yaml.h
      HINTS $ENV{LIBYAML_ROOT} ${CMAKE_PREFIX_PATH}
      PATH_SUFFIXES include
    )
        
        # Note: 'HINTS ... "${CMAKE_PREFIX_PATH}"' is redundant with PATH_SUFFIXES 'lib' and CMAKE_PREFIX_PATH
    find_library(LIBYAML_LIBRARY NAMES yaml libyaml
      HINTS $ENV{LIBYAML_ROOT} ${CMAKE_PREFIX_PATH}
      PATH_SUFFIXES lib
    )
                     
        # 4. Check results from the classic search
        if(NOT (LIBYAML_INCLUDE_DIR AND LIBYAML_LIBRARY))
            message(FATAL_ERROR "libyaml (C) not found. Please set LIBYAML_ROOT or install it.")
        endif()
    if(NOT LIBYAML_INCLUDE_DIR OR NOT LIBYAML_LIBRARY)
      message(FATAL_ERROR "libyaml not found. Set LIBYAML_ROOT or install it (vcpkg: 'libyaml').")
    endif()

    # --- UNIFIED TARGET CREATION (For pkg-config and Classic) ---
    # This block runs only if we successfully found it manually or via pkg-config
    
    # 5. Create the final modern target 'YAML::YAML' (Uppercase)
    if(NOT TARGET YAML::YAML) # Only create if not already handled by CONFIG (unlikely here, but safe)
    add_library(YAML::YAML UNKNOWN IMPORTED)
    set_target_properties(YAML::YAML PROPERTIES
      IMPORTED_LOCATION "${LIBYAML_LIBRARY}"
      INTERFACE_INCLUDE_DIRECTORIES "${LIBYAML_INCLUDE_DIR}"
    )
  else()
    # Use pkg-config result to create a modern target
    add_library(YAML::YAML INTERFACE IMPORTED)
        
        if(LIBYAML_FOUND) # pkg-config results
            target_include_directories(YAML::YAML INTERFACE ${LIBYAML_INCLUDE_DIRS})
            target_link_libraries(YAML::YAML INTERFACE ${LIBYAML_LIBRARIES})
            
        else() # Classic Search results
            target_include_directories(YAML::YAML INTERFACE ${LIBYAML_INCLUDE_DIR})
            target_link_libraries(YAML::YAML INTERFACE ${LIBYAML_LIBRARY})
            
        endif()
    endif()
endif()

# In your custom FindYAML logic, *immediately* after find_package(yaml CONFIG QUIET)

if(YAML_FOUND)
    # Check if the CONFIG file created the lowercase target
    if(TARGET yaml::yaml AND NOT TARGET YAML::YAML)
        # Create the ALIAS that backends/CMakeLists.txt needs
        add_library(YAML::YAML ALIAS yaml::yaml) 
        message(STATUS "SUCCESS: Created ALIAS target YAML::YAML -> yaml::yaml.")
    
    # ... (Other checks for LibYAML::LibYAML, etc.) ...
    
    elseif(NOT TARGET YAML::YAML)
        # If the CONFIG search found the package but didn't create a target we recognize, 
        # the build will fail. Log this for debugging.
        message(WARNING "libyaml found via CONFIG, but failed to create or find target YAML::YAML.")
    set_target_properties(YAML::YAML PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES "${LIBYAML_INCLUDE_DIRS}"
      INTERFACE_LINK_LIBRARIES "${LIBYAML_LIBRARIES}"
    )
  endif()
else
    message(FATAL_ERROR "yaml not found")
endif()