Add cool quote
[alexxy/gromacs.git] / tests / CppCheck.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 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.
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 find_program(CPPCHECK_EXECUTABLE
36     NAMES cppcheck
37     DOC "cppcheck executable")
38 mark_as_advanced(CPPCHECK_EXECUTABLE)
39 option(CPPCHECK_XML_OUTPUT "Whether to produce XML output from cppcheck (mainly for Jenkins)"
40        OFF)
41 mark_as_advanced(CPPCHECK_XML_OUTPUT)
42
43 # Depends on stderr redirection and cat
44 if (CPPCHECK_EXECUTABLE AND UNIX)
45     # Produce the list of files to check
46     file(GLOB_RECURSE _inputfiles RELATIVE ${CMAKE_SOURCE_DIR}
47         ${CMAKE_SOURCE_DIR}/src/*.c
48         ${CMAKE_SOURCE_DIR}/src/*.cpp
49         ${CMAKE_SOURCE_DIR}/src/*.cu)
50     file(GLOB_RECURSE _files_to_ignore RELATIVE ${CMAKE_SOURCE_DIR}
51         ${CMAKE_SOURCE_DIR}/src/nb_kernel_Elec*.c
52         ${CMAKE_SOURCE_DIR}/src/gromacs/linearalgebra/gmx_blas/*.c
53         ${CMAKE_SOURCE_DIR}/src/gromacs/linearalgebra/gmx_lapack/*.c
54         ${CMAKE_SOURCE_DIR}/src/contrib/*.c
55         ${CMAKE_SOURCE_DIR}/src/contrib/*.cpp
56         ${CMAKE_SOURCE_DIR}/src/contrib/*.cu
57         ${CMAKE_SOURCE_DIR}/src/external/*.c
58         ${CMAKE_SOURCE_DIR}/src/external/*.cpp
59         ${CMAKE_SOURCE_DIR}/src/external/*.cu
60         ${CMAKE_SOURCE_DIR}/src/gromacs/selection/scanner.cpp
61         ${CMAKE_SOURCE_DIR}/src/gromacs/selection/parser.cpp
62         ${CMAKE_SOURCE_DIR}/src/gromacs/gpu_utils/ocl_compiler.cpp
63         )
64     list(REMOVE_ITEM _inputfiles ${_files_to_ignore})
65
66     # Set flags for cppcheck
67     set(_outputext txt)
68     set(_outputopt "--template={file}:{line}:{id}: {severity}: {message}")
69     if (CPPCHECK_XML_OUTPUT)
70         set(_outputext xml)
71         set(_outputopt --xml --xml-version=2)
72     endif()
73     set(_common_flags
74         --enable=style -DLINUX -DHAVE_UNISTD_H
75         -I src
76         -I src/external/thread_mpi/include
77         -I src/external/tng_io/include
78         -I ${CMAKE_BINARY_DIR}/src
79         --quiet
80         --inline-suppr
81         ${_outputopt})
82     set(_c_flags
83         --suppress=variableScope
84         --suppress=unnecessaryForwardDeclaration
85         --suppress=unusedVariable
86         --suppress=unreadVariable
87         --suppress=unusedStructMember
88         --suppress=invalidscanf
89         --suppress=sizeofCalculation
90         --suppress=invalidscanf_libc
91         --suppress=*:src/external/Random123-1.08/include/Random123/features/compilerfeatures.h
92         ) 
93     set(_cxx_flags
94         --language=c++ # particularly useful for CUDA files
95         -D__cplusplus
96         --suppress=variableScope
97         --suppress=unnecessaryForwardDeclaration
98         --suppress=memsetClassFloat  #we assume IEEE754
99         --suppress=invalidscanf_libc #seems only important for security on non-std libc
100         --suppress=invalidscanf      #same as last (style and portability checker have the same warning)
101         --suppress=passedByValue:src/gromacs/simd/tests/*
102         --suppress=redundantAssignment:src/gromacs/simd/simd_math.h #seems to be a bug in cppcheck
103         --suppress=noExplicitConstructor # can't be selective about this, see http://sourceforge.net/p/cppcheck/discussion/general/thread/db1e4ba7/
104         --suppress=unusedStructMember:src/gromacs/onlinehelp/tests/helpmanager.cpp
105         --suppress=unusedStructMember:src/gromacs/commandline/cmdlinehelpmodule.cpp
106         --suppress=unusedStructMember:src/gromacs/selection/selhelp.cpp
107         --suppress=redundantPointerOp:src/gromacs/fileio/gmxfio-xdr.cpp
108         --suppress=passedByValue # See comment below
109         --suppress=shiftTooManyBits:src/gromacs/gpu_utils/gpu_utils.cu # CUDA kernel launch false positive
110         )
111         # Passing non-trivial objects by value is rarely a problem for
112         # GROMACS in performance-sensitive code, and shouldn't be
113         # enforced for types that are intended to be used like value
114         # types (e.g. SIMD wrapper types, ArrayRef) , nor for
115         # move-enabled types. cppcheck isn't sensitive to these
116         # subtleties yet.
117
118     # This list will hold the list of all files with cppcheck errors
119     # (one per input file)
120     set(_filelist)
121
122     foreach (_filename ${_inputfiles})
123         set(_target_name cppcheck-${_filename}.${_outputext})
124         string(REPLACE "/" "_" _target_name ${_target_name})
125         list(APPEND _filelist ${_target_name})
126         if (_filename MATCHES "\\.cpp$" OR _filename MATCHES "\\.cu$")
127             set(_lang CXX)
128             set(_lang_flags ${_cxx_flags})
129         else()
130             set(_lang C)
131             set(_lang_flags ${_c_flags})
132         endif()
133         add_custom_command(
134             OUTPUT ${_target_name}
135             COMMAND ${CPPCHECK_EXECUTABLE} ${_common_flags} ${_lang_flags}
136                 ${_filename} 2> ${CMAKE_CURRENT_BINARY_DIR}/${_target_name}
137             WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
138             IMPLICIT_DEPENDS ${_lang} ${CMAKE_SOURCE_DIR}/${_filename}
139             COMMENT "Running cppcheck for ${_filename}"
140             VERBATIM)
141         if (NOT CPPCHECK_XML_OUTPUT)
142             add_custom_command(
143                 OUTPUT ${_target_name}
144                 COMMAND cat ${CMAKE_CURRENT_BINARY_DIR}/${_target_name}
145                 VERBATIM APPEND)
146         endif()
147     endforeach()
148     if (NOT CPPCHECK_XML_OUTPUT)
149         set(_target_name cppcheck-errors.${_outputext})
150         add_custom_command(
151             OUTPUT ${_target_name}
152             COMMAND cat ${_filelist} > ${_target_name}
153             DEPENDS ${_filelist}
154             COMMENT "Combining cppcheck results"
155             VERBATIM)
156         list(APPEND _filelist ${_target_name})
157     endif()
158     add_custom_target(cppcheck DEPENDS ${_filelist})
159 else()
160     add_custom_target(cppcheck
161         COMMAND ${CMAKE_COMMAND} -E echo
162             "cppcheck was not found by CMake. Rerun CMake specifying CPPCHECK_EXECUTABLE."
163         VERBATIM)
164 endif()