/
opamStateConfig.ml
205 lines (187 loc) · 6.23 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
(**************************************************************************)
(* *)
(* Copyright 2015 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
type t = {
root_dir: OpamFilename.Dir.t;
current_switch: OpamSwitch.t option;
switch_from: [ `Env | `Command_line | `Default ];
jobs: int Lazy.t;
dl_jobs: int;
build_test: bool;
build_doc: bool;
dryrun: bool;
makecmd: string Lazy.t;
ignore_constraints_on: name_set;
unlock_base: bool;
no_env_notice: bool;
locked: string option;
}
let default = {
root_dir = OpamFilename.(
concat_and_resolve (Dir.of_string (OpamStd.Sys.home ())) ".opam"
);
current_switch = None;
switch_from = `Default;
jobs = lazy (max 1 (OpamSystem.cpu_count () - 1));
dl_jobs = 3;
build_test = false;
build_doc = 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;
}
type 'a options_fun =
?root_dir:OpamFilename.Dir.t ->
?current_switch:OpamSwitch.t ->
?switch_from:[ `Env | `Command_line | `Default ] ->
?jobs:(int Lazy.t) ->
?dl_jobs:int ->
?build_test:bool ->
?build_doc:bool ->
?dryrun:bool ->
?makecmd:string Lazy.t ->
?ignore_constraints_on:name_set ->
?unlock_base:bool ->
?no_env_notice:bool ->
?locked:string option ->
'a
let setk k t
?root_dir
?current_switch
?switch_from
?jobs
?dl_jobs
?build_test
?build_doc
?dryrun
?makecmd
?ignore_constraints_on
?unlock_base
?no_env_notice
?locked
=
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;
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;
}
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.Config in
let open OpamStd.Option.Op in
let current_switch, switch_from =
match env_string "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:(env_string "ROOT" >>| OpamFilename.Dir.of_string)
?current_switch
?switch_from
?jobs:(env_int "JOBS" >>| fun s -> lazy s)
?dl_jobs:(env_int "DOWNLOADJOBS")
?build_test:(env_bool "WITHTEST" ++ env_bool "BUILDTEST")
?build_doc:(env_bool "WITHDOC" ++ env_bool "BUILDDOC")
?dryrun:(env_bool "DRYRUN")
?makecmd:(env_string "MAKECMD" >>| fun s -> lazy s)
?ignore_constraints_on:
(env_string "IGNORECONSTRAINTS" >>| fun s ->
OpamStd.String.split s ',' |>
List.map OpamPackage.Name.of_string |>
OpamPackage.Name.Set.of_list)
?unlock_base:(env_bool "UNLOCKBASE")
?no_env_notice:(env_bool "NOENVNOTICE")
?locked:(env_string "LOCKED" >>| function "" -> None | s -> Some s)
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 load opamroot =
OpamFile.Config.read_opt (OpamPath.config opamroot)
let local_switch_exists root switch =
OpamPath.Switch.switch_config root switch |>
OpamFile.Switch_config.read_opt |> function
| None -> false
| Some conf -> conf.OpamFile.Switch_config.opam_root = Some root
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 =
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
let load_defaults root_dir =
let current_switch =
match OpamStd.Config.env_string "SWITCH" with
| Some "" | None -> get_current_switch_from_cwd root_dir
| _ -> (* OPAMSWITCH is set, no need to lookup *) None
in
match load root_dir 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:(lazy (OpamFile.Config.jobs conf))
~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"