1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 1998 Institut National de Recherche en Informatique et *)
8 (* en Automatique. *)
9 (* *)
10 (* All rights reserved. This file is distributed under the terms of *)
11 (* the GNU Lesser General Public License version 2.1, with the *)
12 (* special exception on linking described in the file LICENSE. *)
13 (* *)
14 (**************************************************************************)
15
16 open Printf
17
18 let make_archive = ref false;;
19 let with_impl = ref false;;
20 let with_intf = ref false;;
21 let with_mli = ref false;;
22 let with_ml = ref false;;
23
24 let process_file filename =
25 if Filename.check_suffix filename ".ml" then with_ml := true;
26 if Filename.check_suffix filename ".mli" then with_mli := true;
27 ;;
28
29 let usage = "Usage: ocamlcp <options> <files>\noptions are:"
30
31 let incompatible o =
32 fprintf stderr "ocamlcp: profiling is incompatible with the %s option\n" o;
33 exit 2
34
35 module Options = Main_args.Make_bytecomp_options (struct
36 include Main_args.Default.Main
37 let _a () = make_archive := true
38 let _impl _ = with_impl := true
39 let _intf _ = with_intf := true
40 let _pp _ = incompatible "-pp"
41 let _ppx _ = incompatible "-ppx"
42 let anonymous = process_file
43 end);;
44
45 let rev_compargs = ref ([] : string list)
46 let rev_profargs = ref ([] : string list)
47
48 let add_profarg s =
49 rev_profargs := (Filename.quote s) :: "-m" :: !rev_profargs
50 ;;
51
52 let anon filename =
53 process_file filename;
54 rev_compargs := Filename.quote filename :: !rev_compargs
55 ;;
56
57 let optlist =
58 ("-P", Arg.String add_profarg,
59 "[afilmt] Profile constructs specified by argument (default fm):\n\
60 \032 a Everything\n\
61 \032 f Function calls and method calls\n\
62 \032 i if ... then ... else\n\
63 \032 l while and for loops\n\
64 \032 m match ... with\n\
65 \032 t try ... with")
66 :: ("-p", Arg.String add_profarg, "[afilmt] Same as option -P")
67 :: Main_args.options_with_command_line_syntax Options.list rev_compargs
68 in
69 Arg.parse_expand optlist anon usage;
70 if !with_impl && !with_intf then begin
71 fprintf stderr "ocamlcp cannot deal with both \"-impl\" and \"-intf\"\n";
72 fprintf stderr "please compile interfaces and implementations separately\n";
73 exit 2;
74 end else if !with_impl && !with_mli then begin
75 fprintf stderr "ocamlcp cannot deal with both \"-impl\" and .mli files\n";
76 fprintf stderr "please compile interfaces and implementations separately\n";
77 exit 2;
78 end else if !with_intf && !with_ml then begin
79 fprintf stderr "ocamlcp cannot deal with both \"-intf\" and .ml files\n";
80 fprintf stderr "please compile interfaces and implementations separately\n";
81 exit 2;
82 end;
83 if !with_impl then rev_profargs := "-impl" :: !rev_profargs;
84 if !with_intf then rev_profargs := "-intf" :: !rev_profargs;
85 let status =
86 Sys.command
87 (Printf.sprintf "ocamlc -pp \"ocamlprof -instrument %s\" %s %s"
88 (String.concat " " (List.rev !rev_profargs))
89 (if !make_archive then "" else "profiling.cmo")
90 (String.concat " " (List.rev !rev_compargs)))
91 in
92 exit status
93 ;;
94