find_package(PkgConfig REQUIRED QUIET)

set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:${HWLOC_LIB_DIR}/pkgconfig")

if(HWLOC_FIND_REQUIRED)
  set(_HWLOC_OPTS "REQUIRED")
endif()
if(HWLOC_FIND_QUIETLY)
  set(_HWLOC_OPTS "QUIET")
endif()
if(HWLOC_FIND_REQUIRED AND HWLOC_FIND_QUIETLY)
  set(_HWLOC_OPTS "REQUIRED QUIET")
endif()

pkg_check_modules(HWLOC ${_HWLOC_OPTS} IMPORTED_TARGET hwloc)

if(HWLOC_FOUND)
  set(HWLOC_INCLUDE_DIRS "${HWLOC_STATIC_INCLUDE_DIRS};${HWLOC_INCLUDEDIR}")
  set(HWLOC_LIBRARIES "${HWLOC_STATIC_LDFLAGS};${HWLOC_STATIC_LIBRARIES}")
endif(HWLOC_FOUND)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(HWLOC DEFAULT_MSG HWLOC_LIBRARIES HWLOC_INCLUDE_DIRS)
mark_as_advanced(HWLOC_LIBRARIES HWLOC_INCLUDE_DIRS)
