Merge branch 'release-4-6'
[alexxy/gromacs.git] / tests / CppCheck.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2013, by the GROMACS development team, led by
5 # David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 # others, as listed in the AUTHORS file in the top-level source
7 # 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
36 find_program(CPPCHECK_EXECUTABLE
37     NAMES cppcheck
38     DOC "cppcheck executable")
39 mark_as_advanced(CPPCHECK_EXECUTABLE)
40 option(CPPCHECK_XML_OUTPUT "Whether to produce XML output from cppcheck (mainly for Jenkins)"
41        OFF)
42 mark_as_advanced(CPPCHECK_XML_OUTPUT)
43
44 # Depends on stderr redirection and cat
45 if (CPPCHECK_EXECUTABLE AND UNIX)
46     # Produce the list of files to check
47     file(GLOB_RECURSE _inputfiles RELATIVE ${CMAKE_SOURCE_DIR}
48         ${CMAKE_SOURCE_DIR}/src/*.c
49         ${CMAKE_SOURCE_DIR}/src/*.cpp
50         ${CMAKE_SOURCE_DIR}/src/*.cu)
51     file(GLOB_RECURSE _files_to_ignore RELATIVE ${CMAKE_SOURCE_DIR}
52         ${CMAKE_SOURCE_DIR}/src/nb_kernel_Elec*.c
53         ${CMAKE_SOURCE_DIR}/src/gromacs/linearalgebra/gmx_blas/*.c
54         ${CMAKE_SOURCE_DIR}/src/gromacs/linearalgebra/gmx_lapack/*.c
55         ${CMAKE_SOURCE_DIR}/src/contrib/*.c
56         ${CMAKE_SOURCE_DIR}/src/contrib/*.cpp
57         ${CMAKE_SOURCE_DIR}/src/contrib/*.cu)
58     list(REMOVE_ITEM _inputfiles ${_files_to_ignore})
59
60     # Set flags for cppcheck
61     set(_outputext txt)
62     set(_outputopt --template=gcc)
63     if (CPPCHECK_XML_OUTPUT)
64         set(_outputext xml)
65         set(_outputopt --xml)
66     endif()
67     set(_common_flags
68         --enable=style
69         -DSIZEOF_LONG_LONG_INT=8 -DSIZEOF_INT=4 -DLINUX
70         -I src/gromacs/legacyheaders -I src
71         -I ${CMAKE_BINARY_DIR}/src -I ${CMAKE_BINARY_DIR}/src/gromacs/utility
72         --quiet
73         ${_outputopt})
74     set(_c_flags
75         -I src/gromacs/gmxpreprocess
76         -I src/programs/mdrun
77         --suppress=variableScope
78         --suppress=unusedVariable
79         --suppress=unreadVariable
80         --suppress=unusedStructMember
81         --suppress=invalidscanf
82         --suppress=sizeofCalculation
83         --suppress=missingInclude:src/programs/mdrun/gmx_gpu_utils/gmx_gpu_utils.cu
84         --inline-suppr)
85     set(_cxx_flags
86         -D__cplusplus
87         --suppress=variableScope
88         --suppress=unnecessaryForwardDeclaration
89         --suppress=invalidscanf:src/gromacs/gmxlib/matio.cpp
90         --suppress=invalidscanf:src/gromacs/gmxlib/xvgr.cpp
91         --suppress=invalidscanf:src/gromacs/gmxpreprocess/pdb2top.cpp
92         --suppress=*:src/gromacs/selection/scanner.cpp)
93
94     # This list will hold the list of all files with cppcheck errors
95     # (one per input file)
96     set(_filelist)
97
98     foreach (_filename ${_inputfiles})
99         set(_target_name cppcheck-${_filename}.${_outputext})
100         string(REPLACE "/" "_" _target_name ${_target_name})
101         list(APPEND _filelist ${_target_name})
102         if (_filename MATCHES "\\.cpp$")
103             set(_lang CXX)
104             set(_lang_flags ${_cxx_flags})
105         else()
106             set(_lang C)
107             set(_lang_flags ${_c_flags})
108         endif()
109         add_custom_command(
110             OUTPUT ${_target_name}
111             COMMAND ${CPPCHECK_EXECUTABLE} ${_common_flags} ${_lang_flags}
112                 ${_filename} 2> ${CMAKE_CURRENT_BINARY_DIR}/${_target_name}
113             WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
114             IMPLICIT_DEPENDS ${_lang} ${CMAKE_SOURCE_DIR}/${_filename}
115             COMMENT "Running cppcheck for ${_filename}"
116             VERBATIM)
117         if (NOT CPPCHECK_XML_OUTPUT)
118             add_custom_command(
119                 OUTPUT ${_target_name}
120                 COMMAND cat ${CMAKE_CURRENT_BINARY_DIR}/${_target_name}
121                 VERBATIM APPEND)
122         endif()
123     endforeach()
124     if (NOT CPPCHECK_XML_OUTPUT)
125         set(_target_name cppcheck-errors.${_outputext})
126         add_custom_command(
127             OUTPUT ${_target_name}
128             COMMAND cat ${_filelist} > ${_target_name}
129             DEPENDS ${_filelist}
130             COMMENT "Combining cppcheck results"
131             VERBATIM)
132         list(APPEND _filelist ${_target_name})
133     endif()
134     add_custom_target(cppcheck DEPENDS ${_filelist})
135 else()
136     add_custom_target(cppcheck
137         COMMAND ${CMAKE_COMMAND} -E echo
138             "cppcheck was not found by CMake. Rerun CMake specifying CPPCHECK_EXECUTABLE."
139         VERBATIM)
140 endif()