在内核源代码树上使用flycheck / flymake

时间:2015-04-17 21:49:07

标签: linux emacs flycheck

是否有一种简单/自动的方式来配置flycheck或flymake 在文件上写入时显示错误注释 在linux内核源代码树中?假设我正在努力 fs / proc / cmdline.c我想让flycheck下来 两个目录并执行“make fs / proc / cmdline.o”和 然后注释结果。假设ARCH和CROSS_COMPILE 在外部设置。

3 个答案:

答案 0 :(得分:3)

我一直在考虑自己这样做 - 这就是我所拥有的:

您需要找到内核源代码树的基础,因此查找flymake的默认Makefile交易会适得其反。我们将添加我们自己的文件,该文件可以用于查找源代码库,以及包装正常的内核makefile:

将文件flymake.mk添加到内核源代码树的基础(根据您自己的交叉编译需求进行配置):

ARCH=mips
CROSS_COMPILE=mips-linux-gnu-

export ARCH
export CROSS_COMPILE

.PHONY: check-syntax
check-syntax:
    make -f Makefile $(patsubst %_flymake.o,%.o,$(patsubst %.c,%.o,$(CHK_SOURCES)))

要点是删除“_flymake.c”并仅编译真实文件,并构建真实文件,而不仅仅是语法。这可以避免我们意外创建file_flymake.o

我们需要说服flymake查找flymake.mk并对其运行check-syntax - 将这些添加到您的.emacs:

;; Build a custom command-line using flymake.mk
(defun flymake-get-kernel-make-cmdline (source base-dir)
  (list "make"
    (list "-s"
              "-f"
              "flymake.mk"
          "-C"
          base-dir
          (concat "CHK_SOURCES=" source)
          "SYNTAX_CHECK_MODE=1"
          "check-syntax")))

;; Search for flymake.mk to locate kernel tree base
(defun flymake-kernel-make-init ()
  (flymake-simple-make-init-impl 'flymake-create-temp-inplace t t "flymake.mk" 'flymake-get-kernel-make-cmdline))

;; Register against .c files under /linux/ or /kernel/
;; Since the list is parsed in order use `push`
(push '(".+/\\(linux\\|kernel\\)/.+\\.c$" flymake-kernel-make-init) flymake-allowed-file-name-masks)

限制:

  • 无法解析头文件
  • 无法解析flymake临时文件source_flymake.c(确保在运行已保存的文件之前忽略flymake标记)。我有一个强力重新运行flymake的按键。
  • 外部模块中不支持flymake
  • 需要预先注册路径匹配器(参见上面的push行) - 我对flymake不太了解,允许它将一个缓冲区覆盖为一次性。

通过修补内核Makefile本身可以克服头,临时和外部模块的限制,现在我想避免使用。

答案 1 :(得分:1)

类似于Gregs flymake片段,这里有一个我想出的flycheck:

(defun utils/flycheck-search-linux-makefile ()
  "Search for linux top `Makefile' "
  (labels
      ((find-makefile-file-r (path)
        (let* ((parent (file-name-directory path))
               (file (concat parent "Makefile")))
          (cond
           ((file-exists-p file)
            (progn
              (with-temp-buffer
                (insert-file-contents file)
                (if (string-match "VERSION = [0-9]+[[:space:]]*PATCHLEVEL" (buffer-string))
                    (throw 'found-it parent)
                  (find-makefile-file-r (directory-file-name parent))
                  ))))
           ((equal path parent) (throw 'found-it nil))
           (t (find-makefile-file-r (directory-file-name parent)))))))
    (if (buffer-file-name)
        (catch 'found-it
          (find-makefile-file-r (buffer-file-name)))
      (error "buffer is not visiting a file"))))

(flycheck-define-checker utils/flycheck-linux-makefile-checker
  "Linux source checker"
  :command
  (
   "make" "C=1" "-C" (eval (utils/flycheck-search-linux-makefile))
   (eval (concat (file-name-sans-extension (file-relative-name buffer-file-name (utils/flycheck-search-linux-makefile))) ".o"))
   )
  :error-patterns
  ((error line-start
          (message "In file included from") " " (file-name) ":" line ":"
          column ":"
      line-end)
   (info line-start (file-name) ":" line ":" column
         ": note: " (message) line-end)
   (warning line-start (file-name) ":" line ":" column
            ": warning: " (message) line-end)
   (error line-start (file-name) ":" line ":" column
          ": " (or "fatal error" "error") ": " (message) line-end))
  :error-filter
  (lambda (errors)
    (let ((errors (flycheck-sanitize-errors errors)))
      (dolist (err errors)
        (let* ((fn (flycheck-error-filename err))
               (rn0 (file-relative-name fn default-directory)) ; flycheck-fix-error-filename converted to absolute, revert                                                                                  
               (rn1 (expand-file-name rn0 (utils/flycheck-search-linux-makefile))) ; make absolute relative to "make -C dir"                                                                                
               (ef (file-relative-name rn1 default-directory)) ; relative to source                                                                                                                         
               )
          (setf (flycheck-error-filename err) ef)
          )))
    errors)
  :modes (c-mode c++-mode)
  )

(defun utils/flycheck-mode-hook ()
  "Flycheck mode hook."
  (make-variable-buffer-local 'flycheck-linux-makefile)
  (setq flycheck-linux-makefile (utils/flycheck-search-linux-makefile))
  (if flycheck-linux-makefile
      (flycheck-select-checker 'utils/flycheck-linux-makefile-checker))
  )

(add-hook 'flycheck-mode-hook 'utils/flycheck-mode-hook)
  • utils / flycheck-search-linux-makefile:这会搜索包含VERSION =的顶级linux Makefile。... PATCHLEVEL = ...
  • utils / flycheck-linux-makefile-checker:flycheck检查器,它使用“make”“C = 1”“ - c”“......”需要安装稀疏

不完美但可用。

答案 2 :(得分:1)

我使用的是flymake.mk makefile,如

## case 2: called within docker and cross env
ifeq (${_FLYMAKE_WRAPPED},)

_c_sources := $(filter %.c,$(CHK_SOURCES))
_h_sources := $(filter %.h,$(CHK_SOURCES))

_make_wrapped = $(MAKE) \
        kbuild-file=$(abspath $(firstword ${MAKEFILE_LIST})) \
        _FLYMAKE_WRAPPED=$1 _FLYMAKE_TYPE=$2 M=$(dir $*) $3

check-syntax:   $(addprefix .check-syntax_,${_c_sources} ${_h_sources})

$(addprefix .check-syntax_,${_c_sources} ${_h_sources}):.check-syntax_%:
        +$(call _make_wrapped,2,c)

## case 3: checking the files
else ifeq (${_FLYMAKE_WRAPPED},2)

-include $(dir ${CHK_SOURCES})/Makefile
-include $(dir ${CHK_SOURCES})/Kbuild

# Reset object/module list
obj-y :=
obj-m :=
lib-y :=
lib-m :=
always :=
targets :=
subdir-y :=
subdir-m :=

__build: check-syntax
.PHONY: check-syntax

clean_checkflags = $(filter-out -M% -g% -Wp,-M%,$1)

check-syntax: ${CHK_SOURCES}
        $(CC) $(call clean_checkflags,$(c_flags)) -c -o /dev/null -S $<

endif

它既可以使用头文件,也可以使用临时的flymake文件。