swissChili | d98781b | 2021-07-25 21:04:17 -0700 | [diff] [blame] | 1 | (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 |
| 14 | Returns 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 | |
swissChili | 480f176 | 2021-07-26 22:17:34 -0700 | [diff] [blame] | 38 | (defun jmk-other-flags-for (p) |
| 39 | (let* ((args (jmk-arguments-for p)) |
| 40 | (not-includes (cl-remove-if-not (lambda (arg) |
| 41 | (string-prefix-p "-I" arg)) |
| 42 | args)) |
| 43 | (stripped (mapcar #'string-trim not-includes))) |
| 44 | stripped)) |
| 45 | |