1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-07-02 09:51:07 -07:00
|
|
|
(* The batch compiler *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Config
|
2000-03-06 21:02:33 -08:00
|
|
|
open Format
|
1995-07-02 09:51:07 -07:00
|
|
|
open Typedtree
|
|
|
|
|
|
|
|
(* Initialize the search path.
|
|
|
|
The current directory is always searched first,
|
|
|
|
then the directories specified with the -I option (in command-line order),
|
|
|
|
then the standard library directory. *)
|
|
|
|
|
|
|
|
let init_path () =
|
1997-11-18 09:14:54 -08:00
|
|
|
let dirs =
|
2000-11-07 06:41:12 -08:00
|
|
|
if !Clflags.thread_safe
|
|
|
|
then "+threads" :: !Clflags.include_dirs
|
|
|
|
else !Clflags.include_dirs in
|
|
|
|
let exp_dirs =
|
2000-12-27 21:02:43 -08:00
|
|
|
List.map (expand_directory Config.standard_library) dirs in
|
2000-11-07 06:41:12 -08:00
|
|
|
load_path := "" :: List.rev (Config.standard_library :: exp_dirs);
|
1995-07-02 09:51:07 -07:00
|
|
|
Env.reset_cache()
|
|
|
|
|
|
|
|
(* Return the initial environment in which compilation proceeds. *)
|
|
|
|
|
|
|
|
let initial_env () =
|
|
|
|
init_path();
|
|
|
|
try
|
|
|
|
if !Clflags.nopervasives
|
|
|
|
then Env.initial
|
|
|
|
else Env.open_pers_signature "Pervasives" Env.initial
|
|
|
|
with Not_found ->
|
|
|
|
fatal_error "cannot open Pervasives.cmi"
|
|
|
|
|
1996-04-25 09:06:43 -07:00
|
|
|
(* Optionally preprocess a source file *)
|
1995-07-02 09:51:07 -07:00
|
|
|
|
1996-04-25 09:06:43 -07:00
|
|
|
let preprocess sourcefile tmpfile =
|
1996-04-29 06:19:07 -07:00
|
|
|
match !Clflags.preprocessor with
|
1996-04-25 09:06:43 -07:00
|
|
|
None -> sourcefile
|
|
|
|
| Some pp ->
|
|
|
|
let comm = pp ^ " " ^ sourcefile ^ " > " ^ tmpfile in
|
1997-05-15 06:26:34 -07:00
|
|
|
if Ccomp.command comm <> 0 then begin
|
1996-04-25 09:06:43 -07:00
|
|
|
Printf.eprintf "Preprocessing error\n";
|
|
|
|
flush stderr;
|
|
|
|
exit 2
|
|
|
|
end;
|
|
|
|
tmpfile
|
|
|
|
|
1996-04-29 06:19:07 -07:00
|
|
|
let remove_preprocessed inputfile =
|
|
|
|
match !Clflags.preprocessor with
|
|
|
|
None -> ()
|
|
|
|
| Some _ -> remove_file inputfile
|
|
|
|
|
1996-04-25 09:06:43 -07:00
|
|
|
(* Parse a file or get a dumped syntax tree in it *)
|
|
|
|
|
1997-03-05 05:18:03 -08:00
|
|
|
exception Outdated_version
|
|
|
|
|
1996-04-25 09:06:43 -07:00
|
|
|
let parse_file inputfile parse_fun ast_magic =
|
|
|
|
let ic = open_in_bin inputfile in
|
1996-04-23 10:13:52 -07:00
|
|
|
let is_ast_file =
|
|
|
|
try
|
1996-04-25 09:06:43 -07:00
|
|
|
let buffer = String.create (String.length ast_magic) in
|
|
|
|
really_input ic buffer 0 (String.length ast_magic);
|
1997-03-05 05:18:03 -08:00
|
|
|
if buffer = ast_magic then true
|
|
|
|
else if String.sub buffer 0 9 = String.sub ast_magic 0 9 then
|
|
|
|
raise Outdated_version
|
|
|
|
else false
|
|
|
|
with
|
|
|
|
Outdated_version ->
|
1997-08-22 01:55:41 -07:00
|
|
|
fatal_error "Ocaml and preprocessor have incompatible versions"
|
1997-03-05 05:18:03 -08:00
|
|
|
| _ -> false
|
1996-04-23 10:13:52 -07:00
|
|
|
in
|
|
|
|
let ast =
|
|
|
|
try
|
|
|
|
if is_ast_file then begin
|
|
|
|
Location.input_name := input_value ic;
|
|
|
|
input_value ic
|
|
|
|
end else begin
|
|
|
|
seek_in ic 0;
|
1996-04-25 09:06:43 -07:00
|
|
|
Location.input_name := inputfile;
|
|
|
|
parse_fun (Lexing.from_channel ic)
|
1996-04-23 10:13:52 -07:00
|
|
|
end
|
|
|
|
with x -> close_in ic; raise x
|
|
|
|
in
|
|
|
|
close_in ic;
|
1996-04-25 09:06:43 -07:00
|
|
|
ast
|
|
|
|
|
|
|
|
(* Compile a .mli file *)
|
|
|
|
|
2000-03-06 21:02:33 -08:00
|
|
|
let interface ppf sourcefile =
|
1996-04-25 09:06:43 -07:00
|
|
|
let prefixname = Filename.chop_extension sourcefile in
|
1996-10-31 08:03:27 -08:00
|
|
|
let modulename = String.capitalize(Filename.basename prefixname) in
|
1996-04-25 09:06:43 -07:00
|
|
|
let inputfile = preprocess sourcefile (prefixname ^ ".ppi") in
|
|
|
|
let ast = parse_file inputfile Parse.interface ast_intf_magic_number in
|
2000-03-06 21:02:33 -08:00
|
|
|
if !Clflags.dump_parsetree then fprintf ppf "%a@." Printast.interface ast;
|
1996-04-23 10:13:52 -07:00
|
|
|
let sg = Typemod.transl_signature (initial_env()) ast in
|
2000-04-25 04:44:23 -07:00
|
|
|
if !Clflags.print_types
|
|
|
|
then fprintf std_formatter "%a@." Printtyp.signature sg;
|
1996-04-23 10:13:52 -07:00
|
|
|
Env.save_signature sg modulename (prefixname ^ ".cmi");
|
1996-04-29 06:19:07 -07:00
|
|
|
remove_preprocessed inputfile
|
1995-07-02 09:51:07 -07:00
|
|
|
|
|
|
|
(* Compile a .ml file *)
|
|
|
|
|
2000-03-27 07:44:50 -08:00
|
|
|
let print_if ppf flag printer arg =
|
|
|
|
if !flag then fprintf ppf "%a@." printer arg;
|
1995-07-02 09:51:07 -07:00
|
|
|
arg
|
|
|
|
|
1998-04-27 08:16:48 -07:00
|
|
|
let (++) x f = f x
|
|
|
|
let (+++) (x, y) f = (x, f y)
|
|
|
|
|
2000-03-06 21:02:33 -08:00
|
|
|
let implementation ppf sourcefile =
|
1995-10-24 08:37:23 -07:00
|
|
|
let prefixname = Filename.chop_extension sourcefile in
|
1996-10-31 08:03:27 -08:00
|
|
|
let modulename = String.capitalize(Filename.basename prefixname) in
|
1996-04-25 09:06:43 -07:00
|
|
|
let inputfile = preprocess sourcefile (prefixname ^ ".ppo") in
|
1998-04-27 08:16:48 -07:00
|
|
|
let env = initial_env() in
|
1997-05-15 06:26:34 -07:00
|
|
|
Compilenv.reset modulename;
|
1998-04-27 08:16:48 -07:00
|
|
|
parse_file inputfile Parse.implementation ast_impl_magic_number
|
2000-03-27 07:44:50 -08:00
|
|
|
++ print_if ppf Clflags.dump_parsetree Printast.implementation
|
1998-04-27 08:16:48 -07:00
|
|
|
++ Typemod.type_implementation sourcefile prefixname modulename env
|
|
|
|
++ Translmod.transl_store_implementation modulename
|
2000-03-27 07:44:50 -08:00
|
|
|
+++ print_if ppf Clflags.dump_rawlambda Printlambda.lambda
|
1998-04-27 08:16:48 -07:00
|
|
|
+++ Simplif.simplify_lambda
|
2000-03-27 07:44:50 -08:00
|
|
|
+++ print_if ppf Clflags.dump_lambda Printlambda.lambda
|
2000-04-21 01:13:22 -07:00
|
|
|
++ Asmgen.compile_implementation prefixname ppf;
|
1996-04-23 10:13:52 -07:00
|
|
|
Compilenv.save_unit_info (prefixname ^ ".cmx");
|
1996-04-29 06:19:07 -07:00
|
|
|
remove_preprocessed inputfile
|
1995-07-02 09:51:07 -07:00
|
|
|
|
|
|
|
let c_file name =
|
1998-11-06 07:39:43 -08:00
|
|
|
if Ccomp.compile_file name <> 0 then exit 2
|