cpplint.cmake 2.5 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# util to check C++ file style
# * it basically use google cpplint.py.
# * It provide "add_style_check_target" for cmake.
#   Usage see add_style_check_target's document
#
# TODO(yuyang18): Add python style check.

set(STYLE_FILTER)

# diable unwanted filters

# paddle do not indent public/potected/private in class
set(STYLE_FILTER "${STYLE_FILTER}-whitespace/indent,")
# paddle use mutable reference. BUT IT IS NOT RECOMMANDED
set(STYLE_FILTER "${STYLE_FILTER}-runtime/references,")
# paddle use relative path for include.
set(STYLE_FILTER "${STYLE_FILTER}-build/include,")
# paddle use <thread>, <mutex>, etc.
set(STYLE_FILTER "${STYLE_FILTER}-build/c++11,")
# paddle use c style casting. BUT IT IS NOT RECOMMANDED
set(STYLE_FILTER "${STYLE_FILTER}-readability/casting")


# IGNORE SOME FILES
set(IGNORE_PATTERN
    .*ImportanceSampler.*
    .*cblas\\.h.*
D
dongzhihong 已提交
28
    .*\\.pb\\.txt
Z
zhangjinchao01 已提交
29
    .*LtrDataProvider.*
L
liaogang 已提交
30 31
    .*MultiDataProvider.*
    .*pb.*)
Z
zhangjinchao01 已提交
32 33 34 35 36 37 38

# add_style_check_target
#
# attach check code style step for target.
#
# first argument: target name to attach
# rest arguments: source list to check code style.
X
xuwei06 已提交
39
#
Z
zhangjinchao01 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
# NOTE: If WITH_STYLE_CHECK is OFF, then this macro just do nothing.
macro(add_style_check_target TARGET_NAME)
    if(WITH_STYLE_CHECK)
        set(SOURCES_LIST ${ARGN})
        list(REMOVE_DUPLICATES SOURCES_LIST)
        list(SORT SOURCES_LIST)

        foreach(filename ${SOURCES_LIST})
            set(LINT ON)
            foreach(pattern ${IGNORE_PATTERN})
                if(filename MATCHES ${pattern})
                    message(STATUS "DROP LINT ${filename}")
                    set(LINT OFF)
X
xuwei06 已提交
53
                endif()
Z
zhangjinchao01 已提交
54 55
            endforeach()
            if(LINT MATCHES ON)
L
liaogang 已提交
56
                # cpplint code style
X
xuwei06 已提交
57 58
                get_filename_component(base_filename ${filename} NAME)
                set(CUR_GEN ${CMAKE_CURRENT_BINARY_DIR}/${base_filename}.cpplint)
X
xuwei06 已提交
59
                add_custom_command(OUTPUT ${CUR_GEN} PRE_BUILD
L
liaogang 已提交
60 61 62
                    COMMAND "${PYTHON_EXECUTABLE}" "${PROJ_ROOT}/paddle/scripts/cpplint.py"
                            "--filter=${STYLE_FILTER}"
                            "--write-success=${CUR_GEN}" ${filename}
X
xuwei06 已提交
63
                    DEPENDS ${filename} ${PROJ_ROOT}/paddle/scripts/cpplint.py
64
                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
X
xuwei06 已提交
65 66
                add_custom_target(${base_filename}.cpplint DEPENDS ${CUR_GEN})
                add_dependencies(${TARGET_NAME} ${base_filename}.cpplint)
Z
zhangjinchao01 已提交
67 68 69 70
            endif()
        endforeach()
    endif()
endmacro()