Skip to content

Commit 088fa01

Browse files
authored
Merge pull request ocaml#9688 from stedolan/main-compiler-libs
Expose the main entrypoint in compilerlibs
2 parents 0616261 + 34ead63 commit 088fa01

15 files changed

Lines changed: 333 additions & 304 deletions

.depend

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5856,6 +5856,27 @@ driver/errors.cmx : \
58565856
driver/errors.cmi
58575857
driver/errors.cmi :
58585858
driver/main.cmo : \
5859+
driver/maindriver.cmi
5860+
driver/main.cmx : \
5861+
driver/maindriver.cmx
5862+
driver/main_args.cmo : \
5863+
utils/warnings.cmi \
5864+
utils/profile.cmi \
5865+
utils/misc.cmi \
5866+
utils/config.cmi \
5867+
driver/compenv.cmi \
5868+
utils/clflags.cmi \
5869+
driver/main_args.cmi
5870+
driver/main_args.cmx : \
5871+
utils/warnings.cmx \
5872+
utils/profile.cmx \
5873+
utils/misc.cmx \
5874+
utils/config.cmx \
5875+
driver/compenv.cmx \
5876+
utils/clflags.cmx \
5877+
driver/main_args.cmi
5878+
driver/main_args.cmi :
5879+
driver/maindriver.cmo : \
58595880
utils/warnings.cmi \
58605881
utils/profile.cmi \
58615882
driver/makedepend.cmi \
@@ -5869,8 +5890,8 @@ driver/main.cmo : \
58695890
bytecomp/bytepackager.cmi \
58705891
bytecomp/bytelink.cmi \
58715892
bytecomp/bytelibrarian.cmi \
5872-
driver/main.cmi
5873-
driver/main.cmx : \
5893+
driver/maindriver.cmi
5894+
driver/maindriver.cmx : \
58745895
utils/warnings.cmx \
58755896
utils/profile.cmx \
58765897
driver/makedepend.cmx \
@@ -5884,25 +5905,8 @@ driver/main.cmx : \
58845905
bytecomp/bytepackager.cmx \
58855906
bytecomp/bytelink.cmx \
58865907
bytecomp/bytelibrarian.cmx \
5887-
driver/main.cmi
5888-
driver/main.cmi :
5889-
driver/main_args.cmo : \
5890-
utils/warnings.cmi \
5891-
utils/profile.cmi \
5892-
utils/misc.cmi \
5893-
utils/config.cmi \
5894-
driver/compenv.cmi \
5895-
utils/clflags.cmi \
5896-
driver/main_args.cmi
5897-
driver/main_args.cmx : \
5898-
utils/warnings.cmx \
5899-
utils/profile.cmx \
5900-
utils/misc.cmx \
5901-
utils/config.cmx \
5902-
driver/compenv.cmx \
5903-
utils/clflags.cmx \
5904-
driver/main_args.cmi
5905-
driver/main_args.cmi :
5908+
driver/maindriver.cmi
5909+
driver/maindriver.cmi :
59065910
driver/makedepend.cmo : \
59075911
driver/pparse.cmi \
59085912
parsing/parsetree.cmi \
@@ -5972,6 +5976,10 @@ driver/opterrors.cmx : \
59725976
driver/opterrors.cmi
59735977
driver/opterrors.cmi :
59745978
driver/optmain.cmo : \
5979+
driver/optmaindriver.cmi
5980+
driver/optmain.cmx : \
5981+
driver/optmaindriver.cmx
5982+
driver/optmaindriver.cmo : \
59755983
utils/warnings.cmi \
59765984
utils/profile.cmi \
59775985
asmcomp/proc.cmi \
@@ -5990,8 +5998,8 @@ driver/optmain.cmo : \
59905998
asmcomp/asmlink.cmi \
59915999
asmcomp/asmlibrarian.cmi \
59926000
asmcomp/arch.cmo \
5993-
driver/optmain.cmi
5994-
driver/optmain.cmx : \
6001+
driver/optmaindriver.cmi
6002+
driver/optmaindriver.cmx : \
59956003
utils/warnings.cmx \
59966004
utils/profile.cmx \
59976005
asmcomp/proc.cmx \
@@ -6010,8 +6018,8 @@ driver/optmain.cmx : \
60106018
asmcomp/asmlink.cmx \
60116019
asmcomp/asmlibrarian.cmx \
60126020
asmcomp/arch.cmx \
6013-
driver/optmain.cmi
6014-
driver/optmain.cmi :
6021+
driver/optmaindriver.cmi
6022+
driver/optmaindriver.cmi :
60156023
driver/pparse.cmo : \
60166024
utils/warnings.cmi \
60176025
utils/profile.cmi \

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ Working version
218218
(Nicolás Ojeda Bär, review by David Allsopp, Sébastien Hinderer and Gabriel
219219
Scherer)
220220

221+
- #9688: Expose the main entrypoint in compilerlibs
222+
(Stephen Dolan, review by Nicolás Ojeda Bär, Greta Yorsh and David Allsopp)
223+
221224
### Build system:
222225

223226
- #7121, #9558: Always the autoconf-discovered ld in PACKLD. For

compilerlibs/Makefile.compilerlibs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ COMMON=$(UTILS) $(PARSING) $(TYPING) $(LAMBDA) $(COMP)
9999
BYTECOMP=bytecomp/instruct.cmo bytecomp/bytegen.cmo \
100100
bytecomp/printinstr.cmo bytecomp/emitcode.cmo \
101101
bytecomp/bytelink.cmo bytecomp/bytelibrarian.cmo bytecomp/bytepackager.cmo \
102-
driver/errors.cmo driver/compile.cmo
102+
driver/errors.cmo driver/compile.cmo driver/maindriver.cmo
103103
BYTECOMP_CMI=
104104

105105
INTEL_ASM=\
@@ -153,7 +153,7 @@ ASMCOMP=\
153153
asmcomp/branch_relaxation.cmo \
154154
asmcomp/emitaux.cmo asmcomp/emit.cmo asmcomp/asmgen.cmo \
155155
asmcomp/asmlink.cmo asmcomp/asmlibrarian.cmo asmcomp/asmpackager.cmo \
156-
driver/opterrors.cmo driver/optcompile.cmo
156+
driver/opterrors.cmo driver/optcompile.cmo driver/optmaindriver.cmo
157157
ASMCOMP_CMI=$(ARCH_SPECIFIC_ASMCOMP_CMI)
158158

159159
# Files under middle_end/ are not to reference files under asmcomp/.

driver/compenv.ml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
open Clflags
1717

18+
exception Exit_compiler of int
19+
1820
let output_prefix name =
1921
let oname =
2022
match !output_name with
@@ -27,17 +29,19 @@ let print_version_and_library compiler =
2729
print_string Config.version; print_newline();
2830
print_string "Standard library directory: ";
2931
print_string Config.standard_library; print_newline();
30-
exit 0
32+
raise (Exit_compiler 0)
3133

3234
let print_version_string () =
33-
print_string Config.version; print_newline(); exit 0
35+
print_string Config.version; print_newline();
36+
raise (Exit_compiler 0)
3437

3538
let print_standard_library () =
36-
print_string Config.standard_library; print_newline(); exit 0
39+
print_string Config.standard_library; print_newline();
40+
raise (Exit_compiler 0)
3741

3842
let fatal err =
3943
prerr_endline err;
40-
exit 2
44+
raise (Exit_compiler 2)
4145

4246
let extract_output = function
4347
| Some s -> s
@@ -603,7 +607,7 @@ let process_action
603607
| ProcessCFile name ->
604608
readenv ppf (Before_compile name);
605609
Location.input_name := name;
606-
if Ccomp.compile_file name <> 0 then exit 2;
610+
if Ccomp.compile_file name <> 0 then raise (Exit_compiler 2);
607611
ccobjs := c_object_of_filename name :: !ccobjs
608612
| ProcessObjects names ->
609613
ccobjs := names @ !ccobjs

driver/compenv.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
(* *)
1414
(**************************************************************************)
1515

16+
exception Exit_compiler of int
17+
1618
val module_of_filename : string -> string -> string
1719

1820
val output_prefix : string -> string

driver/main.ml

Lines changed: 1 addition & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,2 @@
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-
let usage = "Usage: ocamlc <options> <files>\nOptions are:"
20-
21-
(* Error messages to standard error formatter *)
22-
let ppf = Format.err_formatter
23-
24-
module Options = Main_args.Make_bytecomp_options (Main_args.Default.Main)
25-
26-
let main () =
27-
Clflags.add_arguments __LOC__ Options.list;
28-
Clflags.add_arguments __LOC__
29-
["-depend", Arg.Unit Makedepend.main_from_option,
30-
"<options> Compute dependencies (use 'ocamlc -depend -help' for details)"];
31-
try
32-
readenv ppf Before_args;
33-
Clflags.parse_arguments anonymous usage;
34-
Compmisc.read_clflags_from_env ();
35-
if !Clflags.plugin then
36-
fatal "-plugin is only supported up to OCaml 4.08.0";
37-
begin try
38-
Compenv.process_deferred_actions
39-
(ppf,
40-
Compile.implementation,
41-
Compile.interface,
42-
".cmo",
43-
".cma");
44-
with Arg.Bad msg ->
45-
begin
46-
prerr_endline msg;
47-
Clflags.print_arguments usage;
48-
exit 2
49-
end
50-
end;
51-
readenv ppf Before_link;
52-
if
53-
List.length
54-
(List.filter (fun x -> !x)
55-
[make_archive;make_package;stop_early;output_c_object])
56-
> 1
57-
then begin
58-
let module P = Clflags.Compiler_pass in
59-
match !stop_after with
60-
| None ->
61-
fatal "Please specify at most one of -pack, -a, -c, -output-obj";
62-
| Some ((P.Parsing | P.Typing) as p) ->
63-
assert (P.is_compilation_pass p);
64-
Printf.ksprintf fatal
65-
"Options -i and -stop-after (%s) \
66-
are incompatible with -pack, -a, -output-obj"
67-
(String.concat "|"
68-
(Clflags.Compiler_pass.available_pass_names ~native:false))
69-
| Some P.Scheduling -> assert false (* native only *)
70-
end;
71-
if !make_archive then begin
72-
Compmisc.init_path ();
73-
74-
Bytelibrarian.create_archive
75-
(Compenv.get_objfiles ~with_ocamlparam:false)
76-
(extract_output !output_name);
77-
Warnings.check_fatal ();
78-
end
79-
else if !make_package then begin
80-
Compmisc.init_path ();
81-
let extracted_output = extract_output !output_name in
82-
let revd = get_objfiles ~with_ocamlparam:false in
83-
Compmisc.with_ppf_dump ~file_prefix:extracted_output (fun ppf_dump ->
84-
Bytepackager.package_files ~ppf_dump (Compmisc.initial_env ())
85-
revd (extracted_output));
86-
Warnings.check_fatal ();
87-
end
88-
else if not !stop_early && !objfiles <> [] then begin
89-
let target =
90-
if !output_c_object && not !output_complete_executable then
91-
let s = extract_output !output_name in
92-
if (Filename.check_suffix s Config.ext_obj
93-
|| Filename.check_suffix s Config.ext_dll
94-
|| Filename.check_suffix s ".c")
95-
then s
96-
else
97-
fatal
98-
(Printf.sprintf
99-
"The extension of the output file must be .c, %s or %s"
100-
Config.ext_obj Config.ext_dll
101-
)
102-
else
103-
default_output !output_name
104-
in
105-
Compmisc.init_path ();
106-
Bytelink.link (get_objfiles ~with_ocamlparam:true) target;
107-
Warnings.check_fatal ();
108-
end;
109-
with x ->
110-
Location.report_exception ppf x;
111-
exit 2
112-
1131
let () =
114-
main ();
115-
Profile.print Format.std_formatter !Clflags.profile_columns;
116-
exit 0
2+
exit (Maindriver.main Sys.argv Format.err_formatter)

driver/main_args.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,12 +1875,12 @@ module Default = struct
18751875

18761876
let print_version () =
18771877
Printf.printf "The OCaml toplevel, version %s\n" Sys.ocaml_version;
1878-
exit 0;
1878+
raise (Compenv.Exit_compiler 0);
18791879
;;
18801880

18811881
let print_version_num () =
18821882
Printf.printf "%s\n" Sys.ocaml_version;
1883-
exit 0;
1883+
raise (Compenv.Exit_compiler 0);
18841884
;;
18851885

18861886
let _args (_:string) = (* placeholder: wrap_expand Arg.read_arg *) [||]

0 commit comments

Comments
 (0)