On Fri, May 12 2023, Leo Butler <[email protected]> wrote:
> Hello,
>
> I am using ob-C.el to work with a c++ library (capd::dynsys). The
> current behaviour creates a temporary src and bin file in
> `org-babel-temporary-directory'. There is currently no option to have
> these files named and put somewhere else.
>
> This behaviour is not quite right, in my opinion. I would like to be
> able to compile an executable and then be able to call this same
> executable without needing to recompile from source.
>
> Attached is a patch that uses the :file header to allow the naming of
> the source and compiled binary files.
>
> A test is included, too. All tests pass with this patch applied to HEAD
> (except the expected failures, of course).
>
> Comments?
>
> Leo
>
> ps: It might be better to use a custom header for this purpose,
> something like :bin-file. I am open to suggestions.
Replying to myself: it makes more sense to introduce a special-purpose
header argument for this special purpose, than to use :file with a
different meaning. Revised patch attached.
Leo
diff --git a/lisp/ob-C.el b/lisp/ob-C.el
index 3a6e99623..57633cc9f 100644
--- a/lisp/ob-C.el
+++ b/lisp/ob-C.el
@@ -53,7 +53,8 @@
(main . :any)
(flags . :any)
(cmdline . :any)
- (libs . :any))
+ (libs . :any)
+ (bin-file . :any))
"C/C++-specific header arguments.")
(defconst org-babel-header-args:C++
@@ -128,16 +129,21 @@ This function is called by `org-babel-execute-src-block'."
"Expand a block of C code with org-babel according to its header arguments."
(let ((org-babel-c-variant 'c)) (org-babel-C-expand-C body params)))
+(defun org-babel-C-src/bin-file (params src?)
+ "Return the src or bin file name."
+ (let* ((f (cdr (assq :bin-file params)))
+ (file (or f (if src? "C-src-" "C-bin-")))
+ (ext (if src? (pcase org-babel-c-variant
+ (`c ".c") (`cpp ".cpp") (`d ".d")) org-babel-exeext)))
+ (org-babel-process-file-name
+ (if f (concat file ext)
+ (org-babel-temp-file file ext)))))
+
(defun org-babel-C-execute (body params)
"This function should only be called by `org-babel-execute:C'
or `org-babel-execute:C++' or `org-babel-execute:D'."
- (let* ((tmp-src-file (org-babel-temp-file
- "C-src-"
- (pcase org-babel-c-variant
- (`c ".c") (`cpp ".cpp") (`d ".d"))))
- (tmp-bin-file ;not used for D
- (org-babel-process-file-name
- (org-babel-temp-file "C-bin-" org-babel-exeext)))
+ (let* ((tmp-src-file (org-babel-C-src/bin-file params t))
+ (tmp-bin-file (org-babel-C-src/bin-file params nil)) ;not used for D
(cmdline (cdr (assq :cmdline params)))
(cmdline (if cmdline (concat " " cmdline) ""))
(flags (cdr (assq :flags params)))
diff --git a/testing/examples/ob-C-test.org b/testing/examples/ob-C-test.org
index c7a96f665..57fa42cec 100644
--- a/testing/examples/ob-C-test.org
+++ b/testing/examples/ob-C-test.org
@@ -174,3 +174,13 @@ std::cout << "\"line 1\"\n";
std::cout << "\"line 2\"\n";
std::cout << "\"line 3\"\n";
#+end_src
+
+* File naming
+:PROPERTIES:
+:ID: 1a691f36-f9c1-4531-8fc0-ee7b21ef5975
+:END:
+
+#+source: file_naming
+#+begin_src cpp :includes <iostream> :results output raw drawer :bin-file ./hello-world
+std::cout << "Hello World!\n";
+#+end_src
diff --git a/testing/lisp/test-ob-C.el b/testing/lisp/test-ob-C.el
index b6dbed8e3..3d16d1ae5 100644
--- a/testing/lisp/test-ob-C.el
+++ b/testing/lisp/test-ob-C.el
@@ -192,5 +192,19 @@
"\"line 1\"\n\"line 2\"\n\"line 3\"\n"
(org-babel-execute-src-block))))))
+(ert-deftest ob-C/set-src+bin-file-name ()
+ "Set the name of the src and bin file names created by `org-babel-C-execute'."
+ (if (executable-find org-babel-C++-compiler)
+ (unwind-protect
+ (org-test-at-id "1a691f36-f9c1-4531-8fc0-ee7b21ef5975"
+ (org-babel-next-src-block 1)
+ (should (equal
+ "Hello World!\n"
+ (org-babel-execute-src-block)))
+ (should (file-exists-p "./hello-world"))
+ (should (file-exists-p "./hello-world.cpp")))
+ (ignore-errors (delete-file "./hello-world"))
+ (ignore-errors (delete-file "./hello-world.cpp")))))
+
(provide 'test-ob-C)
;;; test-ob-C.el ends here