-
Notifications
You must be signed in to change notification settings - Fork 11
/
Install.ml
231 lines (207 loc) · 8.64 KB
/
Install.ml
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
(***************************************************************************)
(* OASIS2OPAM: Convert OASIS metadata to OPAM package descriptions *)
(* *)
(* Copyright (C) 2013-2015, Christophe Troestler *)
(* *)
(* This library is free software; you can redistribute it and/or modify *)
(* it under the terms of the GNU General Public License as published by *)
(* the Free Software Foundation; either version 3 of the License, or (at *)
(* your option) any later version, with the OCaml static compilation *)
(* exception. *)
(* *)
(* This library is distributed in the hope that it will be useful, but *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *)
(* COPYING for more details. *)
(* *)
(* You should have received a copy of the GNU Lesser General Public *)
(* License along with this library; if not, write to the Free Software *)
(* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 *)
(* USA. *)
(***************************************************************************)
(** Functions to write a <pkg>.install file. *)
(* FIXME: This code is extremely naive. This functionality should be
in an oasis plugin. Until then, we can live with this... *)
open Format
open OASISTypes
open Utils
let binaries t ~flags =
let gather_exec bins = function
| Executable(cs, bs, es) ->
if eval_conditional flags bs.bs_install then (
(* Binary that will be installed *)
let path = Filename.concat "_build" bs.bs_path in
let exec = try Filename.chop_extension es.exec_main_is
with _ -> es.exec_main_is in
let exec = Filename.concat path exec in
(exec, cs.cs_name) :: bins
)
else bins
| _ -> bins (* skip other sections *) in
List.fold_left gather_exec [] (Tarball.oasis t).sections
(* (Subset of) Possible locations for OPAM. *)
type dest_dir = Lib | Libexec | Bin | Sbin | Share | Etc | Doc | Man
let all_dest_dirs = [Lib; Libexec; Bin; Sbin; Share; Etc; Doc; Man]
let string_of_dest_dir = function
| Lib -> "lib"
| Libexec -> "libexec"
| Bin -> "bin"
| Sbin -> "sbin"
| Share -> "share"
| Etc -> "etc"
| Doc -> "doc"
| Man -> "man"
let default_dest =
(Share, "") (* the second component is the relative path *)
let split_slash d =
try let i = String.index d '/' in
(String.sub d 0 i, String.sub d (i + 1) (String.length d - i - 1))
with Not_found -> (d, "")
let classify_dest d =
let dest, dir = split_slash d in
if dest = "$libdir" then (Lib, dir)
else if dest = "$libexecdir" then (Libexec, dir)
else if dest = "$bindir" then (Bin, dir)
else if dest = "$sbindir" then (Sbin, dir)
else if dest = "$datadir" then (Share, dir)
else if dest = "$sysconfdir" then (Etc, snd(split_slash dir))
(* $sysconfdir/pkg/dir *)
else if dest = "$docdir" then (Doc, dir)
else if dest = "$mandir" then (Man, dir)
else (
warn(sprintf "Target %S not recognized, installing in share/" dest);
default_dest
)
let classify_dest_opt = function
| Some dest -> classify_dest dest
| None -> default_dest
let output_classified ppf (local, (_, dir)) =
if dir = "" then fprintf ppf "@\n%S" local
else let dest = Filename.concat dir (Filename.basename local) in
fprintf ppf "@\n%S {%S}" local dest
(* Gather all DataFiles. *)
let datafiles t ~flags =
let pkg = Tarball.oasis t in
let gather_data_files datas = function
| Library(_, bs, _)
| Object(_, bs, _)
| Executable(_, bs, _) ->
if eval_conditional flags bs.bs_install then (
let d = bs.bs_data_files in
let d = List.map (fun (f, l) -> (Filename.concat bs.bs_path f,
classify_dest_opt l)) d in
d @ datas
)
else datas
| _ -> datas (* skip other sections *) in
List.fold_left gather_data_files [] pkg.sections
let write_bin ppf bins data_bin =
if bins <> [] || data_bin <> [] then (
fprintf ppf "@[<2>bin: [";
let output_bin (exec, name) =
let exec = Utils.escaped exec in
let name = Utils.escaped name in
fprintf ppf "@\n\"?%s.byte\" {\"%s\"}" exec name;
fprintf ppf "@\n\"?%s.native\" {\"%s\"}" exec name;
in
List.iter output_bin bins;
List.iter (output_classified ppf) data_bin;
fprintf ppf "@]@\n]@\n%!"
)
let write_datas ppf datas =
let write_dest dest_dir =
let datas = List.filter (fun (_, (d, _)) -> d = dest_dir) datas in
if datas <> [] then (
fprintf ppf "@[<2>%s: [" (string_of_dest_dir dest_dir);
List.iter (output_classified ppf) datas;
fprintf ppf "@]@\n]@\n"
) in
List.iter write_dest all_dest_dirs;
Format.pp_print_flush ppf ()
let with_install t ?warn:(want_warn=true) ~local f =
let pkg = Tarball.oasis t in
let fname = pkg.name ^ ".install" in
if local then (
(* In local mode, the goal is to generate the opam files in
the repository itself. *)
info(sprintf "Create %S." fname);
let fh = open_out fname in
let ppf = Format.formatter_of_out_channel fh in
f ppf;
close_out fh
)
else (
let buf = Buffer.create 1024 in
let ppf = Format.formatter_of_buffer buf in
f ppf;
let contents = Buffer.contents buf in
match Tarball.install t with
| Some install ->
(* FIXME: more clever comparison is desirable. *)
if String.trim install <> String.trim contents then
warn(sprintf "A %s file was found in the tarball but its \
content differs from the generated one. Make sure \
it is compatible with:\n%s" fname contents)
| None ->
let dir = Filename.concat (Tarball.pkg_opam_dir t) "files" in
let full_fname = Filename.concat dir fname in
if want_warn then
warn(sprintf "No %s file was found at the root of the tarball, \
so creating %s. Its is recommended to add \
it to your repository though (\"oasis2opam --local\" \
may help)." fname full_fname);
(try Unix.mkdir dir 0o777
with Unix.Unix_error (Unix.EEXIST, _, _) -> ());
let fh = open_out full_fname in
output_string fh contents;
close_out fh
)
(* Add a file in the OPAM files/ subdir. *)
let with_file t fname f =
let dir = Filename.concat (Tarball.pkg_opam_dir t) "files" in
let full_fname = Filename.concat dir fname in
(try Unix.mkdir dir 0o777
with Unix.Unix_error (Unix.EEXIST, _, _) -> ());
let fh = open_out full_fname in
let ppf = Format.formatter_of_out_channel fh in
f ppf;
Format.pp_print_flush ppf ();
close_out fh
let opam t ~local flags =
let bins = binaries t ~flags in
let datas = datafiles t ~flags in
let data_bin, datas = List.partition (fun (_, (d, _)) -> d = Bin) datas in
if bins <> [] || datas <> [] then (
with_install t ~local
(fun ppf ->
write_bin ppf bins data_bin;
write_datas ppf datas;
)
)
let remove_script = "_oasis_remove_.ml"
(** Write an .install file to save oasis setup.* in order to be able
to also use oasis for removal. Never put these files in the
repository itself because they are a hack. *)
let oasis t =
with_install t ~local:false ~warn:false
(fun ppf ->
fprintf ppf "@[<2>etc: [@\n\
\"setup.ml\"@\n\
\"setup.data\"@\n\
\"setup.log\"@\n\
\"%s\"\
@]@\n]@\n%!" remove_script;
);
(* oasis setup.ml looks for setup.data in the current working
directory and it is not clear there is a protable way to change
do that in OPAM. This script must be in sync. with how it is
called from the "remove:" section. *)
let script =
"open Printf\n\n\
let () =\n \
let dir = Sys.argv.(1) in\n \
(try Sys.chdir dir\n \
with _ -> eprintf \"Cannot change directory to %s\\n%!\" dir);\n \
exit (Sys.command \"ocaml setup.ml -uninstall\")\n" in
with_file t remove_script
(fun ppf -> fprintf ppf "%s" script)