aboutsummaryrefslogtreecommitdiffstats
path: root/testing/ocaml/060_all_linkorder.patch
blob: 07176b47c6aeedbd6ec6f5e114efacfa8ce42472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Always pass the libraries to link as last argument to the linker.
Should fix build failures with as-needed such as:
https://bugs.gentoo.org/show_bug.cgi?id=331075
https://bugs.gentoo.org/show_bug.cgi?id=331377

Index: ocaml-3.12.0/utils/ccomp.ml
===================================================================
--- ocaml-3.12.0.orig/utils/ccomp.ml
+++ ocaml-3.12.0/utils/ccomp.ml
@@ -98,8 +98,18 @@ type link_mode =
   | MainDll
   | Partial
 
+let is_lib name = String.length name >= 2 && String.sub name 0 2 = "-l"
+
+let rec link_order init libs = function
+	[]   -> List.rev_append init (List.rev libs)
+      | t::q -> if is_lib t then link_order init (t::libs) q else link_order (t::init) libs q
+
+let rec split_libs libs others = function
+        []   -> (libs, others)
+      | t::q -> if is_lib t then split_libs (t::libs) others q else split_libs libs (t::others) q
+
 let call_linker mode output_name files extra =
-  let files = quote_files files in
+  let files = quote_files (link_order [] [] files) in
   let cmd =
     if mode = Partial then
       Printf.sprintf "%s%s %s %s"
@@ -108,7 +118,8 @@ let call_linker mode output_name files e
         files
         extra
     else
-      Printf.sprintf "%s -o %s %s %s %s %s %s %s"
+      let (cclibs,ccopts) = split_libs [] [] !Clflags.ccopts in
+      Printf.sprintf "%s -o %s %s %s %s %s %s %s %s"
         (match !Clflags.c_compiler, mode with
         | Some cc, _ -> cc
         | None, Exe -> Config.mkexe
@@ -120,8 +131,9 @@ let call_linker mode output_name files e
         (if !Clflags.gprofile then Config.cc_profile else "")
         ""  (*(Clflags.std_include_flag "-I")*)
         (quote_prefixed "-L" !Config.load_path)
-        (String.concat " " (List.rev !Clflags.ccopts))
+        (String.concat " " ccopts)
         files
+        (String.concat " " cclibs)
         extra
   in
   command cmd = 0