Improve cmake

This MR improves the current CMake scripts in the following manner:

  • Dependency management is now handled more consistently: system dependencies are found using find_package(), whereas source-only dependencies are found using include_from_source(). This new function integrates a dependency provided its source code is available. If it's not, it will try to download it from its git repository if it was defined.
  • All external dependencies are now searched for in the top-level CMakeLists.txt.
  • More consistent use of targets (we are closer to 100% modern CMake).
  • Adds the gkfs_feature_summary() to allow printing a summary of all GekkoFS configuration options and their values.
  • Moves all option definitions to CMake/gfks-options.cmake, so that the top-level CMakeLists.txt is cleaner. It also defines the gkfs_define_option() and gkfs_define_variable() functions to integrate the definition of options/variables with the automated reporting provided by gkfs_feature_summary().

It also fixes #249 (closed).

Closes #211 (closed)

Edited by Marc Vef

Merge request reports

Loading
+23 −8
Changes for CMake/FindAGIOS.cmake: 23 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -26,21 +26,36 @@
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

find_path(AGIOS_INCLUDE_DIR
find_path(
  AGIOS_INCLUDE_DIR
  NAMES agios.h
  PATH_SUFFIXES include
)

find_library(AGIOS_LIBRARY
    NAMES agios
)
find_library(AGIOS_LIBRARY NAMES agios)

set(AGIOS_INCLUDE_DIRS ${AGIOS_INCLUDE_DIR})
set(AGIOS_LIBRARIES ${AGIOS_LIBRARY})

mark_as_advanced(AGIOS_LIBRARY AGIOS_INCLUDE_DIR)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(AGIOS DEFAULT_MSG AGIOS_LIBRARIES AGIOS_INCLUDE_DIRS)
find_package_handle_standard_args(
  AGIOS
  FOUND_VAR AGIOS_FOUND
  REQUIRED_VARS AGIOS_LIBRARY AGIOS_INCLUDE_DIR
)

mark_as_advanced(
    AGIOS_LIBRARY
    AGIOS_INCLUDE_DIR
if(AGIOS_FOUND)
  set(AGIOS_INCLUDE_DIRS ${AGIOS_INCLUDE_DIR})
  set(AGIOS_LIBRARIES ${AGIOS_LIBRARY})
  if(NOT TARGET AGIOS::AGIOS)
    add_library(AGIOS::AGIOS UNKNOWN IMPORTED)
    set_target_properties(
      AGIOS::AGIOS
      PROPERTIES IMPORTED_LOCATION "${AGIOS_LIBRARY}"
      INTERFACE_INCLUDE_DIRECTORIES "${AGIOS_INCLUDE_DIR}"
    )
  endif()
endif( )
+24 −19
Changes for CMake/FindAIO.cmake: 24 added lines, 19 removed lines.
Original line number Diff line number Diff line

################################################################################
# Copyright 2018-2022, Barcelona Supercomputing Center (BSC), Spain            #
# Copyright 2015-2022, Johannes Gutenberg Universitaet Mainz, Germany          #
@@ -25,29 +26,33 @@
#                                                                              #
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################
find_path(
  AIO_INCLUDE_DIR
  NAMES aio.h
  PATH_SUFFIXES include
)

# - Find Lz4
# Find the lz4 compression library and includes
#
# LZ4_FOUND - True if lz4 found.
# LZ4_LIBRARIES - List of libraries when using lz4.
# LZ4_INCLUDE_DIR - where to find lz4.h, etc.
find_library(AIO_LIBRARY NAMES rt)

find_path(LZ4_INCLUDE_DIR
    NAMES lz4.h
)
mark_as_advanced(AIO_INCLUDE_DIR AIO_LIBRARY)

find_library(LZ4_LIBRARY
    NAMES lz4
find_package_handle_standard_args(
  AIO
  FOUND_VAR AIO_FOUND
  REQUIRED_VARS AIO_INCLUDE_DIR AIO_LIBRARY
)

set(LZ4_LIBRARIES ${LZ4_LIBRARY} )
set(LZ4_INCLUDE_DIRS ${LZ4_INCLUDE_DIR} )

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(LZ4 DEFAULT_MSG LZ4_LIBRARY LZ4_INCLUDE_DIR)
if(AIO_FOUND AND NOT TARGET AIO::AIO)
  add_library(AIO::AIO UNKNOWN IMPORTED)
  if(AIO_INCLUDE_DIR)
    set_target_properties(
      AIO::AIO PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${AIO_INCLUDE_DIR}"
    )
  endif()

mark_as_advanced(
    LZ4_LIBRARY
    LZ4_INCLUDE_DIR
  set_target_properties(
    AIO::AIO PROPERTIES IMPORTED_LOCATION "${AIO_LIBRARY}"
    IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  )
endif()
+213 −0
Changes for CMake/FindArgobots.cmake: 213 added lines, 0 removed lines.
Original line number Diff line number Diff line
################################################################################
# Copyright 2018-2022, Barcelona Supercomputing Center (BSC), Spain            #
# Copyright 2015-2022, Johannes Gutenberg Universitaet Mainz, Germany          #
#                                                                              #
# This software was partially supported by the                                 #
# EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).    #
#                                                                              #
# This software was partially supported by the                                 #
# ADA-FS project under the SPPEXA project funded by the DFG.                   #
#                                                                              #
# This file is part of GekkoFS.                                                #
#                                                                              #
# GekkoFS is free software: you can redistribute it and/or modify              #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# GekkoFS is distributed in the hope that it will be useful,                   #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with GekkoFS.  If not, see <https://www.gnu.org/licenses/>.            #
#                                                                              #
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

#[=======================================================================[.rst:
FindArgobots
---------

Find Argobots include dirs and libraries.

Use this module by invoking find_package with the form::

  find_package(Argobots
    [version] [EXACT]     # Minimum or EXACT version e.g. 0.6.2
    [REQUIRED]            # Fail with error if Argobots is not found
    )

Imported Targets
^^^^^^^^^^^^^^^^

This module provides the following imported targets, if found:

``Argobots::Argobots``
  The Argobots library

Result Variables
^^^^^^^^^^^^^^^^

This will define the following variables:

``Argobots_FOUND``
  True if the system has the Argobots library.
``Argobots_VERSION``
  The version of the Argobots library which was found.
``Argobots_INCLUDE_DIRS``
  Include directories needed to use Argobots.
``Argobots_LIBRARIES``
  Libraries needed to link to Argobots.

Cache Variables
^^^^^^^^^^^^^^^

The following cache variables may also be set:

``ARGOBOTS_INCLUDE_DIR``
  The directory containing ``abt.h``.
``ARGOBOTS_LIBRARY``
  The path to the Argobots library.

#]=======================================================================]

function(_get_pkgconfig_paths target_var)
  set(_lib_dirs)
  if(NOT DEFINED CMAKE_SYSTEM_NAME
     OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
         AND NOT CMAKE_CROSSCOMPILING)
  )
    if(EXISTS "/etc/debian_version") # is this a debian system ?
      if(CMAKE_LIBRARY_ARCHITECTURE)
        list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
      endif()
    else()
      # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
      get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
      if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
        list(APPEND _lib_dirs "lib32/pkgconfig")
      endif()
      get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
      if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
        list(APPEND _lib_dirs "lib64/pkgconfig")
      endif()
      get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
      if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
        list(APPEND _lib_dirs "libx32/pkgconfig")
      endif()
    endif()
  endif()
  if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
    list(APPEND _lib_dirs "libdata/pkgconfig")
  endif()
  list(APPEND _lib_dirs "lib/pkgconfig")
  list(APPEND _lib_dirs "share/pkgconfig")

  set(_extra_paths)
  list(APPEND _extra_paths ${CMAKE_PREFIX_PATH})
  list(APPEND _extra_paths ${CMAKE_FRAMEWORK_PATH})
  list(APPEND _extra_paths ${CMAKE_APPBUNDLE_PATH})

  # Check if directories exist and eventually append them to the
  # pkgconfig path list
  foreach(_prefix_dir ${_extra_paths})
    foreach(_lib_dir ${_lib_dirs})
      if(EXISTS "${_prefix_dir}/${_lib_dir}")
        list(APPEND _pkgconfig_paths "${_prefix_dir}/${_lib_dir}")
        list(REMOVE_DUPLICATES _pkgconfig_paths)
      endif()
    endforeach()
  endforeach()

  set("${target_var}"
      ${_pkgconfig_paths}
      PARENT_SCOPE
  )
endfunction()

# prevent repeating work if the main CMakeLists.txt already called
# find_package(PkgConfig)
if(NOT PKG_CONFIG_FOUND)
  find_package(PkgConfig)
endif()

if(PKG_CONFIG_FOUND)
  pkg_check_modules(PC_ARGOBOTS QUIET argobots)

  find_path(
    ARGOBOTS_INCLUDE_DIR
    NAMES abt.h
    PATHS ${PC_ARGOBOTS_INCLUDE_DIRS}
    PATH_SUFFIXES include
  )

  find_library(
    ARGOBOTS_LIBRARY
    NAMES abt
    PATHS ${PC_ARGOBOTS_LIBRARY_DIRS}
  )

  set(Argobots_VERSION ${PC_ARGOBOTS_VERSION})
else()
  find_path(
    ARGOBOTS_INCLUDE_DIR
    NAMES abt.h
    PATH_SUFFIXES include
  )

  find_library(ARGOBOTS_LIBRARY NAMES abt)

  # even if pkg-config is not available, but Argobots still installs a .pc file
  # that we can use to retrieve library information from. Try to find it at all
  # possible pkgconfig subfolders (depending on the system).
  _get_pkgconfig_paths(_pkgconfig_paths)

  find_file(_argobots_pc_file argobots.pc PATHS "${_pkgconfig_paths}")

  if(NOT _argobots_pc_file)
    message(
      FATAL_ERROR
        "ERROR: Could not find 'argobots.pc' file. Unable to determine library version"
    )
  endif()

  file(STRINGS "${_argobots_pc_file}" _argobots_pc_file_contents
       REGEX "Version: "
  )

  if("${_argobots_pc_file_contents}" MATCHES
     "Version: ([0-9]+\\.[0-9]+\\.[0-9])"
  )
    set(Argobots_VERSION ${CMAKE_MATCH_1})
  else()
    message(FATAL_ERROR "ERROR: Failed to determine library version")
  endif()

  unset(_pkg_config_paths)
  unset(_argobots_pc_file_contents)
endif()

mark_as_advanced(ARGOBOTS_INCLUDE_DIR ARGOBOTS_LIBRARY)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
  Argobots
  FOUND_VAR Argobots_FOUND
  REQUIRED_VARS ARGOBOTS_LIBRARY ARGOBOTS_INCLUDE_DIR
  VERSION_VAR Argobots_VERSION
)

if(Argobots_FOUND)
  set(Argobots_INCLUDE_DIRS ${ARGOBOTS_INCLUDE_DIR})
  set(Argobots_LIBRARIES ${ARGOBOTS_LIBRARY})
  if(NOT TARGET Argobots::Argobots)
    add_library(Argobots::Argobots UNKNOWN IMPORTED)
    set_target_properties(
      Argobots::Argobots
      PROPERTIES IMPORTED_LOCATION "${ARGOBOTS_LIBRARY}"
                 INTERFACE_INCLUDE_DIRECTORIES "${ARGOBOTS_INCLUDE_DIR}"
    )
  endif()
endif()
+171 −28
Changes for CMake/FindMargo.cmake: 171 added lines, 28 removed lines.
Original line number Diff line number Diff line
@@ -26,41 +26,184 @@
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

# Try to find Margo headers and library.
#
# Usage of this module as follows:
#
#     find_package(Margo)
#
# Variables used by this module, they can change the default behaviour and need
# to be set before calling find_package:
#
#  MARGO_ROOT_DIR          Set this variable to the root installation of
#                            Margo if the module has problems finding the
#                            proper installation path.
#
# Variables defined by this module:
#
#  MARGO_FOUND               System has Margo library/headers.
#  MARGO_LIBRARIES           The Margo library.
#  MARGO_INCLUDE_DIRS        The location of Margo headers.


find_path(MARGO_INCLUDE_DIR
#[=======================================================================[.rst:
FindMargo
---------

Find Margo include dirs and libraries.

Use this module by invoking find_package with the form::

  find_package(Margo
    [version] [EXACT]     # Minimum or EXACT version e.g. 0.6.2
    [REQUIRED]            # Fail with error if Margo is not found
    )

Imported Targets
^^^^^^^^^^^^^^^^

This module provides the following imported targets, if found:

``Margo::Margo``
  The Margo library

Result Variables
^^^^^^^^^^^^^^^^

This will define the following variables:

``Margo_FOUND``
  True if the system has the Margo library.
``Margo_VERSION``
  The version of the Margo library which was found.
``Margo_INCLUDE_DIRS``
  Include directories needed to use Margo.
``Margo_LIBRARIES``
  Libraries needed to link to Margo.

Cache Variables
^^^^^^^^^^^^^^^

The following cache variables may also be set:

``MARGO_INCLUDE_DIR``
  The directory containing ``margo.h``.
``MARGO_LIBRARY``
  The path to the Margo library.

#]=======================================================================]

function(_get_pkgconfig_paths target_var)
  set(_lib_dirs)
  if(NOT DEFINED CMAKE_SYSTEM_NAME
     OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
         AND NOT CMAKE_CROSSCOMPILING)
  )
    if(EXISTS "/etc/debian_version") # is this a debian system ?
      if(CMAKE_LIBRARY_ARCHITECTURE)
        list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
      endif()
    else()
      # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
      get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
      if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
        list(APPEND _lib_dirs "lib32/pkgconfig")
      endif()
      get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
      if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
        list(APPEND _lib_dirs "lib64/pkgconfig")
      endif()
      get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
      if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
        list(APPEND _lib_dirs "libx32/pkgconfig")
      endif()
    endif()
  endif()
  if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
    list(APPEND _lib_dirs "libdata/pkgconfig")
  endif()
  list(APPEND _lib_dirs "lib/pkgconfig")
  list(APPEND _lib_dirs "share/pkgconfig")

  set(_extra_paths)
  list(APPEND _extra_paths ${CMAKE_PREFIX_PATH})
  list(APPEND _extra_paths ${CMAKE_FRAMEWORK_PATH})
  list(APPEND _extra_paths ${CMAKE_APPBUNDLE_PATH})

  # Check if directories exist and eventually append them to the
  # pkgconfig path list
  foreach(_prefix_dir ${_extra_paths})
    foreach(_lib_dir ${_lib_dirs})
      if(EXISTS "${_prefix_dir}/${_lib_dir}")
        list(APPEND _pkgconfig_paths "${_prefix_dir}/${_lib_dir}")
        list(REMOVE_DUPLICATES _pkgconfig_paths)
      endif()
    endforeach()
  endforeach()

  set("${target_var}"
      ${_pkgconfig_paths}
      PARENT_SCOPE
  )
endfunction()

# prevent repeating work if the main CMakeLists.txt already called
# find_package(PkgConfig)
if(NOT PKG_CONFIG_FOUND)
  find_package(PkgConfig)
endif()

if(PKG_CONFIG_FOUND)
  pkg_check_modules(PC_MARGO QUIET margo)

  find_path(
    MARGO_INCLUDE_DIR
    NAMES margo.h
    PATHS ${PC_MARGO_INCLUDE_DIRS}
    PATH_SUFFIXES include
  )

find_library(MARGO_LIBRARY
  find_library(
    MARGO_LIBRARY
    NAMES margo
    PATHS ${PC_MARGO_LIBRARY_DIRS}
  )

  set(Margo_VERSION ${PC_MARGO_VERSION})
else()
  find_path(
    MARGO_INCLUDE_DIR
    NAMES margo.h
    PATH_SUFFIXES include
  )

  find_library(MARGO_LIBRARY NAMES margo)

  # even if pkg-config is not available, but Margo still installs a .pc file
  # that we can use to retrieve library information from. Try to find it at all
  # possible pkgconfig subfolders (depending on the system).
  _get_pkgconfig_paths(_pkgconfig_paths)

  find_file(_margo_pc_file margo.pc PATHS "${_pkgconfig_paths}")

  if(NOT _margo_pc_file)
    message(
      FATAL_ERROR
        "ERROR: Could not find 'margo.pc' file. Unable to determine library version"
    )
  endif()

  file(STRINGS "${_margo_pc_file}" _margo_pc_file_contents REGEX "Version: ")

set(MARGO_INCLUDE_DIRS ${MARGO_INCLUDE_DIR})
set(MARGO_LIBRARIES ${MARGO_LIBRARY})
  if("${_margo_pc_file_contents}" MATCHES "Version: ([0-9]+\\.[0-9]+\\.[0-9])")
    set(Margo_VERSION ${CMAKE_MATCH_1})
  else()
    message(FATAL_ERROR "ERROR: Failed to determine library version")
  endif()

  unset(_pkg_config_paths)
  unset(_margo_pc_file_contents)
endif()

mark_as_advanced(MARGO_INCLUDE_DIR MARGO_LIBRARY)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Margo DEFAULT_MSG MARGO_LIBRARY MARGO_INCLUDE_DIR)
find_package_handle_standard_args(
  Margo
  FOUND_VAR Margo_FOUND
  REQUIRED_VARS MARGO_LIBRARY MARGO_INCLUDE_DIR
  VERSION_VAR Margo_VERSION
)

mark_as_advanced(
        MARGO_LIBRARY
        MARGO_INCLUDE_DIR
if(Margo_FOUND)
  set(Margo_INCLUDE_DIRS ${MARGO_INCLUDE_DIR})
  set(Margo_LIBRARIES ${MARGO_LIBRARY})
  if(NOT TARGET Margo::Margo)
    add_library(Margo::Margo UNKNOWN IMPORTED)
    set_target_properties(
      Margo::Margo
      PROPERTIES IMPORTED_LOCATION "${MARGO_LIBRARY}"
                 INTERFACE_INCLUDE_DIRECTORIES "${MARGO_INCLUDE_DIR}"
    )
  endif()
endif()
+280 −0
Changes for CMake/FindMercury.cmake: 280 added lines, 0 removed lines.
Original line number Diff line number Diff line
################################################################################
# Copyright 2018-2022, Barcelona Supercomputing Center (BSC), Spain            #
# Copyright 2015-2022, Johannes Gutenberg Universitaet Mainz, Germany          #
#                                                                              #
# This software was partially supported by the                                 #
# EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).    #
#                                                                              #
# This software was partially supported by the                                 #
# ADA-FS project under the SPPEXA project funded by the DFG.                   #
#                                                                              #
# This file is part of GekkoFS.                                                #
#                                                                              #
# GekkoFS is free software: you can redistribute it and/or modify              #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# GekkoFS is distributed in the hope that it will be useful,                   #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with GekkoFS.  If not, see <https://www.gnu.org/licenses/>.            #
#                                                                              #
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

#[=======================================================================[.rst:
FindMercury
---------

Find Mercury include dirs and libraries.

Use this module by invoking find_package with the form::

  find_package(Mercury
    [version] [EXACT]     # Minimum or EXACT version e.g. 0.6.2
    [REQUIRED]            # Fail with error if Mercury is not found
    )

Some variables one may set to control this module are::

  Mercury_DEBUG            - Set to ON to enable debug output from FindMercury.

Imported Targets
^^^^^^^^^^^^^^^^

This module provides the following imported targets, if found:

``Mercury::Mercury``
  The Mercury library

Result Variables
^^^^^^^^^^^^^^^^

This will define the following variables:

``Mercury_FOUND``
  True if the system has the Mercury library.
``Mercury_VERSION``
  The version of the Mercury library which was found.
``Mercury_INCLUDE_DIRS``
  Include directories needed to use Mercury.
``Mercury_LIBRARIES``
  Libraries needed to link to Mercury.

Cache Variables
^^^^^^^^^^^^^^^

The following cache variables may also be set:

``MERCURY_INCLUDE_DIR``
  The directory containing ``mercury.h``.
``MERCURY_LIBRARY``
  The path to the Mercury library.

#]=======================================================================]

#
# Print debug text if Mercury_DEBUG is set.
# Call example:
# _mercury_DEBUG_PRINT("debug message")
#
function(_mercury_DEBUG_PRINT text)
  if(Mercury_DEBUG)
    message(
      STATUS
        "[ ${CMAKE_CURRENT_LIST_FILE}:${CMAKE_CURRENT_FUNCTION_LIST_LINE} ] ${text}"
    )
  endif()
endfunction()

macro(_mercury_find_component _component_name)

  _mercury_debug_print("Searching for Mercury component: ${_component}")

  string(TOUPPER ${_component} _upper_component)

  # find component header
  find_path(
    ${_upper_component}_INCLUDE_DIR ${_component}
    NAMES ${_component}.h
    PATH_SUFFIXES include
    PATHS ${PC_MERCURY_INCLUDE_DIRS}
  )

  # find component library (release version)
  find_library(
    ${_upper_component}_LIBRARY_RELEASE
    NAMES ${_component}
    PATHS ${PC_MERCURY_LIBRARY_DIRS}
  )

  # find component library (debug version)
  find_library(
    ${_upper_component}_LIBRARY_DEBUG
    NAMES ${_component}_debug
    PATHS ${PC_MERCURY_LIBRARY_DIRS}
  )

  # initialize ${_upper_component}_LIBRARY (e.g. NA_LIBRARY)
  # with the appropriate library for this build configuration
  select_library_configurations(${_upper_component})

  _mercury_debug_print(
    "${_upper_component}_INCLUDE_DIR: ${${_upper_component}_INCLUDE_DIR}"
  )
  _mercury_debug_print(
    "${_upper_component}_LIBRARY: ${${_upper_component}_LIBRARY}"
  )

  # define an imported target for the component
  if (NOT TARGET Mercury::${_component})
    add_library(Mercury::${_component} UNKNOWN IMPORTED)
  endif ()

  if(${_upper_component}_LIBRARY_RELEASE)
    set_property(TARGET Mercury::${_component} APPEND PROPERTY
      IMPORTED_CONFIGURATIONS RELEASE)
    set_target_properties(Mercury::${_component} PROPERTIES
      IMPORTED_LOCATION_RELEASE "${${_upper_component}_LIBRARY_RELEASE}"
    )
  endif()

  if(${_upper_component}_LIBRARY_DEBUG)
    set_property(TARGET Mercury::${_component} APPEND PROPERTY
      IMPORTED_CONFIGURATIONS DEBUG)
    set_target_properties(Mercury::${_component} PROPERTIES
      IMPORTED_LOCATION_DEBUG "${${_upper_component}_LIBRARY_DEBUG}"
      IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
    )
  endif()

  set_target_properties(
    Mercury::${_component}
    PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${MERCURY_INCLUDE_DIR}"
  )

  get_target_property(_configs Mercury::${_component} IMPORTED_CONFIGURATIONS)
  foreach(_config ${_configs})
    _mercury_DEBUG_PRINT("---> ${_config}")

  endforeach()

  set_property(TARGET Mercury::${_component} APPEND PROPERTY
    INTERFACE_LINK_LIBRARIES )


  mark_as_advanced(${_upper_component}_INCLUDE_DIR ${_upper_component}_LIBRARY)

endmacro()

###############################################################################
# Find Mercury headers and library
###############################################################################

include(SelectLibraryConfigurations)

set(_mercury_components na mchecksum mercury_util mercury_hl)

# prevent repeating work if the main CMakeLists.txt already called
# find_package(PkgConfig)
if(NOT PKG_CONFIG_FOUND)
  find_package(PkgConfig REQUIRED)
endif()

if(PKG_CONFIG_FOUND)
  pkg_check_modules(PC_MERCURY QUIET mercury)

  # find mercury header
  find_path(
    MERCURY_INCLUDE_DIR
    NAMES mercury.h
    PATHS ${PC_MERCURY_INCLUDE_DIRS}
    PATH_SUFFIXES include
  )
  # find mercury library (release version)
  find_library(
    MERCURY_LIBRARY_RELEASE
    NAMES mercury
    PATHS ${PC_MERCURY_LIBRARY_DIRS}
  )

  # find mercury library (debug version)
  find_library(
    MERCURY_LIBRARY_DEBUG
    NAMES mercury_debug
    PATHS ${PC_MERCURY_LIBRARY_DIRS}
  )

  # initialize MERCURY_LIBRARY with the appropriate library for this build
  # configuration
  select_library_configurations(MERCURY)

  # Mercury_VERSION needs to be set to the appropriate value for the call
  # to find_package_handle_standard_args() below to work as expected
  set(Mercury_VERSION ${PC_MERCURY_VERSION})
endif()

mark_as_advanced(MERCURY_INCLUDE_DIR MERCURY_LIBRARY)


include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
  Mercury
  FOUND_VAR Mercury_FOUND
  REQUIRED_VARS MERCURY_LIBRARY MERCURY_INCLUDE_DIR
  VERSION_VAR Mercury_VERSION
)

if(Mercury_FOUND)
  set(Mercury_INCLUDE_DIRS ${MERCURY_INCLUDE_DIR})
  set(Mercury_LIBRARIES ${MERCURY_LIBRARY})

  if(NOT TARGET Mercury::Mercury)
    add_library(Mercury::Mercury UNKNOWN IMPORTED)
  endif()

  if(MERCURY_LIBRARY_RELEASE)
    set_property(
      TARGET Mercury::Mercury
      APPEND
      PROPERTY IMPORTED_CONFIGURATIONS RELEASE
    )
    set_target_properties(
      Mercury::Mercury PROPERTIES IMPORTED_LOCATION_RELEASE
                                  "${MERCURY_LIBRARY_RELEASE}"
    )
  endif()

  if(MERCURY_LIBRARY_DEBUG)
    set_property(
      TARGET Mercury::Mercury
      APPEND
      PROPERTY IMPORTED_CONFIGURATIONS DEBUG
    )
    set_target_properties(
      Mercury::Mercury PROPERTIES IMPORTED_LOCATION_DEBUG
                                  "${MERCURY_LIBRARY_DEBUG}"
    )
  endif()

  set_target_properties(
    Mercury::Mercury
    PROPERTIES INTERFACE_COMPILE_OPTIONS "${PC_MERCURY_CFLAGS_OTHER}"
               INTERFACE_INCLUDE_DIRECTORIES "${MERCURY_INCLUDE_DIR}"

  )

  ###############################################################################
  # Find headers and libraries for additional Mercury components
  ###############################################################################
  foreach(_component ${_mercury_components})
    _mercury_find_component(${_component})

    set_target_properties(Mercury::Mercury PROPERTIES
      INTERFACE_LINK_LIBRARIES Mercury::${_component})
  endforeach()
endif()
Loading
Loading