Add Sys.runtime_executable and caml_sys_proc_self_exe#13728
Add Sys.runtime_executable and caml_sys_proc_self_exe#13728dra27 merged 2 commits intoocaml:trunkfrom
Sys.runtime_executable and caml_sys_proc_self_exe#13728Conversation
|
I'm not sure I fully followed your explanations :–) But I think I understand what you want.
|
96e09b6 to
7a3e16e
Compare
|
Your understanding is indeed correct 😊 |
|
Possibly: diff --git a/stdlib/sys.mli b/stdlib/sys.mli
index 999fce322d..f50be349e7 100644
--- a/stdlib/sys.mli
+++ b/stdlib/sys.mli
@@ -32,11 +32,12 @@ val executable_name : string
on the platform and whether the program was compiled to bytecode or a native
executable. *)
-val interpreter : string
-(** The name of the file containing the interpreter currently running. For
- native code, this is just {!executable_name}. This name may be absolute or
- relative to the current directory, depending on the platform (Linux, Windows
- and macOS should all return absolute paths).
+val runtime_executable : string
+(** The name of the file containing the runtime currently running. For
+ native code, this is always {!executable_name}, but in bytecode it may be
+ ocamlrun, for example. This name may be absolute or relative to the current
+ directory, depending on the platform (Linux, Windows and macOS should all
+ return absolute paths).
@since 5.4
*)? |
1ba7ca8 to
6f8f3c0
Compare
I don’t follow - is that as a general comment, or something specific to how I’m presenting these PRs? |
nojb
left a comment
There was a problem hiding this comment.
Exposing the value in the runtime system seems harmless enough. I am only wondering if it is a good idea to expose this to user programs in the standard library. Are there any known use-cases for it? For "internal" use maybe the C primitive would suffice otherwise. @dra27: what do you think?
| on the platform and whether the program was compiled to bytecode or a native | ||
| executable. *) | ||
|
|
||
| val runtime_executable : string |
There was a problem hiding this comment.
Nit, but from a naming perspective, it would be better in my opinion to suffix this with _name to emphasize the similarity with executable_name.
There was a problem hiding this comment.
I find Sys.executable_name quite confusing: I'm never sure if it is a filename or file path. If you want something else here I'd rather suggest Sys.runtime_executable_path or Sys.runtime_executable_filepath.
There was a problem hiding this comment.
I'm not sure either way - part of the problem is that it could be either, although I'd veer towards runtime_executable_path is it's ideally a full path to the file, and only a filename or implicit/relative path in fallback cases?
Sorry. No, nothing against you! It's a comment about the way we practice software evolution through diffs reviews in these web UIs. There is a lack of context which makes it difficult to me to really assess the correctness or suitability of a change (In my own projects I usually apply the diffs of PRs in a branch locally and then look around before rebasing on main). |
|
I don't mind, @nojb - the primitive is definitely the only part I need, and of course exposing it in the stdlib could be added subsequently. That said, it felt that it fits into the same area of introspection properties as |
nojb
left a comment
There was a problem hiding this comment.
LGTM
Since this touches the standard library, a second official approval is needed.
Sys.interpreter and caml_sys_proc_self_exeSys.runtime_executable and caml_sys_proc_self_exe
a630961 to
8f15274
Compare
|
This was approved, maybe it should also be merged? |
I think @damiendoligez is intending to review the PR. |
8f15274 to
bf290a8
Compare
|
Indeed, it introduces a stdlib function, so it’s still in need of a second core dev approval |
bf290a8 to
b11bf5b
Compare
|
Ping @damiendoligez (or another core dev willing to add a second approval)? |
caml_executable_name is always called in native startup and for all the non-default bytecode linking mechanisms. Bytecode startup now always calls caml_executable_name, and this value is stored along with exe_name. caml_sys_proc_self_exe returns this stored value as a string option. It returns None if caml_executable_name is not implemented on a given platform.
In native mode, same as Sys.executable_name, in bytecode, the path to the interpreter executing Sys.executable_name, which may not be the same from the same file.
|
Thanks, @gasche! |
In bytecode,
Sys.executable_namegoes to some lengths to refer to the path to whereever the bytecode image came from. However, in both bytecode and native code, on most platforms, the runtime hascaml_executable_namewhich does whatever's the appropriate equivalent of reading/proc/self/exe.For some additional testing that I wish to add, I need to know whether the runtime implements
caml_executable_nameor just returnsNULL(because it significantly affects the handling of bytecode startup whenargv[0]is being altered). One way to do this is to record the fact inConfig.has_caml_executable_name, but this involves a lot of tedious plumbing and duplication betweenconfigure.ac,utils/config.generated.ml.inandruntime/unix.c, and that's just to support a test harness. Another way is simply to wrapcaml_executable_nameas a primitive (although, confusingly, we already havecaml_sys_executable_namemeaning something else). That seems a bit daft, though -caml_executable_nameis (with only one exception) always called during startup anyway. The value should not change during execution of a program, but the ability to query the running execcutable's name in theory can disappear (!!). The simplest approach, therefore, seems to be to squirrel it away at the same time as the derived executable name incaml_sys_initwhich is what the first commit does, exposing the squirreled value viacaml_sys_proc_self_exe. Usefully, in native code, that squirreled-away value is in fact the same pointer asexe_name, so it doesn't even cost any space 😁As part of the original work on Relocatable OCaml, I found it useful to be able to identify which ocamlrun was being executed from within a bytecode program itself, which is where the second commit originates from.
Sys.interpreteruses this primitive in order to provide the path to the actually executing program. With this primitive, one then has inmake runtop:As ever, completely open to naming suggestions - the primitive goes with the Linux name for the feature, as I figured
caml_sys_GetModuleFileNamemight be less popular and it's worth having a name which is very distinct fromargv[0]orcaml_sys_executable_name, etc.Sys.runtimemight be slightly better thanSys.interpreteras that sounds more consistent in native mode (and we do haveSys.runtime_variant, etc., already).