Skip to content

Commit 94edfe2

Browse files
committed
Re-update src/unix/config/discover.exe to be able to cross-compile lwt
The default case of [C_library_flags.detect] puts systematically [-I/usr/include] and [-L/usr/lib] even in a cross-compilation context which is wrong because [/usr/include/pthread.h] can clash [cross-env/pthread.h] for instance. The default case about underlying libraries (libev or pthread) should be available only from a restrictive set of flags instead of a pervasive one. This patch restricts this set to what we really need instead to pervasively picks some random flags. A clarification about the recognition of [pthread] was made too to really understand the behavior of [discover.exe] and give a chance to be more reproducible afterwards
1 parent 0cec6b4 commit 94edfe2

File tree

1 file changed

+53
-20
lines changed

1 file changed

+53
-20
lines changed

src/unix/config/discover.ml

+53-20
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ sig
160160

161161
val ws2_32_lib : Configurator.t -> unit
162162

163+
val set_c_flags : string list -> unit
164+
val set_link_flags : string list -> unit
163165
val c_flags : unit -> string list
164166
val link_flags : unit -> string list
165167
val add_link_flags : string list -> unit
@@ -249,14 +251,19 @@ struct
249251

250252
| None ->
251253
try
252-
let path =
254+
let _path =
253255
List.find
254256
(fun path -> Sys.file_exists (path // "include" // header))
255257
(Lazy.force search_paths)
256258
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. *)
257264
extend
258-
["-I" ^ (path // "include")]
259-
["-L" ^ (path // "lib"); "-l" ^ library]
265+
[] (* ["-I" ^ (path // "include")] *)
266+
[] (* ["-L" ^ (path // "lib"); "-l" ^ library] *)
260267
with Not_found ->
261268
()
262269

@@ -268,6 +275,9 @@ struct
268275
else
269276
extend unicode ["-lws2_32"]
270277

278+
let set_c_flags lst = c_flags := lst
279+
let set_link_flags lst = link_flags := lst
280+
271281
let c_flags () =
272282
!c_flags
273283

@@ -440,6 +450,7 @@ struct
440450
C_library_flags.add_link_flags ["-lev"];
441451
Some true
442452
| _ ->
453+
C_library_flags.add_link_flags ["-lev"];
443454
C_library_flags.detect context ~library:"ev";
444455
compiles context code
445456
end
@@ -463,26 +474,48 @@ struct
463474
}
464475
|}
465476
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
481514
| _no ->
482-
if !Arguments.android_target = Some true then
515+
if (* 2 *) !Arguments.android_target = Some true then
483516
Some false
484517
else begin
485-
match compiles context code ~link_flags:["-lpthread"] with
518+
match (* 3 *) compiles context code ~link_flags:["-lpthread"] with
486519
| Some true ->
487520
C_library_flags.add_link_flags ["-lpthread"];
488521
Some true

0 commit comments

Comments
 (0)