0

How do I create/issue compilation command on Emacs, so I don't need to switch back and forth between it and console? My usual compilation procedure I'd like to see as Emacs command:

$ export PATH=/toolchain/gcc-linaro-arm-linux-gnueabihf-4.7/bin:$PATH
$ cd my/project 
$ make CROSS_COMPILE=arm-linux-gnueabihf- all

I tried to make my own version of M-x compile command following instructions, but failed as I'm not familiar with Lisp and Emacs internals enough. Please note, that projects in question are big (i.e. kernel) with multi directories and Makefiles, so the approaches (closest Makefile, default directory etc.) described in the previous link are not a solution. Bonus points if you could bind it to a single key, like Fx key on modern IDEs.

P.S. I'm aware that there's similar question, but it's outdated and doesn't cover cross compile issue, I hope there's a better solution nowadays.

Community
  • 1
  • 1
KBart
  • 1,580
  • 10
  • 18

2 Answers2

2

Ctrl U Meta x compile should ask you what compilation command to use. You could type make CROSS_COMPILE=arm-linux-gnueabi all

Otherwise, configure your compilation-command Emacs variable, perhaps in your ~/.emacs; and you might make that a file-local variable.

You could put in your ~/.emacs the following untested lines

 (load-library "compile")
 (global-set-key [f12] 'recompile)
 (setq compilation-command "make CROSS_COMPILE=arm-linux-gnueabihf all")

BTW, your export PATH=... could be added e.g. in your ~/.bashrc, or you could have a shell script running that exact make with the appropriate PATH and have that script be your Emacs compilation-command (perhaps even "temporarily", i.e. just in your emacs session)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • As mentioned in question, I've tried playing with this command, but this solution still doesn't address few key questions: 1) how do I add toolchain? 2) It still requires a lot of typing and keypressing, the point is to have faster solution than switching to console, is it possible to set the default values (directory, make flags) or automate this process? – KBart Jan 21 '15 at 14:48
  • @KBart I think you just forget to use `M-x recompile` instead of `M-x compile` everytime you want to recompile. There should be no more typing. – pmr Jan 21 '15 at 15:39
  • @Basile Starynkevitch, it still doesn't add toolchain, so this function invokes system compiler. see legoscia's answer, it's almost correct, the only thing missing is correct $PATH. – KBart Jan 21 '15 at 16:03
  • Btw, adding toolchain to *~/.bashrc* is not an option as I use few different ones for different projects. – KBart Jan 22 '15 at 07:44
  • You could add some script invoking `make` and use that as a compilation command. – Basile Starynkevitch Jan 22 '15 at 08:07
2

You can create a custom function that runs a specific compile command in a specific directory, like this:

(defun my-compile ()
  (interactive)
  (let ((default-directory "~/my/project"))
    (compile "export PATH=/toolchain/gcc-linaro-arm-linux-gnueabihf-4.7/bin:$PATH && make CROSS_COMPILE=arm-linux-gnueabihf- all")))

And then bind it to some convenient key:

(global-set-key [f12] 'my-compile)
legoscia
  • 39,593
  • 22
  • 116
  • 167
  • Added these lines in ~/.emacs, but when I press F12 an error appears: *Wrong type argument: commandp, my-compile* – KBart Jan 21 '15 at 15:16
  • Right, I forgot `(interactive)`, which lets the function be bound to a key. See edit. – legoscia Jan 21 '15 at 15:35
  • *make* is called with correct parameters, but it cannot find toolchain. I've tried to add *setenv "PATH"* and *setq exec-path* to no avail. – KBart Jan 21 '15 at 16:05