8af7337adc0cc61f6f20940915088be0ef9cb06e
[alexxy/gromacs.git] / cmake / gmxDetectCpu.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013,2014,2015,2016,2017,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.
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 include(gmxTestInlineASM)
36
37 # Ensure things like GMX_TARGET_X86 are available
38 include(gmxDetectTargetArchitecture)
39 gmx_detect_target_architecture()
40
41 # gmx_run_cpu_detection()
42 #
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.
49 #
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.
54 #
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.
59 #
60 function(gmx_run_cpu_detection TYPE)
61     string(TOUPPER ${TYPE} UPPERTYPE)
62     string(TOLOWER ${TYPE} LOWERTYPE)
63
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()
77
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()
85
86             set(_compile_definitions ${GCC_INLINE_ASM_DEFINE};-I${PROJECT_SOURCE_DIR}/src;-DGMX_CPUINFO_STANDALONE=1;-DGMX_TARGET_X86=${GMX_TARGET_X86_VALUE})
87             try_compile(CPU_DETECTION_COMPILED
88                 "${PROJECT_BINARY_DIR}"
89                 "${PROJECT_SOURCE_DIR}/src/gromacs/hardware/cpuinfo.cpp"
90                 COMPILE_DEFINITIONS "${_compile_definitions}"
91                 CMAKE_FLAGS "-DLINK_LIBRARIES=${LINK_LIBRARIES}"
92                 OUTPUT_VARIABLE CPU_DETECTION_COMPILED_OUTPUT
93                 COPY_FILE ${CPU_DETECTION_BINARY})
94             if(NOT CPU_DETECTION_COMPILED AND NOT RUN_CPU_DETECTION_COMPILATION_QUIETLY)
95                 if(GMX_TARGET_X86)
96                     message(WARNING "CPU detection program did not compile on x86 host - this should never happen. It is VERY bad for performance, since you will lose all SIMD support. Please file a bug report.")
97                 else()
98                     message(WARNING "Did not detect build CPU ${LOWERTYPE} - detection program did not compile. Please file a bug report if this is a common platform.")
99                 endif()
100             endif()
101             set(RUN_CPU_DETECTION_COMPILATION_QUIETLY TRUE CACHE INTERNAL "Keep quiet on any future compilation attempts")
102         endif()
103
104         if(CPU_DETECTION_COMPILED)
105             # Run the detection program with -type as the argument.
106
107             if(NOT DEFINED CPU_DETECTION_${UPPERTYPE})
108                 execute_process(COMMAND ${CPU_DETECTION_BINARY} "-${LOWERTYPE}"
109                     RESULT_VARIABLE RESULT_VAR
110                     OUTPUT_VARIABLE OUTPUT_VAR_TEMP
111                     ERROR_QUIET)
112                 if (RESULT_VAR EQUAL 0)
113                     string(STRIP "${OUTPUT_VAR_TEMP}" OUTPUT_VAR)
114                     message(STATUS "Detected build CPU ${LOWERTYPE} - ${OUTPUT_VAR}")
115                     set(CPU_DETECTION_${UPPERTYPE} "${OUTPUT_VAR}" CACHE INTERNAL "Result of running cpu detection code with argument -${LOWERTYPE}")
116                 else()
117                     message(STATUS "Did not detect build CPU ${LOWERTYPE} - detection program did not run successfully")
118                 endif()
119             endif()
120         endif()
121     endif()
122 endfunction()