blob: 0a1ed1e9321b99a5119547d7350417585da44702 [file] [log] [blame]
Austin Schuh7400da02018-01-28 19:54:58 -08001# CLANG-TIDY AND CLANG-FORMAT
2##############################
3
4# Additional targets to perform clang-format/clang-tidy
5
6
7# function to exclude user-defined folders from the clang format- and tidy process
8function(filter_ct_directories allItems excludeDir)
9 foreach (TMP_PATH ${${allItems}})
10 if ("${TMP_PATH}" MATCHES ${excludeDir})
11 list (REMOVE_ITEM ${allItems} ${TMP_PATH})
12 endif()
13 endforeach(TMP_PATH)
14 set(${allItems} ${${allItems}} PARENT_SCOPE)
15endfunction(filter_ct_directories)
16
17function(ct_get_all_srcs ADD_HEADERS)
18
19 set(EXTENSIONS "*.cpp")
20 if (ADD_HEADERS)
21 list(APPEND EXTENSIONS "*.hpp" "*.h")
22 endif()
23
24 # Get all project files
25 file(GLOB_RECURSE
26 ALL_CXX_SOURCE_FILES
27 ${PROJECT_SOURCE_DIR}
28 ${EXTENSIONS}
29 )
30
31
32 # list "external" sources, to be excluded from the format- and tidy process:
33 filter_ct_directories(ALL_CXX_SOURCE_FILES "/external/") # excludes CppAD
34 filter_ct_directories(ALL_CXX_SOURCE_FILES ".tpl.cpp") # excludes CppAD
35 filter_ct_directories(ALL_CXX_SOURCE_FILES "/iit/rbd/") # excludes iit rbd folders
36 filter_ct_directories(ALL_CXX_SOURCE_FILES "/testIrb4600/generated/") # excludes generated code
37 filter_ct_directories(ALL_CXX_SOURCE_FILES "/testhyq/generated/") # excludes generated code
38 filter_ct_directories(ALL_CXX_SOURCE_FILES "/HyA/generated/") # excludes generated code
39 filter_ct_directories(ALL_CXX_SOURCE_FILES "/HyQ/generated/") # excludes generated code
40 filter_ct_directories(ALL_CXX_SOURCE_FILES "/QuadrotorWithLoad/generated/") # excludes generated code
41 filter_ct_directories(ALL_CXX_SOURCE_FILES "transform6d.cpp") #excludes generated inverse kinematics code
42 filter_ct_directories(ALL_CXX_SOURCE_FILES "/ikfast/ikfast.h") #excludes ik fast header
43
44 #message(FATAL_ERROR "sources list: ${ALL_CXX_SOURCE_FILES}")
45 set(ALL_CXX_SOURCE_FILES ${ALL_CXX_SOURCE_FILES} PARENT_SCOPE)
46endfunction()
47
48
49
50# Adding clang-format target if executable is found
51ct_get_all_srcs(TRUE)
52find_program(CLANG_FORMAT_BIN "clang-format")
53if(NOT CLANG_FORMAT_BIN)
54 find_program(CLANG_FORMAT_BIN "clang-format-3.9")
55endif()
56message(WARNING "CLANG FORMAT IS: " ${CLANG_FORMAT_BIN})
57if(NOT CLANG_FORMAT_BIN)
58 message (WARNING "CLANG-FORMAT not found. You can ignore this message if you are not a CT developer.")
59 add_custom_target(clang-format
60 COMMAND ${CMAKE_COMMAND} -E echo_append "clang-format executable not found"
61 VERBATIM)
62else()
63 message (WARNING "FOUND CLANG-FORMAT")
64 add_custom_target(
65 clang-format
66 COMMAND ${CLANG_FORMAT_BIN}
67 -i
68 -style=file
69 ${ALL_CXX_SOURCE_FILES}
70 VERBATIM
71 )
72endif()
73
74# Adding clang-tidy target if clang-tidy executable is found
75function(ct_configure_clang_tidy TIDY_INC_DIRS)
76
77 ct_get_all_srcs(FALSE)
78
79 set(CURRENT_INC_DIRS "")
80
81 #message(FATAL_ERROR "Inc dirs: ${${TIDY_INC_DIRS}}")
82
83 foreach (THIS_INC_DIR ${${TIDY_INC_DIRS}})
84 #message(WARNING "this inc dir: ${THIS_INC_DIR}")
85 list(APPEND CURRENT_INC_DIRS "-I${THIS_INC_DIR}")
86 endforeach()
87
88 #message(FATAL_ERROR "Current inc dirs: ${CURRENT_INC_DIRS}")
89
90 find_program(CLANG_TIDY_BIN "clang-tidy")
91 if(NOT CLANG_TIDY_BIN)
92 find_program(CLANG_TIDY_BIN "clang-tidy-3.9")
93 endif()
94 if(NOT CLANG_TIDY_BIN)
95 find_program(CLANG_TIDY_BIN "clang_tidy")
96 endif()
97 message(${CLANG_TIDY_BIN})
98 if(NOT CLANG_TIDY_BIN)
99 add_custom_target(clang-tidy
100 COMMAND ${CMAKE_COMMAND} -E echo_append "clang-tidy executable not found"
101 VERBATIM)
102 else()
103 message (WARNING "FOUND CLANG-TIDY")
104 set(CLANG_TIDY_COMMAND COMMAND ${CLANG_TIDY_BIN} ${ALL_CXX_SOURCE_FILES} -config='' -header-filter=\".*\\/ct\\/.*\" -- -std=c++11 -fopenmp ${CURRENT_INC_DIRS})
105
106 add_custom_target(
107 clang-tidy
108 COMMAND ${CLANG_TIDY_COMMAND}
109 COMMENT "Launching clang-tidy"
110 WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
111
112 )
113 endif() #CLANG_TIDY_BIN
114
115endfunction()