emacs-orgmode@gnu.org archives
 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

* Re: [RFC/PATCH] naming src/bin files in ob-C.el
  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
  0 siblings, 1 reply; 10+ messages in thread
From: Leo Butler @ 2023-05-12 15:41 UTC (permalink / raw)
  To: Org Mode Mailing List

[-- 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.

Leo


[-- 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";
 #+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

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

* Re: [RFC/PATCH] naming src/bin files in ob-C.el
  2023-05-12 15:41 ` Leo Butler
@ 2023-05-12 16:39   ` Ihor Radchenko
  2023-05-12 20:25     ` Leo Butler
  0 siblings, 1 reply; 10+ messages in thread
From: Ihor Radchenko @ 2023-05-12 16:39 UTC (permalink / raw)
  To: Leo Butler; +Cc: Org Mode Mailing List

Leo Butler <Leo.Butler@umanitoba.ca> writes:

>> Comments?
> ...
> 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.

IMHO, it will be more consistent with other backends to use :results file :file /path/to/executable

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

* Re: [RFC/PATCH] naming src/bin files in ob-C.el
  2023-05-12 16:39   ` Ihor Radchenko
@ 2023-05-12 20:25     ` Leo Butler
  2023-05-13  7:36       ` Ihor Radchenko
  0 siblings, 1 reply; 10+ messages in thread
From: Leo Butler @ 2023-05-12 20:25 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Org Mode Mailing List

On Fri, May 12 2023, Ihor Radchenko <yantar92@posteo.net> wrote:

> Leo Butler <Leo.Butler@umanitoba.ca> writes:
>
>>> Comments?
>> ...
>> 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.
>
> IMHO, it will be more consistent with other backends to use :results file :file /path/to/executable

No, I don't think this is the way to do it. What happens in this
case is:

1. `org-babel-C-execute' creates a named source file, compiles it to the
   named binary file;
2. then executes that binary file, producing output;
3. that output is inserted into the named binary file, overwriting its
   contents.

I don't see how to accomplish my stated goal of saving the binary file
to a named location without introducing a separate header argument
(e.g. ":bin-file").

Leo

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

* Re: [RFC/PATCH] naming src/bin files in ob-C.el
  2023-05-12 20:25     ` Leo Butler
@ 2023-05-13  7:36       ` Ihor Radchenko
  2023-05-14 20:19         ` Leo Butler
  0 siblings, 1 reply; 10+ messages in thread
From: Ihor Radchenko @ 2023-05-13  7:36 UTC (permalink / raw)
  To: Leo Butler; +Cc: Org Mode Mailing List

Leo Butler <Leo.Butler@umanitoba.ca> writes:

>> IMHO, it will be more consistent with other backends to use :results file :file /path/to/executable
>
> No, I don't think this is the way to do it. What happens in this
> case is:
>
> 1. `org-babel-C-execute' creates a named source file, compiles it to the
>    named binary file;
> 2. then executes that binary file, producing output;
> 3. that output is inserted into the named binary file, overwriting its
>    contents.

What I am suggesting is making :results file :file /path/to stop after 1
and produce a link to the binary file.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

* Re: [RFC/PATCH] naming src/bin files in ob-C.el
  2023-05-13  7:36       ` Ihor Radchenko
@ 2023-05-14 20:19         ` Leo Butler
  2023-05-15 11:15           ` Ihor Radchenko
  0 siblings, 1 reply; 10+ messages in thread
From: Leo Butler @ 2023-05-14 20:19 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Org Mode Mailing List

On Sat, May 13 2023, Ihor Radchenko <yantar92@posteo.net> wrote:

> Leo Butler <Leo.Butler@umanitoba.ca> writes:
>
>>> IMHO, it will be more consistent with other backends to use :results file :file /path/to/executable
>>
>> No, I don't think this is the way to do it. What happens in this
>> case is:
>>
>> 1. `org-babel-C-execute' creates a named source file, compiles it to the
>>    named binary file;
>> 2. then executes that binary file, producing output;
>> 3. that output is inserted into the named binary file, overwriting its
>>    contents.
>
> What I am suggesting is making :results file :file /path/to stop after 1
> and produce a link to the binary file.

Ok, stopping after 1 seems reasonable when the code block is meant to
produce just the executable. But, your suggestion would mean that the
code block can *only* produce an executable file. Maybe that is ok, but
since the current semantics allow something like

#+begin_src C++ :includes <iostream> <fstream> :results file :file ./results.csv
  using namespace std;
  for(int i=0; i<10; i++){ cout << i << "," << i*i << endl; }
#+end_src

so I am not sure that we should break that.

On the other hand, I don't see any sense in producing a link to the
binary file. Org can't do anything with that link, so the user would
need to write something like ":results file :file /path/to :wrap
comment". That is why I would prefer something like a :bin-file header.

Leo

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

* Re: [RFC/PATCH] naming src/bin files in ob-C.el
  2023-05-14 20:19         ` Leo Butler
@ 2023-05-15 11:15           ` Ihor Radchenko
  2023-05-15 11:46             ` Leo Butler
  0 siblings, 1 reply; 10+ messages in thread
From: Ihor Radchenko @ 2023-05-15 11:15 UTC (permalink / raw)
  To: Leo Butler; +Cc: Org Mode Mailing List

Leo Butler <Leo.Butler@umanitoba.ca> writes:

> Ok, stopping after 1 seems reasonable when the code block is meant to
> produce just the executable. But, your suggestion would mean that the
> code block can *only* produce an executable file. Maybe that is ok, but
> since the current semantics allow something like
>
> #+begin_src C++ :includes <iostream> <fstream> :results file :file ./results.csv
>   using namespace std;
>   for(int i=0; i<10; i++){ cout << i << "," << i*i << endl; }
> #+end_src
>
> so I am not sure that we should break that.

Fair point.

> On the other hand, I don't see any sense in producing a link to the
> binary file. Org can't do anything with that link, so the user would
> need to write something like ":results file :file /path/to :wrap
> comment". That is why I would prefer something like a :bin-file header.

I am not sure how I feel about such side effects of evaluation.
Is there any other babel backend that is doing something similar?

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

* Re: [RFC/PATCH] naming src/bin files in ob-C.el
  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
  0 siblings, 1 reply; 10+ messages in thread
From: Leo Butler @ 2023-05-15 11:46 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Org Mode Mailing List

On Mon, May 15 2023, Ihor Radchenko <yantar92@posteo.net> wrote:

> Leo Butler <Leo.Butler@umanitoba.ca> writes:
>
>> On the other hand, I don't see any sense in producing a link to the
>> binary file. Org can't do anything with that link, so the user would
>> need to write something like ":results file :file /path/to :wrap
>> comment". That is why I would prefer something like a :bin-file header.
>
> I am not sure how I feel about such side effects of evaluation.
> Is there any other babel backend that is doing something similar?

ob-java.el deals with it by using a :dir header; if non-nil, the
compiled files are placed in that directory instead of
`org-babel-temporary-directory'. (And, by default, the :dir header is
"."). The filename is determined by the packagename.

Leo

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

* [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)
  2023-05-15 11:46             ` Leo Butler
@ 2023-05-15 14:02               ` Ihor Radchenko
  2023-08-21 12:06                 ` Ihor Radchenko
  0 siblings, 1 reply; 10+ messages in thread
From: Ihor Radchenko @ 2023-05-15 14:02 UTC (permalink / raw)
  To: Leo Butler; +Cc: Org Mode Mailing List

Leo Butler <Leo.Butler@umanitoba.ca> writes:

>> I am not sure how I feel about such side effects of evaluation.
>> Is there any other babel backend that is doing something similar?
>
> ob-java.el deals with it by using a :dir header; if non-nil, the
> compiled files are placed in that directory instead of
> `org-babel-temporary-directory'. (And, by default, the :dir header is
> "."). The filename is determined by the packagename.

Well. What ob-java does is mostly for historical reasons and because
some users of Java source blocks tend to use a lot of local classes
defined in current directory. See
https://list.orgmode.org/orgmode/87imadafo0.fsf@iki.fi/
ob-java does not really aim to generate compiled source in :dir for
the sole purpose of generating the compiled source.

I feel slightly reluctant about adding side effects to babel evaluation.
Not too reluctant though.

I'd like to hear from other ob-C users, so I changed this thread into a poll.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


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

* Re: [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)
  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
  0 siblings, 0 replies; 10+ messages in thread
From: Ihor Radchenko @ 2023-08-21 12:06 UTC (permalink / raw)
  To: Leo Butler; +Cc: Org Mode Mailing List

Ihor Radchenko <yantar92@posteo.net> writes:

> I feel slightly reluctant about adding side effects to babel evaluation.
> Not too reluctant though.
>
> I'd like to hear from other ob-C users, so I changed this thread into a poll.

No replies.
Closed.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[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 public inbox

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

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).