forked from ocaml/dune
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a prefix that is executed instead of the command itself. This is useful to run a command under tools like time or perf. Closes ocaml#2691 Signed-off-by: Etienne Millon <me@emillon.org>
- Loading branch information
Showing
3 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"dune exec --prefix wrap ./e.exe args" behaves like "dune exec ./e.exe args" | ||
except that it executes "wrap" with the rest passed as arguments instead. | ||
|
||
This is useful for "adverbial" commands like time or perf. | ||
|
||
$ cat > dune-project << EOF | ||
> (lang dune 1.0) | ||
> EOF | ||
$ cat > dune << EOF | ||
> (executable | ||
> (name e)) | ||
> EOF | ||
|
||
The executable just displays "Hello" and its arguments. | ||
|
||
$ cat > e.ml << EOF | ||
> let () = | ||
> print_endline "Hello"; | ||
> Array.iteri (fun i s -> | ||
> Printf.printf "argv[%d] = %s\n" i s | ||
> ) Sys.argv | ||
> EOF | ||
|
||
The wrapper parses its own arguments and executes the rest. | ||
|
||
$ cat > wrap.sh << 'EOF' | ||
> #!/bin/sh | ||
> while getopts "xy" o; do | ||
> echo "Got option: $o" | ||
> shift $((OPTIND-1)) | ||
> done | ||
> echo Before | ||
> "$@" | ||
> echo After | ||
> EOF | ||
$ chmod +x wrap.sh | ||
|
||
With no wrapper, e is executed with the program name and arguments in argv. | ||
|
||
$ dune exec ./e.exe a b c | ||
Hello | ||
argv[0] = _build/default/e.exe | ||
argv[1] = a | ||
argv[2] = b | ||
argv[3] = c | ||
|
||
With just a string as prefix, the wrapper sees no arguments and executes the | ||
program with its arguments. | ||
|
||
$ dune exec --prefix ./wrap.sh ./e.exe a b c | ||
Before | ||
Hello | ||
argv[0] = _build/default/e.exe | ||
argv[1] = a | ||
argv[2] = b | ||
argv[3] = c | ||
After | ||
|
||
--prefix is actually space-separated: the wrapper gets the ones passed in | ||
--prefix, and the executable gets the rest (this is up to the wrapper, but most | ||
work this way). | ||
|
||
$ dune exec --prefix './wrap.sh -x -y' ./e.exe a b c | ||
Got option: x | ||
Got option: y | ||
Before | ||
Hello | ||
argv[0] = _build/default/e.exe | ||
argv[1] = a | ||
argv[2] = b | ||
argv[3] = c | ||
After | ||
|
||
When the prefix is empty, this is equivalent to when no prefix is passed. | ||
|
||
$ dune exec --prefix '' ./e.exe a b c | ||
Hello | ||
argv[0] = _build/default/e.exe | ||
argv[1] = a | ||
argv[2] = b | ||
argv[3] = c |