Fix CUDA clang-tidy complaints
[alexxy/gromacs.git] / cmake / FindHWLOC.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2019,2020, 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.
8 #
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.
13 #
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.
18 #
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.
23 #
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.
31 #
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.
34
35 ###
36 #
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.
42 #
43 ###
44 #
45 # - Find HWLOC include dirs and libraries
46 # Use this module by invoking find_package with the form:
47 #  find_package(HWLOC
48 #               [REQUIRED]) # Fail with error if hwloc is not found
49 #
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
56 #
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
64
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
71 #
72 # Distributed under the OSI-approved BSD License (the "License");
73 # see accompanying file MORSE-Copyright.txt for details.
74 #
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.)
81
82 include(CheckStructHasMember)
83 include(CheckCSourceCompiles)
84
85 if (NOT HWLOC_FOUND)
86   set(HWLOC_DIR "" CACHE PATH "Installation directory of HWLOC library")
87 endif()
88
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")
95 endif()
96
97 if (NOT HWLOC_FIND_QUIETLY)
98     message(STATUS "Looking for HWLOC")
99 endif()
100
101 # Looking for include
102 # -------------------
103
104 # Add system include paths to search include
105 # ------------------------------------------
106 unset(_inc_env)
107 if(ENV_HWLOC_INCDIR)
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")
113 else()
114     if(WIN32)
115         string(REPLACE ":" ";" _inc_env "$ENV{INCLUDE}")
116     else()
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}")
125     endif()
126 endif()
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)
130
131 # set paths where to look for
132 set(PATH_TO_LOOK_FOR "${_inc_env}")
133
134 # Try to find the hwloc header in the given paths
135 # -------------------------------------------------
136 # call cmake macro to find the header path
137 if(HWLOC_INCDIR)
138     set(HWLOC_hwloc.h_DIRS "HWLOC_hwloc.h_DIRS-NOTFOUND")
139     find_path(HWLOC_hwloc.h_DIRS
140         NAMES hwloc.h
141         HINTS ${HWLOC_INCDIR})
142 else()
143     if(HWLOC_DIR)
144         set(HWLOC_hwloc.h_DIRS "HWLOC_hwloc.h_DIRS-NOTFOUND")
145         find_path(HWLOC_hwloc.h_DIRS
146             NAMES hwloc.h
147             HINTS ${HWLOC_DIR}
148             PATH_SUFFIXES "include" "include/hwloc")
149     else()
150         set(HWLOC_hwloc.h_DIRS "HWLOC_hwloc.h_DIRS-NOTFOUND")
151         find_path(HWLOC_hwloc.h_DIRS
152             NAMES hwloc.h
153             HINTS ${PATH_TO_LOOK_FOR}
154             PATH_SUFFIXES "hwloc")
155     endif()
156 endif()
157 mark_as_advanced(HWLOC_hwloc.h_DIRS)
158
159 # Add path to cmake variable
160 # ------------------------------------
161 if (HWLOC_hwloc.h_DIRS)
162     set(HWLOC_INCLUDE_DIRS "${HWLOC_hwloc.h_DIRS}")
163 else ()
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")
167     endif()
168 endif ()
169
170 if (HWLOC_INCLUDE_DIRS)
171     list(REMOVE_DUPLICATES HWLOC_INCLUDE_DIRS)
172 endif ()
173
174
175 # Looking for lib
176 # ---------------
177
178 # Add system library paths to search lib
179 # --------------------------------------
180 unset(_lib_env)
181 if(ENV_HWLOC_LIBDIR)
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")
186 else()
187     if(WIN32)
188         string(REPLACE ":" ";" _lib_env "$ENV{LIB}")
189     else()
190         if(APPLE)
191             string(REPLACE ":" ";" _lib_env "$ENV{DYLD_LIBRARY_PATH}")
192         else()
193             string(REPLACE ":" ";" _lib_env "$ENV{LD_LIBRARY_PATH}")
194         endif()
195         list(APPEND _lib_env "${CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES}")
196         list(APPEND _lib_env "${CMAKE_C_IMPLICIT_LINK_DIRECTORIES}")
197     endif()
198 endif()
199 list(REMOVE_DUPLICATES _lib_env)
200
201 # set paths where to look for
202 set(PATH_TO_LOOK_FOR "${_lib_env}")
203
204 # Try to find the hwloc lib in the given paths
205 # ----------------------------------------------
206
207 # call cmake macro to find the lib path
208 if(HWLOC_LIBDIR)
209     set(HWLOC_hwloc_LIBRARY "HWLOC_hwloc_LIBRARY-NOTFOUND")
210     find_library(HWLOC_hwloc_LIBRARY
211         NAMES hwloc
212         HINTS ${HWLOC_LIBDIR})
213 else()
214     if(HWLOC_DIR)
215         set(HWLOC_hwloc_LIBRARY "HWLOC_hwloc_LIBRARY-NOTFOUND")
216         find_library(HWLOC_hwloc_LIBRARY
217             NAMES hwloc
218             HINTS ${HWLOC_DIR}
219             PATH_SUFFIXES lib lib32 lib64)
220     else()
221         set(HWLOC_hwloc_LIBRARY "HWLOC_hwloc_LIBRARY-NOTFOUND")
222         find_library(HWLOC_hwloc_LIBRARY
223             NAMES hwloc
224             HINTS ${PATH_TO_LOOK_FOR})
225     endif()
226 endif()
227 mark_as_advanced(HWLOC_hwloc_LIBRARY)
228
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}")
236 else ()
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")
241     endif()
242 endif ()
243
244 if (HWLOC_LIBRARY_DIRS)
245     list(REMOVE_DUPLICATES HWLOC_LIBRARY_DIRS)
246 endif ()
247
248 # check a function to validate the find
249 if(HWLOC_LIBRARIES)
250
251     set(REQUIRED_INCDIRS)
252     set(REQUIRED_LIBDIRS)
253     set(REQUIRED_LIBS)
254
255     # HWLOC
256     if (HWLOC_INCLUDE_DIRS)
257         set(REQUIRED_INCDIRS "${HWLOC_INCLUDE_DIRS}")
258     endif()
259     if (HWLOC_LIBRARY_DIRS)
260         set(REQUIRED_LIBDIRS "${HWLOC_LIBRARY_DIRS}")
261     endif()
262     set(REQUIRED_LIBS "${HWLOC_LIBRARIES}")
263
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}")
269     endforeach()
270     list(APPEND CMAKE_REQUIRED_LIBRARIES "${REQUIRED_LIBS}")
271     string(REGEX REPLACE "^ -" "-" CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}")
272
273     # test link
274     include(CheckFunctionExists)
275     check_function_exists(hwloc_topology_init HWLOC_WORKS)
276     mark_as_advanced(HWLOC_WORKS)
277
278     if(NOT 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")
284         endif()
285     endif()
286     set(CMAKE_REQUIRED_INCLUDES)
287     set(CMAKE_REQUIRED_FLAGS)
288     set(CMAKE_REQUIRED_LIBRARIES)
289 endif(HWLOC_LIBRARIES)
290
291 if (HWLOC_LIBRARIES)
292   if (HWLOC_LIBRARY_DIRS)
293     list(GET HWLOC_LIBRARY_DIRS 0 first_lib_path)
294   else()
295     list(GET HWLOC_LIBRARIES 0 first_lib)
296     get_filename_component(first_lib_path "${first_lib}" PATH)
297   endif()
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)
301   else()
302     set(HWLOC_DIR_FOUND "${first_lib_path}" CACHE PATH "Installation directory of HWLOC library" FORCE)
303   endif()
304 endif()
305 mark_as_advanced(HWLOC_DIR)
306 mark_as_advanced(HWLOC_DIR_FOUND)
307
308 # Function for converting hex version numbers from HWLOC_API_VERSION if necessary
309 function(HEX2DEC str res)
310     string(LENGTH "${str}" len)
311     if("${len}" EQUAL 1)
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)
326         else()
327             return()
328         endif()
329     else()
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)
336     endif()
337 endfunction()
338
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
344             PATH_SUFFIXES bin
345             )
346         mark_as_advanced(HWLOC_INFO)
347
348         if(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)
353
354             if(HWLOC_INFO_ERR)
355                 message(STATUS "Error executing hwloc-info: ${HWLOC_INFO_ERR}")
356             endif()
357             string(REGEX MATCH "[0-9]+.*[0-9]+" HWLOC_INFO_OUT "${HWLOC_INFO_OUT}")
358             set(HWLOC_LIBRARY_VERSION ${HWLOC_INFO_OUT})
359         endif()
360     endif()
361
362     if (NOT HWLOC_FIND_QUIETLY)
363         message(STATUS "hwloc version: ${HWLOC_VERSION}")
364     endif()
365
366     # Parse header if cross-compiling, or if hwloc-info was not found
367     # Also used to check that library and header versions match
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_HEADER_VERSION "${DEC_MAJOR}.${DEC_MINOR}.${DEC_PATCH}")
380
381     if (HWLOC_LIBRARY_VERSION AND HWLOC_HEADER_VERSION)
382         string(SUBSTRING "${HWLOC_LIBRARY_VERSION}" 0 1 LIBRARY_MAJOR)
383         string(SUBSTRING "${HWLOC_HEADER_VERSION}" 0 1 HEADER_MAJOR)
384         string(COMPARE EQUAL "${LIBRARY_MAJOR}" "${HEADER_MAJOR}" HWLOC_VERSION_CHECK)
385         if(NOT HWLOC_VERSION_CHECK)
386             message(FATAL_ERROR "Detected version mismatch between HWLOC headers and library. "
387             "Library version is ${HWLOC_LIBRARY_VERSION}, but header version is ${HWLOC_HEADER_VERSION}. "
388             "Make sure that you have the correct include and library directory set for HWLOC")
389         endif()
390     endif()
391     if (HWLOC_LIBRARY_VERSION)
392         set(HWLOC_VERSION ${HWLOC_LIBRARY_VERSION} CACHE STRING "HWLOC library version")
393
394     else()
395         set(HWLOC_VERSION ${HWLOC_HEADER_VERSION} CACHE STRING "HWLOC library version")
396     endif()
397     set(GMX_HWLOC_API_VERSION ${HWLOC_API_VERSION} CACHE STRING "HWLOC API version during configuration time")
398 endif()
399
400 include(FindPackageHandleStandardArgs)
401 find_package_handle_standard_args(HWLOC
402                                   REQUIRED_VARS HWLOC_LIBRARIES HWLOC_INCLUDE_DIRS
403                                   VERSION_VAR HWLOC_VERSION)
404
405 mark_as_advanced(HWLOC_INCLUDE_DIRS HWLOC_LIBRARIES HWLOC_VERSION GMX_HWLOC_API_VERSION)