2002-02-08 08:55:44 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
2002-02-08 08:55:44 -08:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 2002 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* "Package" a set of .cmx/.o files into one .cmx/.o file having the
|
|
|
|
original compilation units as sub-modules. *)
|
|
|
|
|
|
|
|
open Printf
|
|
|
|
open Misc
|
|
|
|
open Lambda
|
|
|
|
open Clambda
|
2010-05-19 04:29:38 -07:00
|
|
|
open Cmx_format
|
2002-02-08 08:55:44 -08:00
|
|
|
|
|
|
|
type error =
|
|
|
|
Illegal_renaming of string * string
|
|
|
|
| Forward_reference of string * string
|
2005-08-01 08:51:09 -07:00
|
|
|
| Wrong_for_pack of string * string
|
2002-02-08 08:55:44 -08:00
|
|
|
| Linking_error
|
|
|
|
| Assembler_error of string
|
|
|
|
| File_not_found of string
|
2005-08-01 08:51:09 -07:00
|
|
|
|
2002-02-08 08:55:44 -08:00
|
|
|
|
|
|
|
exception Error of error
|
|
|
|
|
|
|
|
(* Read the unit information from a .cmx file. *)
|
|
|
|
|
2004-04-09 06:32:28 -07:00
|
|
|
type pack_member_kind = PM_intf | PM_impl of unit_infos
|
|
|
|
|
|
|
|
type pack_member =
|
|
|
|
{ pm_file: string;
|
|
|
|
pm_name: string;
|
|
|
|
pm_kind: pack_member_kind }
|
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
let read_member_info pack_path file =
|
2004-04-09 06:32:28 -07:00
|
|
|
let name =
|
2007-02-23 05:44:51 -08:00
|
|
|
String.capitalize(Filename.basename(chop_extensions file)) in
|
2004-04-09 06:32:28 -07:00
|
|
|
let kind =
|
|
|
|
if Filename.check_suffix file ".cmx" then begin
|
|
|
|
let (info, crc) = Compilenv.read_unit_info file in
|
|
|
|
if info.ui_name <> name
|
|
|
|
then raise(Error(Illegal_renaming(file, info.ui_name)));
|
2005-08-01 08:51:09 -07:00
|
|
|
if info.ui_symbol <>
|
|
|
|
(Compilenv.current_unit_infos()).ui_symbol ^ "__" ^ info.ui_name
|
|
|
|
then raise(Error(Wrong_for_pack(file, pack_path)));
|
2004-04-09 06:32:28 -07:00
|
|
|
Asmlink.check_consistency file info crc;
|
2006-10-17 05:33:58 -07:00
|
|
|
Compilenv.cache_unit_info info;
|
2004-04-09 06:32:28 -07:00
|
|
|
PM_impl info
|
|
|
|
end else
|
|
|
|
PM_intf in
|
|
|
|
{ pm_file = file; pm_name = name; pm_kind = kind }
|
2002-02-08 08:55:44 -08:00
|
|
|
|
|
|
|
(* Check absence of forward references *)
|
|
|
|
|
2004-04-09 06:32:28 -07:00
|
|
|
let check_units members =
|
2002-02-08 08:55:44 -08:00
|
|
|
let rec check forbidden = function
|
|
|
|
[] -> ()
|
2004-04-09 06:32:28 -07:00
|
|
|
| mb :: tl ->
|
|
|
|
begin match mb.pm_kind with
|
|
|
|
| PM_intf -> ()
|
|
|
|
| PM_impl infos ->
|
|
|
|
List.iter
|
|
|
|
(fun (unit, _) ->
|
|
|
|
if List.mem unit forbidden
|
|
|
|
then raise(Error(Forward_reference(mb.pm_file, unit))))
|
|
|
|
infos.ui_imports_cmx
|
|
|
|
end;
|
|
|
|
check (list_remove mb.pm_name forbidden) tl in
|
|
|
|
check (List.map (fun mb -> mb.pm_name) members) members
|
2002-02-08 08:55:44 -08:00
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
(* Make the .o file for the package *)
|
2002-02-08 08:55:44 -08:00
|
|
|
|
2012-01-20 06:21:03 -08:00
|
|
|
let make_package_object members targetobj targetname coercion =
|
2007-03-01 05:38:54 -08:00
|
|
|
let objtemp =
|
2007-11-06 07:16:56 -08:00
|
|
|
if !Clflags.keep_asm_file
|
|
|
|
then chop_extension_if_any targetobj ^ ".pack" ^ Config.ext_obj
|
2010-01-22 04:48:24 -08:00
|
|
|
else
|
2007-11-06 07:16:56 -08:00
|
|
|
(* Put the full name of the module in the temporary file name
|
2010-01-22 04:48:24 -08:00
|
|
|
to avoid collisions with MSVC's link /lib in case of successive
|
|
|
|
packs *)
|
2007-11-06 07:16:56 -08:00
|
|
|
Filename.temp_file (Compilenv.make_symbol (Some "")) Config.ext_obj in
|
2004-04-09 06:32:28 -07:00
|
|
|
let components =
|
|
|
|
List.map
|
|
|
|
(fun m ->
|
|
|
|
match m.pm_kind with
|
|
|
|
| PM_intf -> None
|
|
|
|
| PM_impl _ -> Some(Ident.create_persistent m.pm_name))
|
|
|
|
members in
|
2003-03-06 07:59:55 -08:00
|
|
|
Asmgen.compile_implementation
|
2012-01-20 06:21:03 -08:00
|
|
|
(chop_extension_if_any objtemp)
|
2003-03-06 07:59:55 -08:00
|
|
|
(Translmod.transl_store_package
|
2004-04-09 06:32:28 -07:00
|
|
|
components (Ident.create_persistent targetname) coercion);
|
|
|
|
let objfiles =
|
|
|
|
List.map
|
|
|
|
(fun m -> chop_extension_if_any m.pm_file ^ Config.ext_obj)
|
|
|
|
(List.filter (fun m -> m.pm_kind <> PM_intf) members) in
|
2007-11-15 08:09:57 -08:00
|
|
|
let ok =
|
|
|
|
Ccomp.call_linker Ccomp.Partial targetobj (objtemp :: objfiles) ""
|
|
|
|
in
|
2002-02-08 08:55:44 -08:00
|
|
|
remove_file objtemp;
|
2007-11-15 08:09:57 -08:00
|
|
|
if not ok then raise(Error Linking_error)
|
2002-02-08 08:55:44 -08:00
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
(* Make the .cmx file for the package *)
|
|
|
|
|
|
|
|
let build_package_cmx members cmxfile =
|
|
|
|
let unit_names =
|
|
|
|
List.map (fun m -> m.pm_name) members in
|
|
|
|
let filter lst =
|
|
|
|
List.filter (fun (name, crc) -> not (List.mem name unit_names)) lst in
|
|
|
|
let union lst =
|
|
|
|
List.fold_left
|
|
|
|
(List.fold_left
|
|
|
|
(fun accu n -> if List.mem n accu then accu else n :: accu))
|
|
|
|
[] lst in
|
|
|
|
let units =
|
2006-01-04 08:55:50 -08:00
|
|
|
List.fold_right
|
|
|
|
(fun m accu ->
|
2005-08-01 08:51:09 -07:00
|
|
|
match m.pm_kind with PM_intf -> accu | PM_impl info -> info :: accu)
|
2006-01-04 08:55:50 -08:00
|
|
|
members [] in
|
2005-08-01 08:51:09 -07:00
|
|
|
let ui = Compilenv.current_unit_infos() in
|
|
|
|
let pkg_infos =
|
|
|
|
{ ui_name = ui.ui_name;
|
|
|
|
ui_symbol = ui.ui_symbol;
|
|
|
|
ui_defines =
|
2006-01-04 08:55:50 -08:00
|
|
|
List.flatten (List.map (fun info -> info.ui_defines) units) @
|
|
|
|
[ui.ui_symbol];
|
2005-08-01 08:51:09 -07:00
|
|
|
ui_imports_cmi =
|
|
|
|
(ui.ui_name, Env.crc_of_unit ui.ui_name) ::
|
|
|
|
filter(Asmlink.extract_crc_interfaces());
|
|
|
|
ui_imports_cmx =
|
|
|
|
filter(Asmlink.extract_crc_implementations());
|
|
|
|
ui_approx = ui.ui_approx;
|
|
|
|
ui_curry_fun =
|
|
|
|
union(List.map (fun info -> info.ui_curry_fun) units);
|
|
|
|
ui_apply_fun =
|
|
|
|
union(List.map (fun info -> info.ui_apply_fun) units);
|
|
|
|
ui_send_fun =
|
|
|
|
union(List.map (fun info -> info.ui_send_fun) units);
|
|
|
|
ui_force_link =
|
2007-11-06 07:16:56 -08:00
|
|
|
List.exists (fun info -> info.ui_force_link) units;
|
2005-08-01 08:51:09 -07:00
|
|
|
} in
|
|
|
|
Compilenv.write_unit_info pkg_infos cmxfile
|
|
|
|
|
2002-02-08 08:55:44 -08:00
|
|
|
(* Make the .cmx and the .o for the package *)
|
|
|
|
|
2012-01-20 06:21:03 -08:00
|
|
|
let package_object_files files targetcmx
|
2003-03-06 07:59:55 -08:00
|
|
|
targetobj targetname coercion =
|
2005-08-01 08:51:09 -07:00
|
|
|
let pack_path =
|
|
|
|
match !Clflags.for_package with
|
|
|
|
| None -> targetname
|
|
|
|
| Some p -> p ^ "." ^ targetname in
|
|
|
|
let members = map_left_right (read_member_info pack_path) files in
|
2004-04-09 06:32:28 -07:00
|
|
|
check_units members;
|
2012-01-20 06:21:03 -08:00
|
|
|
make_package_object members targetobj targetname coercion;
|
2005-08-01 08:51:09 -07:00
|
|
|
build_package_cmx members targetcmx
|
2002-02-08 08:55:44 -08:00
|
|
|
|
|
|
|
(* The entry point *)
|
|
|
|
|
2012-01-20 06:21:03 -08:00
|
|
|
let package_files files targetcmx =
|
2004-04-09 06:32:28 -07:00
|
|
|
let files =
|
2002-02-08 08:55:44 -08:00
|
|
|
List.map
|
|
|
|
(fun f ->
|
|
|
|
try find_in_path !Config.load_path f
|
|
|
|
with Not_found -> raise(Error(File_not_found f)))
|
|
|
|
files in
|
2007-02-23 05:44:51 -08:00
|
|
|
let prefix = chop_extensions targetcmx in
|
2002-02-08 08:55:44 -08:00
|
|
|
let targetcmi = prefix ^ ".cmi" in
|
2007-02-23 05:44:51 -08:00
|
|
|
let targetobj = chop_extension_if_any targetcmx ^ Config.ext_obj in
|
2002-02-08 08:55:44 -08:00
|
|
|
let targetname = String.capitalize(Filename.basename prefix) in
|
2005-08-01 08:51:09 -07:00
|
|
|
(* Set the name of the current "input" *)
|
|
|
|
Location.input_name := targetcmx;
|
|
|
|
(* Set the name of the current compunit *)
|
|
|
|
Compilenv.reset ?packname:!Clflags.for_package targetname;
|
2002-02-08 08:55:44 -08:00
|
|
|
try
|
2004-04-09 06:32:28 -07:00
|
|
|
let coercion = Typemod.package_units files targetcmi targetname in
|
2012-01-20 06:21:03 -08:00
|
|
|
package_object_files files targetcmx targetobj targetname coercion
|
2002-02-08 08:55:44 -08:00
|
|
|
with x ->
|
2003-03-06 07:59:55 -08:00
|
|
|
remove_file targetcmx; remove_file targetobj;
|
2002-02-08 08:55:44 -08:00
|
|
|
raise x
|
|
|
|
|
|
|
|
(* Error report *)
|
|
|
|
|
|
|
|
open Format
|
|
|
|
|
|
|
|
let report_error ppf = function
|
|
|
|
Illegal_renaming(file, id) ->
|
|
|
|
fprintf ppf "Wrong file naming: %s@ contains the code for@ %s"
|
|
|
|
file id
|
|
|
|
| Forward_reference(file, ident) ->
|
|
|
|
fprintf ppf "Forward reference to %s in file %s" ident file
|
2005-08-01 08:51:09 -07:00
|
|
|
| Wrong_for_pack(file, path) ->
|
2006-01-04 08:55:50 -08:00
|
|
|
fprintf ppf "File %s@ was not compiled with the `-for-pack %s' option"
|
2005-08-01 08:51:09 -07:00
|
|
|
file path
|
2002-02-08 08:55:44 -08:00
|
|
|
| File_not_found file ->
|
|
|
|
fprintf ppf "File %s not found" file
|
|
|
|
| Assembler_error file ->
|
|
|
|
fprintf ppf "Error while assembling %s" file
|
|
|
|
| Linking_error ->
|
|
|
|
fprintf ppf "Error during partial linking"
|