Introduce smell generator plus some small fixes
[u/mrichter/AliRoot.git] / cmake / FindRuleChecker.cmake
1 # -*- mode: cmake -*-
2
3 # AliRoot Build System Module to find and configure IRST ALICE Coding Coventions RuleChecker
4 #
5 # Author: Johny Jose (johny.jose@cern.ch)
6 #         Port of previous Makefile build to cmake
7
8 cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
9
10 find_program(RULECHECKER_SRCML NAMES src2srcml)
11 message(STATUS "Check for src2srcml: ${RULECHECKER_SRCML}")
12 find_package(Java)
13 message(STATUS "Check for Java: ${JAVA_RUNTIME}")
14
15 set(IRST_INSTALLDIR $ENV{IRST_INSTALLDIR})
16 if(NOT IRST_INSTALLDIR)
17   if(ALICE)
18     message(STATUS "Setting IRST_INSTALLDIR to ${ALICE}/local/ALICENewRuleChecker")
19     set(IRST_INSTALLDIR ${ALICE}/local/ALICENewRuleChecker)
20   endif(ALICE)
21 endif(NOT IRST_INSTALLDIR)
22
23 if(IRST_INSTALLDIR)
24   find_file(RULECHECKER_JAR NAMES NewRuleChecker.jar PATHS ${IRST_INSTALLDIR}/NewRuleChecker NO_DEFAULT_PATH)
25   find_file(RULECHECKER_RULES NAMES CodingConventions.xml PATHS ${IRST_INSTALLDIR}/NewRuleChecker/config NO_DEFAULT_PATH)
26   find_file(FACTEXTRACTOR_JAR NAMES FactExtractor.jar PATHS ${IRST_INSTALLDIR}/FactExtractor NO_DEFAULT_PATH)
27   find_file(SMELLDETECTOR_JAR NAMES SmellDetector.jar PATHS ${IRST_INSTALLDIR} NO_DEFAULT_PATH)
28   if(RULECHECKER_JAR AND RULECHECKER_RULES AND RULECHECKER_SRCML AND JAVA_RUNTIME AND SMELLDETECTOR_JAR)
29     set(RULECHECKER_FOUND TRUE)
30     message(STATUS "RuleChecker jar :           ${RULECHECKER_JAR}")
31     message(STATUS "RuleChecker rules :         ${RULECHECKER_RULES}")
32     message(STATUS "RuleChecker factextractor : ${FACTEXTRACTOR_JAR}")
33     message(STATUS "RuleChecker smelldetector : ${SMELLDETECTOR_JAR}")
34     message(STATUS "RuleChecker found on the system")
35
36     if(NOT EXISTS ${CMAKE_BINARY_DIR}/check-hxml-touchfile)
37       file(WRITE ${CMAKE_BINARY_DIR}/check-hxml-touchfile "Dummy dependency for factfile")
38     endif(NOT EXISTS ${CMAKE_BINARY_DIR}/check-hxml-touchfile)
39     set(FACTFILE ${CMAKE_BINARY_DIR}/factFile.xml)
40     set(_factfile_deps)
41
42     file(GLOB_RECURSE _root_headers  ${ROOTSYS}/include/*.h)
43     foreach(_root_header ${_root_headers})
44       if(NOT _root_header MATCHES ".*G__ci.h")
45         string (REGEX REPLACE "${ROOTSYS}/include/" "" _rel_root_header ${_root_header})
46         string (REGEX REPLACE "h$" "h.xml" _rel_root_hxml ${_rel_root_header})
47         get_filename_component(_rel_root_header_path ${_rel_root_hxml} PATH)
48         set(_root_hxml ${CMAKE_BINARY_DIR}/roothxml/${_rel_root_hxml})
49         if(NOT EXISTS ${CMAKE_BINARY_DIR}/roothxml/${_rel_root_header_path})
50           file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/roothxml/${_rel_root_header_path})
51         endif(NOT EXISTS ${CMAKE_BINARY_DIR}/roothxml/${_rel_root_header_path})
52         list(APPEND _factfile_deps ${_root_hxml})
53         add_custom_command(OUTPUT  ${_root_hxml}
54                            COMMAND ${RULECHECKER_SRCML} ${_root_header} ${_root_hxml}
55                            DEPENDS ${_root_header})
56       endif(NOT _root_header MATCHES ".*G__ci.h")
57     endforeach(_root_header ${_root_headers})
58   else()
59     message(STATUS "RuleChecker not found on this system")
60   endif(RULECHECKER_JAR AND RULECHECKER_RULES AND RULECHECKER_SRCML AND JAVA_RUNTIME AND SMELLDETECTOR_JAR)
61 else()
62   message(STATUS "RuleChecker not found on this system")
63 endif(IRST_INSTALLDIR)
64
65 macro(ALICE_CheckModule)
66   if(RULECHECKER_FOUND)
67     set(CHECKDIR ${CMAKE_BINARY_DIR}/${MODULE}/check)
68
69     file(GLOB_RECURSE _headers  ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
70     file(GLOB_RECURSE _sources_tmp  ${CMAKE_CURRENT_SOURCE_DIR}/*.cxx)
71     list(APPEND _sources_tmp ${_headers})
72     foreach(_srcfile ${_sources_tmp})
73       string(REPLACE ".h"   "" _srcfile_tmp ${_srcfile})
74       string(REPLACE ".cxx" "" _srcfile ${_srcfile_tmp})
75       list(APPEND _sources ${_srcfile})
76     endforeach(_srcfile ${_sources_tmp})
77     list(REMOVE_DUPLICATES _sources)
78     list(SORT _sources)
79
80     set(_violfiles)
81     set(_smellfiles)
82     set(_module_factfile_deps)
83     foreach(_srcfile ${_sources})
84       if(NOT _srcfile MATCHES "^.*LinkDef$" AND NOT _srcfile MATCHES ".*PYTHIA8/pythia8.*")
85         set(_violdeps)
86         string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" _srcfile_short ${_srcfile})
87         set(_viol ${CHECKDIR}/${_srcfile_short}.viol)
88         set(_smell ${CHECKDIR}/${_srcfile_short}.smell)
89         get_filename_component(_viol_path ${_viol} PATH)
90         list(APPEND _violfiles ${_viol})
91         list(APPEND _smellfiles ${_smell})
92         if(EXISTS ${_srcfile}.h)
93           add_custom_command(OUTPUT ${CHECKDIR}/${_srcfile_short}.h.xml
94                              COMMAND ${CMAKE_COMMAND} -E make_directory ${_viol_path}
95                              COMMAND ${RULECHECKER_SRCML} ${_srcfile}.h ${CHECKDIR}/${_srcfile_short}.h.xml
96                              COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/check-hxml-touchfile
97                              DEPENDS ${_srcfile}.h)
98           list(APPEND _violdeps ${CHECKDIR}/${_srcfile_short}.h.xml)
99           list(APPEND _module_factfile_deps ${CHECKDIR}/${_srcfile_short}.h.xml)
100         endif(EXISTS ${_srcfile}.h)
101         if(EXISTS ${_srcfile}.cxx)
102           add_custom_command(OUTPUT ${CHECKDIR}/${_srcfile_short}.cxx.xml
103                              COMMAND ${CMAKE_COMMAND} -E make_directory ${_viol_path}
104                              COMMAND ${RULECHECKER_SRCML} ${_srcfile}.cxx ${CHECKDIR}/${_srcfile_short}.cxx.xml
105                              DEPENDS ${_srcfile}.cxx)
106           list(APPEND _violdeps ${CHECKDIR}/${_srcfile_short}.cxx.xml)
107         endif(EXISTS ${_srcfile}.cxx)
108         add_custom_command( OUTPUT ${_viol}
109                             COMMAND ${JAVA_RUNTIME} -Xmx1024M -jar ${RULECHECKER_JAR} ${CHECKDIR}/${_srcfile_short}.cxx.xml ${CHECKDIR}/${_srcfile_short}.h.xml ${FACTFILE} ${RULECHECKER_RULES} > ${_viol}
110                             DEPENDS ${_violdeps}
111                             WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
112         add_custom_command( OUTPUT ${_smell}
113                             COMMAND ${JAVA_RUNTIME} -Xmx1024M -jar ${SMELLDETECTOR_JAR} ${CHECKDIR}/${_srcfile_short}.cxx.xml ${CHECKDIR}/${_srcfile_short}.h.xml > ${_smell}
114                             DEPENDS ${_violdeps}
115                             WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
116       endif(NOT _srcfile MATCHES "^.*LinkDef$" AND NOT _srcfile MATCHES ".*PYTHIA8/pythia8.*")
117     endforeach(_srcfile ${_sources})
118
119     if(_violfiles)
120       add_custom_target(${MODULE}-check DEPENDS ${_violfiles})
121       add_dependencies(${MODULE}-check factfile)
122       add_dependencies(check-all ${MODULE}-check)
123
124       add_custom_target(${MODULE}-smell DEPENDS ${_smellfiles})
125       add_dependencies(smell-all ${MODULE}-smell)
126
127       if(_module_factfile_deps)
128         add_custom_target(${MODULE}-hxml DEPENDS ${_module_factfile_deps})
129         add_dependencies(check-hxml ${MODULE}-hxml)
130       endif(_module_factfile_deps)
131     endif(_violfiles)
132
133
134   endif(RULECHECKER_FOUND)
135 endmacro(ALICE_CheckModule)
136