Use CMake to propagate versions and hashes to gitlab jobs
[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 by the GROMACS development team.
5 # Copyright (c) 2017,2019,2020, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
9 #
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
14 #
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24 #
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
32 #
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
35
36 include(gmxTestInlineASM)
37
38 # Ensure things like GMX_TARGET_X86 are available
39 include(gmxDetectTargetArchitecture)
40 gmx_detect_target_architecture()
41
42 # gmx_run_cpu_detection()
43 #
44 # Try to detect information about the CPU of the build host by
45 # building and running the same detection code used by mdrun. This
46 # works on all architectures where we are not cross-compiling;
47 # depending on the architecture the detection will either use special
48 # assembly instructions (like cpuid), preprocessor defines, or probing
49 # /proc/cpuinfo on Linux.
50 #
51 # The TYPE argument is passed as a command-line argument to the
52 # detection program, and the terminal output is captured and stored in
53 # the cache variable CPU_DETECTION_${TYPE} as the result. If the detection
54 # program fails to compile, or fails to run, no value is stored.
55 #
56 # The function caches information about whether the detection program
57 # has already been built or run with this TYPE, so this function
58 # should be called freely, even if the call might be repeated within
59 # or across invocations of cmake.
60 #
61 function(gmx_run_cpu_detection TYPE)
62     string(TOUPPER ${TYPE} UPPERTYPE)
63     string(TOLOWER ${TYPE} LOWERTYPE)
64
65     set(OUTPUT_VAR "")
66     # We need to execute the binary, so this only works if not
67     # cross-compiling. However, note that we are NOT limited to x86.
68     if(CMAKE_CROSSCOMPILING)
69         # TODO Need we explain that we're not detecting because we are cross compiling?
70     else()
71         set(CPU_DETECTION_BINARY "${PROJECT_BINARY_DIR}/CMakeFiles/GmxDetectCpu${CMAKE_EXECUTABLE_SUFFIX}")
72         if(NOT CPU_DETECTION_COMPILED)
73             # Compile the detection program
74             set(GMX_TARGET_X86_VALUE 0)
75             if(GMX_TARGET_X86)
76                 set(GMX_TARGET_X86_VALUE 1)
77             endif()
78
79             # for x86 we need inline assembly to use cpuid
80             gmx_test_inline_asm_gcc_x86(GMX_X86_GCC_INLINE_ASM)
81             if(GMX_X86_GCC_INLINE_ASM)
82                 set(GCC_INLINE_ASM_DEFINE -DGMX_X86_GCC_INLINE_ASM=1)
83             else()
84                 set(GCC_INLINE_ASM_DEFINE -DGMX_X86_GCC_INLINE_ASM=0)
85             endif()
86
87             set(_compile_definitions ${GCC_INLINE_ASM_DEFINE};-I${PROJECT_SOURCE_DIR}/src;-DGMX_CPUINFO_STANDALONE=1;-DGMX_TARGET_X86=${GMX_TARGET_X86_VALUE})
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                 if(GMX_TARGET_X86)
97                     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.")
98                 else()
99                     message(WARNING "Did not detect build CPU ${LOWERTYPE} - detection program did not compile. Please file a bug report if this is a common platform.")
100                 endif()
101             endif()
102             set(RUN_CPU_DETECTION_COMPILATION_QUIETLY TRUE CACHE INTERNAL "Keep quiet on any future compilation attempts")
103         endif()
104
105         if(CPU_DETECTION_COMPILED)
106             # Run the detection program with -type as the argument.
107
108             if(NOT DEFINED CPU_DETECTION_${UPPERTYPE})
109                 execute_process(COMMAND ${CPU_DETECTION_BINARY} "-${LOWERTYPE}"
110                     RESULT_VARIABLE RESULT_VAR
111                     OUTPUT_VARIABLE OUTPUT_VAR_TEMP
112                     ERROR_QUIET)
113                 if (RESULT_VAR EQUAL 0)
114                     string(STRIP "${OUTPUT_VAR_TEMP}" OUTPUT_VAR)
115                     message(STATUS "Detected build CPU ${LOWERTYPE} - ${OUTPUT_VAR}")
116                     set(CPU_DETECTION_${UPPERTYPE} "${OUTPUT_VAR}" CACHE INTERNAL "Result of running cpu detection code with argument -${LOWERTYPE}")
117                 else()
118                     message(STATUS "Did not detect build CPU ${LOWERTYPE} - detection program did not run successfully")
119                 endif()
120             endif()
121         endif()
122     endif()
123 endfunction()