First step for rulechecker -- not working yet
[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_ROOT}/cmakelocal/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 ${ALICE_ROOT}/cmake/RuleChecker)
25   find_file(RULECHECKER_RULES NAMES CodingConventions.xml PATHS ${IRST_INSTALLDIR}/NewRuleChecker/config ${ALICE_ROOT}/cmake/RuleChecker)
26   find_file(FACTEXTRACTOR_JAR NAME FactExtractor.jar PATHS ${IRST_INSTALLDIR}/FactExtractor ${ALICE_ROOT}/cmake/RuleChecker)
27   if(RULECHECKER_JAR AND RULECHECKER_RULES AND RULECHECKER_SRCML AND JAVA_RUNTIME)
28     set(RULECHECKER_FOUND TRUE)
29     message(STATUS "RuleChecker jar : ${RULECHECKER_JAR}")
30     message(STATUS "RuleChecker rules : ${RULECHECKER_RULES}")
31     message(STATUS "RuleChecker factextractor : ${FACTEXTRACTOR_JAR}")
32     message(STATUS "RuleChecker found on the system")
33
34     set(FACTFILE ${CMAKE_BINARY_DIR}/factFile.xml)
35     set(_factfile_deps)
36
37     file(GLOB_RECURSE _root_headers  ${ROOTSYS}/include/*.h)
38     foreach(_root_header ${_root_headers})
39       string (REGEX REPLACE "${ROOTSYS}/include/" "" _rel_root_header ${_root_header})
40       string (REGEX REPLACE "h$" "h.xml" _rel_root_hxml ${_rel_root_header})
41       get_filename_component(_rel_root_header_path ${_rel_root_hxml} PATH)
42       set(_root_hxml roothxml/${_rel_root_hxml})
43       if(NOT EXISTS roothxml/${_rel_root_header_path})
44         file(MAKE_DIRECTORY roothxml/${_rel_root_header_path})
45       endif(NOT EXISTS roothxml/${_rel_root_header_path})
46       list(APPEND _factfile_deps ${_root_hxml})
47       add_custom_command(OUTPUT  ${_root_hxml}
48                          COMMAND ${RULECHECKER_SRCML} ${_root_header} ${_root_hxml}
49                          DEPENDS ${_root_header})
50     endforeach(_root_header ${_root_headers})
51   else()
52     message(STATUS "RuleChecker not found on this system")
53   endif(RULECHECKER_JAR AND RULECHECKER_RULES AND RULECHECKER_SRCML AND JAVA_RUNTIME)
54 else()
55   message(STATUS "RuleChecker not found on this system")
56 endif(IRST_INSTALLDIR)
57
58 macro(ALICE_CheckModule)
59   if(RULECHECKER_FOUND)
60     set(CHECKDIR ${CMAKE_BINARY_DIR}/${MODULE}/check)
61     set(violFiles)
62
63     foreach(_srcfile ${SRCS})
64       string (REGEX REPLACE "cxx$" "h" _header ${_srcfile})
65       get_filename_component(_srcname ${_srcfile} NAME)
66       string (REGEX REPLACE "cxx$" "viol" _viol ${_srcname})
67       string (REGEX REPLACE "cxx$" "cxx.xml" _srcxml ${_srcname})
68       string (REGEX REPLACE "cxx$" "h.xml" _hxml ${_srcname})
69       string (REGEX REPLACE ".cxx$" "" _class ${_srcname})
70       set(_depends ${_srcfile})
71
72       if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_header})
73         list(APPEND _depends ${_header})
74         list(APPEND _factfile_deps ${_hxml})
75         add_custom_command( OUTPUT ${_viol}
76                           COMMAND ${RULECHECKER_SRCML} ${_srcfile} ${CHECKDIR}/${_srcxml}
77                           COMMAND ${RULECHECKER_SRCML} ${_header} ${CHECKDIR}/${_hxml}
78                           COMMAND ${JAVA_RUNTIME} -jar ${RULECHECKER_JAR} ${CHECKDIR}/${_srcxml} ${CHECKDIR}/${_hxml} ${FACTFILE} ${RULECHECKER_RULES} > ${CHECKDIR}/viols/${_viol}
79                           DEPENDS ${_depends}
80                           WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
81         list(APPEND violFiles ${_viol})
82       else()
83         add_custom_command( OUTPUT ${_viol}
84                           COMMAND ${RULECHECKER_SRCML} ${_srcfile} ${CHECKDIR}/${_srcxml}
85                           COMMAND ${JAVA_RUNTIME} -jar ${RULECHECKER_JAR} ${CHECKDIR}/${_srcxml} ${CHECKDIR}/${_hxml} ${FACTFILE} ${RULECHECKER_RULES} > ${CHECKDIR}/viols/${_viol}
86                           DEPENDS ${_depends}
87                           WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
88         list(APPEND violFiles ${_viol})
89       endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${_header})
90       if(CLASSCHECK STREQUAL "YES")
91         add_custom_target(${MODULE}-${_class}-check DEPENDS ${_viol})
92       endif(CLASSCHECK STREQUAL "YES")
93     endforeach(_srcfile ${SRCS})
94     if(violFiles)
95       add_custom_target(${PACKAGE}-check DEPENDS ${FACTFILE} ${violFiles})
96 #      add_dependencies(${PACKAGE}-check ${FACTFILE} ${violFiles})
97       add_dependencies(${MODULE}-check-all ${PACKAGE}-check)
98     endif(violFiles)
99     add_custom_command(TARGET clean
100                        COMMAND ${CMAKE_COMMAND} -E remove_directory ${CHECKDIR})
101
102   endif(RULECHECKER_FOUND)
103 endmacro(ALICE_CheckModule)
104