if(NOT SPIRV_TOOLS_CORE_LIBRARY)
  find_path(SPIRV_TOOLS_CORE_INCLUDE_DIR "spirv-tools/libspirv.hpp")
  find_library(SPIRV_TOOLS_CORE_LIBRARY_DEBUG
    NAMES
      SPIRV-Tools-d  # Windows
      SPIRV-Tools
    PATH_SUFFIXES
      lib
  )
  find_library(SPIRV_TOOLS_CORE_LIBRARY_RELEASE
    NAMES
      SPIRV-Tools
    PATH_SUFFIXES
      lib
  )
  set(SPIRV_TOOLS_CORE_LIBRARY
    debug ${SPIRV_TOOLS_CORE_LIBRARY_DEBUG}
    optimized ${SPIRV_TOOLS_CORE_LIBRARY_RELEASE}
  )
endif()

FIND_PACKAGE_HANDLE_STANDARD_ARGS(SPIRV_TOOLS_CORE REQUIRED_VARS
  SPIRV_TOOLS_CORE_LIBRARY SPIRV_TOOLS_CORE_INCLUDE_DIR)

mark_as_advanced(SPIRV_TOOLS_CORE_INCLUDE_DIRS)
mark_as_advanced(SPIRV_TOOLS_CORE_LIBRARIES)

if(SPIRV_TOOLS_CORE_FOUND)
  set(SPIRV_TOOLS_CORE_INCLUDE_DIRS ${SPIRV_TOOLS_CORE_INCLUDE_DIR})
  set(SPIRV_TOOLS_CORE_LIBRARIES ${SPIRV_TOOLS_CORE_LIBRARY})

  if(NOT TARGET SpirVTools::core)
    add_library(SpirVTools::core UNKNOWN IMPORTED)
    set_target_properties(SpirVTools::core PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES "${SPIRV_TOOLS_CORE_INCLUDE_DIRS}")

    set_property(TARGET SpirVTools::core APPEND PROPERTY
      IMPORTED_CONFIGURATIONS RELEASE)
    set_property(TARGET SpirVTools::core APPEND PROPERTY
      IMPORTED_CONFIGURATIONS DEBUG)

    if(SPIRV_TOOLS_CORE_LIBRARY_DEBUG AND SPIRV_TOOLS_CORE_LIBRARY_RELEASE)
      set_target_properties(SpirVTools::core PROPERTIES
        IMPORTED_LOCATION_DEBUG "${SPIRV_TOOLS_CORE_LIBRARY_DEBUG}"
        IMPORTED_LOCATION_RELEASE "${SPIRV_TOOLS_CORE_LIBRARY_RELEASE}"
      )
    else()
      set_target_properties(SpirVTools::core PROPERTIES
        IMPORTED_LOCATION "${SPIRV_TOOLS_CORE_LIBRARY_RELEASE}"
      )
    endif()
  endif()
endif()

################################################################################

if(NOT SPIRV_TOOLS_OPTIMIZER_LIBRARY)
  find_path(SPIRV_TOOLS_OPTIMIZER_INCLUDE_DIR "spirv-tools/optimizer.hpp")
  find_library(SPIRV_TOOLS_OPTIMIZER_LIBRARY_DEBUG
    NAMES
      SPIRV-Tools-opt-d  # Windows
      SPIRV-Tools-opt
    PATH_SUFFIXES
      lib
  )
  find_library(SPIRV_TOOLS_OPTIMIZER_LIBRARY_RELEASE
    NAMES
      SPIRV-Tools-opt
    PATH_SUFFIXES
      lib
  )
  set(SPIRV_TOOLS_OPTIMIZER_LIBRARY
    debug ${SPIRV_TOOLS_OPTIMIZER_LIBRARY_DEBUG}
    optimized ${SPIRV_TOOLS_OPTIMIZER_LIBRARY_RELEASE}
  )
endif()

FIND_PACKAGE_HANDLE_STANDARD_ARGS(SPIRV_TOOLS_OPTIMIZER REQUIRED_VARS
  SPIRV_TOOLS_OPTIMIZER_LIBRARY SPIRV_TOOLS_OPTIMIZER_INCLUDE_DIR)

mark_as_advanced(SPIRV_TOOLS_OPTIMIZER_INCLUDE_DIRS)
mark_as_advanced(SPIRV_TOOLS_OPTIMIZER_LIBRARIES)

if(SPIRV_TOOLS_OPTIMIZER_FOUND)
  set(SPIRV_TOOLS_OPTIMIZER_INCLUDE_DIRS ${SPIRV_TOOLS_OPTIMIZER_INCLUDE_DIR})
  set(SPIRV_TOOLS_OPTIMIZER_LIBRARIES ${SPIRV_TOOLS_OPTIMIZER_LIBRARY})

  if(NOT TARGET SpirVTools::optimizer)
    add_library(SpirVTools::optimizer UNKNOWN IMPORTED)
    set_target_properties(SpirVTools::optimizer PROPERTIES
      INTERFACE_INCLUDE_DIRECTORIES "${SPIRV_TOOLS_OPTIMIZER_INCLUDE_DIRS}")

    set_property(TARGET SpirVTools::optimizer APPEND PROPERTY
      IMPORTED_CONFIGURATIONS RELEASE)
    set_property(TARGET SpirVTools::optimizer APPEND PROPERTY
      IMPORTED_CONFIGURATIONS DEBUG)

    if(SPIRV_TOOLS_OPTIMIZER_LIBRARY_DEBUG AND SPIRV_TOOLS_OPTIMIZER_LIBRARY_RELEASE)
      set_target_properties(SpirVTools::optimizer PROPERTIES
        IMPORTED_LOCATION_DEBUG "${SPIRV_TOOLS_OPTIMIZER_LIBRARY_DEBUG}"
        IMPORTED_LOCATION_RELEASE "${SPIRV_TOOLS_OPTIMIZER_LIBRARY_RELEASE}"
      )
    else()
      set_target_properties(SpirVTools::optimizer PROPERTIES
        IMPORTED_LOCATION "${SPIRV_TOOLS_OPTIMIZER_LIBRARY_RELEASE}"
      )
    endif()
  endif()
endif()
