Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / cmake / cpplint.cmake
1 # Copyright (C) 2018-2019 Intel Corporation
2 #
3 # SPDX-License-Identifier: Apache-2.0
4 #
5
6 if(ENABLE_CPPLINT)
7     find_package(PythonInterp 2.7 EXACT)
8
9     if(NOT PYTHONINTERP_FOUND)
10         message(WARNING "Python was not found (required for cpplint check)")
11         set(ENABLE_CPPLINT OFF)
12     endif()
13 endif()
14
15 if(ENABLE_CPPLINT)
16     add_custom_target(cpplint_all ALL)
17     set(CPPLINT_ALL_OUTPUT_FILES "" CACHE INTERNAL "All cpplint output files")
18 endif()
19
20 function(add_cpplint_target TARGET_NAME)
21     if(NOT ENABLE_CPPLINT)
22         return()
23     endif()
24
25     set(options "")
26     set(oneValueArgs "")
27     set(multiValueArgs "FOR_TARGETS" "FOR_SOURCES" "EXCLUDE_PATTERNS")
28     cmake_parse_arguments(CPPLINT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
29
30     foreach(target IN LISTS CPPLINT_FOR_TARGETS)
31         get_target_property(target_sources "${target}" SOURCES)
32         list(APPEND CPPLINT_FOR_SOURCES ${target_sources})
33     endforeach()
34     list(REMOVE_DUPLICATES CPPLINT_FOR_SOURCES)
35
36     set(all_output_files "")
37     foreach(source_file IN LISTS CPPLINT_FOR_SOURCES)
38         set(exclude FALSE)
39         foreach(pattern IN LISTS CPPLINT_EXCLUDE_PATTERNS)
40             if(source_file MATCHES "${pattern}")
41                 set(exclude TRUE)
42                 break()
43             endif()
44         endforeach()
45
46         if(exclude)
47             continue()
48         endif()
49
50         file(RELATIVE_PATH source_file_relative "${CMAKE_CURRENT_SOURCE_DIR}" "${source_file}")
51         set(output_file "${CMAKE_CURRENT_BINARY_DIR}/cpplint/${source_file_relative}.cpplint")
52         string(REPLACE ".." "__" output_file "${output_file}")
53         get_filename_component(output_dir "${output_file}" DIRECTORY)
54         file(MAKE_DIRECTORY "${output_dir}")
55
56         add_custom_command(
57             OUTPUT
58                 "${output_file}"
59             COMMAND
60                 "${CMAKE_COMMAND}"
61                 -D "PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}"
62                 -D "CPPLINT_SCRIPT=${IE_MAIN_SOURCE_DIR}/scripts/cpplint.py"
63                 -D "INPUT_FILE=${source_file}"
64                 -D "OUTPUT_FILE=${output_file}"
65                 -D "WORKING_DIRECTORY=${CMAKE_CURRENT_SOURCE_DIR}"
66                 -D "SKIP_RETURN_CODE=${ENABLE_CPPLINT_REPORT}"
67                 -P "${IE_MAIN_SOURCE_DIR}/cmake/cpplint_run.cmake"
68             DEPENDS
69                 "${source_file}"
70                 "${IE_MAIN_SOURCE_DIR}/scripts/cpplint.py"
71                 "${IE_MAIN_SOURCE_DIR}/cmake/cpplint_run.cmake"
72             COMMENT
73                 "[cpplint] ${source_file}"
74             VERBATIM)
75
76         list(APPEND all_output_files "${output_file}")
77     endforeach()
78
79     set(CPPLINT_ALL_OUTPUT_FILES
80         ${CPPLINT_ALL_OUTPUT_FILES} ${all_output_files}
81         CACHE INTERNAL
82         "All cpplint output files")
83
84     add_custom_target(${TARGET_NAME} ALL
85         DEPENDS ${all_output_files}
86         COMMENT "[cpplint] ${TARGET_NAME}")
87
88     if(CPPLINT_FOR_TARGETS)
89         foreach(target IN LISTS CPPLINT_FOR_TARGETS)
90             add_dependencies(${target} ${TARGET_NAME})
91         endforeach()
92     endif()
93
94     add_dependencies(cpplint_all ${TARGET_NAME})
95 endfunction()
96
97 function(add_cpplint_report_target)
98     if(NOT ENABLE_CPPLINT OR NOT ENABLE_CPPLINT_REPORT)
99         return()
100     endif()
101
102     set(cpplint_output_file "${CMAKE_BINARY_DIR}/cpplint/final_output.cpplint")
103     add_custom_command(
104         OUTPUT
105             "${cpplint_output_file}"
106         COMMAND
107             "${CMAKE_COMMAND}"
108             -D "FINAL_OUTPUT_FILE=${cpplint_output_file}"
109             -D "OUTPUT_FILES=${CPPLINT_ALL_OUTPUT_FILES}"
110             -P "${IE_MAIN_SOURCE_DIR}/cmake/cpplint_merge.cmake"
111         DEPENDS
112             ${CPPLINT_ALL_OUTPUT_FILES}
113             "${IE_MAIN_SOURCE_DIR}/cmake/cpplint_merge.cmake"
114         COMMENT
115             "[cpplint] Merge all output files"
116         VERBATIM)
117
118     set(cppcheck_output_file "${CMAKE_BINARY_DIR}/cpplint/cpplint-cppcheck-result.xml")
119     add_custom_command(
120         OUTPUT
121             "${cppcheck_output_file}"
122         COMMAND
123             "${CMAKE_COMMAND}"
124             -D "PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}"
125             -D "CONVERT_SCRIPT=${IE_MAIN_SOURCE_DIR}/scripts/cpplint_to_cppcheckxml.py"
126             -D "INPUT_FILE=${cpplint_output_file}"
127             -D "OUTPUT_FILE=${cppcheck_output_file}"
128             -P "${IE_MAIN_SOURCE_DIR}/cmake/cpplint_to_cppcheck_xml.cmake"
129         DEPENDS
130             ${cpplint_output_file}
131             "${IE_MAIN_SOURCE_DIR}/scripts/cpplint_to_cppcheckxml.py"
132             "${IE_MAIN_SOURCE_DIR}/cmake/cpplint_to_cppcheck_xml.cmake"
133         COMMENT
134             "[cpplint] Convert to cppcheck XML format"
135         VERBATIM)
136
137     set(report_dir "${IE_MAIN_SOURCE_DIR}/report/cpplint")
138     set(html_output_file "${report_dir}/index.html")
139     add_custom_command(
140         OUTPUT
141             "${html_output_file}"
142         COMMAND
143             "${CMAKE_COMMAND}"
144             -D "PYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}"
145             -D "CONVERT_SCRIPT=${IE_MAIN_SOURCE_DIR}/scripts/cppcheck-htmlreport.py"
146             -D "INPUT_FILE=${cppcheck_output_file}"
147             -D "REPORT_DIR=${report_dir}"
148             -D "SOURCE_DIR=${IE_MAIN_SOURCE_DIR}"
149             -D "TITLE=${CMAKE_PROJECT_NAME}"
150             -P "${IE_MAIN_SOURCE_DIR}/cmake/cpplint_html.cmake"
151         DEPENDS
152             "${cppcheck_output_file}"
153             "${IE_MAIN_SOURCE_DIR}/scripts/cppcheck-htmlreport.py"
154             "${IE_MAIN_SOURCE_DIR}/cmake/cpplint_html.cmake"
155         COMMENT
156             "[cpplint] Generate HTML report"
157         VERBATIM)
158
159     add_custom_target(cpplint_report
160         DEPENDS "${html_output_file}"
161         COMMENT "[cpplint] Generate report")
162 endfunction()