Skip to content

Commit

Permalink
initial import of ocaml-print-intf
Browse files Browse the repository at this point in the history
  • Loading branch information
avsm committed Jan 8, 2020
0 parents commit fa0bc5d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.merlin
_build
*.install
19 changes: 19 additions & 0 deletions README.md
@@ -0,0 +1,19 @@
# ocaml-print-intf

Pretty prints a compiled interface file into the corresponding human-readable
OCaml signature.

This is a convenient alternative to `ocamlc -i` which does this on source .ml
files.

## Example

On this repository:

```
$ dune build
$ dune exec -- ocaml-print-intf ./_build/default/.ocaml_print_intf.eobjs/byte/dune__exe__Ocaml_print_intf.cmt
val print_intf : string -> unit
val version : unit -> string
val usage : unit -> unit
```
4 changes: 4 additions & 0 deletions dune
@@ -0,0 +1,4 @@
(executable
(name ocaml_print_intf)
(public_name ocaml-print-intf)
(libraries compiler-libs.common dune-build-info))
20 changes: 20 additions & 0 deletions dune-project
@@ -0,0 +1,20 @@
(lang dune 2.0)
(generate_opam_files true)
(name ocaml-print-intf)

(source (github avsm/ocaml-print-intf))
(license ISC)
(authors "Anil Madhavapeddy")
(maintainers "anil@recoil.org")

(package
(name ocaml-print-intf)
(synopsis "Display human-readable OCaml interface from a compiled .cmi")
(description "This tool parses a compiled .cmi interface file and outputs
the corresponding textual .mli file. This can be useful to quickly generate
a skeleton interface file to then annotate with comments or add abstraction.")
(depends
(ocaml (>= 4.03))
compiler-libs.common
dune-build-info))

33 changes: 33 additions & 0 deletions ocaml-print-intf.opam
@@ -0,0 +1,33 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
synopsis: "Display human-readable OCaml interface from a compiled .cmi"
description: """
This tool parses a compiled .cmi interface file and outputs
the corresponding textual .mli file. This can be useful to quickly generate
a skeleton interface file to then annotate with comments or add abstraction."""
maintainer: ["anil@recoil.org"]
authors: ["Anil Madhavapeddy"]
license: "ISC"
homepage: "https://github.com/avsm/ocaml-print-intf"
bug-reports: "https://github.com/avsm/ocaml-print-intf/issues"
depends: [
"dune" {>= "2.0"}
"ocaml" {>= "4.03"}
"compiler-libs.common"
"dune-build-info"
]
build: [
["dune" "subst"] {pinned}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/avsm/ocaml-print-intf.git"
42 changes: 42 additions & 0 deletions ocaml_print_intf.ml
@@ -0,0 +1,42 @@
(* Copyright (c) 2020 Anil Madhavapeddy <anil@recoil.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Format

let print_intf f =
let {Cmi_format.cmi_sign;_} = Cmi_format.read_cmi f in
Printtyp.signature std_formatter cmi_sign;
print_flush ();
print_newline ();
flush stdout

let version () =
match Build_info.V1.version () with
| None -> "n/a"
| Some v -> Build_info.V1.Version.to_string v

let usage () =
eprintf "%s <file>\n<file> can be a .cmi or .cmt or .cmti compiled with OCaml %s\n"
Sys.argv.(0) Sys.ocaml_version

let () =
match Sys.argv with
| [| _; "--version" |] -> print_endline (version ())
| [| _; file |] -> begin
try print_intf file
with Sys_error err -> prerr_endline err; exit 1
| Cmi_format.Error _ -> prerr_endline "Error: not a valid .cmi .cmt or .cmti file"; exit 1
end
| _ -> usage (); exit 1

0 comments on commit fa0bc5d

Please sign in to comment.