From af448464d15f60b87f1c9ef41a7816911c889459 Mon Sep 17 00:00:00 2001 From: Alexander Turenko <alexander.turenko@tarantool.org> Date: Wed, 11 Nov 2020 10:09:25 +0300 Subject: [PATCH] tools: fix luacheck invocation in different cases Now `make luacheck` gracefully handles different cases[^1]: in-source and out-of-source build (within the source tree or outside), current working directory as a real path or with symlink components. As result of looking into those problems I filed the issue [1] against luacheck. It seems, there are problems around absolute paths with symlinks components. [^1]: We have the similar problems with LuaJIT's luacheck rule. They will be fixed in a separate patch. [1]: https://github.com/mpeterv/luacheck/issues/208 Reviewed-by: Sergey Bronnikov <sergeyb@tarantool.org> Reviewed-by: Igor Munkin <imun@tarantool.org> --- CMakeLists.txt | 35 +++++++++++++++++++++++++++++++++-- cmake/utils.cmake | 28 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b9f4ec4656..feb56dfca0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,12 +158,43 @@ add_custom_target(ctags DEPENDS tags) # # Enable 'make luacheck' target. # - +# The code looks tricky, because of luacheck side problems +# (see [1]). +# +# The following circumstances may lead to missing of source files +# or exclusions: +# +# * Calling `luacheck "${dir}/.luacheckrc" "${dir}"` from +# outside of ${dir} or when ${dir} is not a real path. +# * Using of a path with symlink components in --exclude-files. +# +# We should exclude the build directory (when it is inside the +# source tree), at least because it contains LuaJIT's generated +# files and the temporary directory for testing (test/var). +# +# .luacheckrc provides corresponding exclusion rules for the +# in-source build case (`cmake .`). +# +# [1]: https://github.com/mpeterv/luacheck/issues/208 +# +set(EXCLUDE_FILES) +get_filename_component(BINARY_REALPATH "${PROJECT_BINARY_DIR}" REALPATH) +get_filename_component(SOURCE_REALPATH "${PROJECT_SOURCE_DIR}" REALPATH) +file_is_in_directory(BINARY_DIR_INSIDE_SOURCE_DIR "${BINARY_REALPATH}" + "${SOURCE_REALPATH}") +if (BINARY_DIR_INSIDE_SOURCE_DIR) + set(EXCLUDE_FILES --exclude-files "${BINARY_REALPATH}/**/*.lua") +endif() add_custom_target(luacheck DEPENDS LuaJIT-luacheck) add_custom_command(TARGET luacheck - COMMAND ${LUACHECK} --codes --config "${PROJECT_SOURCE_DIR}/.luacheckrc" "${PROJECT_SOURCE_DIR}" + COMMAND ${LUACHECK} --codes --config .luacheckrc . ${EXCLUDE_FILES} + WORKING_DIRECTORY ${SOURCE_REALPATH} COMMENT "Perform static analysis of Lua code" ) +unset(BINARY_REALPATH) +unset(SOURCE_REALPATH) +unset(BINARY_DIR_INSIDE_SOURCE_DIR) +unset(EXCLUDE_FILES) if (WITH_JEPSEN) ExternalProject_Add( diff --git a/cmake/utils.cmake b/cmake/utils.cmake index 6d69604682..ab64690de0 100644 --- a/cmake/utils.cmake +++ b/cmake/utils.cmake @@ -88,3 +88,31 @@ function(bin_source varname srcfile dstfile) endfunction() +# +# Whether a file is descendant to a directory. +# +# If the file is the directory itself, the answer is FALSE. +# +function(file_is_in_directory varname file dir) + file(RELATIVE_PATH file_relative "${dir}" "${file}") + + # Tricky point: one may find <STREQUAL ".."> and + # <MATCHES "^\\.\\./"> if-branches quite similar and coalesce + # them as <MATCHES "^\\.\\.">. However it'll match paths like + # "..." or "..foo/bar", whose are definitely descendant to + # the directory. + if (file_relative STREQUAL "") + # <file> and <dir> is the same directory. + set(${varname} FALSE PARENT_SCOPE) + elseif (file_relative STREQUAL "..") + # <dir> inside a <file> (so it is a directory too), not + # vice versa. + set(${varname} FALSE PARENT_SCOPE) + elseif (file_relative MATCHES "^\\.\\./") + # <file> somewhere outside of the <dir>. + set(${varname} FALSE PARENT_SCOPE) + else() + # <file> is descendant to <dir>. + set(${varname} TRUE PARENT_SCOPE) + endif() +endfunction() -- GitLab