Skip to content

Commit 96debe1

Browse files
committed
Add dune-workspace support
Signed-off-by: Romain Beauxis <romain.beauxis@gmail.com>
1 parent a587cb8 commit 96debe1

File tree

5 files changed

+165
-18
lines changed

5 files changed

+165
-18
lines changed

src/dune_rules/context.ml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,12 @@ and t =
9292
; which : Filename.t -> Path.t option Memo.t
9393
}
9494

95-
let target_exec toolchain =
96-
match !Dune_engine.Clflags.target_exec, Context_name.to_string toolchain with
97-
| Some (name, prog, args), toolchain when name = toolchain -> Some (prog, args)
95+
let default_target_exec ~target_exec toolchain =
96+
match
97+
target_exec, !Dune_engine.Clflags.target_exec, Context_name.to_string toolchain
98+
with
99+
| _, Some (name, prog, args), toolchain when name = toolchain -> Some (prog, args)
100+
| Some target_exec, _, _ -> Some target_exec
98101
| _ -> None
99102
;;
100103

@@ -576,15 +579,15 @@ module Group = struct
576579
in
577580
List.filter_map targets ~f:(function
578581
| Native -> None
579-
| Named findlib_toolchain ->
582+
| Named { name = findlib_toolchain; target_exec } ->
580583
Some
581584
(Memo.Lazy.create ~name:"findlib_toolchain" (fun () ->
582585
let name = Context_name.target builder.name ~toolchain:findlib_toolchain in
583586
create
584587
{ builder with
585588
name
586589
; findlib_toolchain = Some findlib_toolchain
587-
; target_exec = target_exec findlib_toolchain
590+
; target_exec = default_target_exec ~target_exec findlib_toolchain
588591
}
589592
~kind
590593
|> Memo.return)))

src/dune_rules/per_context.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let all =
1717
let targets =
1818
List.filter_map targets ~f:(function
1919
| Native -> None
20-
| Named toolchain ->
20+
| Named { name = toolchain; _ } ->
2121
let name = Context_name.target native ~toolchain in
2222
Some (name, `Target (context, toolchain)))
2323
in

src/source/workspace.ml

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,29 +292,65 @@ end
292292

293293
module Context = struct
294294
module Target = struct
295+
type named =
296+
{ name : Context_name.t
297+
; target_exec : (string * string list) option
298+
}
299+
295300
type t =
296301
| Native
297-
| Named of Context_name.t
302+
| Named of named
298303

299304
let equal x y =
300305
match x, y with
301306
| Native, Native -> true
302307
| Native, _ | _, Native -> false
303-
| Named x, Named y -> Context_name.equal x y
308+
| Named x, Named y -> Context_name.equal x.name y.name
304309
;;
305310

306311
let t =
307-
let+ context_name = Context_name.decode in
308-
match Context_name.to_string context_name with
309-
| "native" -> Native
310-
| _ -> Named context_name
312+
let open Dune_lang.Decoder in
313+
peek
314+
>>= function
315+
| Some (List _) ->
316+
enter
317+
(fields
318+
(let+ name = field "name" Context_name.decode
319+
and+ target_exec =
320+
field_o
321+
"target_exec"
322+
(Dune_lang.Syntax.since Stanza.syntax (3, 21)
323+
>>>
324+
let+ prog = string
325+
and+ args = repeat string in
326+
prog, args)
327+
in
328+
match Context_name.to_string name, target_exec with
329+
| "native", Some _ ->
330+
User_error.raise
331+
[ Pp.text "Native target cannot have target_exec specified" ]
332+
| "native", None -> Native
333+
| _ -> Named { name; target_exec }))
334+
| _ ->
335+
let+ context_name = Context_name.decode in
336+
(match Context_name.to_string context_name with
337+
| "native" -> Native
338+
| _ -> Named { name = context_name; target_exec = None })
311339
;;
312340

313341
let to_dyn =
314342
let open Dyn in
315343
function
316344
| Native -> variant "Native" []
317-
| Named name -> variant "Named" [ Context_name.to_dyn name ]
345+
| Named ({ name; target_exec } : named) ->
346+
variant
347+
"Named"
348+
[ record
349+
[ "name", Context_name.to_dyn name
350+
; ( "target_exec"
351+
, option (fun (prog, args) -> list string (prog :: args)) target_exec )
352+
]
353+
]
318354
;;
319355

320356
let add ts x =
@@ -653,7 +689,7 @@ module Context = struct
653689
n
654690
:: List.filter_map (targets t) ~f:(function
655691
| Native -> None
656-
| Named s -> Some (Context_name.target n ~toolchain:s))
692+
| Named { name; _ } -> Some (Context_name.target n ~toolchain:name))
657693
;;
658694

659695
let default ~x ~profile ~instrument_with =
@@ -682,7 +718,7 @@ module Context = struct
682718
native
683719
:: List.filter_map (targets t) ~f:(function
684720
| Native -> None
685-
| Named toolchain ->
721+
| Named { name = toolchain; _ } ->
686722
let name = Context_name.target name ~toolchain in
687723
Some (Build_context.create ~name))
688724
;;
@@ -966,7 +1002,9 @@ let step1 clflags =
9661002
Path.Source.root
9671003
| Some (In_source_dir s) -> s)
9681004
in
969-
let x = Option.map x ~f:(fun s -> Context.Target.Named s) in
1005+
let x =
1006+
Option.map x ~f:(fun s -> Context.Target.Named { name = s; target_exec = None })
1007+
in
9701008
let superpose_with_command_line cl field =
9711009
let+ x = field in
9721010
lazy (Option.value cl ~default:(Lazy.force x))
@@ -1086,7 +1124,9 @@ let default clflags =
10861124
=
10871125
clflags
10881126
in
1089-
let x = Option.map x ~f:(fun s -> Context.Target.Named s) in
1127+
let x =
1128+
Option.map x ~f:(fun s -> Context.Target.Named { name = s; target_exec = None })
1129+
in
10901130
let config =
10911131
create_final_config
10921132
~config_from_config_file

src/source/workspace.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ end
3838

3939
module Context : sig
4040
module Target : sig
41+
type named =
42+
{ name : Context_name.t
43+
; target_exec : (string * string list) option
44+
}
45+
4146
type t =
4247
| Native
43-
| Named of Context_name.t
48+
| Named of named
4449

4550
val equal : t -> t -> bool
4651
end

test/blackbox-tests/test-cases/custom-cross-compilation/target-exec.t

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,103 @@ Test runexec action (should always run host binary, bypassing wrapper)
238238
---- HELLO END ----
239239

240240

241+
Build with dune-workspace
241242

243+
$ cat > dune-workspace <<EOF
244+
> (lang dune 3.21)
245+
> (context
246+
> (default
247+
> (targets
248+
> native
249+
> ((name test_toolchain) (target_exec test_toolchain_wrapper.sh)))))
250+
> EOF
251+
252+
$ PATH="$PWD/bin:$PATH" dune build @runhello --force
253+
---- HELLO START ----
254+
Hello from OCaml!
255+
PWD: $TESTCASE_ROOT/_build/default
256+
Args: ./hello.exe --hello-arg1 --hello-arg2
257+
---- HELLO END ----
258+
259+
=== TEST_TOOLCHAIN WRAPPER START ===
260+
WRAPPER PWD: $TESTCASE_ROOT/_build/default.test_toolchain
261+
WRAPPER executing: $TESTCASE_ROOT/_build/default.test_toolchain/hello.exe --hello-arg1 --hello-arg2
262+
=== WRAPPER EXEC ===
263+
---- HELLO START ----
264+
Hello from OCaml!
265+
PWD: $TESTCASE_ROOT/_build/default.test_toolchain
266+
Args: $TESTCASE_ROOT/_build/default.test_toolchain/hello.exe --hello-arg1 --hello-arg2
267+
---- HELLO END ----
268+
269+
270+
Build with dune-workspace and arguments
271+
272+
$ cat > dune-workspace <<EOF
273+
> (lang dune 3.21)
274+
> (context
275+
> (default
276+
> (targets
277+
> native
278+
> ((name test_toolchain) (target_exec wrapper_with_args.sh --arg1 --arg2)))))
279+
> EOF
280+
281+
$ PATH="$PWD/bin:$PATH" dune build @runhello --force
282+
---- HELLO START ----
283+
Hello from OCaml!
284+
PWD: $TESTCASE_ROOT/_build/default
285+
Args: ./hello.exe --hello-arg1 --hello-arg2
286+
---- HELLO END ----
287+
288+
=== WRAPPER WITH ARGS START ===
289+
WRAPPER: all args: --arg1 --arg2 $TESTCASE_ROOT/_build/default.test_toolchain/hello.exe --hello-arg1 --hello-arg2
290+
=== WRAPPER EXEC ===
291+
---- HELLO START ----
292+
Hello from OCaml!
293+
PWD: $TESTCASE_ROOT/_build/default.test_toolchain
294+
Args: $TESTCASE_ROOT/_build/default.test_toolchain/hello.exe --hello-arg1 --hello-arg2
295+
---- HELLO END ----
296+
297+
Build with dune-workspace and --target-exec should give priority to --target-exec
298+
299+
$ cat > bin/cli_wrapper.sh <<'EOF'
300+
> #!/bin/sh
301+
> echo "=== CLI WRAPPER START ==="
302+
> echo "WRAPPER: all args: $@"
303+
> echo "=== WRAPPER EXEC ==="
304+
> exec "$@"
305+
> EOF
306+
307+
$ chmod +x bin/cli_wrapper.sh
308+
309+
$ PATH="$PWD/bin:$PATH" dune build @runhello --target-exec test_toolchain=cli_wrapper.sh --force
310+
---- HELLO START ----
311+
Hello from OCaml!
312+
PWD: $TESTCASE_ROOT/_build/default
313+
Args: ./hello.exe --hello-arg1 --hello-arg2
314+
---- HELLO END ----
315+
316+
=== CLI WRAPPER START ===
317+
WRAPPER: all args: $TESTCASE_ROOT/_build/default.test_toolchain/hello.exe --hello-arg1 --hello-arg2
318+
=== WRAPPER EXEC ===
319+
---- HELLO START ----
320+
Hello from OCaml!
321+
PWD: $TESTCASE_ROOT/_build/default.test_toolchain
322+
Args: $TESTCASE_ROOT/_build/default.test_toolchain/hello.exe --hello-arg1 --hello-arg2
323+
---- HELLO END ----
324+
325+
326+
Build with invalid dune-workspace
327+
328+
$ cat > dune-workspace <<EOF
329+
> (lang dune 3.21)
330+
> (context
331+
> (default
332+
> (targets
333+
> ((name native) (target_exec test_toolchain_wrapper.sh)))))
334+
> EOF
335+
336+
$ PATH="$PWD/bin:$PATH" dune build @runhello --force
337+
Error: Native target cannot have target_exec specified
338+
[1]
339+
340+
$ rm -f dune-workspace

0 commit comments

Comments
 (0)