Remove cycle suppression
[gromacs.git] / cmake / gmxDetectCpu.cmake
blobd5a8f12a2c3fffce16a1425198e98070521d752d
2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012,2013,2014,2015,2016,2017, 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.
35 include(gmxTestInlineASM)
37 # Ensure things like GMX_TARGET_X86 are available
38 include(gmxDetectTargetArchitecture)
39 gmx_detect_target_architecture()
41 # gmx_run_cpu_detection()
43 # Try to detect information about the CPU of the build host by
44 # building and running the same detection code used by mdrun. This
45 # works on all architectures where we are not cross-compiling;
46 # depending on the architecture the detection will either use special
47 # assembly instructions (like cpuid), preprocessor defines, or probing
48 # /proc/cpuinfo on Linux.
50 # The TYPE argument is passed as a command-line argument to the
51 # detection program, and the terminal output is captured and stored in
52 # the cache variable CPU_DETECTION_${TYPE} as the result. If the detection
53 # program fails to compile, or fails to run, no value is stored.
55 # The function caches information about whether the detection program
56 # has already been built or run with this TYPE, so this function
57 # should be called freely, even if the call might be repeated within
58 # or across invocations of cmake.
60 function(gmx_run_cpu_detection TYPE)
61     string(TOUPPER ${TYPE} UPPERTYPE)
62     string(TOLOWER ${TYPE} LOWERTYPE)
64     set(OUTPUT_VAR "")
65     # We need to execute the binary, so this only works if not
66     # cross-compiling. However, note that we are NOT limited to x86.
67     if(CMAKE_CROSSCOMPILING)
68         # TODO Need we explain that we're not detecting because we are cross compiling?
69     else()
70         set(CPU_DETECTION_BINARY "${PROJECT_BINARY_DIR}/CMakeFiles/GmxDetectCpu${CMAKE_EXECUTABLE_SUFFIX}")
71         if(NOT CPU_DETECTION_COMPILED)
72             # Compile the detection program
73             set(GMX_TARGET_X86_VALUE 0)
74             if(GMX_TARGET_X86)
75                 set(GMX_TARGET_X86_VALUE 1)
76             endif()
78             # for x86 we need inline assembly to use cpuid
79             gmx_test_inline_asm_gcc_x86(GMX_X86_GCC_INLINE_ASM)
80             if(GMX_X86_GCC_INLINE_ASM)
81                 set(GCC_INLINE_ASM_DEFINE "-DGMX_X86_GCC_INLINE_ASM=1")
82             else()
83                 set(GCC_INLINE_ASM_DEFINE "-DGMX_X86_GCC_INLINE_ASM=0")
84             endif()
86             set(_compile_definitions "${GCC_INLINE_ASM_DEFINE} -I${PROJECT_SOURCE_DIR}/src -DGMX_CPUINFO_STANDALONE ${GMX_STDLIB_CXX_FLAGS} -DGMX_TARGET_X86=${GMX_TARGET_X86_VALUE}")
87             set(LINK_LIBRARIES "${GMX_STDLIB_LIBRARIES}")
88             try_compile(CPU_DETECTION_COMPILED
89                 "${PROJECT_BINARY_DIR}"
90                 "${PROJECT_SOURCE_DIR}/src/gromacs/hardware/cpuinfo.cpp"
91                 COMPILE_DEFINITIONS "${_compile_definitions}"
92                 CMAKE_FLAGS "-DLINK_LIBRARIES=${LINK_LIBRARIES}"
93                 OUTPUT_VARIABLE CPU_DETECTION_COMPILED_OUTPUT
94                 COPY_FILE ${CPU_DETECTION_BINARY})
95             if(NOT CPU_DETECTION_COMPILED AND NOT RUN_CPU_DETECTION_COMPILATION_QUIETLY)
96                 message(STATUS "Did not detect build CPU ${LOWERTYPE} - detection program did not compile")
97             endif()
98             set(RUN_CPU_DETECTION_COMPILATION_QUIETLY TRUE CACHE INTERNAL "Keep quiet on any future compilation attempts")
99         endif()
101         if(CPU_DETECTION_COMPILED)
102             # Run the detection program with -type as the argument.
104             if(NOT DEFINED CPU_DETECTION_${UPPERTYPE})
105                 execute_process(COMMAND ${CPU_DETECTION_BINARY} "-${LOWERTYPE}"
106                     RESULT_VARIABLE RESULT_VAR
107                     OUTPUT_VARIABLE OUTPUT_VAR_TEMP
108                     ERROR_QUIET)
109                 if (RESULT_VAR EQUAL 0)
110                     string(STRIP "${OUTPUT_VAR_TEMP}" OUTPUT_VAR)
111                     message(STATUS "Detected build CPU ${LOWERTYPE} - ${OUTPUT_VAR}")
112                     set(CPU_DETECTION_${UPPERTYPE} "${OUTPUT_VAR}" CACHE INTERNAL "Result of running cpu detection code with argument -${LOWERTYPE}")
113                 else()
114                     message(STATUS "Did not detect build CPU ${LOWERTYPE} - detection program did not run successfully")
115                 endif()
116             endif()
117         endif()
118     endif()
119 endfunction()