this repo has no description
at main 68 lines 1.9 kB view raw
1(* ocamlobjinfo *) 2 3open Bos 4let ocamlobjinfo = Cmd.v "ocamlobjinfo" 5 6let source_possibilities file = 7 let default = [ file ] in 8 let generated = 9 if Astring.String.is_suffix ~affix:"-gen" file then 10 let pos = String.length file - 4 in 11 [ Astring.String.take ~max:pos file ] 12 else [] 13 in 14 let pp = 15 if Astring.String.is_suffix ~affix:".pp.ml" file then 16 let pos = String.length file - 5 in 17 [ Astring.String.take ~max:pos file ^ "ml" ] 18 else [] 19 in 20 pp @ default @ generated 21 22let get_source file srcdirs = 23 let cmd = Cmd.(ocamlobjinfo % p file) in 24 let lines_res = 25 Worker_pool.submit ("Ocamlobjinfo " ^ Fpath.to_string file) cmd None 26 in 27 let lines = 28 match lines_res with 29 | Ok l -> String.split_on_char '\n' l.output 30 | Error e -> 31 Logs.err (fun m -> 32 m "Error finding source for module %a: %s" Fpath.pp file 33 (Printexc.to_string e)); 34 [] 35 in 36 let f = 37 List.filter_map 38 (fun line -> 39 let affix = "Source file: " in 40 if Astring.String.is_prefix ~affix line then 41 let name = 42 String.sub line (String.length affix) 43 (String.length line - String.length affix) 44 in 45 let name = Fpath.(filename (v name)) in 46 let possibilities = 47 List.map 48 (fun dir -> 49 List.map 50 (fun poss -> Fpath.(dir / poss)) 51 (source_possibilities name)) 52 srcdirs 53 |> List.flatten 54 in 55 List.find_opt 56 (fun f -> 57 Logs.debug (fun m -> m "src: checking %a" Fpath.pp f); 58 Sys.file_exists (Fpath.to_string f)) 59 possibilities 60 else None) 61 lines 62 in 63 match f with 64 | [] -> None 65 | x :: _ :: _ -> 66 Logs.warn (fun m -> m "Multiple source files found for %a" Fpath.pp file); 67 Some x 68 | x :: _ -> Some x