| 1 |
(**************************************************************************) |
| 2 |
(* *) |
| 3 |
(* Copyright 2012-2019 OCamlPro *) |
| 4 |
(* Copyright 2012 INRIA *) |
| 5 |
(* *) |
| 6 |
(* All rights reserved. This file is distributed under the terms of the *) |
| 7 |
(* GNU Lesser General Public License version 2.1, with the special *) |
| 8 |
(* exception on linking described in the file LICENSE. *) |
| 9 |
(* *) |
| 10 |
(**************************************************************************) |
| 11 |
|
| 12 |
open OpamStd.Op |
| 13 |
|
| 14 |
let log fmt = OpamConsole.log "PACKAGE" fmt |
| 15 |
let slog = OpamConsole.slog |
| 16 |
|
| 17 |
module Version = struct |
| 18 |
|
| 19 |
type version_style = |
| 20 |
| Triple (* E.g "1.2.3" *) |
| 21 |
| V_triple (* E.g "v1.2.3" *) |
| 22 |
|
| 23 |
let version_style_equal a b = match (a, b) with |
| 24 |
| Triple, Triple | V_triple, V_triple -> true |
| 25 |
| _ -> false |
| 26 |
|
| 27 |
(* Alternative reprentation for some common patterns of version number that |
| 28 |
can be efficiently compared. This is micro-optimized because solving |
| 29 |
dependencies requires comparing a large number of package versions. *) |
| 30 |
type version_small = |
| 31 |
{ int : Int64.t |
| 32 |
; style : version_style |
| 33 |
; has_suffix : [`None | `Tilde | `Some ] |
| 34 |
} |
| 35 |
|
| 36 |
type version = |
| 37 |
{ raw : string |
| 38 |
; small : version_small option |
| 39 |
} |
| 40 |
|
| 41 |
let empty = { raw = "" ; small = None } |
| 42 |
|
| 43 |
type t = version |
| 44 |
|
| 45 |
let to_string x = x.raw |
| 46 |
|
| 47 |
(* CR rgrinberg: get rid of this once we drop 4.08 support *) |
| 48 |
let rev_concat_map t ~f = |
| 49 |
let rec aux f acc = function |
| 50 |
| [] -> acc |
| 51 |
| x :: l -> |
| 52 |
let xs = f x in |
| 53 |
aux f (List.rev_append xs acc) l |
| 54 |
in |
| 55 |
aux f [] t |
| 56 |
;; |
| 57 |
|
| 58 |
let concat_map t ~f = List.rev (rev_concat_map t ~f) |
| 59 |
|
| 60 |
let small_of_string s = |
| 61 |
let parts = |
| 62 |
String.split_on_char '+' s |
| 63 |
|> concat_map ~f:(String.split_on_char '~') |
| 64 |
|> concat_map ~f:(String.split_on_char '-') |
| 65 |
in |
| 66 |
let has_suffix = |
| 67 |
match parts with |
| 68 |
| [] -> `None |
| 69 |
| ["~"] -> `Tilde |
| 70 |
| _ :: _ -> `Some |
| 71 |
in |
| 72 |
match String.split_on_char '.' (List.nth parts 0) with |
| 73 |
| [] -> None |
| 74 |
| major :: rest -> |
| 75 |
let style, major = |
| 76 |
if String.length major > 0 && major.[0] = 'v' |
| 77 |
then V_triple, String.sub major 1 (String.length major - 1) |
| 78 |
else Triple, major |
| 79 |
in |
| 80 |
let major_opt = int_of_string_opt major in |
| 81 |
let minor_opt, patch_opt = match rest with |
| 82 |
| [] -> Some 0, Some 0 |
| 83 |
| minor :: rest -> |
| 84 |
let minor_opt = int_of_string_opt minor in |
| 85 |
let patch_opt = match rest with |
| 86 |
| [] -> Some 0 |
| 87 |
| [ patch ] -> int_of_string_opt patch |
| 88 |
| _ -> |
| 89 |
None |
| 90 |
in |
| 91 |
minor_opt, patch_opt |
| 92 |
in |
| 93 |
match major_opt, minor_opt, patch_opt with |
| 94 |
| Some major, Some minor, Some patch -> |
| 95 |
let max = 2 lsl 15 - 1 in |
| 96 |
if major <= max && minor <= max && patch <= max |
| 97 |
then |
| 98 |
(* If each part of the version number can fit in a 16-bit |
| 99 |
integer pack them into a single 64-bit int so they can be |
| 100 |
efficiently compared. *) |
| 101 |
let int = |
| 102 |
(Int64.shift_left (Int64.of_int major) 32) |
| 103 |
|> Int64.logor (Int64.shift_left (Int64.of_int minor) 16) |
| 104 |
|> Int64.logor (Int64.of_int patch) |
| 105 |
in |
| 106 |
Some { style; int; has_suffix } |
| 107 |
else None |
| 108 |
| _ -> None |
| 109 |
|
| 110 |
let of_string x = |
| 111 |
if String.length x = 0 then failwith "Package version can't be empty"; |
| 112 |
String.iter (function |
| 113 |
| 'a'..'z' | 'A'..'Z' | '0'..'9' | '-' | '_' | '+' | '.' | '~' -> () |
| 114 |
| c -> |
| 115 |
failwith |
| 116 |
(Printf.sprintf "Invalid character '%c' in package version %S" c x)) |
| 117 |
x; |
| 118 |
{ raw = x |
| 119 |
; small = small_of_string x |
| 120 |
} |
| 121 |
|
| 122 |
let default = of_string "dev" |
| 123 |
|
| 124 |
let small_compare a b = |
| 125 |
if version_style_equal a.style b.style then |
| 126 |
let c = Int64.compare a.int b.int in |
| 127 |
if c = 0 then |
| 128 |
(* Only consider the suffix if the numeric part of both versions is the same. *) |
| 129 |
match a.has_suffix, b.has_suffix with |
| 130 |
| `Tilde, `Tilde |
| 131 |
| `None, `None -> Some 0 |
| 132 |
| `Tilde, `None -> Some (-1) |
| 133 |
| `None, `Tilde -> Some 1 |
| 134 |
| _ , _ -> |
| 135 |
(* If both versions have suffixes then run the full comparison. *) |
| 136 |
None |
| 137 |
else |
| 138 |
Some c |
| 139 |
else |
| 140 |
None |
| 141 |
|
| 142 |
let compare a b = |
| 143 |
match a.small, b.small with |
| 144 |
| Some small_a, Some small_b -> ( |
| 145 |
match small_compare small_a small_b with |
| 146 |
| Some c -> c |
| 147 |
| None -> OpamVersionCompare.compare a.raw b.raw) |
| 148 |
| _ -> OpamVersionCompare.compare a.raw b.raw |
| 149 |
|
| 150 |
let equal v1 v2 = |
| 151 |
compare v1 v2 = 0 |
| 152 |
|
| 153 |
let to_json x = |
| 154 |
`String (to_string x) |
| 155 |
let of_json = function |
| 156 |
| `String x -> (try Some (of_string x) with _ -> None) |
| 157 |
| _ -> None |
| 158 |
|
| 159 |
module O = struct |
| 160 |
type t = version |
| 161 |
let to_string = to_string |
| 162 |
let compare = compare |
| 163 |
let to_json = to_json |
| 164 |
let of_json = of_json |
| 165 |
end |
| 166 |
|
| 167 |
module Set = OpamStd.Set.Make(O) |
| 168 |
|
| 169 |
module Map = OpamStd.Map.Make(O) |
| 170 |
|
| 171 |
end |
| 172 |
|
| 173 |
module Name = struct |
| 174 |
|
| 175 |
type t = string |
| 176 |
|
| 177 |
let to_string x = x |
| 178 |
|
| 179 |
let of_string x = |
| 180 |
match |
| 181 |
OpamStd.String.fold_left (fun acc c -> |
| 182 |
if acc = Some false then acc else match c with |
| 183 |
| 'a'..'z' | 'A'..'Z' -> Some true |
| 184 |
| '0'..'9' | '-' | '_' | '+' -> acc |
| 185 |
| _ -> Some false) |
| 186 |
None x |
| 187 |
with |
| 188 |
| Some false -> |
| 189 |
failwith |
| 190 |
(Printf.sprintf "Invalid character in package name %S" x) |
| 191 |
| None -> |
| 192 |
failwith |
| 193 |
(Printf.sprintf "Package name %S should contain at least one letter" x) |
| 194 |
| Some true -> |
| 195 |
x |
| 196 |
|
| 197 |
let compare = OpamStd.String.compare_case |
| 198 |
|
| 199 |
let equal n1 n2 = |
| 200 |
compare n1 n2 = 0 |
| 201 |
|
| 202 |
let to_json x = `String x |
| 203 |
let of_json = function |
| 204 |
| `String s -> (try Some (of_string s) with _ -> None) |
| 205 |
| _ -> None |
| 206 |
|
| 207 |
module O = struct |
| 208 |
type t = string |
| 209 |
let to_string = to_string |
| 210 |
let compare = compare |
| 211 |
let to_json = to_json |
| 212 |
let of_json = of_json |
| 213 |
end |
| 214 |
|
| 215 |
module Set = OpamStd.Set.Make(O) |
| 216 |
|
| 217 |
module Map = OpamStd.Map.Make(O) |
| 218 |
|
| 219 |
end |
| 220 |
|
| 221 |
type t = { |
| 222 |
name : Name.t; |
| 223 |
version: Version.t; |
| 224 |
} |
| 225 |
|
| 226 |
let create name version = { name; version } |
| 227 |
|
| 228 |
let name_to_string t = Name.to_string t.name |
| 229 |
let version_to_string t = Version.to_string t.version |
| 230 |
|
| 231 |
let name t = t.name |
| 232 |
|
| 233 |
let version t = t.version |
| 234 |
|
| 235 |
let sep = '.' |
| 236 |
|
| 237 |
let of_string_opt s = |
| 238 |
if OpamStd.String.contains_char s ' ' || |
| 239 |
OpamStd.String.contains_char s '\n' then |
| 240 |
None |
| 241 |
else match OpamStd.String.cut_at s sep with |
| 242 |
| None -> None |
| 243 |
| Some (n, v) -> |
| 244 |
try Some { name = Name.of_string n; version = Version.of_string v } |
| 245 |
with Failure _ -> None |
| 246 |
|
| 247 |
let of_string s = match of_string_opt s with |
| 248 |
| Some x -> x |
| 249 |
| None -> failwith "OpamPackage.of_string" |
| 250 |
|
| 251 |
let to_string t = |
| 252 |
match Version.to_string t.version with |
| 253 |
| "" -> Name.to_string t.name |
| 254 |
| _ -> Printf.sprintf "%s%c%s" (Name.to_string t.name) sep (Version.to_string t.version) |
| 255 |
|
| 256 |
let compare nv1 nv2 = |
| 257 |
match Name.compare nv1.name nv2.name with |
| 258 |
| 0 -> Version.compare nv1.version nv2.version |
| 259 |
| i -> i |
| 260 |
|
| 261 |
let hash nv = Hashtbl.hash nv |
| 262 |
|
| 263 |
let equal nv1 nv2 = |
| 264 |
compare nv1 nv2 = 0 |
| 265 |
|
| 266 |
let to_json nv = |
| 267 |
`O [ ("name", Name.to_json (name nv)); |
| 268 |
("version", Version.to_json (version nv)); |
| 269 |
] |
| 270 |
let of_json = function |
| 271 |
| `O dict -> |
| 272 |
(try |
| 273 |
let open OpamStd.Option.Op in |
| 274 |
Name.of_json (OpamStd.List.assoc String.equal "name" dict) |
| 275 |
>>= fun name -> |
| 276 |
Version.of_json (OpamStd.List.assoc String.equal "version" dict) |
| 277 |
>>= fun version -> Some {name; version} |
| 278 |
with Not_found -> None) |
| 279 |
| _ -> None |
| 280 |
|
| 281 |
module O = struct |
| 282 |
type tmp = t |
| 283 |
type t = tmp |
| 284 |
let compare p1 p2 = |
| 285 |
let r = Name.compare p1.name p2.name in |
| 286 |
if r = 0 then Version.compare p1.version p2.version else r |
| 287 |
let to_string = to_string |
| 288 |
let to_json = to_json |
| 289 |
let of_json = of_json |
| 290 |
end |
| 291 |
|
| 292 |
module Set = OpamStd.Set.Make (O) |
| 293 |
|
| 294 |
module Map = OpamStd.Map.Make (O) |
| 295 |
|
| 296 |
let to_map nv = |
| 297 |
Set.fold (fun nv map -> |
| 298 |
let name = name nv in |
| 299 |
let version = version nv in |
| 300 |
try Name.Map.add name |
| 301 |
(Version.Set.add version (Name.Map.find name map)) map |
| 302 |
with Not_found -> Name.Map.add name (Version.Set.singleton version) map |
| 303 |
) nv Name.Map.empty |
| 304 |
|
| 305 |
let of_map nvm = |
| 306 |
Name.Map.fold (fun n -> Version.Set.fold (fun v -> Set.add (create n v))) |
| 307 |
nvm Set.empty |
| 308 |
|
| 309 |
let keys map = |
| 310 |
Map.fold (fun nv _ set -> Set.add nv set) map Set.empty |
| 311 |
|
| 312 |
(* $DIR/$NAME.$VERSION/ *) |
| 313 |
let of_dirname f = |
| 314 |
f |
| 315 |
|> OpamFilename.basename_dir |
| 316 |
|> OpamFilename.Base.to_string |
| 317 |
|> of_string_opt |
| 318 |
|
| 319 |
(* $DIR/$NAME.$VERSION/opam *) |
| 320 |
let of_filename f = |
| 321 |
if OpamFilename.basename f = OpamFilename.Base.of_string "opam" then |
| 322 |
of_dirname (OpamFilename.dirname f) |
| 323 |
else if OpamFilename.check_suffix f ".opam" then |
| 324 |
of_string_opt OpamFilename.(Base.to_string (basename (chop_extension f))) |
| 325 |
else |
| 326 |
None |
| 327 |
|
| 328 |
(* $NAME.$VERSION+opam.tar.gz *) |
| 329 |
let of_archive f = |
| 330 |
let base = OpamFilename.basename f in |
| 331 |
match OpamStd.String.cut_at (OpamFilename.Base.to_string base) '+' with |
| 332 |
| None -> None |
| 333 |
| Some (s,_) -> of_string_opt s |
| 334 |
|
| 335 |
let list dir = |
| 336 |
log "list %a" (slog OpamFilename.Dir.to_string) dir; |
| 337 |
if OpamFilename.exists_dir dir then ( |
| 338 |
let files = OpamFilename.rec_files dir in |
| 339 |
List.fold_left (fun set f -> |
| 340 |
match of_filename f with |
| 341 |
| None -> set |
| 342 |
| Some p -> |
| 343 |
if not (Set.mem p set) then Set.add p set |
| 344 |
else |
| 345 |
let suffix = Filename.concat (to_string p) "opam" in |
| 346 |
let files = List.filter (OpamFilename.ends_with suffix) files in |
| 347 |
OpamConsole.error_and_exit `File_error |
| 348 |
"Multiple definition of package %s in %s:\n%s" |
| 349 |
(to_string p) (OpamFilename.Dir.to_string dir) |
| 350 |
(OpamStd.Format.itemize ~bullet:"" OpamFilename.to_string files); |
| 351 |
) Set.empty files |
| 352 |
) else |
| 353 |
Set.empty |
| 354 |
|
| 355 |
let prefixes repodir = |
| 356 |
log "prefixes %a" (slog OpamFilename.Dir.to_string) repodir; |
| 357 |
if OpamFilename.exists_dir repodir then ( |
| 358 |
let files = OpamFilename.rec_files repodir in |
| 359 |
List.fold_left (fun map f -> |
| 360 |
match of_filename f with |
| 361 |
| None -> map |
| 362 |
| Some p -> |
| 363 |
let pkgdir = OpamFilename.dirname_dir (OpamFilename.dirname f) in |
| 364 |
let prefix = |
| 365 |
match OpamFilename.remove_prefix_dir repodir pkgdir with |
| 366 |
| "" -> None |
| 367 |
| p -> Some p |
| 368 |
in |
| 369 |
Map.add p prefix map |
| 370 |
) Map.empty files |
| 371 |
) else |
| 372 |
Map.empty |
| 373 |
|
| 374 |
let versions_of_packages nvset = |
| 375 |
Set.fold |
| 376 |
(fun nv vset -> Version.Set.add (version nv) vset) |
| 377 |
nvset |
| 378 |
Version.Set.empty |
| 379 |
|
| 380 |
let has_name nvset n = |
| 381 |
Set.exists (fun nv -> name nv = n) nvset |
| 382 |
|
| 383 |
let names_of_packages nvset = |
| 384 |
Set.fold |
| 385 |
(fun nv vset -> Name.Set.add (name nv) vset) |
| 386 |
nvset |
| 387 |
Name.Set.empty |
| 388 |
|
| 389 |
let package_of_name_aux empty split filter nv n = |
| 390 |
if n = "" then empty else |
| 391 |
let inf = {name = String.sub n 0 (String.length n - 1); version= Version.empty} in |
| 392 |
let sup = {name = n^"\000"; version = Version.empty} in |
| 393 |
let _, _, nv = split inf nv in |
| 394 |
let nv, _, _ = split sup nv in |
| 395 |
filter nv |
| 396 |
|
| 397 |
let packages_of_name nv n = |
| 398 |
package_of_name_aux Set.empty Set.split |
| 399 |
(Set.filter (fun nv -> nv.name = n)) |
| 400 |
nv n |
| 401 |
|
| 402 |
let packages_of_name_map nv n = |
| 403 |
package_of_name_aux Map.empty Map.split |
| 404 |
(Map.filter (fun nv _ -> nv.name = n)) |
| 405 |
nv n |
| 406 |
|
| 407 |
let package_of_name nvset n = |
| 408 |
Set.choose (packages_of_name nvset n) |
| 409 |
|
| 410 |
let package_of_name_opt nvset n = |
| 411 |
try Some (package_of_name nvset n) with Not_found -> None |
| 412 |
|
| 413 |
let packages_of_names nvset nameset = |
| 414 |
Name.Set.fold |
| 415 |
(fun name acc -> |
| 416 |
Set.union acc (packages_of_name nvset name)) |
| 417 |
nameset Set.empty |
| 418 |
|
| 419 |
let versions_of_name packages n = |
| 420 |
versions_of_packages |
| 421 |
(packages_of_name packages n) |
| 422 |
|
| 423 |
let filter_name_out packages name = |
| 424 |
Set.diff packages (packages_of_name packages name) |
| 425 |
|
| 426 |
let max_version set name = |
| 427 |
let versions = versions_of_name set name in |
| 428 |
let version = Version.Set.max_elt versions in |
| 429 |
create name version |