caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Missing the cmi files required to use toplevellib.cma
@ 2009-10-01 12:09 rixed
  2009-10-01 13:54 ` [Caml-list] " Richard Jones
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: rixed @ 2009-10-01 12:09 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 933 bytes --]

While learning OCaml, I just coded a small program that dumps
the full content of a cmi file. I find this more usefull than
ocamlbrowser or to use the toplevel to have a small command line
driven dumper, and it was also a good pretext to have a look
under the cover.

The problem is : most of the usefull types and functions are
installed in the toplevellib.cma but I can't use this without
the proper cmi files (I need config.cmi for cmi_magic_number,
printtyp.cmi and typemod.cmi for printing signatures, but
env.cmi would be nice to have as well for read_signature).

Of course I can use those left in ocaml-3.11.1 directory after
compilation, but having them installed would help the creation
and distribution of such tools.

But maybe there is an other way to use toplevellib.cma that I'm
unaware of ? If not, then why not install these cmi files along
with toplevellib ?

Anyway, if anyone is interrested the tool is attached.

[-- Attachment #2: cmidump.ml --]
[-- Type: text/plain, Size: 2490 bytes --]

open Format

let want_magic = ref false
let want_crc = ref false
let want_name = ref false
let want_flags = ref false
let want_sig = ref false
let simplify_sig = ref false

(* Stolen from env.ml - would be great of exported *)
type flags = Rectypes

let print_flags flags =
	let flag_name = function
	| Rectypes -> "Rectypes" in
	printf "@[Flags@ =@ [@[" ;
	List.iter (fun f -> printf "%s@ " (flag_name f)) flags ;
	printf "]@]@]@."

let print_magic m =
	printf "@[Cmi magic number@ =@ %s (%s)@]@."
		m (if m = Config.cmi_magic_number then "OK" else "Wrong!")

let print_name name = printf "@[Name@ =@ %s@]@." name

let print_crcs crcs =
	let print_crc (modname, crc) = printf "@[%s [%s]@]@ " modname (String.escaped crc) in
	printf "@[CRCs = @[" ; List.iter print_crc crcs ; printf "@]@]@."

let print_sig sign =
	fprintf std_formatter "@[Signature@ =@ @[<1>%a@]@]@."
		Printtyp.signature
		(if !simplify_sig then (Typemod.simplify_signature sign) else sign)

(* Reading persistent structures from .cmi files - Stolen from typing/env.ml
 * We'd rather use this instead of Env.read_signature so that we get all
 * components of the cmi files, not just signature *)

let process_cmi_file filename =
	let show_sig = !want_sig ||
		(not !want_magic && not !want_crc && not !want_name && not !want_flags) in
	let ic = open_in_bin filename in
	let magic_len = String.length (Config.cmi_magic_number) in
	let buffer = String.create magic_len in
	really_input ic buffer 0 magic_len ;
	let (name, sign) = input_value ic in
	let crcs = input_value ic in
	let flags = input_value ic in
	close_in ic ;
	if !want_magic then print_magic buffer ;
	if !want_name  then print_name name ;
	if !want_crc   then print_crcs crcs ;
	if !want_flags then print_flags flags ;
	if show_sig    then print_sig sign

let process_file fname =
	if Filename.check_suffix fname "cmi" then process_cmi_file fname
	else printf "Don't know what to do with file '%s'\n" fname

let _ =
	Arg.parse [
		"-magic",    Arg.Set want_magic,   " Display cmi magic number" ;
		"-name",     Arg.Set want_name,    " Display module name" ;
		"-crc",      Arg.Set want_crc,     " Display CRC" ;
		"-flags",    Arg.Set want_flags,   " Display flags" ;
		"-sig",      Arg.Set want_sig,     " Display signature" ;
		"-simplify", Arg.Set simplify_sig, " Do not simplify the signature" ]
		process_file
		"Syntax :
	cmidump [options] files...
Shows the content of cmi files.
If no option is given, shows only the signature.
Possible options :
"

[-- Attachment #3: Makefile --]
[-- Type: text/plain, Size: 690 bytes --]

OCAMLC   = ocamlfind ocamlc
OCAMLDEP = ocamlfind ocamldep
OCAMLFLAGS    = -w Ae -g

EXTCMI = env.cmi printtyp.cmi typemod.cmi config.cmi

.PHONY: all clean install uninstall

all: checkcmi cmidump

cmidump: cmidump.cmo
	$(OCAMLC) -o $@ /usr/local/lib/ocaml/toplevellib.cma $(OCAMLFLAGS) $^

checkcmi:
	@for i in $(EXTCMI) ; do if ! test -f $$i ; then echo "Ocamlc will fail if it cant access $$i" ; fi ; done

# Common rules
.SUFFIXES: .ml .mli .cmo .cmi .cmx

.ml.cmo:
	$(OCAMLC) $(OCAMLFLAGS) -I /usr/local/lib/ocaml -c $<

.mli.cmi:
	$(OCAMLC) $(OCAMLFLAGS) -c $<

# Clean up
clean:
	rm -f cmidump.cmi *.cmo *.s

# Dependencies
.depend: *.ml
	$(OCAMLDEP) *.ml > .depend

include .depend

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2009-10-01 16:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-01 12:09 Missing the cmi files required to use toplevellib.cma rixed
2009-10-01 13:54 ` [Caml-list] " Richard Jones
2009-10-01 16:03   ` rixed
2009-10-01 14:02 ` Mehdi Dogguy
2009-10-01 16:06   ` rixed
2009-10-01 14:07 ` Gerd Stolpmann
2009-10-01 15:35 ` David Allsopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).