]> git.eshelyaron.com Git - emacs.git/commitdiff
New function internal--c-header-file-path
authorStefan Kangas <stefankangas@gmail.com>
Mon, 6 Jan 2025 09:08:01 +0000 (10:08 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 8 Jan 2025 08:45:42 +0000 (09:45 +0100)
It is not clear to me where this function properly belongs, so let's put
it in subr.el for now.  This avoids code duplication without introducing
a dependency between man and ffap.  It can always be moved later.

* lisp/subr.el (internal--c-header-file-path): New function.
* lisp/man.el (Man-header-file-path):
* lisp/ffap.el (ffap-c-path): Use above new function.

* test/lisp/subr-tests.el (ert-x): Require.
(subr-tests-internal--c-header-file-path)
(subr-tests-internal--c-header-file-path/gcc-mocked): New tests.

(cherry picked from commit 888ff3755d499ca74f9d8cdf9e4d2c7dc0331236)

lisp/man.el
lisp/subr.el
test/lisp/subr-tests.el

index 25a4ccee897ef53f852d6d320131143783e7ed1e..bafb4d20f68f40778f5fdd233a99f506f3676fd9 100644 (file)
@@ -225,17 +225,7 @@ the associated section number."
   :type '(repeat (cons (string :tag "Bogus Section")
                       (string :tag "Real Section"))))
 
-(defcustom Man-header-file-path
-  (let ((arch (with-temp-buffer
-                (when (eq 0 (ignore-errors
-                              (call-process "gcc" nil '(t nil) nil
-                                            "-print-multiarch")))
-                  (goto-char (point-min))
-                  (buffer-substring (point) (line-end-position)))))
-        (base '("/usr/include" "/usr/local/include")))
-    (if (zerop (length arch))
-        base
-      (append base (list (expand-file-name arch "/usr/include")))))
+(defcustom Man-header-file-path (internal--c-header-file-path)
   "C Header file search path used in Man."
   :version "24.1"                       ; add multiarch
   :type '(repeat string))
index a810c3940eab9be45b97a79c637b24c397dac22e..f1bf4a4e671545f86a05190c54712982066efa2d 100644 (file)
@@ -7077,4 +7077,16 @@ that does the same thing as `caadr'."
 
 (defun evenp (integer) "Return t if INTEGER is even." (eq (logand integer 1) 0))
 
+(defun internal--c-header-file-path ()
+  "Return search path for C header files (a list of strings)."
+  (let ((arch (with-temp-buffer
+                (when (eq 0 (ignore-errors
+                              (call-process "gcc" nil '(t nil) nil
+                                            "-print-multiarch")))
+                  (goto-char (point-min))
+                  (buffer-substring (point) (line-end-position)))))
+        (base '("/usr/include" "/usr/local/include")))
+    (if (seq-empty-p arch) base
+      (append base (list (expand-file-name arch "/usr/include"))))))
+
 ;;; subr.el ends here
index e6964c42ca89aa2cb672ee6e9aee8a18c061219e..2cb0b616074e90e5dd2e59c6115e25c0351f2550 100644 (file)
@@ -27,6 +27,7 @@
 
 ;;; Code:
 (require 'ert)
+(require 'ert-x)
 (eval-when-compile (require 'cl-lib))
 
 (ert-deftest let-when-compile ()
@@ -1382,5 +1383,32 @@ final or penultimate step during initialization."))
                  (props-out (object-intervals out)))
             (should (equal props-out props-in))))))))
 
+(ert-deftest subr-tests-internal--c-header-file-path ()
+  (should (seq-every-p #'stringp (internal--c-header-file-path)))
+  (should (member "/usr/include" (internal--c-header-file-path)))
+  (should (equal (internal--c-header-file-path)
+                 (delete-dups (internal--c-header-file-path))))
+  ;; Return a meaningful result even if calling some compiler fails.
+  (cl-letf (((symbol-function 'call-process)
+             (lambda (_program &optional _infile _destination _display &rest _args) 1)))
+    (should (seq-every-p #'stringp (internal--c-header-file-path)))
+    (should (member "/usr/include" (internal--c-header-file-path)))
+    (should (equal (internal--c-header-file-path)
+                   (delete-dups (internal--c-header-file-path))))))
+
+(ert-deftest subr-tests-internal--c-header-file-path/gcc-mocked ()
+  ;; Handle empty values of "gcc -print-multiarch".
+  (cl-letf (((symbol-function 'call-process)
+             (lambda (_program &optional _infile _destination _display &rest args)
+               (when (equal (car args) "-print-multiarch")
+                 (insert "\n") 0))))
+    (should (member "/usr/include" (internal--c-header-file-path))))
+  ;; Handle single values of "gcc -print-multiarch".
+  (cl-letf (((symbol-function 'call-process)
+             (lambda (_program &optional _infile _destination _display &rest args)
+               (when (equal (car args) "-print-multiarch")
+                 (insert "x86_64-linux-gnu\n") 0))))
+    (should (member "/usr/include/x86_64-linux-gnu" (internal--c-header-file-path)))))
+
 (provide 'subr-tests)
 ;;; subr-tests.el ends here