From: Stefan Kangas Date: Mon, 6 Jan 2025 09:08:01 +0000 (+0100) Subject: New function internal--c-header-file-path X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=e287fb3d547e08159607210c87eaa1a203f2fe78;p=emacs.git New function internal--c-header-file-path 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) --- diff --git a/lisp/man.el b/lisp/man.el index 25a4ccee897..bafb4d20f68 100644 --- a/lisp/man.el +++ b/lisp/man.el @@ -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)) diff --git a/lisp/subr.el b/lisp/subr.el index a810c3940ea..f1bf4a4e671 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -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 diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index e6964c42ca8..2cb0b616074 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -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