2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2015,2016, 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 # This file implements the custom build type "MSAN", to be used to run
36 # the Memory Sanitizer checker in clang. The code here follows the
37 # approaches documented at
38 # https://code.google.com/p/memory-sanitizer/wiki/LibcxxHowTo and
39 # https://code.google.com/p/memory-sanitizer/wiki/MemorySanitizer. In
40 # particular, the dependency stack must also be built with
41 # MemorySanitizer - notably including the C++ standard library, and
42 # notably excluding the C and math libraries.
44 # CMake cache variable GMX_MSAN_PATH should be set (e.g. on the
45 # command line) to point to the location where the dependencies that
46 # have been built with MemorySanitizer support can be found. This will
47 # be preprended to preprocessor and linker search paths. The MSAN
48 # build type is highly unlikely to work usefully without this variable
49 # set appropriately, and the dependencies installed there. In
50 # particular, suitable zlib needs to be installed there.
52 # Where GROMACS can optionally use bundled dependency code, such as
53 # fftpack and XDR, only the bundled code is supported. MSAN build type
54 # does not support static linking at this time.
56 # This build type needs to set linker flags in order to function, and
57 # CMake does not directly support linker flags that are specific to a
58 # build type (except on Windows). Thus, it is not clear whether
59 # changing the build type to/from MSAN, or any other modification to
60 # CMAKE_*_LINKER_FLAGS will work well. If in doubt, clean the build
61 # tree and start again.
62 function(gmxManageMsanBuild)
63 string(TOUPPER "${CMAKE_BUILD_TYPE}" _cmake_build_type)
65 if (NOT "${_cmake_build_type}" STREQUAL "MSAN")
66 # Nothing needs to be done now
70 set(_flags "-O2 -g -fsanitize=memory -fno-omit-frame-pointer")
72 foreach(_language C CXX)
73 string(REPLACE "X" "+" _human_readable_language ${_language})
74 if (CMAKE_${_language}_COMPILER_ID MATCHES "Clang" AND
75 NOT CMAKE_${_language}_COMPILER_VERSION VERSION_LESS 3.4)
76 # Can't do Memory Sanitizer build with this compiler, so don't
78 if(${_language} MATCHES CXX)
79 set(_language_flags "${_flags} -stdlib=libc++")
81 set(_language_flags "${_language_flags} -I${GMX_MSAN_PATH}/include -I${GMX_MSAN_PATH}/include/c++/v1")
84 set(_language_flags "${_flags}")
86 set(CMAKE_${_language}_FLAGS_MSAN ${_language_flags} CACHE STRING "${_human_readable_language} flags for Memory Sanitizer")
87 mark_as_advanced(CMAKE_${_language}_FLAGS_MSAN)
89 message(FATAL_ERROR "The Memory Sanitizer build is only available with clang ${_human_readable_language} compiler >= 3.4, but it was ${CMAKE_${_language}_COMPILER_ID} and ${CMAKE_${_language}_COMPILER_VERSION}.")
93 # Per-build-type linker flags like CMAKE_EXE_LINKER_FLAGS_MSAN
94 # only seem to be supported by CMake on Windows, so we can't use
96 set(_linker_flags "-fsanitize=memory -stdlib=libc++")
98 set(_linker_flags "${_linker_flags} -L${GMX_MSAN_PATH}/lib -lc++abi -I${GMX_MSAN_PATH}/include -I${GMX_MSAN_PATH}/include/c++/v1 -Wl,-rpath,${GMX_MSAN_PATH}/lib")
100 set(CMAKE_EXE_LINKER_FLAGS "${_linker_flags} ${CMAKE_EXE_LINKER_FLAGS}" PARENT_SCOPE)
101 set(CMAKE_SHARED_LINKER_FLAGS "${_linker_flags} ${CMAKE_SHARED_LINKER_FLAGS}" PARENT_SCOPE)
102 # Static linking is not supported for this build type
103 # set(CMAKE_STATIC_LINKER_FLAGS ${_linker_flags} CACHE STRING "Linker flags for Memory Sanitizer" FORCE)
105 set(GMX_FFT_LIBRARY "fftpack" CACHE STRING "Only fftpack is supported with MSAN")
106 set(GMX_SYSTEM_XDR OFF CACHE STRING "Only internal XDR is supported with MSAN")