Loading cmake/FindYAML.cmake +24 −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. # - Find libyaml # Find the native Yaml headers and libraries. # # YAML_INCLUDE_DIRS - where to find yaml.h # YAML_LIBRARIES - List of libraries when using libyaml # YAML_FOUND - True if libyaml is found. # # 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) find_path(YAML_INCLUDE_DIR yaml.h PATH_SUFFIXES yaml) find_library(YAML_LIBRARY NAMES yaml) # Determine if the CONFIG search succeeded 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 include(FindPackageHandleStandardArgs) find_package_handle_standard_args(YAML DEFAULT_MSG YAML_LIBRARY YAML_INCLUDE_DIR) # --- ENSURE UPPERCASE ALIAS --- # Most vcpkg/Conan config files define 'yaml::yaml' (lowercase). # We must ensure the required 'YAML::YAML' (uppercase) target exists. # Copy the results to the output variables and target. if(YAML_FOUND) set(YAML_LIBRARIES ${YAML_LIBRARY}) set(YAML_INCLUDE_DIRS ${YAML_INCLUDE_DIR}) # 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 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() if(NOT TARGET Yaml::Yaml) add_library(Yaml::Yaml UNKNOWN IMPORTED) set_target_properties(Yaml::Yaml PROPERTIES IMPORTED_LOCATION "${YAML_LIBRARY}" INTERFACE_INCLUDE_DIRECTORIES "${YAML_INCLUDE_DIR}") 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 mark_as_advanced(YAML_INCLUDE_DIR YAML_LIBRARY) No newline at end of file Loading
cmake/FindYAML.cmake +24 −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. # - Find libyaml # Find the native Yaml headers and libraries. # # YAML_INCLUDE_DIRS - where to find yaml.h # YAML_LIBRARIES - List of libraries when using libyaml # YAML_FOUND - True if libyaml is found. # # 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) find_path(YAML_INCLUDE_DIR yaml.h PATH_SUFFIXES yaml) find_library(YAML_LIBRARY NAMES yaml) # Determine if the CONFIG search succeeded 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 include(FindPackageHandleStandardArgs) find_package_handle_standard_args(YAML DEFAULT_MSG YAML_LIBRARY YAML_INCLUDE_DIR) # --- ENSURE UPPERCASE ALIAS --- # Most vcpkg/Conan config files define 'yaml::yaml' (lowercase). # We must ensure the required 'YAML::YAML' (uppercase) target exists. # Copy the results to the output variables and target. if(YAML_FOUND) set(YAML_LIBRARIES ${YAML_LIBRARY}) set(YAML_INCLUDE_DIRS ${YAML_INCLUDE_DIR}) # 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 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() if(NOT TARGET Yaml::Yaml) add_library(Yaml::Yaml UNKNOWN IMPORTED) set_target_properties(Yaml::Yaml PROPERTIES IMPORTED_LOCATION "${YAML_LIBRARY}" INTERFACE_INCLUDE_DIRECTORIES "${YAML_INCLUDE_DIR}") 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 mark_as_advanced(YAML_INCLUDE_DIR YAML_LIBRARY) No newline at end of file