find_package(PkgConfig QUIET)
pkg_check_modules(PC_HWMALLOC QUIET hwmalloc)

find_path(HWMALLOC_INCLUDE_DIR heap.hpp
    HINTS
    ${HWMALLOC_ROOT}  ENV HWMALLOC_ROOT
    ${HWMALLOC_DIR}   ENV HWMALLOC_DIR
    PATH_SUFFIXES include)

find_library(HWMALLOC_LIBRARY NAMES hwmalloc
    HINTS
    ${HWMALLOC_ROOT} ENV HWMALLOC_ROOT
    ${HWMALLOC_DIR}  ENV HWMALLOC_DIR
    PATH_SUFFIXES lib lib64)

set(HWMALLOC_LIBRARIES    ${HWMALLOC_LIBRARY} CACHE INTERNAL "")
set(HWMALLOC_INCLUDE_DIRS ${HWMALLOC_INCLUDE_DIR} CACHE INTERNAL "")

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(HWMALLOC DEFAULT_MSG HWMALLOC_LIBRARY HWMALLOC_INCLUDE_DIR)

mark_as_advanced(HWMALLOC_ROOT HWMALLOC_LIBRARY HWMALLOC_INCLUDE_DIR)

if(NOT TARGET HWMALLOC::hwmalloc AND HWMALLOC_FOUND)
    add_library(HWMALLOC::hwmalloc SHARED IMPORTED)
    set_target_properties(HWMALLOC::hwmalloc PROPERTIES
        IMPORTED_LOCATION ${HWMALLOC_LIBRARY}
        INTERFACE_INCLUDE_DIRECTORIES ${HWMALLOC_INCLUDE_DIR})
endif()


