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-08-21 Thread Ihor Radchenko
Ihor Radchenko  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 .
Support Org development at ,
or support my work at 



[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 Thread Ihor Radchenko
Leo Butler  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@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 .
Support Org development at ,
or support my work at 



Re: [RFC/PATCH] naming src/bin files in ob-C.el

2023-05-15 Thread Leo Butler
On Mon, May 15 2023, Ihor Radchenko  wrote:

> Leo Butler  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


Re: [RFC/PATCH] naming src/bin files in ob-C.el

2023-05-15 Thread Ihor Radchenko
Leo Butler  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   :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 .
Support Org development at ,
or support my work at 



Re: [RFC/PATCH] naming src/bin files in ob-C.el

2023-05-14 Thread Leo Butler
On Sat, May 13 2023, Ihor Radchenko  wrote:

> Leo Butler  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   :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


Re: [RFC/PATCH] naming src/bin files in ob-C.el

2023-05-13 Thread Ihor Radchenko
Leo Butler  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 .
Support Org development at ,
or support my work at 



Re: [RFC/PATCH] naming src/bin files in ob-C.el

2023-05-12 Thread Leo Butler
On Fri, May 12 2023, Ihor Radchenko  wrote:

> Leo Butler  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


Re: [RFC/PATCH] naming src/bin files in ob-C.el

2023-05-12 Thread Ihor Radchenko
Leo Butler  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 .
Support Org development at ,
or support my work at 



Re: [RFC/PATCH] naming src/bin files in ob-C.el

2023-05-12 Thread Leo Butler
On Fri, May 12 2023, Leo Butler  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  :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


[RFC/PATCH] naming src/bin files in ob-C.el

2023-05-12 Thread Leo Butler
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.

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  :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