Add copyright notices and new function String.chomp
[ocaml.git] / camlp4 / mkcamlp4.ml
blobca19dd70fd3c205e1090092dacd67a3109e628b4
1 (****************************************************************************)
2 (* *)
3 (* Objective Caml *)
4 (* *)
5 (* INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 2006 Institut National de Recherche en Informatique et *)
8 (* en Automatique. All rights reserved. This file is distributed under *)
9 (* the terms of the GNU Library General Public License, with the special *)
10 (* exception on linking described in LICENSE at the top of the Objective *)
11 (* Caml source tree. *)
12 (* *)
13 (****************************************************************************)
15 (* Authors:
16 * - Daniel de Rauglaudre: initial shell version
17 * - Nicolas Pouillard: rewriting in OCaml
20 (* $Id$ *)
22 open Camlp4;
23 open Camlp4_config;
24 open Filename;
25 open Format;
27 value (interfaces, options, includes) =
28 let rec self (interf, opts, incl) =
29 fun
30 [ [] -> (List.rev interf, List.rev opts, List.rev incl)
31 | ["-I"; dir :: args] -> self (interf, opts, [dir; "-I" :: incl]) args
32 | ["-version" :: _] ->
33 do { printf "mkcamlp4, version %s@." version; exit 0 }
34 | [ arg :: args ] when check_suffix arg ".cmi" ->
35 let basename = String.capitalize (Filename.chop_suffix
36 (Filename.basename arg) ".cmi") in
37 self ([ basename :: interf ], opts, incl) args
38 | [ arg :: args ] ->
39 self (interf, [ arg :: opts ], incl) args ]
40 in self ([], [], ["."; "-I"]) (List.tl (Array.to_list Sys.argv));
42 value run l =
43 let cmd = String.concat " " l in
44 let () = Format.printf "%s@." cmd in
45 let st =
46 Sys.command cmd
47 (* 0 *)
49 if st <> 0 then failwith ("Exit: " ^ string_of_int st) else ();
51 value crc_ml = Filename.temp_file "crc_" ".ml";
52 value crc = Filename.chop_suffix crc_ml ".ml";
53 value clean () = run ["rm"; "-f"; crc_ml; crc^".cmi"; crc^".cmo"];
55 try do {
56 run ([ocaml_standard_library^"/extract_crc"; "-I"; camlp4_standard_library]
57 @ includes @ interfaces @ [">"; crc_ml]);
59 let cout = open_out_gen [Open_wronly; Open_append; Open_text] 0o666 crc_ml in do {
60 output_string cout "let _ = Dynlink.add_available_units crc_unit_list\n";
61 close_out cout
64 run (["ocamlc"; "-I"; camlp4_standard_library; "Camlp4.cma"; crc_ml]
65 @ includes @ options @ ["Camlp4Bin.cmo"; "-linkall"]);
66 clean();
68 with exc -> do { clean (); raise exc };