all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [RFC/PATCH] naming src/bin files in ob-C.el
@ 2023-05-12 15:05 Leo Butler
  2023-05-12 15:41 ` Leo Butler
  0 siblings, 1 reply; 10+ messages in thread
From: Leo Butler @ 2023-05-12 15:05 UTC (permalink / raw)
  To: Org Mode Mailing List

[-- Attachment #1: Type: text/plain, Size: 815 bytes --]

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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch.patch --]
[-- Type: text/x-diff; name="0001-On-ltb-ob-max-ob-C-file-names.patch", Size: 3094 bytes --]

diff --git a/lisp/ob-C.el b/lisp/ob-C.el
index 3a6e99623..76842acb6 100644
--- a/lisp/ob-C.el
+++ b/lisp/ob-C.el
@@ -128,16 +128,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 :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..1a104ae6a 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 :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

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2023-08-21 12:07 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-12 15:05 [RFC/PATCH] naming src/bin files in ob-C.el Leo Butler
2023-05-12 15:41 ` Leo Butler
2023-05-12 16:39   ` Ihor Radchenko
2023-05-12 20:25     ` Leo Butler
2023-05-13  7:36       ` Ihor Radchenko
2023-05-14 20:19         ` Leo Butler
2023-05-15 11:15           ` Ihor Radchenko
2023-05-15 11:46             ` Leo Butler
2023-05-15 14:02               ` [POLL] ob-C: Should we allow saving compiled src block to specified binary excecutable? (was: [RFC/PATCH] naming src/bin files in ob-C.el) Ihor Radchenko
2023-08-21 12:06                 ` Ihor Radchenko

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.