summaryrefslogtreecommitdiffstats
path: root/parrot-alchemy.el
blob: caa7caa5359825b595582ce4043038637ac7a625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
;;; -*- lexical-binding: t -*-

;; example of configuration:
;;
;; (with-eval-after-load 'parrot-alchemy
;;   (alchemy-generate-popup "delos-v3" "delos-v3" "~/.minidrones" ?d)
;;   (alchemy-generate-popup "jpsumo-evo" "jpsumo-evo" "~/.jpsumo-minidrones" ?j)
;;   (alchemy-generate-popup "powerup" "powerup-dev" "~/.powerup-minidrones" ?p)
;;   (alchemy-generate-popup "rsedu-v3" "delos-v3" "~/.rsedu-minidrones" ?r)
;;   (alchemy-generate-popup "chimera" "ardrone3-chimera" "~/.ardrone" ?c)
;;   (alchemy-generate-popup "evinrude" "evinrude" "~/.evinrude" ?e)
;;   (alchemy-generate-popup "bebop" "ardrone3-milos" "~/.ardrone" ?b))


(require 'subr-x) ; string-join

(defgroup parrot-alchemy nil
  "Controlling Alchemy from Emacs."
  :group 'parrot)

(defun alchemy-split-arguments (args)
  (let ((normal '())
        (env '()))
    (while args
      (push (car args) (if (string-match "=" (car args)) env normal))
      (setq args (cdr args)))
    (list normal env)))

(defun alchemy-generic= (s1 s2 sep)
  (let ((regex (concat "\\(.*\\)" sep))
        (m1 nil)
        (m2 nil))
    (when (string-match regex s1)
      (setq m1 (match-string 1 s1)))
    (when (string-match regex s2)
      (setq m2 (match-string 1 s2)))
    (and m1 m2 (string= m1 m2))))

(defun alchemy-option= (s1 s2)
  (alchemy-generic= s1 s2 " "))

(defun alchemy-env= (s1 s2)
  (alchemy-generic= s1 s2 "="))

(defun alchemy-arg= (s1 s2)
  (or (string= s1 s2)
      (alchemy-option= s1 s2)
      (alchemy-env= s1 s2)))

(defun alchemy-is-in-spec (elt spec)
  (seq-find (lambda (spec-elt)
              (alchemy-arg= elt spec-elt))
            spec))

(defun alchemy-remove-dup (common spec)
  (seq-filter (lambda (elt)
                (not (alchemy-is-in-spec elt spec)))
              common))

(defun alchemy-merge-arguments (name)
  (let* ((common-raw (alchemy-arguments))
         (spec-raw (funcall (intern (format "alchemy-%s-arguments" name))))
         (common-raw-no-dups (alchemy-remove-dup common-raw spec-raw))
         (common (alchemy-split-arguments common-raw-no-dups))
         (spec (alchemy-split-arguments spec-raw))
         (joined-normal (string-join (append (car common) (car spec)) " "))
         (joined-env (string-join (append (cadr common) (cadr spec)) " ")))
    (cons joined-normal joined-env)))

(defun alchemy-str-common (arg name product path)
  (let* ((args (alchemy-merge-arguments name))
         (normal (car args))
         (env (cdr args)))
    (format "cd %s && %s ./build.sh %s -p %s %s"
            path env normal product arg)))

(defun alchemy-compile (command name product path)
  (let ((compilation-buffer-name (format "*compilation-alchemy-%s*" name)))
    (when (get-buffer compilation-buffer-name)
      (kill-buffer compilation-buffer-name))
    (with-temp-buffer
      (compile (alchemy-str-common command name product path)))
    (with-current-buffer "*compilation*"
      (rename-buffer compilation-buffer-name))))

(defun alchemy-all-and-flash (name product path)
  (interactive)
  (alchemy-compile "-t all -t flash" name product path))

(defun alchemy-all (name product path)
  (interactive)
  (alchemy-compile "-t all" name product path))

(defun alchemy-build (name product path)
  (interactive)
  (alchemy-compile "-t build" name product path))

(defun alchemy-flash (name product path)
  (interactive)
  (alchemy-compile "-t flash" name product path))

(defun alchemy-xterm-cmd (name arg)
  (let* ((name-without-spaces (replace-regexp-in-string " " "-" name))
         (buffer-name (format "*XTerm-%s*" name-without-spaces)))
    (start-process-shell-command
     name nil (format "xterm -class %s -e \"%s\"" buffer-name arg))))

(defun alchemy-menuconfig (name product path)
  (interactive)
  (alchemy-xterm-cmd
   (format "%s-menuconfig" name)
   (alchemy-str-common "-t menuconfig" name product path)))

(defun alchemy-linux-menuconfig (name product path)
  (interactive)
  (alchemy-xterm-cmd
   (format "%s-linux-menuconfig" name)
   (alchemy-str-common "-t linux-menuconfig" name product path)))

(defun alchemy-sync (name path)
  (interactive)
  (oe-term
   (format "%s-sync" name) nil t
   (format "cd %s && repo sync" path)))

(defun alchemy-sync-d (name path)
  (interactive)
  (oe-term
   (format "%s-sync-d" name) nil t
   (format "cd %s && repo sync -d" path)))

(defun alchemy-image (name product path)
  (interactive)
  (alchemy-compile "-A image" name product path))

;; linux tags are updated with a separate command
;;   - to make the tag generation faster
;;   - to avoid wrong search results (because linux code is unrelated to
;;     userland code)
(defun alchemy-gtags (name product path)
  (interactive)
  (require 'ggtags)
  (require 'ag)
  (let* ((extra-paths
          (mapcar (lambda (str) (format str product))
                  '("out/%s/build/libARCommands/gen"
                    "out/%s/build/libARMavlink/generated"
                    "out/%s/build/libcivetweb"
                    "out/%s/build/libconfig"
                    "out/%s/build/json")))
         (exception-paths '("out"
                            "packages/linux_p6"
                            "packages/linux_p7"
                            "GPATH" "GRTAGS" "GTAGS"))
         (command (string-join
                   `(,(format "cd %s" path)
                     "&& rm -f GPATH GRTAGS GTAGS && ("
                     ,@(mapcar (lambda (extra-path)
                                 (format "%s --no-color -g %s;"
                                         ag-executable extra-path))
                               extra-paths)
                     ,(format "%s --no-color -g ." ag-executable)
                     ,@(mapcar (lambda (exception-path)
                                 (format "--ignore %s" exception-path))
                                exception-paths)
                     ,(format ") | tee /dev/tty | %s -v -f -"
                              (ggtags-program-path "gtags")))
                   " ")))
    (oe-term (format "%s-gtags" name) nil t command)))

(defun alchemy-linux-gtags (name path)
  (interactive)
  (let ((command
         (string-join
          (list
           (format "cd %s/packages/linux*" path)
           "&& rm -f GPATH GRTAGS GTAGS && gtags -v")
          " ")))
    (oe-term (format "%s-linux-gtags" name) nil t command)))

(defun alchemy-crashdump-to-links (_name product path)
  (interactive)
  (let* ((output-buffer (get-buffer-create "*crashdump*"))
         (regexp "  pc  \\(?1:[[:xdigit:]]\\{8\\}\\)  \\(?2:[^ ]*\\)")
         (staging (format "%s/out/%s/staging" path product))
         (org-link-format "[[file://%s::%s][%s:%s]]\n")
         (prefix-path (format "^%s/" path)))
    (with-current-buffer output-buffer (erase-buffer))
    (save-restriction
      (when (region-active-p)
        (narrow-to-region (region-beginning) (region-end)))
      (goto-char (point-min))
      (while (re-search-forward regexp nil t)
        (let* ((addr (match-string 1))
               (bin (concat staging (match-string 2)))
               (cmd (format "addr2line -e %s %s" bin addr))
               (raw-out (substring (shell-command-to-string cmd) 0 -1))
               (split-out (split-string raw-out ":"))
               (path (car split-out))
               (short-path (replace-regexp-in-string prefix-path "" path))
               (right (cadr split-out))
               (line (car (split-string right " ")))
               (out (format org-link-format path line short-path right)))
          (with-current-buffer output-buffer (insert out)))))
    (switch-to-buffer output-buffer))
  (org-mode))

(defvar *alchemy-path-relative-popups* nil
  "Association list from paths to alchemy popups, used to quickly
  open the right popup based on current directory.")

(defun alchemy-popup-dwim ()
  "Start the alchemy popup associated with current directory,
either default-directory or the buffer's file directory, when it
exists."
  (interactive)
  (if-let (path (or default-directory (buffer-file-name)))
    (let ((path (file-truename path)))
      (if-let (popup (cdr
                      (find-if (lambda (dir) (search dir path))
                               *alchemy-path-relative-popups*
                               :key (lambda (map) (file-truename (car map))))))
        (call-interactively popup)
        (message "No alchemy popup found for current directory")))
    (message "Cannot determine current directory")))

;; Here we use a macro because NAME needs to be known at compile time.
;;;###autoload
(defmacro alchemy-generate-popup (name product path key)
  (let ((popup-name (intern (concat "alchemy-" name "-popup"))))
    `(progn
       ;; Unbinding the symbol allows to redefine a popup upon
       ;; evaluation, because unfortunately (i) magit-define-popup
       ;; uses defvar, and (ii) eval-defun (C-M-x), which is supposed
       ;; to reset defvars, does not reach defvar forms inside progns.
       (when (boundp ',popup-name)
         (makunbound ',popup-name))
       ;; unlike pushnew, allow redefinitions
       (setf *alchemy-path-relative-popups*
             (cl-acons ,path
                       ',popup-name
                       (cl-remove (cons ',path ',popup-name)
				  *alchemy-path-relative-popups*
				  :test 'cl-equalp)))
       (magit-define-popup ,popup-name ,name
         :switches alchemy-specific-switches
         :options alchemy-specific-options
         :actions ',(alchemy-specific-actions name product path))
       (magit-define-popup-action 'alchemy-popup ,key ,name ',popup-name))))

(defcustom alchemy-arguments ()
  "Alchemy global arguments")

(defvar alchemy-common-options '((?j "jobs" "-j ")))
(defvar alchemy-common-switches '((?c "ccache" "USE_CCACHE=1")))
(defvar alchemy-common-actions '((?s "pdir search" pdir-search)))
(defvar alchemy-specific-switches '((?v "verbose" "V=1")
                                    (?c "clean" "-t clean")
                                    (?k "compile kernel" "-A linux")))
(defvar alchemy-specific-options '((?b "version" "-b ")
                                   (?A "alchemy" "-A ")
                                   (?j "jobs" "-j ")))

(defun alchemy-specific-actions (name product path)
  `((?B "all and flash" (lambda () (interactive) (alchemy-all-and-flash ,name ,product ,path)))
    (?b "build" (lambda () (interactive) (alchemy-build ,name ,product ,path)))
    (?a "all" (lambda () (interactive) (alchemy-all ,name ,product ,path)))
    (?f "flash" (lambda () (interactive) (alchemy-flash ,name ,product ,path)))
    (?m "menuconfig" (lambda () (interactive) (alchemy-menuconfig ,name ,product ,path)))
    (?l "linux menuconfig" (lambda () (interactive) (alchemy-linux-menuconfig ,name ,product ,path)))
    (?i "image" (lambda () (interactive) (alchemy-image ,name ,product ,path)))
    (?g "gtags" (lambda () (interactive) (alchemy-gtags ,name ,product ,path)))
    (?G "linux gtags" (lambda () (interactive) (alchemy-linux-gtags ,name ,path)))
    (?c "crashdump to links" (lambda () (interactive) (alchemy-crashdump-to-links ,name ,product ,path)))
    (?s "sync" (lambda () (interactive) (alchemy-sync ,name ,path)))
    (?S "sync -d" (lambda () (interactive) (alchemy-sync-d ,name ,path)))))

(require 'magit-popup)

;;;###autoload (autoload 'alchemy-popup "parrot-alchemy" "" t nil)
(magit-define-popup alchemy-popup
  "alchemy"
  :switches alchemy-common-switches
  :options alchemy-common-options
  :actions alchemy-common-actions)

(provide 'parrot-alchemy)