emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Leo Butler <Leo.Butler@umanitoba.ca>
To: Org Mode Mailing List <emacs-orgmode@gnu.org>
Subject: Re: [RFC/PATCH] naming src/bin files in ob-C.el
Date: Fri, 12 May 2023 15:41:24 +0000	[thread overview]
Message-ID: <87a5y9eevv.fsf@t14.reltub.ca> (raw)
In-Reply-To: <87fs81egk5.fsf@t14.reltub.ca> (Leo Butler's message of "Fri, 12 May 2023 15:05:15 +0000")

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

On Fri, May 12 2023, Leo Butler <Leo.Butler@umanitoba.ca> 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.


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

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";
+* File naming
+:ID:       1a691f36-f9c1-4531-8fc0-ee7b21ef5975
+#+source: file_naming
+#+begin_src cpp :includes <iostream> :results output raw drawer :bin-file ./hello-world
+std::cout << "Hello World!\n";
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"
+(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

  reply	other threads:[~2023-05-12 15:42 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-12 15:05 [RFC/PATCH] naming src/bin files in ob-C.el Leo Butler
2023-05-12 15:41 ` Leo Butler [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a5y9eevv.fsf@t14.reltub.ca \
    --to=leo.butler@umanitoba.ca \
    --cc=emacs-orgmode@gnu.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).