1 (**************************************************************************)
2 (* *)
3 (* OCaml *)
4 (* *)
5 (* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
6 (* *)
7 (* Copyright 1996 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 Clflags
17 open Compenv
18
19 module Backend = struct
20 (* See backend_intf.mli. *)
21
22 let symbol_for_global' = Compilenv.symbol_for_global'
23 let closure_symbol = Compilenv.closure_symbol
24
25 let really_import_approx = Import_approx.really_import_approx
26 let import_symbol = Import_approx.import_symbol
27
28 let size_int = Arch.size_int
29 let big_endian = Arch.big_endian
30
31 let max_sensible_number_of_arguments =
32 (* The "-1" is to allow for a potential closure environment parameter. *)
33 Proc.max_arguments_for_tailcalls - 1
34 end
35 let backend = (module Backend : Backend_intf.S)
36
37 let usage = "Usage: ocamlopt <options> <files>\nOptions are:"
38
39 module Options = Main_args.Make_optcomp_options (Main_args.Default.Optmain)
40 let main () =
41 native_code := true;
42 let ppf = Format.err_formatter in
43 try
44 readenv ppf Before_args;
45 Clflags.add_arguments __LOC__ (Arch.command_line_options @ Options.list);
46 Clflags.add_arguments __LOC__
47 ["-depend", Arg.Unit Makedepend.main_from_option,
48 "<options> Compute dependencies \
49 (use 'ocamlopt -depend -help' for details)"];
50 Clflags.parse_arguments anonymous usage;
51 Compmisc.read_clflags_from_env ();
52 if !Clflags.plugin then
53 fatal "-plugin is only supported up to OCaml 4.08.0";
54 begin try
55 Compenv.process_deferred_actions
56 (ppf,
57 Optcompile.implementation ~backend,
58 Optcompile.interface,
59 ".cmx",
60 ".cmxa");
61 with Arg.Bad msg ->
62 begin
63 prerr_endline msg;
64 Clflags.print_arguments usage;
65 exit 2
66 end
67 end;
68 readenv ppf Before_link;
69 if
70 List.length (List.filter (fun x -> !x)
71 [make_package; make_archive; shared;
72 compile_only; output_c_object]) > 1
73 then
74 fatal "Please specify at most one of -pack, -a, -shared, -c, \
75 -output-obj";
76 if !make_archive then begin
77 Compmisc.init_path ();
78 let target = extract_output !output_name in
79 Asmlibrarian.create_archive
80 (get_objfiles ~with_ocamlparam:false) target;
81 Warnings.check_fatal ();
82 end
83 else if !make_package then begin
84 Compmisc.init_path ();
85 let target = extract_output !output_name in
86 Compmisc.with_ppf_dump ~file_prefix:target (fun ppf_dump ->
87 Asmpackager.package_files ~ppf_dump (Compmisc.initial_env ())
88 (get_objfiles ~with_ocamlparam:false) target ~backend);
89 Warnings.check_fatal ();
90 end
91 else if !shared then begin
92 Compmisc.init_path ();
93 let target = extract_output !output_name in
94 Compmisc.with_ppf_dump ~file_prefix:target (fun ppf_dump ->
95 Asmlink.link_shared ~ppf_dump
96 (get_objfiles ~with_ocamlparam:false) target);
97 Warnings.check_fatal ();
98 end
99 else if not !compile_only && !objfiles <> [] then begin
100 let target =
101 if !output_c_object then
102 let s = extract_output !output_name in
103 if (Filename.check_suffix s Config.ext_obj
104 || Filename.check_suffix s Config.ext_dll)
105 then s
106 else
107 fatal
108 (Printf.sprintf
109 "The extension of the output file must be %s or %s"
110 Config.ext_obj Config.ext_dll
111 )
112 else
113 default_output !output_name
114 in
115 Compmisc.init_path ();
116 Compmisc.with_ppf_dump ~file_prefix:target (fun ppf_dump ->
117 Asmlink.link ~ppf_dump (get_objfiles ~with_ocamlparam:true) target);
118 Warnings.check_fatal ();
119 end;
120 with x ->
121 Location.report_exception ppf x;
122 exit 2
123
124 let () =
125 main ();
126 Profile.print Format.std_formatter !Clflags.profile_columns;
127 exit 0
128