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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
(***********************************************************************)
(* *)
(* ocamlbuild *)
(* *)
(* Nicolas Pouillard, Berke Durak, projet Gallium, INRIA Rocquencourt *)
(* *)
(* Copyright 2007 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the Q Public License version 1.0. *)
(* *)
(***********************************************************************)
(* Original author: Romain Bardou *)
open My_std
open My_unix
open Command
type command_spec = Command.spec
type error =
| Cannot_run_ocamlfind
| Dependency_not_found of string * string (* package, dependency *)
| Package_not_found of string
| Cannot_parse_query of string * string (* package, explaination *)
exception Findlib_error of error
let error x = raise (Findlib_error x)
let string_of_error = function
| Cannot_run_ocamlfind ->
"Cannot run Ocamlfind."
| Dependency_not_found(p, d) ->
Printf.sprintf
"Ocamlfind returned \"%s\" as a dependency for package \"%s\" but does \
not know this dependency." d p
| Package_not_found p ->
Printf.sprintf "Findlib package not found: \"%s\"." p
| Cannot_parse_query(p, e) ->
Printf.sprintf "Cannot parse Ocamlfind query for package \"%s\": %s" p e
let report_error e =
prerr_endline (string_of_error e);
exit 2
let ocamlfind = "ocamlfind"
type package = {
name: string;
description: string;
version: string;
archives_byte: string;
archives_native: string;
link_options: string;
location: string;
dependencies: package list;
}
let packages = Hashtbl.create 42
let run_and_parse lexer command =
Printf.ksprintf
(fun command -> lexer & Lexing.from_string & run_and_read command)
command
let run_and_read command =
Printf.ksprintf run_and_read command
let rec query name =
try
Hashtbl.find packages name
with Not_found ->
try
let n, d, v, a_byte, lo, l =
run_and_parse Lexers.ocamlfind_query
"%s query -l -predicates byte %s" ocamlfind name
in
let a_native =
run_and_parse Lexers.trim_blanks
"%s query -a-format -predicates native %s" ocamlfind name
in
let deps =
run_and_parse Lexers.blank_sep_strings "%s query -r -p-format %s" ocamlfind name
in
let deps = List.filter ((<>) n) deps in
let deps =
try
List.map query deps
with Findlib_error (Package_not_found dep_name) ->
(* Ocamlfind cannot find a package which it returned as a dependency.
This should not happen. *)
error (Dependency_not_found (name, dep_name))
in
let package = {
name = n;
description = d;
version = v;
archives_byte = a_byte;
archives_native = a_native;
link_options = lo;
location = l;
dependencies = deps;
} in
Hashtbl.add packages n package;
package
with
| Failure _ ->
(* TODO: Improve to differenciate whether ocamlfind cannot be
run or is not installed *)
error Cannot_run_ocamlfind
| Lexers.Error s ->
error (Cannot_parse_query (name, s))
let split_nl s =
let x = ref [] in
let rec go s =
let pos = String.index s '\n' in
x := (String.before s pos)::!x;
go (String.after s (pos + 1))
in
try
go s
with Not_found -> !x
let before_space s =
try
String.before s (String.index s ' ')
with Not_found -> s
let list () =
List.map before_space (split_nl & run_and_read "%s list" ocamlfind)
(* The closure algorithm is easy because the dependencies are already closed
and sorted for each package. We only have to make the union. We could also
make another ocamlfind query such as:
ocamlfind query -p-format -r package1 package2 ... *)
let topological_closure l =
let add l x = if List.mem x l then l else x :: l in
let l = List.fold_left begin fun acc p ->
add (List.fold_left add acc p.dependencies) p
end [] l in
List.rev l
module SSet = Set.Make(String)
let add_atom a l = match a, l with
| A "", _ -> l
| _ -> a :: l
let compile_flags l =
let pkgs = topological_closure l in
let locations = List.fold_left begin fun acc p ->
SSet.add p.location acc
end SSet.empty pkgs in
let flags = [] in
(* includes *)
let flags =
List.fold_left begin fun acc l ->
add_atom (P l) (add_atom (A "-I") acc)
end flags (SSet.elements locations)
in
S (List.rev flags)
let compile_flags_byte = compile_flags
let compile_flags_native = compile_flags
let link_flags f l =
let pkgs = topological_closure l in
let locations = List.fold_left begin fun acc p ->
SSet.add p.location acc
end SSet.empty pkgs in
let flags = [] in
(* includes *)
let flags =
List.fold_left begin fun acc l ->
add_atom (P l) (add_atom (A "-I") acc)
end flags (SSet.elements locations)
in
(* special link options *)
let flags =
List.fold_left begin fun acc x ->
add_atom (A x.link_options) acc
end flags pkgs
in
(* archives *)
let flags =
List.fold_left begin fun acc x ->
add_atom (A (f x)) acc
end flags pkgs
in
S (List.rev flags)
let link_flags_byte = link_flags (fun x -> x.archives_byte)
let link_flags_native = link_flags (fun x -> x.archives_native)
|