-
Notifications
You must be signed in to change notification settings - Fork 358
/
opamStateConfig.ml
408 lines (365 loc) · 13.1 KB
/
opamStateConfig.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
(**************************************************************************)
(* *)
(* Copyright 2015-2020 OCamlPro *)
(* *)
(* All rights reserved. This file is distributed under the terms of the *)
(* GNU Lesser General Public License version 2.1, with the special *)
(* exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
open OpamTypes
open OpamStateTypes
module E = struct
type OpamStd.Config.E.t +=
| BUILDDOC of bool option
| BUILDTEST of bool option
| DOWNLOADJOBS of int option
| DRYRUN of bool option
| IGNORECONSTRAINTS of string option
| JOBS of int option
| LOCKED of string option
| MAKECMD of string option
| NODEPEXTS of bool option
| NOENVNOTICE of bool option
| ROOT of string option
| SWITCH of string option
| UNLOCKBASE of bool option
| WITHDEVSETUP of bool option
| WITHDOC of bool option
| WITHTEST of bool option
open OpamStd.Config.E
let builddoc = value (function BUILDDOC b -> b | _ -> None)
let buildtest = value (function BUILDTEST b -> b | _ -> None)
let downloadjobs = value (function DOWNLOADJOBS i -> i | _ -> None)
let dryrun = value (function DRYRUN b -> b | _ -> None)
let ignoreconstraints = value (function IGNORECONSTRAINTS s -> s | _ -> None)
let jobs = value (function JOBS i -> i | _ -> None)
let locked = value (function LOCKED s -> s | _ -> None)
let makecmd = value (function MAKECMD s -> s | _ -> None)
let nodepexts = value (function NODEPEXTS b -> b | _ -> None)
let noenvnotice = value (function NOENVNOTICE b -> b | _ -> None)
let root = value (function ROOT s -> s | _ -> None)
let switch = value (function SWITCH s -> s | _ -> None)
let unlockbase = value (function UNLOCKBASE b -> b | _ -> None)
let withdevsetup = value (function WITHDEVSETUP b -> b | _ -> None)
let withdoc = value (function WITHDOC b -> b | _ -> None)
let withtest = value (function WITHTEST b -> b | _ -> None)
end
type t = {
root_dir: OpamFilename.Dir.t;
current_switch: OpamSwitch.t option;
switch_from: provenance;
jobs: int Lazy.t;
dl_jobs: int;
build_test: bool;
build_doc: bool;
dev_setup: bool;
dryrun: bool;
makecmd: string Lazy.t;
ignore_constraints_on: name_set;
unlock_base: bool;
no_env_notice: bool;
locked: string option;
no_depexts: bool;
}
let default = {
root_dir = (
(* On Windows, if a .opam directory is found in %HOME% or %USERPROFILE% then
then we'll use it. Otherwise, we use %LOCALAPPDATA%. *)
let home_location =
let open OpamFilename in
concat_and_resolve (Dir.of_string (OpamStd.Sys.home ())) ".opam"
in
if not Sys.win32 || OpamFilename.exists_dir home_location then
home_location
else
let open OpamFilename in
let local_appdata =
(* CSIDL_LOCAL_APPDATA = 0x1c *)
Dir.of_string (OpamStubs.(shGetFolderPath 0x1c SHGFP_TYPE_CURRENT))
in
concat_and_resolve local_appdata "opam"
);
current_switch = None;
switch_from = `Default;
jobs = lazy (max 1 (OpamSysPoll.cores () - 1));
dl_jobs = 3;
build_test = false;
build_doc = false;
dev_setup = false;
dryrun = false;
makecmd = lazy OpamStd.Sys.(
match os () with
| FreeBSD | OpenBSD | NetBSD | DragonFly -> "gmake"
| _ -> "make"
);
ignore_constraints_on = OpamPackage.Name.Set.empty;
unlock_base = false;
no_env_notice = false;
locked = None;
no_depexts = false;
}
type 'a options_fun =
?root_dir:OpamFilename.Dir.t ->
?current_switch:OpamSwitch.t ->
?switch_from:provenance ->
?jobs:(int Lazy.t) ->
?dl_jobs:int ->
?build_test:bool ->
?build_doc:bool ->
?dev_setup:bool ->
?dryrun:bool ->
?makecmd:string Lazy.t ->
?ignore_constraints_on:name_set ->
?unlock_base:bool ->
?no_env_notice:bool ->
?locked:string option ->
?no_depexts: bool ->
'a
let setk k t
?root_dir
?current_switch
?switch_from
?jobs
?dl_jobs
?build_test
?build_doc
?dev_setup
?dryrun
?makecmd
?ignore_constraints_on
?unlock_base
?no_env_notice
?locked
?no_depexts
=
let (+) x opt = match opt with Some x -> x | None -> x in
k {
root_dir = t.root_dir + root_dir;
current_switch =
(match current_switch with None -> t.current_switch | s -> s);
switch_from = t.switch_from + switch_from;
jobs = t.jobs + jobs;
dl_jobs = t.dl_jobs + dl_jobs;
build_test = t.build_test + build_test;
build_doc = t.build_doc + build_doc;
dev_setup = t.dev_setup + dev_setup;
dryrun = t.dryrun + dryrun;
makecmd = t.makecmd + makecmd;
ignore_constraints_on = t.ignore_constraints_on + ignore_constraints_on;
unlock_base = t.unlock_base + unlock_base;
no_env_notice = t.no_env_notice + no_env_notice;
locked = t.locked + locked;
no_depexts = t.no_depexts + no_depexts;
}
let set t = setk (fun x () -> x) t
let r = ref default
let update ?noop:_ = setk (fun cfg () -> r := cfg) !r
let initk k =
let open OpamStd.Option.Op in
let current_switch, switch_from =
match E.switch () with
| Some "" | None -> None, None
| Some s -> Some (OpamSwitch.of_string s), Some `Env
in
setk (setk (fun c -> r := c; k)) !r
?root_dir:(E.root () >>| OpamFilename.Dir.of_string)
?current_switch
?switch_from
?jobs:(E.jobs () >>| fun s -> lazy s)
?dl_jobs:(E.downloadjobs ())
?build_test:(E.withtest () ++ E.buildtest ())
?build_doc:(E.withdoc () ++ E.builddoc ())
?dev_setup:(E.withdevsetup())
?dryrun:(E.dryrun ())
?makecmd:(E.makecmd () >>| fun s -> lazy s)
?ignore_constraints_on:
(E.ignoreconstraints () >>| fun s ->
OpamStd.String.split s ',' |>
List.map OpamPackage.Name.of_string |>
OpamPackage.Name.Set.of_list)
?unlock_base:(E.unlockbase ())
?no_env_notice:(E.noenvnotice ())
?locked:(E.locked () >>| function "" -> None | s -> Some s)
?no_depexts:(E.nodepexts ())
let init ?noop:_ = initk (fun () -> ())
let opamroot ?root_dir () =
let open OpamStd.Option.Op in
(root_dir >>+ fun () ->
OpamStd.Env.getopt "OPAMROOT" >>| OpamFilename.Dir.of_string)
+! default.root_dir
let is_newer_raw = function
| Some v ->
OpamVersion.compare v OpamFile.Config.root_version > 0
| None -> false
let is_newer config =
is_newer_raw (Some (OpamFile.Config.opam_root_version config))
(** none -> shouldn't load (write attempt in readonly)
Some true -> everything is fine normal read
Some false -> readonly accorded, load with best effort *)
let is_readonly_opamroot_raw ?(lock_kind=`Lock_write) version =
let newer = is_newer_raw version in
let write = lock_kind = `Lock_write in
if newer && write then None else
Some (newer && not write)
let is_readonly_opamroot_t ?lock_kind gt =
is_readonly_opamroot_raw ?lock_kind
(Some (OpamFile.Config.opam_root_version gt.config))
let is_newer_than_self ?lock_kind gt =
is_readonly_opamroot_t ?lock_kind gt <> Some false
let load_if_possible_raw ?lock_kind root version (read,read_wo_err) f =
match is_readonly_opamroot_raw ?lock_kind version with
| None ->
OpamConsole.error_and_exit `Locked
"Refusing write access to %s, which is more recent than this version of \
opam (%s > %s), aborting."
(OpamFilename.Dir.to_string root)
(OpamStd.Option.to_string OpamVersion.to_string version)
OpamVersion.(to_string OpamFile.Config.root_version)
| Some true -> read_wo_err f
| Some false -> read f
let load_if_possible_t ?lock_kind opamroot config readf f =
load_if_possible_raw ?lock_kind
opamroot (Some (OpamFile.Config.opam_root_version config)) readf f
let load_if_possible ?lock_kind gt =
load_if_possible_t ?lock_kind gt.root gt.config
let load_config_root ?lock_kind readf opamroot =
let f = OpamPath.config opamroot in
load_if_possible_raw ?lock_kind
opamroot
(OpamFile.Config.raw_root_version f)
readf f
let safe read read' default =
let safe r f = OpamStd.Option.default default @@ r f in
safe read, safe read'
let safe_load ?lock_kind opamroot =
load_config_root ?lock_kind
OpamFile.Config.(safe read_opt BestEffort.read_opt empty) opamroot
let load ?lock_kind opamroot =
load_config_root ?lock_kind
OpamFile.Config.(read_opt, BestEffort.read_opt) opamroot
(* switches *)
module Switch = struct
let load_raw ?lock_kind root config readf switch =
load_if_possible_t ?lock_kind root config readf
(OpamPath.Switch.switch_config root switch)
let safe_load_t ?lock_kind root switch =
let config = safe_load ~lock_kind:`Lock_read root in
load_raw ?lock_kind root config
OpamFile.Switch_config.(safe read_opt BestEffort.read_opt empty)
switch
let load ?lock_kind gt readf switch =
load_raw ?lock_kind gt.root gt.config readf switch
let safe_load ?lock_kind gt switch =
load ?lock_kind gt
OpamFile.Switch_config.(safe read_opt BestEffort.read_opt empty)
switch
let read_opt ?lock_kind gt switch =
load ?lock_kind gt
OpamFile.Switch_config.(read_opt, BestEffort.read_opt)
switch
let safe_read_selections ?lock_kind gt switch =
load_if_possible ?lock_kind gt
OpamFile.SwitchSelections.(safe read_opt BestEffort.read_opt empty)
(OpamPath.Switch.selections gt.root switch)
end
(* repos *)
module Repos = struct
let safe_read ?lock_kind gt =
load_if_possible ?lock_kind gt
OpamFile.Repos_config.(safe read_opt BestEffort.read_opt empty)
(OpamPath.repos_config gt.root)
end
let downgrade_2_1_switch f =
let filename = OpamFile.filename f in
let str_f = OpamFilename.to_string filename in
let opamfile = OpamParser.FullPos.file str_f in
let opamfile' =
let open OpamParserTypes.FullPos in
{ opamfile with
file_contents =
List.map (fun item ->
match item.pelem with
| Variable (({pelem = "opam-version"; _} as opam_v),
({pelem = String "2.1"; _} as v)) ->
{ item with
pelem = Variable ({opam_v with pelem = "opam-version"},
{v with pelem = String "2.0"})}
| _ -> item)
opamfile.file_contents}
in
if opamfile' = opamfile then None else
Some (opamfile'
|> OpamPrinter.FullPos.opamfile
|> OpamFile.Switch_config.read_from_string)
let local_switch_exists root switch =
(* we don't use safe loading function to avoid errors displaying *)
let f = OpamPath.Switch.switch_config root switch in
match OpamFile.Switch_config.BestEffort.read_opt f with
| None -> false
| Some conf -> conf.OpamFile.Switch_config.opam_root = Some root
| exception (OpamPp.Bad_version _ as e) ->
match OpamFile.Config.raw_root_version (OpamPath.config root) with
| None -> raise e
| Some _ ->
match downgrade_2_1_switch f with
| None -> raise e
| Some conf ->
if conf.OpamFile.Switch_config.opam_root = Some root then
(OpamFile.Switch_config.write f conf; true)
else false
let resolve_local_switch root s =
let switch_root = OpamSwitch.get_root root s in
if OpamSwitch.is_external s && OpamFilename.dirname_dir switch_root = root
then OpamSwitch.of_string (OpamFilename.remove_prefix_dir root switch_root)
else s
let get_current_switch_from_cwd root =
try
let open OpamStd.Option.Op in
OpamFilename.find_in_parents (fun dir ->
OpamSwitch.of_string (OpamFilename.Dir.to_string dir) |>
local_switch_exists root)
(OpamFilename.cwd ())
>>| OpamSwitch.of_dirname
>>| resolve_local_switch root
with OpamPp.Bad_version _ -> None
(* do we want `load_defaults` to fail / run a format upgrade ? *)
let load_defaults ?lock_kind root_dir =
let current_switch =
match E.switch () with
| Some "" | None -> get_current_switch_from_cwd root_dir
| _ -> (* OPAMSWITCH is set, no need to lookup *) None
in
match try load ?lock_kind root_dir with OpamPp.Bad_version _ -> None with
| None ->
update ?current_switch ();
None
| Some conf ->
let open OpamStd.Option.Op in
OpamRepositoryConfig.update
?download_tool:(OpamFile.Config.dl_tool conf >>| function
| (CString c,None)::_ as t
when OpamStd.String.ends_with ~suffix:"curl" c -> lazy (t, `Curl)
| t -> lazy (t, `Default))
~validation_hook:(OpamFile.Config.validation_hook conf)
();
update
?current_switch:(OpamFile.Config.switch conf)
~switch_from:`Default
?jobs:(OpamFile.Config.jobs conf >>| fun s -> lazy s)
~dl_jobs:(OpamFile.Config.dl_jobs conf)
();
update ?current_switch ();
Some conf
let get_switch_opt () =
match !r.current_switch with
| Some s ->
Some (resolve_local_switch !r.root_dir s)
| None -> None
let get_switch () =
match get_switch_opt () with
| Some s -> s
| None ->
OpamConsole.error_and_exit `Configuration_error
"No switch is currently set. Please use 'opam switch' to set or install \
a switch"