Decouple CMake support from C-C++ Layer

This commit is contained in:
Alexander Dalshov 2018-01-20 19:08:51 +03:00 committed by syl20bnr
parent 617d63095a
commit 8bc08ade89
7 changed files with 131 additions and 69 deletions

View File

@ -1,7 +1,6 @@
#+TITLE: C/C++ layer
[[file:img/ccpp.jpg]]
[[file:img/cmake.png]]
* Table of Contents :TOC_4_gh:noexport:
- [[#description][Description]]
@ -12,7 +11,6 @@
- [[#clang-configuration][Clang Configuration]]
- [[#clang-format][clang-format]]
- [[#company-clang-and-flycheck][Company-clang and flycheck]]
- [[#cmake-configuration][CMake configuration]]
- [[#rtags-configuration][RTags configuration]]
- [[#enable-google-set-c-style][Enable google-set-c-style]]
- [[#key-bindings][Key Bindings]]
@ -21,8 +19,7 @@
- [[#rtags][RTags]]
* Description
This layer adds configuration for C/C++ language as well support for [[https://cmake.org/][CMake]]
scripts.
This layer adds configuration for C/C++ language.
** Features:
- Support syntax checking via flycheck with Clang.
@ -39,10 +36,7 @@ scripts.
company-clang (when =c-c++-enable-clang-support= is turned on), or
company-ycmd (when =ycmd= layer is included).
- Support for [[https://github.com/realgud/realgud][realgud]] debugger.
- Support for [[https://github.com/Andersbakken/rtags][rtags]]
- Support for CMake configure/build (with limited support for other build systems),
automatic generation of =compile_commands.json= (compile flags), on-the-fly configuration
of flycheck, company-clang and RTags (if installed) with [[https://github.com/atilaneves/cmake-ide][cmake-ide]] .
- Support for [[https://github.com/Andersbakken/rtags][rtags]].
* Install
** Layer
@ -100,33 +94,6 @@ Not only does this allow proper autocomplete on projects with extra includes and
flags, but there is also support for flycheck so that it doesn't complain about
missing header files.
** CMake configuration
To enable CMake projects support set the layer variable =c-c++-enable-cmake-ide-support=
to =t= in the dotfile:
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
'((c-c++ :variables c-c++-enable-cmake-ide-support t)))
#+END_SRC
=cmake-ide= plugin has several useful configuration options.
To configure project you need to create =.dir-locals.el= file. In case of using
make as CMake backend you can use =helm-make= to select required build target.
Here is a sample configuration. This configuration forces =cmake-ide= to use the
local directory and pass that directory to =helm-make=. Such config allows to
build your project with ~SPC c c~ key binding.
#+BEGIN_SRC emacs-lisp
((nil .
((cmake-ide-project-dir . "~/Project")
(cmake-ide-build-dir . "~/Project/build")
(cmake-ide-cmake-opts . "-DCMAKE_BUILD_TYPE=Debug")
(helm-make-build-dir . "build")
(helm-make-arguments . "-j7"))))
#+END_SRC
** RTags configuration
To enable support for =rtags=, set the layer variable
=c-c++-enable-rtags-support= to =t= in your dotfile.
@ -168,18 +135,14 @@ set that up like this:
* Key Bindings
| Key Binding | Description |
|-------------+-------------------------------------------------------------------------|
| ~SPC m g a~ | open matching file |
| | (e.g. switch between .cpp and .h, requires a project to work) |
| ~SPC m g A~ | open matching file in another window |
| | (e.g. switch between .cpp and .h, requires a project to work) |
| ~SPC m D~ | disaster: disassemble c/c++ code |
| ~SPC m r~ | srefactor: refactor thing at point. |
| ~SPC m p c~ | Run CMake and set compiler flags for auto-completion and flycheck |
| ~SPC m p C~ | Run CMake if compilation database JSON file is not found |
| ~SPC m p d~ | Remove file connected to current buffer and kill buffer, then run CMake |
| ~SPC m c c~ | Compile project |
| Key Binding | Description |
|-------------+---------------------------------------------------------------|
| ~SPC m g a~ | open matching file |
| | (e.g. switch between .cpp and .h, requires a project to work) |
| ~SPC m g A~ | open matching file in another window |
| | (e.g. switch between .cpp and .h, requires a project to work) |
| ~SPC m D~ | disaster: disassemble c/c++ code |
| ~SPC m r~ | srefactor: refactor thing at point. |
*Note:* [[https://github.com/tuhdo/semantic-refactor][semantic-refactor]] is only available for Emacs 24.4+.

View File

@ -31,9 +31,6 @@
(defvar c-c++-enable-rtags-support nil
"If non nil Rtags related packages and configuration are enabled.")
(defvar c-c++-enable-cmake-ide-support nil
"If non nil CMake related packages and configuration are enabled.")
(defvar c-c++-enable-clang-format-on-save nil
"If non-nil, automatically format code with ClangFormat on
save. Clang support has to be enabled for this to work.")

View File

@ -14,8 +14,6 @@
cc-mode
disaster
clang-format
cmake-ide
cmake-mode
company
(company-c-headers :requires company)
(company-rtags :requires company rtags)
@ -81,23 +79,6 @@
"==" 'spacemacs/clang-format-region-or-buffer
"=f" 'spacemacs/clang-format-function)))))
(defun c-c++/init-cmake-ide ()
(use-package cmake-ide
:if c-c++-enable-cmake-ide-support
:config
(progn
(cmake-ide-setup)
(dolist (mode c-c++-modes)
(spacemacs/set-leader-keys-for-major-mode mode
"cc" 'cmake-ide-compile
"pc" 'cmake-ide-run-cmake
"pC" 'cmake-ide-maybe-run-cmake
"pd" 'cmake-ide-delete-file)))))
(defun c-c++/init-cmake-mode ()
(use-package cmake-mode
:mode (("CMakeLists\\.txt\\'" . cmake-mode) ("\\.cmake\\'" . cmake-mode))))
(defun c-c++/post-init-company ()
(when (configuration-layer/package-used-p 'cmake-mode)
(spacemacs|add-company-backends :backends company-cmake :modes cmake-mode))

View File

@ -0,0 +1,61 @@
#+TITLE: CMake layer
[[file:img/cmake.png]]
* Table of Contents :TOC_4_gh:noexport:
- [[#description][Description]]
- [[#features][Features:]]
- [[#install][Install]]
- [[#layer][Layer]]
- [[#cmake-ide-configuration][CMake-ide configuration]]
- [[#key-bindings][Key Bindings]]
* Description
This layer adds support [[https://cmake.org/][CMake]] scripts.
** Features:
- Support for CMake configure/build (with limited support for other build systems),
automatic generation of =compile_commands.json= (compile flags), on-the-fly configuration
of flycheck, company-clang and RTags (if installed) with [[https://github.com/atilaneves/cmake-ide][cmake-ide]] .
- Run selected test using =Helm= interface via =helm-ctest=.
* Install
** Layer
To use this configuration layer, add it to your =~/.spacemacs=. You will need to
add =cmake= to the existing =dotspacemacs-configuration-layers= list in this
file.
** CMake-ide configuration
To enable CMake projects support set the layer variable =cmake-enable-cmake-ide-support=
to =t= in the dotfile:
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
'((cmake :variables cmake-enable-cmake-ide-support t)))
#+END_SRC
=cmake-ide= plugin has several useful configuration options.
To configure project you need to create =.dir-locals.el= file. In case of using
make as CMake backend you can use =helm-make= to select required build target.
Here is a sample configuration. This configuration forces =cmake-ide= to use the
local directory and pass that directory to =helm-make=. Such config allows to
build your project with ~SPC c c~ key binding.
#+BEGIN_SRC emacs-lisp
((nil .
((cmake-ide-project-dir . "~/Project")
(cmake-ide-build-dir . "~/Project/build")
(cmake-ide-cmake-opts . "-DCMAKE_BUILD_TYPE=Debug")
(helm-make-build-dir . "build")
(helm-make-arguments . "-j7"))))
#+END_SRC
* Key Bindings
| Key Binding | Description |
|-------------+-------------------------------------------------------------------------|
| ~SPC m p c~ | Run CMake and set compiler flags for auto-completion and flycheck |
| ~SPC m p C~ | Run CMake if compilation database JSON file is not found |
| ~SPC m p d~ | Remove file connected to current buffer and kill buffer, then run CMake |
| ~SPC m p t~ | Run CTest |

View File

@ -0,0 +1,18 @@
;;; config.el --- CMake Layer config File for Spacemacs
;;
;; Copyright (c) 2018 Sylvain Benner & Contributors
;;
;; Author: Alexander Dalshov <dalshov@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
;; variables
(defconst cmake-modes '(c-mode c++-mode cmake-mode)
"Primary major modes where `cmake-ide' could be used.")
(defvar cmake-enable-cmake-ide-support nil
"If non nil CMake related packages and configuration are enabled.")

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -0,0 +1,42 @@
;;; packages.el --- CMake layer packages fuke for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Alexander Dalshov <dalshov@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(setq cmake-packages
'(
cmake-mode
cmake-ide
(helm-ctest :requires helm)
))
(defun cmake/init-cmake-ide ()
(use-package cmake-ide
:if cmake-enable-cmake-ide-support
:config
(progn
(cmake-ide-setup)
(dolist (mode cmake-modes)
(spacemacs/declare-prefix-for-mode mode "mc" "compile")
(spacemacs/declare-prefix-for-mode mode "mp" "project")
(spacemacs/set-leader-keys-for-major-mode mode
"cc" 'cmake-ide-compile
"pc" 'cmake-ide-run-cmake
"pC" 'cmake-ide-maybe-run-cmake
"pd" 'cmake-ide-delete-file)))))
(defun cmake/init-cmake-mode ()
(use-package cmake-mode
:mode (("CMakeLists\\.txt\\'" . cmake-mode) ("\\.cmake\\'" . cmake-mode))))
(defun cmake/init-helm-ctest ()
(use-package helm-ctest
:config
(dolist (mode cmake-modes)
(spacemacs/set-leader-keys-for-major-mode mode
"pt" 'helm-ctest))))