Skip to content

Commit 7a8a703

Browse files
authored
Merge pull request #11707 from Alizter/test-foreign-enabled_if
test: enabled_if in foreign_library not respected
2 parents e76fe6c + b48296f commit 7a8a703

File tree

6 files changed

+97
-15
lines changed

6 files changed

+97
-15
lines changed

doc/changes/11707.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- fix: Evaluate `enabled_if` when computing the stubs for stanzas such as
2+
`foreign_library` (#11707, @Alizter, @rgrinberg)

src/dune_rules/dir_contents.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ end = struct
292292
; foreign_sources =
293293
Memo.lazy_ (fun () ->
294294
let dune_version = Dune_project.dune_version project in
295-
stanzas >>| Foreign_sources.make ~dune_version ~dirs)
295+
stanzas >>= Foreign_sources.make ~dir ~dune_version ~dirs)
296296
; coq =
297297
Memo.lazy_ (fun () ->
298298
stanzas >>| Coq_sources.of_dir ~dir ~include_subdirs ~dirs)
@@ -370,7 +370,7 @@ end = struct
370370
let foreign_sources =
371371
Memo.lazy_ (fun () ->
372372
let dune_version = Dune_project.dune_version project in
373-
stanzas >>| Foreign_sources.make ~dune_version ~dirs)
373+
stanzas >>= Foreign_sources.make ~dir ~dune_version ~dirs)
374374
in
375375
let coq =
376376
Memo.lazy_ (fun () ->

src/dune_rules/foreign_sources.ml

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
open Import
2+
open Memo.O
23

34
type t =
45
{ libraries : Foreign.Sources.t Lib_name.Map.t
@@ -215,24 +216,23 @@ let make stanzas ~(sources : Unresolved.t) ~dune_version =
215216
List.fold_left
216217
stanzas
217218
~init:([], [], [])
218-
~f:(fun ((libs, foreign_libs, exes) as acc) stanza ->
219-
match Stanza.repr stanza with
220-
| Library.T lib ->
219+
~f:(fun (libs, foreign_libs, exes) stanza ->
220+
match stanza with
221+
| `Library (lib : Library.t) ->
221222
let all =
222223
eval_foreign_stubs lib.buildable.foreign_stubs lib.buildable.ctypes
223224
in
224225
(lib, all) :: libs, foreign_libs, exes
225-
| Foreign_library.T library ->
226+
| `Foreign_library (library : Foreign_library.t) ->
226227
let all = eval_foreign_stubs [ library.stubs ] None in
227228
( libs
228229
, (library.archive_name, (library.archive_name_loc, all)) :: foreign_libs
229230
, exes )
230-
| Executables.T exe | Tests.T { exes = exe; _ } ->
231+
| `Executables exe | `Tests { Tests.exes = exe; _ } ->
231232
let all =
232233
eval_foreign_stubs exe.buildable.foreign_stubs exe.buildable.ctypes
233234
in
234-
libs, foreign_libs, (exe, all) :: exes
235-
| _ -> acc)
235+
libs, foreign_libs, (exe, all) :: exes)
236236
in
237237
List.(rev libs, rev foreign_libs, rev exes)
238238
in
@@ -337,3 +337,23 @@ let make stanzas ~dune_version ~dirs =
337337
let sources = Unresolved.load_dirs ~dune_version dirs in
338338
make stanzas ~dune_version ~sources
339339
;;
340+
341+
let make stanzas ~dir ~dune_version ~dirs =
342+
let+ stanzas =
343+
List.filter_map stanzas ~f:(fun stanza ->
344+
match Stanza.repr stanza with
345+
| Library.T lib -> Some (`Library lib, lib.enabled_if)
346+
| Foreign_library.T lib -> Some (`Foreign_library lib, lib.enabled_if)
347+
| Executables.T exe -> Some (`Executables exe, exe.enabled_if)
348+
| Tests.T ({ exes = exe; _ } as tests) -> Some (`Tests tests, exe.enabled_if)
349+
| _ -> None)
350+
|> Memo.parallel_map ~f:(fun (stanza, enabled_if) ->
351+
let* expander = Expander0.get ~dir in
352+
Expander0.eval_blang expander enabled_if
353+
>>| function
354+
| false -> None
355+
| true -> Some stanza)
356+
>>| List.filter_opt
357+
in
358+
make stanzas ~dune_version ~dirs
359+
;;

src/dune_rules/foreign_sources.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ val for_exes : t -> first_exe:string -> Foreign.Sources.t
1111

1212
val make
1313
: Stanza.t list
14+
-> dir:Path.Build.t
1415
-> dune_version:Syntax.Version.t
1516
-> dirs:Source_file_dir.t list
16-
-> t
17+
-> t Memo.t
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Interaction of the foreign_library stanza and the enabled_if field.
2+
3+
$ cat > dune-project <<EOF
4+
> (lang dune 3.18)
5+
> EOF
6+
7+
We should allow multiple foreign libraries to define the same archive if only
8+
one of them is enabled:
9+
10+
$ cat > dune <<EOF
11+
> (foreign_library
12+
> (enabled_if true)
13+
> (language c)
14+
> (archive_name a)
15+
> (names a))
16+
>
17+
> (foreign_library
18+
> (enabled_if false)
19+
> (language c)
20+
> (archive_name a)
21+
> (names a))
22+
>
23+
> (foreign_library
24+
> (enabled_if false)
25+
> (language c)
26+
> (archive_name a)
27+
> (names a))
28+
> EOF
29+
$ cat > a.c
30+
$ dune build
31+
32+
33+
Repeat the test, but now two of the libraries are indeed enabled which is
34+
illegal:
35+
36+
$ cat > dune <<EOF
37+
> (foreign_library
38+
> (enabled_if true)
39+
> (language c)
40+
> (archive_name a)
41+
> (names a))
42+
>
43+
> (foreign_library
44+
> (enabled_if true)
45+
> (language c)
46+
> (archive_name a)
47+
> (names a))
48+
>
49+
> (foreign_library
50+
> (enabled_if false)
51+
> (language c)
52+
> (archive_name a)
53+
> (names a))
54+
> EOF
55+
$ cat > a.c
56+
$ dune build
57+
File "dune", line 5, characters 8-9:
58+
5 | (names a))
59+
^
60+
Error: Multiple definitions for the same object file "a". See another
61+
definition at dune:11.
62+
Hint: You can avoid the name clash by renaming one of the objects, or by
63+
placing it into a different directory.
64+
[1]

test/blackbox-tests/test-cases/foreign-stubs/github10675.t

-5
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,3 @@ stubs, dune should not crash. See #10675.
2020
$ touch startup.c main.ml
2121

2222
$ dune build
23-
File "dune", line 3, characters 7-11:
24-
3 | (name main))
25-
^^^^
26-
Error: Executables with same name "main" use different foreign sources
27-
[1]

0 commit comments

Comments
 (0)