160
160
161
161
val ws2_32_lib : Configurator .t -> unit
162
162
163
+ val set_c_flags : string list -> unit
164
+ val set_link_flags : string list -> unit
163
165
val c_flags : unit -> string list
164
166
val link_flags : unit -> string list
165
167
val add_link_flags : string list -> unit
@@ -249,14 +251,19 @@ struct
249
251
250
252
| None ->
251
253
try
252
- let path =
254
+ let _path =
253
255
List. find
254
256
(fun path -> Sys. file_exists (path // " include" // header))
255
257
(Lazy. force search_paths)
256
258
in
259
+ (* NOTE: for the cross-compilation sake, we should not arbitrarily
260
+ * include ([-I]) some paths which can clash some cross-compilation's
261
+ * definitions with host's definitions. The default case about flags
262
+ * should always be less than more - and we should put these flags
263
+ * only we really require them. *)
257
264
extend
258
- [" -I" ^ (path // " include" )]
259
- [" -L" ^ (path // " lib" ); " -l" ^ library]
265
+ [] (* [ "-I" ^ (path // "include")] *)
266
+ [] (* [ "-L" ^ (path // "lib"); "-l" ^ library] *)
260
267
with Not_found ->
261
268
()
262
269
@@ -268,6 +275,9 @@ struct
268
275
else
269
276
extend unicode [" -lws2_32" ]
270
277
278
+ let set_c_flags lst = c_flags := lst
279
+ let set_link_flags lst = link_flags := lst
280
+
271
281
let c_flags () =
272
282
! c_flags
273
283
@@ -440,6 +450,7 @@ struct
440
450
C_library_flags. add_link_flags [" -lev" ];
441
451
Some true
442
452
| _ ->
453
+ C_library_flags. add_link_flags [" -lev" ];
443
454
C_library_flags. detect context ~library: " ev" ;
444
455
compiles context code
445
456
end
@@ -463,26 +474,48 @@ struct
463
474
}
464
475
| }
465
476
in
466
- (* On some platforms, pthread is included in the standard library, but
467
- linking with -lpthread fails. So, try to link the test code without
468
- any flags first.
469
-
470
- If that fails and we are not targetting Android, try to link with
471
- -lpthread. If *that* fails, search for libpthread in the filesystem.
472
-
473
- When targetting Android, compiling without -lpthread is the only way
474
- to link with pthread, and we don't to search for libpthread, because
475
- if we find it, it is likely the host's libpthread. *)
476
- match compiles ~c_flags: [] ~link_flags: [] context code,
477
- compiles context code with
478
- | Some true , Some true -> Some true
479
- | Some false , Some true
480
- | Some true , Some false -> Some true
477
+ (* To clarify the semantic of the recognition of [pthread]:
478
+ 1) [pthread] can be _standalone_ (included in the standard library)
479
+ depending on the C compiler
480
+ 1.1) A restrictive context (such as a cross-compilation context)
481
+ requires, at least, [-lpthread] but [-I] and [-L] can
482
+ disturb the compilation between the host's [pthread] and the
483
+ cross-compiled [pthread]. We test above all and for all this
484
+ tricky context with **only one** flag [-lpthread]
485
+ 1.2) On some platforms, if [pthread] is standalone, the linker
486
+ fails when we link with [-lpthread]. So we test our code
487
+ with **default** flags (such as [-I/usr/include] and
488
+ [-L/usr/lib]) and **without** [-pthread]
489
+ 2) On Android, compiling without [-lpthread] is the only way to link
490
+ with [pthread], and we don't to search for [pthread.a], because
491
+ if we find it, it is likely the host's [pthread]
492
+ 3) We finally retest our code with [-lpthread] and basic [-L] and
493
+ [-I] flags (from the host system)
494
+
495
+ NOTE(dinosaure):
496
+ - 2) and 1.1) should be merged, we definitely should try to compile
497
+ the code **without any flags** and see results - by this way, we
498
+ consider that the _toolchain_ leads us about where is
499
+ [pthread].
500
+ - 3) is too ~vague~ and obviously works but it's difficult to really
501
+ understand which [pthread] we really use.
502
+ - A question remains about priorities: do we want to prioritize
503
+ the [dune]'s context or do we prefer a compilation for the host
504
+ system first?
505
+ - In anyway, [discover.exe] should be less pervasives (no [ref]
506
+ about flags) and more strict and reproducible *)
507
+ match (* 1.2 *) compiles context code,
508
+ (* 1.1 *) compiles ~c_flags: [] ~link_flags: [ " -lpthread" ] context code with
509
+ | _ , Some true (* prioritize [dune]'s context and cross-compilation *) ->
510
+ C_library_flags. set_c_flags [] ;
511
+ C_library_flags. set_link_flags [ " -lpthread" ] ;
512
+ Some true
513
+ | Some true , _ -> Some true
481
514
| _no ->
482
- if ! Arguments. android_target = Some true then
515
+ if (* 2 *) ! Arguments. android_target = Some true then
483
516
Some false
484
517
else begin
485
- match compiles context code ~link_flags: [" -lpthread" ] with
518
+ match (* 3 *) compiles context code ~link_flags: [" -lpthread" ] with
486
519
| Some true ->
487
520
C_library_flags. add_link_flags [" -lpthread" ];
488
521
Some true
0 commit comments