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 supports things like `dune exec time %{bin:e}`. The syntax is consistent with what support in `dune build` and backwards compatible in cases where no arguments start with `%`. The resolution mechanism is slightly different for the program and the rest of the arguments: - the program is always considered a possible dependency, either in pform syntax (`%{bin:e}` or in string syntax (`./path/to/e`, `_build/default/path/to/e`). - arguments are only interpreted as dependencies if they are in pform syntax. Closes ocaml#2691 Signed-off-by: Etienne Millon <me@emillon.org> Signed-off-by: Marek Kubica <marek@tarides.com>
1 parent
7cc79e2
commit 8bd2622
Showing
6 changed files
with
235 additions
and
49 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
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,3 @@ | ||
- `dune exec`: support syntax like `%{bin:program}`. This can appear anywhere | ||
in the command line, so things like `dune exec time %{bin:program}` now work. | ||
(#6035, #8474, fixes #2691, @emillon, @Leonidas-from-XIV) |
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,108 @@ | ||
$ cat > dune-project << EOF | ||
> (lang dune 1.1) | ||
> | ||
> (package | ||
> (name e)) | ||
> EOF | ||
$ cat > dune << EOF | ||
> (executable | ||
> (public_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 | ||
|
||
By default, 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 | ||
|
||
The special form %{bin:public_name} is supported. | ||
|
||
$ dune exec %{bin:e} a b c | ||
Hello | ||
argv[0] = _build/install/default/bin/e | ||
argv[1] = a | ||
argv[2] = b | ||
argv[3] = c | ||
|
||
This wrapper parses its own arguments and executes the rest. | ||
|
||
$ cat > wrap.sh << 'EOF' | ||
> #!/bin/bash | ||
> while getopts "xy" o; do | ||
> echo "Got option: $o" | ||
> done | ||
> shift $((OPTIND-1)) | ||
> echo Before | ||
> "$@" | ||
> echo After | ||
> EOF | ||
$ chmod +x wrap.sh | ||
|
||
It is possible to put the %{bin:...} pform in arguments rather than first. | ||
|
||
$ dune exec -- ./wrap.sh -x -y %{bin:e} a b c | ||
Got option: x | ||
Got option: y | ||
Before | ||
Hello | ||
argv[0] = _build/install/default/bin/e | ||
argv[1] = a | ||
argv[2] = b | ||
argv[3] = c | ||
After | ||
|
||
The first item is still looked up in PATH. | ||
|
||
$ dune exec ls %{bin:e} | ||
_build/install/default/bin/e | ||
|
||
Pforms can appear several times. | ||
|
||
$ dune exec ls %{bin:e} %{bin:e} | ||
_build/install/default/bin/e | ||
_build/install/default/bin/e | ||
|
||
It should also be possible to call another program that is also supposed to be | ||
built if referenced, for this we create a new binary that calls its first | ||
argument: | ||
|
||
$ cat > call_arg.ml << EOF | ||
> let () = | ||
> let first = Sys.argv.(1) in | ||
> Printf.printf "Calling my first arg, %S:\n" first; | ||
> let inch, outch = Unix.open_process_args first [|first|] in | ||
> print_endline (input_line inch); | ||
> let status = Unix.close_process (inch, outch) in | ||
> match status with | ||
> | Unix.WEXITED 0 -> print_endline "All good" | ||
> | _ -> print_endline "Something is Rotten in the State of Dune" | ||
> EOF | ||
$ cat > called.ml << EOF | ||
> let () = print_endline "I was called" | ||
> EOF | ||
$ cat > dune << EOF | ||
> (executables | ||
> (public_names e call_arg called) | ||
> (libraries unix)) | ||
> EOF | ||
|
||
If we then ask it to execute, both `call_arg` and `called` should be compiled | ||
and run, successfully. | ||
|
||
$ dune exec %{bin:call_arg} %{bin:called} | ||
Calling my first arg, "_build/install/default/bin/called": | ||
I was called | ||
All good |