Debian Codegraph @

tools/dune_rule_gen/path.ml

raw symbols SCIP
1 (************************************************************************)
2 (* This file is licensed under The MIT License *)
3 (* See LICENSE for more information *)
4 (************************************************************************)
5
6 (* We distinguish Path arguments as to adjust for subdirs in Coq
7 theory evaluation *)
8
9 type t = Rel of string | Abs of string
10
11 let to_string = function Rel p -> p | Abs p -> p
12
13 (* paths which begin with a dune variable are considered absolute
14 (eg %{project_root}/bla) *)
15 let is_relative path =
16 not (CString.is_prefix "%{" path) && Filename.is_relative path
17
18 let make path = if is_relative path then Rel path else Abs path
19
20 let map ~f = function Rel p -> Rel (f p) | Abs p -> Abs (f p)
21
22 let fold ~f = function Rel p -> f p | Abs p -> f p
23
24 let relative dir path = map dir ~f:(fun dir -> Filename.concat dir path)
25
26 let gen_sub n = String.concat Filename.dir_sep @@ List.init n (fun _ -> "..")
27
28 let adjust ~lvl = function
29 | Rel path -> Rel (Filename.concat (gen_sub lvl) path)
30 | Abs path -> Abs path
31
32 let compare p1 p2 = match p1, p2 with
33 | Rel p1, Rel p2 -> String.compare p1 p2
34 | Abs p1, Abs p2 -> String.compare p1 p2
35 | Rel _, Abs _ -> -1
36 | Abs _, Rel _ -> 1
37
38 let pp fmt = function
39 | Rel p -> Format.fprintf fmt "%s" p
40 | Abs p -> Format.fprintf fmt "%s" p
41
42 let basename = fold ~f:Filename.basename
43
44 let add_extension ~ext = map ~f:(fun p -> p ^ ext)
45
46 let replace_ext ~ext = map ~f:(fun p -> Filename.remove_extension p ^ ext)
47
48 (* Coqdep strips files with dirname "." of the basename, so we need
49 to fixup that until we can fix coqdep... [fixing coqdep breaks dune] *)
50 let coqdep_fixup_dir = function
51 | Abs file -> Abs file
52 | Rel file ->
53 match Filename.dirname file with
54 | "." -> Rel (Filename.basename file)
55 | _ -> Rel file