blob: 8de49497035376f248f1961ce682602c6f7ce578 [file] [log] [blame]
swissChilid98781b2021-07-25 21:04:17 -07001(defun jmk-find-parent-makefile (p)
2 "Find the parent makefile of file P."
3 (let ((path (or p (expand-file-name "."))))
4 (while (not (or (string= path "/")
5 (file-exists-p (concat path "/Makefile"))))
6 (setq path (expand-file-name (concat path "/.."))))
7 (unless (string= path "/")
8 (concat path "/Makefile"))))
9
10(defun jmk-find-makefile (p)
11 "Find the makefile for the source file P.
12/a/b/src/hello.c -> /a/b/src/Makefile
13/a/b/include/hello.h -> /a/b/src/Makefile
14Returns nil if nothing can be found"
15 (if-let ((makefile (jmk-find-parent-makefile p)))
16 makefile
17 (jmk-find-parent-makefile (replace-regexp-in-string
18 (regexp-quote "include")
19 "src" p nil 'literal))))
20
21(defun jmk-arguments-for (p)
22 (if-let (path (jmk-find-makefile p))
23 (split-string (shell-command-to-string
24 (concat "make -f " path " show_cflags")) " ")
25 nil))
26
27(defun jmk-includes-for (p)
28 (let* ((args (jmk-arguments-for p))
29 (includes (cl-remove-if-not (lambda (arg)
30 (string-prefix-p "-I" arg))
31 args))
32 (names (mapcar (lambda (arg)
33 (string-trim (substring arg 2)))
34 includes)))
35 (message "includes: %s" names)
36 names))
37