2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2019, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
37 # @copyright (c) 2009-2014 The University of Tennessee and The University
38 # of Tennessee Research Foundation.
39 # All rights reserved.
40 # @copyright (c) 2012-2014 Inria. All rights reserved.
41 # @copyright (c) 2012-2014 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
45 # - Find HWLOC include dirs and libraries
46 # Use this module by invoking find_package with the form:
48 # [REQUIRED]) # Fail with error if hwloc is not found
50 # This module finds headers and hwloc library.
51 # Results are reported in variables:
52 # HWLOC_FOUND - True if headers and requested libraries were found
53 # HWLOC_INCLUDE_DIRS - hwloc include directories
54 # HWLOC_LIBRARY_DIRS - Link directories for hwloc libraries
55 # HWLOC_LIBRARIES - hwloc component libraries to be linked
57 # The user can give specific paths where to find the libraries adding cmake
58 # options at configure (ex: cmake path/to/project -DHWLOC_DIR=path/to/hwloc):
59 # HWLOC_DIR - Where to find the base directory of hwloc
60 # HWLOC_INCDIR - Where to find the header files
61 # HWLOC_LIBDIR - Where to find the library files
62 # The module can also look for the following environment variables if paths
63 # are not given as cmake variable: HWLOC_DIR, HWLOC_INCDIR, HWLOC_LIBDIR
65 #=============================================================================
66 # Copyright 2012-2013 Inria
67 # Copyright 2012-2013 Emmanuel Agullo
68 # Copyright 2012-2013 Mathieu Faverge
69 # Copyright 2012 Cedric Castagnede
70 # Copyright 2013 Florent Pruvost
72 # Distributed under the OSI-approved BSD License (the "License");
73 # see accompanying file MORSE-Copyright.txt for details.
75 # This software is distributed WITHOUT ANY WARRANTY; without even the
76 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
77 # See the License for more information.
78 #=============================================================================
79 # (To distribute this file outside of Morse, substitute the full
80 # License text for the above reference.)
82 include(CheckStructHasMember)
83 include(CheckCSourceCompiles)
86 set(HWLOC_DIR "" CACHE PATH "Installation directory of HWLOC library")
89 set(ENV_HWLOC_DIR "$ENV{HWLOC_DIR}")
90 set(ENV_HWLOC_INCDIR "$ENV{HWLOC_INCDIR}")
91 set(ENV_HWLOC_LIBDIR "$ENV{HWLOC_LIBDIR}")
92 set(HWLOC_GIVEN_BY_USER "FALSE")
93 if ( HWLOC_DIR OR ( HWLOC_INCDIR AND HWLOC_LIBDIR) OR ENV_HWLOC_DIR OR (ENV_HWLOC_INCDIR AND ENV_HWLOC_LIBDIR) )
94 set(HWLOC_GIVEN_BY_USER "TRUE")
97 if (NOT HWLOC_FIND_QUIETLY)
98 message(STATUS "Looking for HWLOC")
101 # Looking for include
102 # -------------------
104 # Add system include paths to search include
105 # ------------------------------------------
108 list(APPEND _inc_env "${ENV_HWLOC_INCDIR}")
109 elseif(ENV_HWLOC_DIR)
110 list(APPEND _inc_env "${ENV_HWLOC_DIR}")
111 list(APPEND _inc_env "${ENV_HWLOC_DIR}/include")
112 list(APPEND _inc_env "${ENV_HWLOC_DIR}/include/hwloc")
115 string(REPLACE ":" ";" _inc_env "$ENV{INCLUDE}")
117 string(REPLACE ":" ";" _path_env "$ENV{INCLUDE}")
118 list(APPEND _inc_env "${_path_env}")
119 string(REPLACE ":" ";" _path_env "$ENV{C_INCLUDE_PATH}")
120 list(APPEND _inc_env "${_path_env}")
121 string(REPLACE ":" ";" _path_env "$ENV{CPATH}")
122 list(APPEND _inc_env "${_path_env}")
123 string(REPLACE ":" ";" _path_env "$ENV{INCLUDE_PATH}")
124 list(APPEND _inc_env "${_path_env}")
127 list(APPEND _inc_env "${CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES}")
128 list(APPEND _inc_env "${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES}")
129 list(REMOVE_DUPLICATES _inc_env)
131 # set paths where to look for
132 set(PATH_TO_LOOK_FOR "${_inc_env}")
134 # Try to find the hwloc header in the given paths
135 # -------------------------------------------------
136 # call cmake macro to find the header path
138 set(HWLOC_hwloc.h_DIRS "HWLOC_hwloc.h_DIRS-NOTFOUND")
139 find_path(HWLOC_hwloc.h_DIRS
141 HINTS ${HWLOC_INCDIR})
144 set(HWLOC_hwloc.h_DIRS "HWLOC_hwloc.h_DIRS-NOTFOUND")
145 find_path(HWLOC_hwloc.h_DIRS
148 PATH_SUFFIXES "include" "include/hwloc")
150 set(HWLOC_hwloc.h_DIRS "HWLOC_hwloc.h_DIRS-NOTFOUND")
151 find_path(HWLOC_hwloc.h_DIRS
153 HINTS ${PATH_TO_LOOK_FOR}
154 PATH_SUFFIXES "hwloc")
157 mark_as_advanced(HWLOC_hwloc.h_DIRS)
159 # Add path to cmake variable
160 # ------------------------------------
161 if (HWLOC_hwloc.h_DIRS)
162 set(HWLOC_INCLUDE_DIRS "${HWLOC_hwloc.h_DIRS}")
164 set(HWLOC_INCLUDE_DIRS "HWLOC_INCLUDE_DIRS-NOTFOUND")
165 if(NOT HWLOC_FIND_QUIETLY)
166 message(STATUS "Looking for hwloc -- hwloc.h not found")
170 if (HWLOC_INCLUDE_DIRS)
171 list(REMOVE_DUPLICATES HWLOC_INCLUDE_DIRS)
178 # Add system library paths to search lib
179 # --------------------------------------
182 list(APPEND _lib_env "${ENV_HWLOC_LIBDIR}")
183 elseif(ENV_HWLOC_DIR)
184 list(APPEND _lib_env "${ENV_HWLOC_DIR}")
185 list(APPEND _lib_env "${ENV_HWLOC_DIR}/lib")
188 string(REPLACE ":" ";" _lib_env "$ENV{LIB}")
191 string(REPLACE ":" ";" _lib_env "$ENV{DYLD_LIBRARY_PATH}")
193 string(REPLACE ":" ";" _lib_env "$ENV{LD_LIBRARY_PATH}")
195 list(APPEND _lib_env "${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES}")
196 list(APPEND _lib_env "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
199 list(REMOVE_DUPLICATES _lib_env)
201 # set paths where to look for
202 set(PATH_TO_LOOK_FOR "${_lib_env}")
204 # Try to find the hwloc lib in the given paths
205 # ----------------------------------------------
207 # call cmake macro to find the lib path
209 set(HWLOC_hwloc_LIBRARY "HWLOC_hwloc_LIBRARY-NOTFOUND")
210 find_library(HWLOC_hwloc_LIBRARY
212 HINTS ${HWLOC_LIBDIR})
215 set(HWLOC_hwloc_LIBRARY "HWLOC_hwloc_LIBRARY-NOTFOUND")
216 find_library(HWLOC_hwloc_LIBRARY
219 PATH_SUFFIXES lib lib32 lib64)
221 set(HWLOC_hwloc_LIBRARY "HWLOC_hwloc_LIBRARY-NOTFOUND")
222 find_library(HWLOC_hwloc_LIBRARY
224 HINTS ${PATH_TO_LOOK_FOR})
227 mark_as_advanced(HWLOC_hwloc_LIBRARY)
229 # If found, add path to cmake variable
230 # ------------------------------------
231 if (HWLOC_hwloc_LIBRARY)
232 get_filename_component(hwloc_lib_path ${HWLOC_hwloc_LIBRARY} PATH)
233 # set cmake variables (respects naming convention)
234 set(HWLOC_LIBRARIES "${HWLOC_hwloc_LIBRARY}")
235 set(HWLOC_LIBRARY_DIRS "${hwloc_lib_path}")
237 set(HWLOC_LIBRARIES "HWLOC_LIBRARIES-NOTFOUND")
238 set(HWLOC_LIBRARY_DIRS "HWLOC_LIBRARY_DIRS-NOTFOUND")
239 if(NOT HWLOC_FIND_QUIETLY)
240 message(STATUS "Looking for hwloc -- lib hwloc not found")
244 if (HWLOC_LIBRARY_DIRS)
245 list(REMOVE_DUPLICATES HWLOC_LIBRARY_DIRS)
248 # check a function to validate the find
251 set(REQUIRED_INCDIRS)
252 set(REQUIRED_LIBDIRS)
256 if (HWLOC_INCLUDE_DIRS)
257 set(REQUIRED_INCDIRS "${HWLOC_INCLUDE_DIRS}")
259 if (HWLOC_LIBRARY_DIRS)
260 set(REQUIRED_LIBDIRS "${HWLOC_LIBRARY_DIRS}")
262 set(REQUIRED_LIBS "${HWLOC_LIBRARIES}")
264 # set required libraries for link
265 set(CMAKE_REQUIRED_INCLUDES "${REQUIRED_INCDIRS}")
266 set(CMAKE_REQUIRED_LIBRARIES)
267 foreach(lib_dir ${REQUIRED_LIBDIRS})
268 list(APPEND CMAKE_REQUIRED_LIBRARIES "-L${lib_dir}")
270 list(APPEND CMAKE_REQUIRED_LIBRARIES "${REQUIRED_LIBS}")
271 string(REGEX REPLACE "^ -" "-" CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
274 include(CheckFunctionExists)
275 check_function_exists(hwloc_topology_init HWLOC_WORKS)
276 mark_as_advanced(HWLOC_WORKS)
279 if(NOT HWLOC_FIND_QUIETLY)
280 message(STATUS "Looking for hwloc : test of hwloc_topology_init with hwloc library fails")
281 message(STATUS "CMAKE_REQUIRED_LIBRARIES: ${CMAKE_REQUIRED_LIBRARIES}")
282 message(STATUS "CMAKE_REQUIRED_INCLUDES: ${CMAKE_REQUIRED_INCLUDES}")
283 message(STATUS "Check in CMakeFiles/CMakeError.log to figure out why it fails")
286 set(CMAKE_REQUIRED_INCLUDES)
287 set(CMAKE_REQUIRED_FLAGS)
288 set(CMAKE_REQUIRED_LIBRARIES)
289 endif(HWLOC_LIBRARIES)
292 if (HWLOC_LIBRARY_DIRS)
293 list(GET HWLOC_LIBRARY_DIRS 0 first_lib_path)
295 list(GET HWLOC_LIBRARIES 0 first_lib)
296 get_filename_component(first_lib_path "${first_lib}" PATH)
298 if (${first_lib_path} MATCHES "/lib(32|64)?$")
299 string(REGEX REPLACE "/lib(32|64)?$" "" not_cached_dir "${first_lib_path}")
300 set(HWLOC_DIR_FOUND "${not_cached_dir}" CACHE PATH "Installation directory of HWLOC library" FORCE)
302 set(HWLOC_DIR_FOUND "${first_lib_path}" CACHE PATH "Installation directory of HWLOC library" FORCE)
305 mark_as_advanced(HWLOC_DIR)
306 mark_as_advanced(HWLOC_DIR_FOUND)
308 # Function for converting hex version numbers from HWLOC_API_VERSION if necessary
309 function(HEX2DEC str res)
310 string(LENGTH "${str}" len)
312 if("${str}" MATCHES "[0-9]")
313 set(${res} "${str}" PARENT_SCOPE)
314 elseif( "${str}" MATCHES "[aA]")
315 set(${res} 10 PARENT_SCOPE)
316 elseif( "${str}" MATCHES "[bB]")
317 set(${res} 11 PARENT_SCOPE)
318 elseif( "${str}" MATCHES "[cC]")
319 set(${res} 12 PARENT_SCOPE)
320 elseif( "${str}" MATCHES "[dD]")
321 set(${res} 13 PARENT_SCOPE)
322 elseif( "${str}" MATCHES "[eE]")
323 set(${res} 14 PARENT_SCOPE)
324 elseif( "${str}" MATCHES "[fF]")
325 set(${res} 15 PARENT_SCOPE)
330 string(SUBSTRING "${str}" 0 1 str1)
331 string(SUBSTRING "${str}" 1 -1 str2)
332 hex2dec(${str1} res1)
333 hex2dec(${str2} res2)
334 math(EXPR val "16 * ${res1} + ${res2}")
335 set(${res} "${val}" PARENT_SCOPE)
339 if(HWLOC_INCLUDE_DIRS)
340 # If we are not cross-compiling we try to use the hwloc-info program
341 if(NOT CMAKE_CROSSCOMPILING)
342 find_program(HWLOC_INFO "hwloc-info"
343 HINTS "${HWLOC_DIR}" ENV HWLOC_DIR
346 mark_as_advanced(HWLOC_INFO)
349 execute_process(COMMAND ${HWLOC_INFO} "--version"
350 RESULT_VARIABLE HWLOC_INFO_RES
351 OUTPUT_VARIABLE HWLOC_INFO_OUT
352 ERROR_VARIABLE HWLOC_INFO_ERR)
355 message(STATUS "Error executing hwloc-info: ${HWLOC_INFO_ERR}")
357 string(REGEX MATCH "[0-9]+.*[0-9]+" HWLOC_INFO_OUT "${HWLOC_INFO_OUT}")
358 set(HWLOC_VERSION ${HWLOC_INFO_OUT} CACHE STRING "HWLOC library version")
362 if (NOT HWLOC_FIND_QUIETLY)
363 message(STATUS "hwloc version: ${HWLOC_VERSION}")
366 # Parse header if cross-compiling, or if hwloc-info was not found
367 if(NOT HWLOC_VERSION)
368 # HWLOC is never installed as a framework on OS X, so this should always work.
369 file(READ "${HWLOC_INCLUDE_DIRS}/hwloc.h"
370 HEADER_CONTENTS LIMIT 16384)
371 string(REGEX REPLACE ".*#define HWLOC_API_VERSION (0[xX][0-9a-fA-F]+).*" "\\1"
372 HWLOC_API_VERSION "${HEADER_CONTENTS}")
373 string(SUBSTRING "${HWLOC_API_VERSION}" 4 2 HEX_MAJOR)
374 string(SUBSTRING "${HWLOC_API_VERSION}" 6 2 HEX_MINOR)
375 string(SUBSTRING "${HWLOC_API_VERSION}" 8 2 HEX_PATCH)
376 hex2dec(${HEX_MAJOR} DEC_MAJOR)
377 hex2dec(${HEX_MINOR} DEC_MINOR)
378 hex2dec(${HEX_PATCH} DEC_PATCH)
379 set(HWLOC_VERSION "${DEC_MAJOR}.${DEC_MINOR}.${DEC_PATCH}" CACHE STRING "HWLOC library version")
383 include(FindPackageHandleStandardArgs)
384 find_package_handle_standard_args(HWLOC
385 REQUIRED_VARS HWLOC_LIBRARIES HWLOC_INCLUDE_DIRS
386 VERSION_VAR HWLOC_VERSION)
388 mark_as_advanced(HWLOC_INCLUDE_DIRS HWLOC_LIBRARIES HWLOC_VERSION)