-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhog.el
722 lines (606 loc) · 26.4 KB
/
hog.el
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
;;; hog.el --- Functions for working with Hog -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2021-2024 Andrew Peck
;; Author: Andrew Peck <peckandrew@gmail.com>
;; URL: https://github.com/andrewpeck/hog-emacs
;; Version: 0.1.0
;; Package-Requires: ((emacs "27.1"))
;; Keywords: tools vhdl fpga
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>
;;; Commentary:
;;
;; This extension facilitates the use of the FPGA build system Hog:
;;
;; https://hog.readthedocs.io
;;; Code:
;; TODO: parsing of ise projects
;; TODO: don't just dumbly error if the project isn't found
;; TODO: open should only find existing projects with XMLs
;; TODO: add a create-and-open command
(require 'xml)
(require 'subr-x)
(require 'json)
(require 'cl-lib)
(require 'thingatpt)
(defvar hog-vivado-path "/opt/Xilinx/Vivado/2021.1"
"Path to the Xilinx Vivado installation.
Can be set in dir-locals to be changed on a per-project basis.")
(defvar hog-number-of-jobs 4
"Number of jobs to use in Hog builds.")
(defvar hog-template-cache-dir user-emacs-directory
"Directory in which to cache Vivado templates.")
(defun hog--check-for-vivado ()
"Check to see if vivado exists at the specified path."
(when (not (file-exists-p hog-vivado-path))
(error (concat "Vivado not found at " hog-vivado-path))))
(defun hog--project-root ()
"Get the root of the current version controlled project."
(expand-file-name (or (vc-root-dir)
(locate-dominating-file default-directory ".git"))))
(defun hog--get-projects ()
"Get a list of available Hog projects."
;; convert the full directory into the path, e.g.
;; /home/topham/project/Top/myproject --> myproject
;; list all directories in the Top/ folder
(let ((hog-top-folder (concat (hog--project-root) "Top")))
(when (file-directory-p hog-top-folder)
(sort (thread-last (shell-command-to-string (format "find %s -name hog.conf -type f" hog-top-folder))
(replace-regexp-in-string (concat hog-top-folder "/") "")
(replace-regexp-in-string "/hog.conf" "")
(split-string)) #'string<))))
(defun hog--get-project-xml (project)
"Return the XML (XPR) file for a given Hog PROJECT."
(let* ((base (format "%sProjects/%s/%s" (hog--project-root) project project))
(xpr (format "%s.xpr" base))
(ppr (format "%s.ppr" base)))
(cond
((file-exists-p xpr) xpr)
((file-exists-p ppr) ppr)
(t (error (format "Project %s XML not found!" project))))))
(defun hog--get-project ()
"Interactively get a hog project."
(completing-read "Project: " (hog--get-projects) nil t))
(defun hog--get-project-and-do-lisp (fn)
"Interactively get a Hog project and call FN on it.
FN should be a function which take a project as an argument."
(let ((project (hog--get-project)))
(if (not (string-empty-p project))
(funcall fn project)
(message "You must specify a valid project!"))))
(defun hog--get-project-and-run-command (command)
"Interactively get a hog project and run a COMMAND on it."
(hog--get-project-and-do-lisp
(lambda (project) (hog--run-command command project))))
;;;###autoload
(defun hog-create-project ()
"Create a Hog project."
(interactive)
(hog--get-project-and-run-command "Hog/Do -recreate C"))
;;;###autoload
(defun hog-launch-synthesis ()
"Launch Hog Project Synthesis."
(interactive)
(hog--get-project-and-run-command (format "Hog/Do -njobs %d SYNTHESIS" hog-number-of-jobs)))
;;;###autoload
(defun hog-launch-implementation ()
"Launch Hog Project Implementation."
(interactive)
(hog--get-project-and-run-command (format "Hog/Do -njobs %d IMPLEMENT" hog-number-of-jobs)))
;;;###autoload
(defun hog-launch-workflow ()
"Launch Hog Project Workflow."
(interactive)
(hog--get-project-and-run-command (format "Hog/Do W -njobs %d" hog-number-of-jobs)))
;;;###autoload
(defun hog-open-project ()
"Open the Hog PROJECT."
;; TODO: check if the xml file exists, prompt to create if it doesn't
(hog--get-project-and-do-lisp
(lambda (project)
(progn
(hog--check-for-vivado)
(let ((project-file (hog--get-project-xml project)))
(when (not project-file)
(error (concat "No project file found for " project)))
(when (not (file-exists-p project-file))
(error (concat "Project file does not exist at " project-file)))
(let ((command (format "cd %s && source %s && vivado %s &"
(hog--project-root)
(concat hog-vivado-path "/settings64.sh")
project-file)))
(message (format "Opening Hog Project %s" project))
(call-process "bash" nil 0 nil "-c" command)))))))
(defun hog--run-command (command project &rest args)
"Run a Hog COMMAND for a given PROJECT.
colorize it using CCZE, with the Hog arguments ARGS."
(hog--check-for-vivado)
(let* ((name (format "%s" command))
(buf (format "*%s*" name)))
;; construct the output command
(let ((cmd-str (format "cd %s && source %s && %s | tee hog.log %s"
(hog--project-root) ;; cd %s
(concat hog-vivado-path "/settings64.sh") ;; source vivado
(concat
;; path/Hog/Launch{X}.sh project <args>
(hog--project-root) command " " project " " (string-join args " "))
;; optional ccze
(if (executable-find "ccze") " | ccze -A" ""))))
;; ... and run it
(compile cmd-str))
;; change the output buffer to a read-only, evil normal state buffer
(with-current-buffer buf (view-mode))))
;;--------------------------------------------------------------------------------
;; Intelligence for reading project xpr/ppr files
;;--------------------------------------------------------------------------------
(defun hog--read-lines-from-file (file-path)
"Return a list of lines of a file at FILE-PATH."
(with-temp-buffer
(insert-file-contents file-path)
(split-string (buffer-string) "\n" t)))
(defun hog--parse-vivado-xpr (project-file)
"Parse a Vivado XPR PROJECT-FILE into a list of libraries and their sources."
(let ((lib-list (list)))
(dolist (file-node
;; get a list of all the Project -> FileSets -> FileSet --> File nodes
(xml-get-children (assq 'FileSet (assq 'FileSets (assq 'Project (xml-parse-file project-file)))) 'File))
;; for each node, extract the path to the .src file
(let ((src-file
;; strip off the vivado relative path; make it relative to the repo root instead
;; (regexp-quote "$PPRDIR/../../")
(replace-regexp-in-string "$PPRDIR\/\.\.\/\.\.\/" "" (xml-get-attribute file-node 'Path ))))
;; for each node, extract the library property (only applies to vhdl sources)
(dolist (attr (xml-get-children (assq 'FileInfo (cdr file-node)) 'Attr))
(when (equal (xml-get-attribute attr 'Name) "Library")
(let ((lib (xml-get-attribute attr 'Val)))
(setf lib-list (hog--append-to-library lib-list lib src-file)))))))
lib-list))
(defun hog--parse-ise-ppr (project-file)
"Parse an ISE PPR PROJECT-FILE.
Parses the PPR file into a list of libraries and their sources."
;; FIXME need to parse the dang thing
project-file)
(defun hog--parse-xml (xml)
"Parse a Xilinx XML file into a list.
The resulting list is of the form:
((library1 (file1 file2 file3))
(library2 (file1 file2 file3)))"
(let* ((extension (file-name-extension xml)))
(cond ((string-equal extension "xpr")
(hog--parse-vivado-xpr xml))
((string-equal extension "ppr")
(hog--parse-ise-ppr xml)))))
(defun hog--parse-project-xml (project)
"Parse a PROJECT xml file into a list."
(hog--parse-xml (hog--get-project-xml project)))
(defun hog--append-to-library (src-list lib-name file-name)
"SRC-LIST LIB-NAME FILE-NAME."
(let ((lib (assoc lib-name src-list)))
(when (eq lib nil)
(setf src-list (append src-list (list (list lib-name (list)))))
(setq lib (assoc lib-name src-list)))
(setf (cadr lib) (append (cadr lib) (list file-name) )))
src-list)
(defvar hog-ieee-library
'("ieee" (
"/usr/local/lib/ghdl/src/synopsys/*.vhdl"
"/usr/local/lib/ghdl/src/std/v08/*.vhdl"
"/usr/local/lib/ghdl/src/ieee2008/*.vhdl"
"/usr/lib/ghdl/src/synopsys/*.vhdl"
"/usr/lib/ghdl/src/std/v08/*.vhdl"
"/usr/lib/ghdl/src/ieee2008/*.vhdl")))
(defvar hog-unisim-library
(list "unisim"
(list (format "%s/data/vhdl/src/unisims/unisim_VCOMP.vhd" hog-vivado-path))))
;;------------------------------------------------------------------------------
;; VHDL Tool YAML Config Generation
;;------------------------------------------------------------------------------
(defvar hog-vhdl-tool-preferences
'(("TypeCheck" . t)
("MultiLineErrors" . t)
("CheckOnChange" . t)
("Lint" . t)
("FirstSyntaxErrorOnly" . t)))
(defvar hog-vhdl-tool-lint-settings
'(("Threshold" ."Warning")
("DeclaredNotAssigned" .
(("enabled" . t)
("severity" . "Warning")))
("DeclaredNotRead" . t)
("ReadNotAssigned" . t)
("SensitivityListCheck" . t)
("ExtraSensitivityListCheck" . t)
("DuplicateSensitivity" . t)
("LatchCheck" . t)
("VariableNotRead" . t)
("PortNotRead" . t)
("PortNotWritten" . t)
("NoPrimaryUnit" . t)
("DuplicateLibraryImport" . t)
("DuplicatePackageUsage" . t)
("DeprecatedPackages" . t)
("ImplicitLibraries" . t)
("DisconnectedPorts" . t)
("IntNoRange" . t)))
;;;###autoload
(defun hog-vhdl-tool-create-project-yaml ()
"Create a VHDL-tool yaml file for a Hog PROJECT."
(hog--get-project-and-do-lisp
(lambda (project)
(with-temp-file (format "%s/vhdltool-config.yaml" (hog--project-root))
(progn
(insert
(json-encode
(list (cons 'Libraries
(mapcar (lambda (lib)
(list (cons 'name (car lib))
(cons 'paths (apply #'vector (cadr lib)))))
(append
(hog--parse-project-xml project)
(list hog-ieee-library)
(list hog-unisim-library))))
(cons 'Preferences
hog-vhdl-tool-preferences)
(cons 'Lint
hog-vhdl-tool-lint-settings)))))
(json-pretty-print-buffer)))))
;;------------------------------------------------------------------------------
;; VHDL LS TOML Project File Creation
;;------------------------------------------------------------------------------
(defun hog--vhdl-ls-lib-to-string (library)
"LIBRARY."
(let ((lib-name (car library))
(lib-files (car (cdr library)))
(pad " "))
(format "\n%s.files = [\n%s]\n"
lib-name
(string-join (mapcar (lambda (file)
(concat pad "\"" file "\",\n" )) lib-files)))))
(defun hog--vhdl-ls-parse-libs (libraries)
"LIBRARIES."
(let ((text "[libraries]\n"))
(setq libraries (append libraries (list hog-ieee-library)))
(setq libraries (append libraries (list hog-unisim-library)))
(dolist (library libraries)
;;(concat text (hog--vhdl-ls-lib-to-string library))
;;(print (concat text (hog--vhdl-ls-lib-to-string library)))
(setq text (concat text (hog--vhdl-ls-lib-to-string library))))
text))
;;;###autoload
(defun hog-vhdl-ls-create-project-toml ()
"Create a VHDL-ls yaml file for a Hog PROJECT."
(hog--get-project-and-do-lisp
(lambda (project)
(let ((yaml (hog--vhdl-ls-parse-libs (hog--parse-project-xml project))))
(shell-command (format "echo '%s' > %svhdl_ls.toml" yaml (hog--project-root)))))))
;;------------------------------------------------------------------------------
;; GHDL-LS JSON Project File Creation
;;------------------------------------------------------------------------------
(defvar hog--ghdl-ls-options
(let ((options (make-hash-table)))
(puthash 'ghdl_analysis
'("--workdir=work"
"--ieee=synopsys"
"-fexplicit"
"--warn-library"
"--warn-default-binding"
"--warn-binding"
"--warn-reserved"
"--warn-nested-comment"
"--warn-parenthesis"
"--warn-vital-generic"
"--warn-delayed-checks"
"--warn-body"
"--warn-specs"
"--warn-runtime-error"
"--warn-shared"
"--warn-hide"
"--warn-unused"
"--warn-others"
"--warn-pure"
"--warn-static"
"--std=08"
"-fexplicit")
options) options))
(defun hog--ghdl-ls-format-lib (lib)
""
(let ((libname (car lib))
(libfiles (cadr lib)))
(cl-flet ((format-lib-file (file)
(list `(file . ,file)
'(language . "vhdl")
`(library . ,libname))))
(mapcar #'format-lib-file libfiles))))
(defun hog--ghdl-ls-format-libs (libs)
""
(apply #'append
(mapcar #'hog--ghdl-ls-format-lib libs)))
(defun hog--ghdl-ls-make-config (libs)
""
(let ((config (make-hash-table)))
(puthash 'options hog--ghdl-ls-options config)
(puthash 'files (hog--ghdl-ls-format-libs libs) config)
config))
;;;###autoload
(defun hog-ghdl-ls-create-project-json ()
"Create GHDL-LS Json File."
(hog--get-project-and-do-lisp
(lambda (project)
(if (string-equal project "")
(message "You must specify a valid project!")
(let ((output-file (format "%shdl-prj.json" (hog--project-root)))
(config (hog--ghdl-ls-make-config
(append (list hog-unisim-library)
(hog--parse-project-xml project)))))
(with-temp-file output-file
(progn (insert (json-encode config))
(json-pretty-print-buffer))))))))
;;------------------------------------------------------------------------------
;; Hog Source File Mode
;;------------------------------------------------------------------------------
(defvar hog--src-property-re
(rx (seq (one-or-more " ")
(group (or "lib" "top"))
"="
(group (one-or-more nonl)))))
(defvar hog--file-name-re
(rx (seq
(? "#") ; optional comment
(submatch (* (any alphanumeric ?* ?_ ?- ?/ ?.))) ; file path
(? (+ " ")) ; optional whitespace
(submatch (* (any alphanumeric ?* ?_ ?- ?/ ?.)))))) ; file path
(defvar hog--src-symbols-list
'("locked" "93" "nosynth" "noimpl" "nosim" "source" "SystemVerilog"
"verilog_header" "XDC"))
(defun hog--get-link-at-point ()
"Return the link at point in a Hog src file."
(let ((inhibit-message t))
(let ((filename
(progn (thing-at-point-looking-at hog--file-name-re)
(match-string-no-properties 1))))
(if (and filename (not (string-empty-p filename)))
(concat (hog--project-root) filename)
(buffer-substring-no-properties (line-beginning-position) (line-end-position))))))
;;;###autoload
(defun hog-init-project ()
"Init a Hog project in the current directory."
(interactive)
(shell-command "git submodule add https://gitlab.com/hog-cern/Hog.git")
(when (not (file-exists-p ".gitignore"))
(shell-command "cp Hog/Templates/gitignore .gitignore"))
(when (not (file-exists-p ".gitlab-ci.yml"))
(shell-command "cp Hog/Templates/gitlab-ci.yml .gitlab-ci.yml"))
(when (not (file-exists-p "Top/project/hog.conf"))
(mkdir "Top/project" t)
(mkdir "Top/project/list" t)
(shell-command "cp Hog/Templates/hog_vivado.conf Top/project/hog.conf")))
;; FIXME: does not work with file names with spaces
;; spaces should be escaped
;;;###autoload
(defun hog-follow-link-at-point ()
"Follow the Hog source file at point."
(interactive)
(let ((file-with-path (hog--get-link-at-point)))
(when (file-exists-p file-with-path)
(find-file file-with-path))
;; check if the file contains a wildcard suffix, if so open it as a directory
(when (file-expand-wildcards file-with-path)
(find-file (file-name-directory
(car (file-expand-wildcards file-with-path)))))))
;;;###autoload
(defun hog-expand-glob-at-point ()
"Unglob a globbed entry in a source file.
When pointed at a globbed (wildcard) in a source file, this
function will unglob it and insert the explicit list of all files
in that path"
(interactive)
(save-excursion
(let* (;; Get the thing the cursor is looking at
(filename (progn (thing-at-point-looking-at hog--file-name-re)
(match-string-no-properties 1)))
;; Concat the absolute path onto the file name which is relative to
;; the root of the repo
(file-with-path (concat (hog--project-root) filename))
;; Expand the absolute path
(files (file-expand-wildcards file-with-path nil))
;; Converted the expanded absolute file names back to relative ones
(files-relative (mapcar (lambda (x) (replace-regexp-in-string (hog--project-root) "" x)) files))
;; Concate everything together into a string
(files-expanded (string-join files-relative "\n")))
(end-of-line)
(newline)
(insert files-expanded))))
(defvar hog-src-mode-map (make-sparse-keymap)
"Keymap for `hog-src-mode'.")
(define-key hog-src-mode-map (kbd "M-RET") #'hog-follow-link-at-point)
(define-generic-mode 'hog-src-mode
;; comment list
'("#")
;; keyword list
nil
;; font lock list:
`((,(regexp-opt hog--src-symbols-list 'symbols) . font-lock-keyword-face) ; symbol highlighting
(,hog--src-property-re . (1 font-lock-keyword-face)) ; lib=
(,hog--src-property-re . (2 font-lock-builtin-face)) ; =ieee
(,hog--file-name-re . (1 font-lock-string-face)) ; file name
(,hog--file-name-re . (2 font-lock-doc-face))) ; ipbus decoder file
;; auto mode list
'("\\.sim\\'" "\\.src\\'" "\\.con\\'" "\\.lst\\'")
;; function list
(list
(lambda ()
;; (modify-syntax-entry ?= ".")
(use-local-map hog-src-mode-map)))
;; docstring
"Major mode for Hog src files")
;;;###autoload
(defun hog-clean-vivado-xci ()
"Clean the output products of Vivado XCI Files."
(interactive)
(let ((file-count 0)
(file-size 0))
(let ((xci-files
(split-string
(shell-command-to-string
(concat "find " (hog--project-root) " -name *.xci")) "\n" t)))
(dolist (xci xci-files)
(let ((file-name-noext (file-name-sans-extension xci))
(dirname (file-name-directory xci)))
(let ((files-to-remove
(append
(mapcar (lambda (x) (concat file-name-noext x))
'(".dcp" ".veo" ".vho" ".xml" "_sim_netlist.v"
"_ooc.xdc" "_sim_netlist.vhdl" "_stub.v" "_stub.vhdl")))))
(dolist (file files-to-remove)
(when (file-exists-p file)
(setq file-count (+ 1 file-count))
(setq file-size (+ (file-attribute-size (file-attributes file)) file-size))
(princ (format "Removing %s\n" file))
(delete-file file))))
(let ((directories-to-remove
(append (mapcar (lambda (x) (concat dirname x))
'("hdl" "synth" "doc" "sim" "ila_v6_2")))))
(dolist (file directories-to-remove)
(when (file-exists-p file)
(setq file-count (+ (string-to-number (shell-command-to-string (concat "find " file " | wc -l"))) file-count))
(setq file-size (+ (string-to-number (car (split-string (shell-command-to-string (concat "du " file))))) file-size))
(princ (format "Removing %s\n" file))
(delete-directory file t)))))))
(princ (format "Removed %d files, %s"
file-count
(file-size-human-readable file-size)))))
;;------------------------------------------------------------------------------
;; Vivado Template Insertion
;;------------------------------------------------------------------------------
(cl-defun hog--vivado-collect-templates (nodes &key components parents)
"Collect a list of all Vivado templates from xml NODES.
It is called recursively, tracking the collective COMPONENTS and
the hierarchy of their PARENTS."
(let* ((children (xml-node-children nodes)))
(dolist (child children)
(when (listp child)
(let* ((child-name (xml-get-attribute child 'label))
(tree-type (xml-get-attribute child 'treetype))
(template-path
(if (string= tree-type "template")
(cons child-name parents) nil)))
(when template-path
(push (reverse template-path) components))
(let ((sub-components
(hog--vivado-collect-templates child
:components components
:parents (cons child-name parents))))
(when sub-components
(setq components sub-components)))))))
components)
(defun hog--walk-vivado-template-xml (FILE)
"Walks through a vivado XML template FILE and collects templates."
(hog--vivado-collect-templates
(assq 'RootFolder (xml-parse-file FILE))))
(defun hog--stringify-templates (templates)
"Stringifies a list of TEMPLATES.
It joins together the path into a single string with separated by arrows."
(mapcar (lambda (x) (string-join x " -> ")) templates))
(defun hog--template-cache (lang)
"Return the path of the hog cached template list for a given LANG."
(concat hog-template-cache-dir "/" (symbol-name lang) ".json"))
(defun hog--template-xml-path (lang)
"Return the path of the vivado xml template file for a given LANG."
(concat hog-vivado-path
"/data/parts/xilinx/templates/vivado/"
"/" (symbol-name lang) ".xml"))
(defun hog--get-templates (lang)
"Return the template list for a given LANG.
This uses either cached values stored in JSON, or creating the
JSON file if it does not exist."
;; if the cache file does not exist, create it
(when (not (file-exists-p (hog--template-cache lang)))
(with-temp-file (hog--template-cache lang)
(insert (json-encode
(cons "Templates" (hog--walk-vivado-template-xml
(hog--template-xml-path lang)))))))
;; else read the cache file
(let ((json-array-type 'list))
(cdr (json-read-file (hog--template-cache lang)))))
(defun hog--vivado-decend-template (nodes path)
"Return a single Vivado template text.
This walks through a collection of XML NODES, and finds the
template at a specific PATH."
(let ((children (xml-node-children nodes))
(template-content nil))
(dolist (child children)
(when path
(when (listp child)
(let ((child-name (xml-get-attribute child 'label))
(treetype (xml-get-attribute child 'treetype)))
(when (string= child-name (car path))
(setq template-content
(if (not (string= treetype "template"))
(hog--vivado-decend-template child (cdr path))
(car (last child)))))))))
template-content))
(defun hog--insert-template (lang)
"Insert a vivado template for a specific LANG."
(let ((template
(completing-read
"Template: "
(hog--stringify-templates (hog--get-templates lang)))))
(message (concat "Inserting: " template))
(let ((template-text
(hog--vivado-decend-template
(assq 'RootFolder (xml-parse-file (hog--template-xml-path lang)))
(split-string template " -> "))))
;; replace trailing tabs and insert the template
(insert (replace-regexp-in-string "[[:blank:]]*$" "" template-text)))))
;;;###autoload
(defun hog-insert-vhdl-template ()
"Insert a vivado vhdl template."
(interactive (hog--insert-template 'vhdl)))
;;;###autoload
(defun hog-insert-verilog-template ()
"Insert a vivado verilog template."
(interactive (hog--insert-template 'verilog)))
;;;###autoload
(defun hog-insert-xdc-template ()
"Insert a vivado XDC template."
(interactive (hog--insert-template 'xdc)))
;;;###autoload
(defun hog-insert-systemverilog-template ()
"Insert a vivado systemverilog template."
(interactive (hog--insert-template 'systemverilog)))
;;;###autoload
(defun hog-check-src-file ()
"Check a Hog source file for broken links."
(let ((errors 0))
(save-excursion
(goto-char (point-min))
(while (< (line-number-at-pos) (line-number-at-pos (point-max)))
;; skip comments
(when (not (string-match "^#.*"
(buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(let* ((link (hog--get-link-at-point)))
(when (and link
(not (string-empty-p link))
(not (file-expand-wildcards link))
(not (file-exists-p link)))
(setq errors (+ 1 errors))
(princ (format "Error:%d \"%s\" not found\n" (line-number-at-pos) link)))))
(forward-line))) errors))
(provide 'hog)
;;; hog.el ends here
;; LocalWords: xml vivado vhdl verilog systemverilog stringifies globbed unglob SRC