Commit 35cc09bc authored by jan.koester's avatar jan.koester
Browse files

test

parent 4c7709bf
Loading
Loading
Loading
Loading
+92 −24
Original line number Original line Diff line number Diff line
# - Find libyaml
# CMake strategy to find the C library libyaml using multiple approaches:
# Find the native Yaml headers and libraries.
# The goal is to set the final target to 'YAML::YAML' in all successful cases.
#

#  YAML_INCLUDE_DIRS - where to find yaml.h
# 1. CONFIG Package Search (e.g., vcpkg, Conan)
#  YAML_LIBRARIES    - List of libraries when using libyaml
# Check for both 'yaml' (common vcpkg) and 'LibYAML' (sometimes provided by system)
#  YAML_FOUND        - True if libyaml is found.
find_package(yaml CONFIG QUIET)
#
find_package(LibYAML CONFIG QUIET)


find_path(YAML_INCLUDE_DIR yaml.h PATH_SUFFIXES yaml)
# Determine if the CONFIG search succeeded
find_library(YAML_LIBRARY NAMES yaml)
if(YAML_FOUND OR LibYAML_FOUND)

    message(STATUS "Found libyaml via CONFIG.")
# handle the QUIET and REQUIRED arguments and
    
# set YAML_FOUND to TRUE if all variables are non-zero
    # --- ENSURE UPPERCASE ALIAS ---
include(FindPackageHandleStandardArgs)
    # Most vcpkg/Conan config files define 'yaml::yaml' (lowercase).
find_package_handle_standard_args(YAML DEFAULT_MSG YAML_LIBRARY YAML_INCLUDE_DIR)
    # We must ensure the required 'YAML::YAML' (uppercase) target exists.

    
# Copy the results to the output variables and target.
    # 1. If the lowercase target exists, create the uppercase alias
if(YAML_FOUND)
    if(TARGET yaml::yaml AND NOT TARGET YAML::YAML)
  set(YAML_LIBRARIES ${YAML_LIBRARY})
        add_library(YAML::YAML ALIAS yaml::yaml)
  set(YAML_INCLUDE_DIRS ${YAML_INCLUDE_DIR})
        message(STATUS "Created ALIAS target YAML::YAML -> yaml::yaml.")
    
    # 2. Check if the uppercase target already exists (e.g., from LibYAML config)
    elseif(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.
    
    
  if(NOT TARGET Yaml::Yaml)
    else()
    add_library(Yaml::Yaml UNKNOWN IMPORTED)
        message(FATAL_ERROR "libyaml found via CONFIG, but no recognizable target (yaml::yaml or YAML::YAML) was created.")
    set_target_properties(Yaml::Yaml PROPERTIES
      IMPORTED_LOCATION "${YAML_LIBRARY}"
      INTERFACE_INCLUDE_DIRECTORIES "${YAML_INCLUDE_DIR}")
    endif()
    endif()

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


mark_as_advanced(YAML_INCLUDE_DIR YAML_LIBRARY)
    # Determine if a manual search is necessary
 No newline at end of file
    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 include/yaml
                     )
        
        # 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()
    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 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.")
    endif()
endif()
 No newline at end of file