|
| 1 | +(****************************************************************************** |
| 2 | + * flambda-backend * |
| 3 | + * Simon Spies, Jane Street * |
| 4 | + * -------------------------------------------------------------------------- * |
| 5 | + * MIT License * |
| 6 | + * * |
| 7 | + * Copyright (c) 2025 Jane Street Group LLC * |
| 8 | + * opensource-contacts@janestreet.com * |
| 9 | + * * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a * |
| 11 | + * copy of this software and associated documentation files (the "Software"), * |
| 12 | + * to deal in the Software without restriction, including without limitation * |
| 13 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * |
| 14 | + * and/or sell copies of the Software, and to permit persons to whom the * |
| 15 | + * Software is furnished to do so, subject to the following conditions: * |
| 16 | + * * |
| 17 | + * The above copyright notice and this permission notice shall be included * |
| 18 | + * in all copies or substantial portions of the Software. * |
| 19 | + * * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * |
| 23 | + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * |
| 25 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * |
| 26 | + * DEALINGS IN THE SOFTWARE. * |
| 27 | + ******************************************************************************) |
| 28 | + |
| 29 | +open! Value_shapes |
| 30 | + |
| 31 | +(* Argument Parsing *) |
| 32 | +let easily_readable = ref false |
| 33 | + |
| 34 | +let verbose = ref false |
| 35 | + |
| 36 | +let output_file = ref None |
| 37 | + |
| 38 | +let include_dirs = ref [] |
| 39 | + |
| 40 | +let hidden_include_dirs = ref [] |
| 41 | + |
| 42 | +let open_modules = ref [] |
| 43 | + |
| 44 | +let files = ref [] |
| 45 | + |
| 46 | +let spec_list = |
| 47 | + [ "-readable", Arg.Set easily_readable, "Output in easily readable format"; |
| 48 | + "-verbose", Arg.Set verbose, "Print errors instead of failing silently"; |
| 49 | + ( "-output-file", |
| 50 | + Arg.String (fun s -> output_file := Some s), |
| 51 | + "Optional output file; prints to stdout if not present" ); |
| 52 | + ( "-I", |
| 53 | + Arg.String |
| 54 | + (fun s -> |
| 55 | + include_dirs |
| 56 | + := List.rev_append (String.split_on_char ',' s) !include_dirs), |
| 57 | + "A directory with .cmi files to include for lookups" ); |
| 58 | + ( "-H", |
| 59 | + Arg.String |
| 60 | + (fun s -> |
| 61 | + hidden_include_dirs |
| 62 | + := List.rev_append (String.split_on_char ',' s) !hidden_include_dirs), |
| 63 | + "Hidden includes" ); |
| 64 | + ( "-open", |
| 65 | + Arg.String |
| 66 | + (fun s -> |
| 67 | + open_modules |
| 68 | + := List.rev_append (String.split_on_char ',' s) !open_modules), |
| 69 | + "Modules to open" ) ] |
| 70 | + |
| 71 | +let parse_arguments () = |
| 72 | + Arg.parse spec_list |
| 73 | + (fun a -> files := !files @ [a]) |
| 74 | + "Usage: externals.exe <options> <files>\nOptions are:" |
| 75 | + |
| 76 | +(* Pretty Printing for Externals in Readable Format*) |
| 77 | + |
| 78 | +let pp_ext_funs ~readable fmt extfuns = |
| 79 | + if readable |
| 80 | + then Value_shapes.print_extfuns_readable fmt extfuns |
| 81 | + else Value_shapes.print_extfuns fmt extfuns |
| 82 | + |
| 83 | +let output_shapes ~output_file ~readable externals = |
| 84 | + match output_file with |
| 85 | + | None -> pp_ext_funs ~readable Format.std_formatter externals |
| 86 | + | Some file -> |
| 87 | + Out_channel.with_open_bin file (fun out -> |
| 88 | + let fmt = Format.formatter_of_out_channel out in |
| 89 | + pp_ext_funs ~readable fmt externals; |
| 90 | + Format.pp_print_newline fmt (); |
| 91 | + Out_channel.flush out) |
| 92 | + |
| 93 | +(* Typed Extraction *) |
| 94 | +let extract_shapes_from_cmt ~verbose file = |
| 95 | + match Cmt_format.read_cmt file with |
| 96 | + | exception Sys_error s -> |
| 97 | + if verbose |
| 98 | + then Format.eprintf "Exception raised while reading .cmt file: %s\n" s; |
| 99 | + [] |
| 100 | + | exception _ -> |
| 101 | + if verbose |
| 102 | + then Format.eprintf "Exception raised while reading .cmt file %s\n" file; |
| 103 | + [] |
| 104 | + | { cmt_annots = Implementation tt; _ } -> |
| 105 | + Traverse_typed_tree.extract_from_typed_tree tt |
| 106 | + | _ -> assert false |
| 107 | + |
| 108 | +let extract_shapes_from_cmts ~includes ~verbose files = |
| 109 | + Clflags.include_dirs := includes @ !Clflags.include_dirs; |
| 110 | + Clflags.open_modules := !open_modules @ !Clflags.open_modules; |
| 111 | + Clflags.hidden_include_dirs |
| 112 | + := !hidden_include_dirs @ !Clflags.hidden_include_dirs; |
| 113 | + Compmisc.init_path (); |
| 114 | + List.iter |
| 115 | + (fun file -> |
| 116 | + if not (String.ends_with file ~suffix:".cmt") |
| 117 | + then Misc.fatal_errorf "File %s is not a .cmt file; aborting\n" file) |
| 118 | + files; |
| 119 | + List.concat_map (extract_shapes_from_cmt ~verbose) files |
| 120 | + |
| 121 | +let externals_version = "v0.1" |
| 122 | + |
| 123 | +let extract_and_output_from_cmts ~readable ~includes ~output_file ~verbose files |
| 124 | + = |
| 125 | + let externals = extract_shapes_from_cmts ~includes ~verbose files in |
| 126 | + output_shapes ~output_file ~readable |
| 127 | + { version = externals_version; extfuns = externals } |
| 128 | + |
| 129 | +let _ = |
| 130 | + parse_arguments (); |
| 131 | + extract_and_output_from_cmts ~readable:!easily_readable |
| 132 | + ~includes:!include_dirs ~output_file:!output_file ~verbose:!verbose !files |
0 commit comments