cmake.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright (C) 2012 Intel Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  16. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  20. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  22. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. """Supports checking WebKit style in cmake files.(.cmake, CMakeLists.txt)"""
  24. import re
  25. from common import TabChecker
  26. class CMakeChecker(object):
  27. """Processes CMake lines for checking style."""
  28. # NO_SPACE_CMDS list are based on commands section of CMake document.
  29. # Now it is generated from
  30. # http://www.cmake.org/cmake/help/v2.8.10/cmake.html#section_Commands.
  31. # Some commands are from default CMake modules such as pkg_check_modules.
  32. # Please keep list in alphabet order.
  33. #
  34. # For commands in this list, spaces should not be added it and its
  35. # parentheses. For eg, message("testing"), not message ("testing")
  36. #
  37. # The conditional commands like if, else, endif, foreach, endforeach,
  38. # while, endwhile and break are listed in ONE_SPACE_CMDS
  39. NO_SPACE_CMDS = [
  40. 'add_custom_command', 'add_custom_target', 'add_definitions',
  41. 'add_dependencies', 'add_executable', 'add_library',
  42. 'add_subdirectory', 'add_test', 'aux_source_directory',
  43. 'build_command',
  44. 'cmake_minimum_required', 'cmake_policy', 'configure_file',
  45. 'create_test_sourcelist',
  46. 'define_property',
  47. 'enable_language', 'enable_testing', 'endfunction', 'endmacro',
  48. 'execute_process', 'export',
  49. 'file', 'find_file', 'find_library', 'find_package', 'find_path',
  50. 'find_program', 'fltk_wrap_ui', 'function',
  51. 'get_cmake_property', 'get_directory_property',
  52. 'get_filename_component', 'get_property', 'get_source_file_property',
  53. 'get_target_property', 'get_test_property',
  54. 'include', 'include_directories', 'include_external_msproject',
  55. 'include_regular_expression', 'install',
  56. 'link_directories', 'list', 'load_cache', 'load_command',
  57. 'macro', 'mark_as_advanced', 'math', 'message',
  58. 'option',
  59. #From FindPkgConfig.cmake
  60. 'pkg_check_modules',
  61. 'project',
  62. 'qt_wrap_cpp', 'qt_wrap_ui',
  63. 'remove_definitions', 'return',
  64. 'separate_arguments', 'set', 'set_directory_properties', 'set_property',
  65. 'set_source_files_properties', 'set_target_properties',
  66. 'set_tests_properties', 'site_name', 'source_group', 'string',
  67. 'target_link_libraries', 'try_compile', 'try_run',
  68. 'unset',
  69. 'variable_watch',
  70. ]
  71. # CMake conditional commands, require one space between command and
  72. # its parentheses, such as "if (", "foreach (", etc.
  73. ONE_SPACE_CMDS = [
  74. 'if', 'else', 'elseif', 'endif',
  75. 'foreach', 'endforeach',
  76. 'while', 'endwhile',
  77. 'break',
  78. ]
  79. def __init__(self, file_path, handle_style_error):
  80. self._handle_style_error = handle_style_error
  81. self._tab_checker = TabChecker(file_path, handle_style_error)
  82. def check(self, lines):
  83. self._tab_checker.check(lines)
  84. self._num_lines = len(lines)
  85. for l in xrange(self._num_lines):
  86. self._process_line(l + 1, lines[l])
  87. def _process_line(self, line_number, line_content):
  88. if re.match('(^|\ +)#', line_content):
  89. # ignore comment line
  90. return
  91. l = line_content.expandtabs(4)
  92. # check command like message( "testing")
  93. if re.search('\(\ +', l):
  94. self._handle_style_error(line_number, 'whitespace/parentheses', 5,
  95. 'No space after "("')
  96. # check command like message("testing" )
  97. if re.search('\ +\)', l) and not re.search('^\ +\)$', l):
  98. self._handle_style_error(line_number, 'whitespace/parentheses', 5,
  99. 'No space before ")"')
  100. self._check_trailing_whitespace(line_number, l)
  101. self._check_no_space_cmds(line_number, l)
  102. self._check_one_space_cmds(line_number, l)
  103. self._check_indent(line_number, line_content)
  104. def _check_trailing_whitespace(self, line_number, line_content):
  105. line_content = line_content.rstrip('\n') # chr(10), newline
  106. line_content = line_content.rstrip('\r') # chr(13), carriage return
  107. line_content = line_content.rstrip('\x0c') # chr(12), form feed, ^L
  108. stripped = line_content.rstrip()
  109. if line_content != stripped:
  110. self._handle_style_error(line_number, 'whitespace/trailing', 5,
  111. 'No trailing spaces')
  112. def _check_no_space_cmds(self, line_number, line_content):
  113. # check command like "SET (" or "Set("
  114. for t in self.NO_SPACE_CMDS:
  115. self._check_non_lowercase_cmd(line_number, line_content, t)
  116. if re.search('(^|\ +)' + t.lower() + '\ +\(', line_content):
  117. msg = 'No space between command "' + t.lower() + '" and its parentheses, should be "' + t + '("'
  118. self._handle_style_error(line_number, 'whitespace/parentheses', 5, msg)
  119. def _check_one_space_cmds(self, line_number, line_content):
  120. # check command like "IF (" or "if(" or "if (" or "If ()"
  121. for t in self.ONE_SPACE_CMDS:
  122. self._check_non_lowercase_cmd(line_number, line_content, t)
  123. if re.search('(^|\ +)' + t.lower() + '(\(|\ \ +\()', line_content):
  124. msg = 'One space between command "' + t.lower() + '" and its parentheses, should be "' + t + ' ("'
  125. self._handle_style_error(line_number, 'whitespace/parentheses', 5, msg)
  126. def _check_non_lowercase_cmd(self, line_number, line_content, cmd):
  127. if re.search('(^|\ +)' + cmd + '\ *\(', line_content, flags=re.IGNORECASE) and \
  128. (not re.search('(^|\ +)' + cmd.lower() + '\ *\(', line_content)):
  129. msg = 'Use lowercase command "' + cmd.lower() + '"'
  130. self._handle_style_error(line_number, 'command/lowercase', 5, msg)
  131. def _check_indent(self, line_number, line_content):
  132. #TODO (halton): add indent checking
  133. pass