Commit 19ca800e authored by jan.koester's avatar jan.koester
Browse files

test

parent 1d95b4ea
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
project(libconfplus)
cmake_minimum_required(VERSION 3.10)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
project(libconfplus)

set(WINDOWS_EXPORT_ALL_SYMBOLS ON)

+91 −41
Original line number Diff line number Diff line
# CMake strategy to find the C library libyaml using ONLY the classic search 
# and creating the modern target 'YAML::YAML'.
# 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.

# --- 1. Classic CMake Search (Manual Fallback) ---
# 1. CONFIG Package Search (e.g., vcpkg, Conan)
# Check for both 'yaml' (common vcpkg) and 'LibYAML' (sometimes provided by system)
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(TARGET yaml::yaml AND NOT TARGET YAML::YAML)
        add_library(YAML::YAML ALIAS yaml::yaml)
        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.
    
    else()
        message(FATAL_ERROR "libyaml found via CONFIG, but no recognizable target (yaml::yaml or YAML::YAML) was created.")
    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()

    # Determine if a manual search is necessary
    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
                     )
                     
# --- 2. Check Results and Set Required Variables ---
if(LIBYAML_INCLUDE_DIR AND LIBYAML_LIBRARY)
    message(STATUS "Found libyaml (C) headers: ${LIBYAML_INCLUDE_DIR}")
    message(STATUS "Found libyaml (C) library: ${LIBYAML_LIBRARY}")
        # 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
    
    # --- 3. EXPORT Modern CMake Target ---
    if(NOT TARGET YAML::YAML)
        # Create an INTERFACE IMPORTED target for modern linking
    # 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) 
        
        # Set the properties for the modern target
        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})
            
        message(STATUS "SUCCESS: Created modern IMPORTED target YAML::YAML.")
        endif()

    # Define YAML_FOUND for use outside this script (if needed by other Find modules)
    set(YAML_FOUND TRUE CACHE BOOL "libyaml C library found." FORCE)

# --- 4. Failure Condition ---
else()
    # Check if the variables are set to notify the user what is missing
    if(NOT LIBYAML_INCLUDE_DIR)
        message(STATUS "libyaml header (yaml.h) NOT found.")
    endif()
    if(NOT LIBYAML_LIBRARY)
        message(STATUS "libyaml library NOT found.")
endif()

    # FATAL_ERROR only if necessary
    message(FATAL_ERROR "libyaml (C) not found. Please set LIBYAML_ROOT or ensure files are in CMAKE_PREFIX_PATH.")
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.")
    
# The final goal in your consuming CMakeLists.txt:
# target_link_libraries(yamlconf PRIVATE YAML::YAML)
 No newline at end of file
    # ... (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()
else
    message(FATAL_ERROR "yaml not found")
endif()
 No newline at end of file