2 # This file is part of the GROMACS molecular simulation package.
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.
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)
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?
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)
75 set(GMX_TARGET_X86_VALUE 1)
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")
83 set(GCC_INLINE_ASM_DEFINE "-DGMX_X86_GCC_INLINE_ASM=0")
86 set(_compile_definitions "${GCC_INLINE_ASM_DEFINE} -I${PROJECT_SOURCE_DIR}/src -DGMX_CPUINFO_STANDALONE -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 message(STATUS "Did not detect build CPU ${LOWERTYPE} - detection program did not compile")
97 set(RUN_CPU_DETECTION_COMPILATION_QUIETLY TRUE CACHE INTERNAL "Keep quiet on any future compilation attempts")
100 if(CPU_DETECTION_COMPILED)
101 # Run the detection program with -type as the argument.
103 if(NOT DEFINED CPU_DETECTION_${UPPERTYPE})
104 execute_process(COMMAND ${CPU_DETECTION_BINARY} "-${LOWERTYPE}"
105 RESULT_VARIABLE RESULT_VAR
106 OUTPUT_VARIABLE OUTPUT_VAR_TEMP
108 if (RESULT_VAR EQUAL 0)
109 string(STRIP "${OUTPUT_VAR_TEMP}" OUTPUT_VAR)
110 message(STATUS "Detected build CPU ${LOWERTYPE} - ${OUTPUT_VAR}")
111 set(CPU_DETECTION_${UPPERTYPE} "${OUTPUT_VAR}" CACHE INTERNAL "Result of running cpu detection code with argument -${LOWERTYPE}")
113 message(STATUS "Did not detect build CPU ${LOWERTYPE} - detection program did not run successfully")