Home | History | Annotate | Download | only in cmake
      1 ###
      2 #
      3 # @copyright (c) 2009-2014 The University of Tennessee and The University
      4 #                          of Tennessee Research Foundation.
      5 #                          All rights reserved.
      6 # @copyright (c) 2012-2014 Inria. All rights reserved.
      7 # @copyright (c) 2012-2014 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
      8 #
      9 ###
     10 #
     11 # - Find HWLOC include dirs and libraries
     12 # Use this module by invoking find_package with the form:
     13 #  find_package(HWLOC
     14 #               [REQUIRED]) # Fail with error if hwloc is not found
     15 #
     16 # This module finds headers and hwloc library.
     17 # Results are reported in variables:
     18 #  HWLOC_FOUND           - True if headers and requested libraries were found
     19 #  HWLOC_INCLUDE_DIRS    - hwloc include directories
     20 #  HWLOC_LIBRARY_DIRS    - Link directories for hwloc libraries
     21 #  HWLOC_LIBRARIES       - hwloc component libraries to be linked
     22 #
     23 # The user can give specific paths where to find the libraries adding cmake
     24 # options at configure (ex: cmake path/to/project -DHWLOC_DIR=path/to/hwloc):
     25 #  HWLOC_DIR             - Where to find the base directory of hwloc
     26 #  HWLOC_INCDIR          - Where to find the header files
     27 #  HWLOC_LIBDIR          - Where to find the library files
     28 # The module can also look for the following environment variables if paths
     29 # are not given as cmake variable: HWLOC_DIR, HWLOC_INCDIR, HWLOC_LIBDIR
     30 
     31 #=============================================================================
     32 # Copyright 2012-2013 Inria
     33 # Copyright 2012-2013 Emmanuel Agullo
     34 # Copyright 2012-2013 Mathieu Faverge
     35 # Copyright 2012      Cedric Castagnede
     36 # Copyright 2013      Florent Pruvost
     37 #
     38 # Distributed under the OSI-approved BSD License (the "License");
     39 # see accompanying file MORSE-Copyright.txt for details.
     40 #
     41 # This software is distributed WITHOUT ANY WARRANTY; without even the
     42 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     43 # See the License for more information.
     44 #=============================================================================
     45 # (To distribute this file outside of Morse, substitute the full
     46 #  License text for the above reference.)
     47 
     48 include(CheckStructHasMember)
     49 include(CheckCSourceCompiles)
     50 
     51 if (NOT HWLOC_FOUND)
     52   set(HWLOC_DIR "" CACHE PATH "Installation directory of HWLOC library")
     53   if (NOT HWLOC_FIND_QUIETLY)
     54     message(STATUS "A cache variable, namely HWLOC_DIR, has been set to specify the install directory of HWLOC")
     55   endif()
     56 endif()
     57 
     58 set(ENV_HWLOC_DIR "$ENV{HWLOC_DIR}")
     59 set(ENV_HWLOC_INCDIR "$ENV{HWLOC_INCDIR}")
     60 set(ENV_HWLOC_LIBDIR "$ENV{HWLOC_LIBDIR}")
     61 set(HWLOC_GIVEN_BY_USER "FALSE")
     62 if ( HWLOC_DIR OR ( HWLOC_INCDIR AND HWLOC_LIBDIR) OR ENV_HWLOC_DIR OR (ENV_HWLOC_INCDIR AND ENV_HWLOC_LIBDIR) )
     63   set(HWLOC_GIVEN_BY_USER "TRUE")
     64 endif()
     65 
     66 # Optionally use pkg-config to detect include/library dirs (if pkg-config is available)
     67 # -------------------------------------------------------------------------------------
     68 include(FindPkgConfig)
     69 find_package(PkgConfig QUIET)
     70 if( PKG_CONFIG_EXECUTABLE AND NOT HWLOC_GIVEN_BY_USER )
     71 
     72   pkg_search_module(HWLOC hwloc)
     73   if (NOT HWLOC_FIND_QUIETLY)
     74     if (HWLOC_FOUND AND HWLOC_LIBRARIES)
     75       message(STATUS "Looking for HWLOC - found using PkgConfig")
     76       #if(NOT HWLOC_INCLUDE_DIRS)
     77       #    message("${Magenta}HWLOC_INCLUDE_DIRS is empty using PkgConfig."
     78       #        "Perhaps the path to hwloc headers is already present in your"
     79       #        "C(PLUS)_INCLUDE_PATH environment variable.${ColourReset}")
     80       #endif()
     81     else()
     82       message(STATUS "${Magenta}Looking for HWLOC - not found using PkgConfig."
     83 	"\n   Perhaps you should add the directory containing hwloc.pc to"
     84 	"\n   the PKG_CONFIG_PATH environment variable.${ColourReset}")
     85     endif()
     86   endif()
     87 
     88 endif( PKG_CONFIG_EXECUTABLE AND NOT HWLOC_GIVEN_BY_USER )
     89 
     90 if( (NOT PKG_CONFIG_EXECUTABLE) OR (PKG_CONFIG_EXECUTABLE AND NOT HWLOC_FOUND) OR (HWLOC_GIVEN_BY_USER) )
     91 
     92   if (NOT HWLOC_FIND_QUIETLY)
     93     message(STATUS "Looking for HWLOC - PkgConfig not used")
     94   endif()
     95 
     96   # Looking for include
     97   # -------------------
     98 
     99   # Add system include paths to search include
    100   # ------------------------------------------
    101   unset(_inc_env)
    102   if(ENV_HWLOC_INCDIR)
    103     list(APPEND _inc_env "${ENV_HWLOC_INCDIR}")
    104   elseif(ENV_HWLOC_DIR)
    105     list(APPEND _inc_env "${ENV_HWLOC_DIR}")
    106     list(APPEND _inc_env "${ENV_HWLOC_DIR}/include")
    107     list(APPEND _inc_env "${ENV_HWLOC_DIR}/include/hwloc")
    108   else()
    109     if(WIN32)
    110       string(REPLACE ":" ";" _inc_env "$ENV{INCLUDE}")
    111     else()
    112       string(REPLACE ":" ";" _path_env "$ENV{INCLUDE}")
    113       list(APPEND _inc_env "${_path_env}")
    114       string(REPLACE ":" ";" _path_env "$ENV{C_INCLUDE_PATH}")
    115       list(APPEND _inc_env "${_path_env}")
    116       string(REPLACE ":" ";" _path_env "$ENV{CPATH}")
    117       list(APPEND _inc_env "${_path_env}")
    118       string(REPLACE ":" ";" _path_env "$ENV{INCLUDE_PATH}")
    119       list(APPEND _inc_env "${_path_env}")
    120     endif()
    121   endif()
    122   list(APPEND _inc_env "${CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES}")
    123   list(APPEND _inc_env "${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}")
    124   list(REMOVE_DUPLICATES _inc_env)
    125 
    126   # set paths where to look for
    127   set(PATH_TO_LOOK_FOR "${_inc_env}")
    128 
    129   # Try to find the hwloc header in the given paths
    130   # -------------------------------------------------
    131   # call cmake macro to find the header path
    132   if(HWLOC_INCDIR)
    133     set(HWLOC_hwloc.h_DIRS "HWLOC_hwloc.h_DIRS-NOTFOUND")
    134     find_path(HWLOC_hwloc.h_DIRS
    135       NAMES hwloc.h
    136       HINTS ${HWLOC_INCDIR})
    137   else()
    138     if(HWLOC_DIR)
    139       set(HWLOC_hwloc.h_DIRS "HWLOC_hwloc.h_DIRS-NOTFOUND")
    140       find_path(HWLOC_hwloc.h_DIRS
    141 	NAMES hwloc.h
    142 	HINTS ${HWLOC_DIR}
    143 	PATH_SUFFIXES "include" "include/hwloc")
    144     else()
    145       set(HWLOC_hwloc.h_DIRS "HWLOC_hwloc.h_DIRS-NOTFOUND")
    146       find_path(HWLOC_hwloc.h_DIRS
    147 	NAMES hwloc.h
    148 	HINTS ${PATH_TO_LOOK_FOR}
    149 	PATH_SUFFIXES "hwloc")
    150     endif()
    151   endif()
    152   mark_as_advanced(HWLOC_hwloc.h_DIRS)
    153 
    154   # Add path to cmake variable
    155   # ------------------------------------
    156   if (HWLOC_hwloc.h_DIRS)
    157     set(HWLOC_INCLUDE_DIRS "${HWLOC_hwloc.h_DIRS}")
    158   else ()
    159     set(HWLOC_INCLUDE_DIRS "HWLOC_INCLUDE_DIRS-NOTFOUND")
    160     if(NOT HWLOC_FIND_QUIETLY)
    161       message(STATUS "Looking for hwloc -- hwloc.h not found")
    162     endif()
    163   endif ()
    164 
    165   if (HWLOC_INCLUDE_DIRS)
    166     list(REMOVE_DUPLICATES HWLOC_INCLUDE_DIRS)
    167   endif ()
    168 
    169 
    170   # Looking for lib
    171   # ---------------
    172 
    173   # Add system library paths to search lib
    174   # --------------------------------------
    175   unset(_lib_env)
    176   if(ENV_HWLOC_LIBDIR)
    177     list(APPEND _lib_env "${ENV_HWLOC_LIBDIR}")
    178   elseif(ENV_HWLOC_DIR)
    179     list(APPEND _lib_env "${ENV_HWLOC_DIR}")
    180     list(APPEND _lib_env "${ENV_HWLOC_DIR}/lib")
    181   else()
    182     if(WIN32)
    183       string(REPLACE ":" ";" _lib_env "$ENV{LIB}")
    184     else()
    185       if(APPLE)
    186 	string(REPLACE ":" ";" _lib_env "$ENV{DYLD_LIBRARY_PATH}")
    187       else()
    188 	string(REPLACE ":" ";" _lib_env "$ENV{LD_LIBRARY_PATH}")
    189       endif()
    190       list(APPEND _lib_env "${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES}")
    191       list(APPEND _lib_env "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
    192     endif()
    193   endif()
    194   list(REMOVE_DUPLICATES _lib_env)
    195 
    196   # set paths where to look for
    197   set(PATH_TO_LOOK_FOR "${_lib_env}")
    198 
    199   # Try to find the hwloc lib in the given paths
    200   # ----------------------------------------------
    201 
    202   # call cmake macro to find the lib path
    203   if(HWLOC_LIBDIR)
    204     set(HWLOC_hwloc_LIBRARY "HWLOC_hwloc_LIBRARY-NOTFOUND")
    205     find_library(HWLOC_hwloc_LIBRARY
    206       NAMES hwloc
    207       HINTS ${HWLOC_LIBDIR})
    208   else()
    209     if(HWLOC_DIR)
    210       set(HWLOC_hwloc_LIBRARY "HWLOC_hwloc_LIBRARY-NOTFOUND")
    211       find_library(HWLOC_hwloc_LIBRARY
    212 	NAMES hwloc
    213 	HINTS ${HWLOC_DIR}
    214 	PATH_SUFFIXES lib lib32 lib64)
    215     else()
    216       set(HWLOC_hwloc_LIBRARY "HWLOC_hwloc_LIBRARY-NOTFOUND")
    217       find_library(HWLOC_hwloc_LIBRARY
    218 	NAMES hwloc
    219 	HINTS ${PATH_TO_LOOK_FOR})
    220     endif()
    221   endif()
    222   mark_as_advanced(HWLOC_hwloc_LIBRARY)
    223 
    224   # If found, add path to cmake variable
    225   # ------------------------------------
    226   if (HWLOC_hwloc_LIBRARY)
    227     get_filename_component(hwloc_lib_path ${HWLOC_hwloc_LIBRARY} PATH)
    228     # set cmake variables (respects naming convention)
    229     set(HWLOC_LIBRARIES    "${HWLOC_hwloc_LIBRARY}")
    230     set(HWLOC_LIBRARY_DIRS "${hwloc_lib_path}")
    231   else ()
    232     set(HWLOC_LIBRARIES    "HWLOC_LIBRARIES-NOTFOUND")
    233     set(HWLOC_LIBRARY_DIRS "HWLOC_LIBRARY_DIRS-NOTFOUND")
    234     if(NOT HWLOC_FIND_QUIETLY)
    235       message(STATUS "Looking for hwloc -- lib hwloc not found")
    236     endif()
    237   endif ()
    238 
    239   if (HWLOC_LIBRARY_DIRS)
    240     list(REMOVE_DUPLICATES HWLOC_LIBRARY_DIRS)
    241   endif ()
    242 
    243   # check a function to validate the find
    244   if(HWLOC_LIBRARIES)
    245 
    246     set(REQUIRED_INCDIRS)
    247     set(REQUIRED_LIBDIRS)
    248     set(REQUIRED_LIBS)
    249 
    250     # HWLOC
    251     if (HWLOC_INCLUDE_DIRS)
    252       set(REQUIRED_INCDIRS "${HWLOC_INCLUDE_DIRS}")
    253     endif()
    254     if (HWLOC_LIBRARY_DIRS)
    255       set(REQUIRED_LIBDIRS "${HWLOC_LIBRARY_DIRS}")
    256     endif()
    257     set(REQUIRED_LIBS "${HWLOC_LIBRARIES}")
    258 
    259     # set required libraries for link
    260     set(CMAKE_REQUIRED_INCLUDES "${REQUIRED_INCDIRS}")
    261     set(CMAKE_REQUIRED_LIBRARIES)
    262     foreach(lib_dir ${REQUIRED_LIBDIRS})
    263       list(APPEND CMAKE_REQUIRED_LIBRARIES "-L${lib_dir}")
    264     endforeach()
    265     list(APPEND CMAKE_REQUIRED_LIBRARIES "${REQUIRED_LIBS}")
    266     string(REGEX REPLACE "^ -" "-" CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
    267 
    268     # test link
    269     unset(HWLOC_WORKS CACHE)
    270     include(CheckFunctionExists)
    271     check_function_exists(hwloc_topology_init HWLOC_WORKS)
    272     mark_as_advanced(HWLOC_WORKS)
    273 
    274     if(NOT HWLOC_WORKS)
    275       if(NOT HWLOC_FIND_QUIETLY)
    276 	message(STATUS "Looking for hwloc : test of hwloc_topology_init with hwloc library fails")
    277 	message(STATUS "CMAKE_REQUIRED_LIBRARIES: ${CMAKE_REQUIRED_LIBRARIES}")
    278 	message(STATUS "CMAKE_REQUIRED_INCLUDES: ${CMAKE_REQUIRED_INCLUDES}")
    279 	message(STATUS "Check in CMakeFiles/CMakeError.log to figure out why it fails")
    280       endif()
    281     endif()
    282     set(CMAKE_REQUIRED_INCLUDES)
    283     set(CMAKE_REQUIRED_FLAGS)
    284     set(CMAKE_REQUIRED_LIBRARIES)
    285   endif(HWLOC_LIBRARIES)
    286 
    287 endif( (NOT PKG_CONFIG_EXECUTABLE) OR (PKG_CONFIG_EXECUTABLE AND NOT HWLOC_FOUND) OR (HWLOC_GIVEN_BY_USER) )
    288 
    289 if (HWLOC_LIBRARIES)
    290   if (HWLOC_LIBRARY_DIRS)
    291     list(GET HWLOC_LIBRARY_DIRS 0 first_lib_path)
    292   else()
    293     list(GET HWLOC_LIBRARIES 0 first_lib)
    294     get_filename_component(first_lib_path "${first_lib}" PATH)
    295   endif()
    296   if (${first_lib_path} MATCHES "/lib(32|64)?$")
    297     string(REGEX REPLACE "/lib(32|64)?$" "" not_cached_dir "${first_lib_path}")
    298     set(HWLOC_DIR_FOUND "${not_cached_dir}" CACHE PATH "Installation directory of HWLOC library" FORCE)
    299   else()
    300     set(HWLOC_DIR_FOUND "${first_lib_path}" CACHE PATH "Installation directory of HWLOC library" FORCE)
    301   endif()
    302 endif()
    303 mark_as_advanced(HWLOC_DIR)
    304 mark_as_advanced(HWLOC_DIR_FOUND)
    305 
    306 # check that HWLOC has been found
    307 # -------------------------------
    308 include(FindPackageHandleStandardArgs)
    309 if (PKG_CONFIG_EXECUTABLE AND HWLOC_FOUND)
    310   find_package_handle_standard_args(HWLOC DEFAULT_MSG
    311     HWLOC_LIBRARIES)
    312 else()
    313   find_package_handle_standard_args(HWLOC DEFAULT_MSG
    314     HWLOC_LIBRARIES
    315     HWLOC_WORKS)
    316 endif()
    317 
    318 if (HWLOC_FOUND)
    319   set(HWLOC_SAVE_CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES})
    320   list(APPEND CMAKE_REQUIRED_INCLUDES ${HWLOC_INCLUDE_DIRS})
    321 
    322   # test headers to guess the version
    323   check_struct_has_member( "struct hwloc_obj" parent hwloc.h HAVE_HWLOC_PARENT_MEMBER )
    324   check_struct_has_member( "struct hwloc_cache_attr_s" size hwloc.h HAVE_HWLOC_CACHE_ATTR )
    325   check_c_source_compiles( "#include <hwloc.h>
    326 	    int main(void) { hwloc_obj_t o; o->type = HWLOC_OBJ_PU; return 0;}" HAVE_HWLOC_OBJ_PU)
    327   include(CheckLibraryExists)
    328   check_library_exists(${HWLOC_LIBRARIES} hwloc_bitmap_free "" HAVE_HWLOC_BITMAP)
    329 
    330   set(CMAKE_REQUIRED_INCLUDES ${HWLOC_SAVE_CMAKE_REQUIRED_INCLUDES})
    331 endif()
    332