2001-10-30 01:31:01 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
2001-10-30 01:31:01 -08:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 2001 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. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
open Printf
|
2013-12-14 08:34:18 -08:00
|
|
|
open Ocamlmklibconfig
|
2001-10-30 01:31:01 -08:00
|
|
|
|
2010-01-20 08:26:46 -08:00
|
|
|
(* PR#4783: under Windows, don't use absolute paths because we do
|
|
|
|
not know where the binary distribution will be installed. *)
|
|
|
|
let compiler_path name =
|
|
|
|
if Sys.os_type = "Win32" then name else Filename.concat bindir name
|
|
|
|
|
2001-10-30 01:31:01 -08:00
|
|
|
let bytecode_objs = ref [] (* .cmo,.cma,.ml,.mli files to pass to ocamlc *)
|
|
|
|
and native_objs = ref [] (* .cmx,.cmxa,.ml,.mli files to pass to ocamlopt *)
|
2015-09-11 04:58:31 -07:00
|
|
|
and c_objs = ref [] (* .o, .a, .obj, .lib, .dll, .dylib, .so files to
|
|
|
|
pass to mksharedlib and ar *)
|
2001-10-30 01:31:01 -08:00
|
|
|
and caml_libs = ref [] (* -cclib to pass to ocamlc, ocamlopt *)
|
|
|
|
and caml_opts = ref [] (* -ccopt to pass to ocamlc, ocamlopt *)
|
|
|
|
and dynlink = ref supports_shared_libraries
|
|
|
|
and failsafe = ref false (* whether to fall back on static build only *)
|
|
|
|
and c_libs = ref [] (* libs to pass to mksharedlib and ocamlc -cclib *)
|
2015-09-11 04:58:31 -07:00
|
|
|
and c_Lopts = ref [] (* options to pass to mksharedlib and ocamlc -cclib *)
|
|
|
|
and c_opts = ref [] (* options to pass to mksharedlib and ocamlc -ccopt *)
|
2002-02-13 02:10:18 -08:00
|
|
|
and ld_opts = ref [] (* options to pass only to the linker *)
|
2010-01-20 08:26:46 -08:00
|
|
|
and ocamlc = ref (compiler_path "ocamlc")
|
2014-12-27 00:44:46 -08:00
|
|
|
and ocamlc_opts = ref [] (* options to pass only to ocamlc *)
|
2010-01-20 08:26:46 -08:00
|
|
|
and ocamlopt = ref (compiler_path "ocamlopt")
|
2014-12-27 00:44:46 -08:00
|
|
|
and ocamlopt_opts = ref [] (* options to pass only to ocamlc *)
|
2012-02-10 08:15:24 -08:00
|
|
|
and output = ref "a" (* Output name for OCaml part of library *)
|
2001-10-30 01:31:01 -08:00
|
|
|
and output_c = ref "" (* Output name for C part of library *)
|
|
|
|
and rpath = ref [] (* rpath options *)
|
2014-12-27 00:44:46 -08:00
|
|
|
and debug = ref false (* -g option *)
|
2001-10-30 01:31:01 -08:00
|
|
|
and verbose = ref false
|
|
|
|
|
|
|
|
let starts_with s pref =
|
|
|
|
String.length s >= String.length pref &&
|
|
|
|
String.sub s 0 (String.length pref) = pref
|
|
|
|
let ends_with = Filename.check_suffix
|
|
|
|
let chop_prefix s pref =
|
|
|
|
String.sub s (String.length pref) (String.length s - String.length pref)
|
|
|
|
let chop_suffix = Filename.chop_suffix
|
|
|
|
|
|
|
|
exception Bad_argument of string
|
|
|
|
|
2004-11-26 17:04:19 -08:00
|
|
|
let print_version () =
|
|
|
|
printf "ocamlmklib, version %s\n" Sys.ocaml_version;
|
|
|
|
exit 0;
|
|
|
|
;;
|
|
|
|
|
2010-05-20 07:06:29 -07:00
|
|
|
let print_version_num () =
|
|
|
|
printf "%s\n" Sys.ocaml_version;
|
|
|
|
exit 0;
|
|
|
|
;;
|
|
|
|
|
2001-10-30 01:31:01 -08:00
|
|
|
let parse_arguments argv =
|
|
|
|
let i = ref 1 in
|
|
|
|
let next_arg () =
|
|
|
|
if !i + 1 >= Array.length argv
|
|
|
|
then raise (Bad_argument("Option " ^ argv.(!i) ^ " expects one argument"));
|
|
|
|
incr i; argv.(!i) in
|
|
|
|
while !i < Array.length argv do
|
|
|
|
let s = argv.(!i) in
|
|
|
|
if ends_with s ".cmo" || ends_with s ".cma" then
|
|
|
|
bytecode_objs := s :: !bytecode_objs
|
|
|
|
else if ends_with s ".cmx" || ends_with s ".cmxa" then
|
|
|
|
native_objs := s :: !native_objs
|
|
|
|
else if ends_with s ".ml" || ends_with s ".mli" then
|
|
|
|
(bytecode_objs := s :: !bytecode_objs;
|
|
|
|
native_objs := s :: !native_objs)
|
2015-08-22 14:54:14 -07:00
|
|
|
else if List.exists (ends_with s)
|
2015-09-11 04:58:31 -07:00
|
|
|
[".o"; ".a"; ".obj"; ".lib"; ".dll"; ".dylib"; ".so"]
|
2015-08-22 14:54:14 -07:00
|
|
|
then
|
2001-10-30 01:31:01 -08:00
|
|
|
c_objs := s :: !c_objs
|
|
|
|
else if s = "-cclib" then
|
|
|
|
caml_libs := next_arg () :: "-cclib" :: !caml_libs
|
|
|
|
else if s = "-ccopt" then
|
2001-11-06 08:04:00 -08:00
|
|
|
caml_opts := next_arg () :: "-ccopt" :: !caml_opts
|
2001-10-30 01:31:01 -08:00
|
|
|
else if s = "-custom" then
|
|
|
|
dynlink := false
|
|
|
|
else if s = "-I" then
|
|
|
|
caml_opts := next_arg () :: "-I" :: !caml_opts
|
|
|
|
else if s = "-failsafe" then
|
|
|
|
failsafe := true
|
2014-12-27 00:44:46 -08:00
|
|
|
else if s = "-g" then
|
|
|
|
debug := true
|
2008-01-08 07:39:47 -08:00
|
|
|
else if s = "-h" || s = "-help" || s = "--help" then
|
2002-02-13 02:10:18 -08:00
|
|
|
raise (Bad_argument "")
|
|
|
|
else if s = "-ldopt" then
|
|
|
|
ld_opts := next_arg () :: !ld_opts
|
2001-10-30 01:31:01 -08:00
|
|
|
else if s = "-linkall" then
|
|
|
|
caml_opts := s :: !caml_opts
|
|
|
|
else if starts_with s "-l" then
|
|
|
|
c_libs := s :: !c_libs
|
|
|
|
else if starts_with s "-L" then
|
2012-07-17 11:32:05 -07:00
|
|
|
(c_Lopts := s :: !c_Lopts;
|
2001-10-30 01:31:01 -08:00
|
|
|
let l = chop_prefix s "-L" in
|
|
|
|
if not (Filename.is_relative l) then rpath := l :: !rpath)
|
2014-12-27 00:44:46 -08:00
|
|
|
else if s = "-ocamlcflags" then
|
|
|
|
ocamlc_opts := next_arg () :: !ocamlc_opts
|
2001-10-30 01:31:01 -08:00
|
|
|
else if s = "-ocamlc" then
|
|
|
|
ocamlc := next_arg ()
|
|
|
|
else if s = "-ocamlopt" then
|
|
|
|
ocamlopt := next_arg ()
|
2014-12-27 00:44:46 -08:00
|
|
|
else if s = "-ocamloptflags" then
|
|
|
|
ocamlopt_opts := next_arg () :: !ocamlopt_opts
|
2001-10-30 01:31:01 -08:00
|
|
|
else if s = "-o" then
|
|
|
|
output := next_arg()
|
|
|
|
else if s = "-oc" then
|
|
|
|
output_c := next_arg()
|
|
|
|
else if s = "-dllpath" || s = "-R" || s = "-rpath" then
|
|
|
|
rpath := next_arg() :: !rpath
|
|
|
|
else if starts_with s "-R" then
|
|
|
|
rpath := chop_prefix s "-R" :: !rpath
|
|
|
|
else if s = "-Wl,-rpath" then
|
|
|
|
(let a = next_arg() in
|
|
|
|
if starts_with a "-Wl,"
|
2001-12-10 21:59:59 -08:00
|
|
|
then rpath := chop_prefix a "-Wl," :: !rpath
|
2001-10-30 01:31:01 -08:00
|
|
|
else raise (Bad_argument("Option -Wl,-rpath expects a -Wl, argument")))
|
|
|
|
else if starts_with s "-Wl,-rpath," then
|
2001-12-10 21:59:59 -08:00
|
|
|
rpath := chop_prefix s "-Wl,-rpath," :: !rpath
|
2001-10-30 01:31:01 -08:00
|
|
|
else if starts_with s "-Wl,-R" then
|
2001-12-10 21:59:59 -08:00
|
|
|
rpath := chop_prefix s "-Wl,-R" :: !rpath
|
2001-10-30 01:31:01 -08:00
|
|
|
else if s = "-v" || s = "-verbose" then
|
|
|
|
verbose := true
|
2004-11-26 17:04:19 -08:00
|
|
|
else if s = "-version" then
|
|
|
|
print_version ()
|
2010-05-20 07:06:29 -07:00
|
|
|
else if s = "-vnum" then
|
|
|
|
print_version_num ()
|
2002-05-28 20:51:53 -07:00
|
|
|
else if starts_with s "-F" then
|
|
|
|
c_opts := s :: !c_opts
|
|
|
|
else if s = "-framework" then
|
|
|
|
(let a = next_arg() in c_opts := a :: s :: !c_opts)
|
2001-10-30 01:31:01 -08:00
|
|
|
else if starts_with s "-" then
|
2001-11-01 00:45:25 -08:00
|
|
|
prerr_endline ("Unknown option " ^ s)
|
2001-10-30 01:31:01 -08:00
|
|
|
else
|
|
|
|
raise (Bad_argument("Don't know what to do with " ^ s));
|
|
|
|
incr i
|
|
|
|
done;
|
|
|
|
List.iter
|
|
|
|
(fun r -> r := List.rev !r)
|
2010-01-20 08:26:46 -08:00
|
|
|
[ bytecode_objs; native_objs; caml_libs; caml_opts;
|
2002-05-28 20:51:53 -07:00
|
|
|
c_libs; c_objs; c_opts; ld_opts; rpath ];
|
2012-07-17 11:32:05 -07:00
|
|
|
(* Put -L options in front of -l options in -cclib to mimic -ccopt behavior *)
|
|
|
|
c_libs := !c_Lopts @ !c_libs;
|
2007-11-06 07:16:56 -08:00
|
|
|
|
2001-10-30 01:31:01 -08:00
|
|
|
if !output_c = "" then output_c := !output
|
|
|
|
|
|
|
|
let usage = "\
|
2013-09-04 08:12:37 -07:00
|
|
|
Usage: ocamlmklib [options] <.cmo|.cma|.cmx|.cmxa|.ml|.mli|.o|.a|.obj|.lib|\
|
2015-08-22 14:54:14 -07:00
|
|
|
.dll|.dylib files>\
|
2011-07-20 02:17:07 -07:00
|
|
|
\nOptions are:\
|
|
|
|
\n -cclib <lib> C library passed to ocamlc -a or ocamlopt -a only\
|
|
|
|
\n -ccopt <opt> C option passed to ocamlc -a or ocamlopt -a only\
|
2014-12-27 00:44:46 -08:00
|
|
|
\n -custom Disable dynamic loading\
|
|
|
|
\n -g Build with debug information\
|
2011-07-20 02:17:07 -07:00
|
|
|
\n -dllpath <dir> Add <dir> to the run-time search path for DLLs\
|
|
|
|
\n -F<dir> Specify a framework directory (MacOSX)\
|
|
|
|
\n -framework <name> Use framework <name> (MacOSX)\
|
|
|
|
\n -help Print this help message and exit\
|
|
|
|
\n --help Same as -help\
|
|
|
|
\n -h Same as -help\
|
2012-02-10 08:15:24 -08:00
|
|
|
\n -I <dir> Add <dir> to the path searched for OCaml object files\
|
2011-07-20 02:17:07 -07:00
|
|
|
\n -failsafe fall back to static linking if DLL construction failed\
|
|
|
|
\n -ldopt <opt> C option passed to the shared linker only\
|
2012-02-10 08:15:24 -08:00
|
|
|
\n -linkall Build OCaml archive with link-all behavior\
|
2011-07-20 02:17:07 -07:00
|
|
|
\n -l<lib> Specify a dependent C library\
|
|
|
|
\n -L<dir> Add <dir> to the path searched for C libraries\
|
|
|
|
\n -ocamlc <cmd> Use <cmd> in place of \"ocamlc\"\
|
2014-12-27 00:44:46 -08:00
|
|
|
\n -ocamlcflags <opt> Pass <opt> to ocamlc\
|
2011-07-20 02:17:07 -07:00
|
|
|
\n -ocamlopt <cmd> Use <cmd> in place of \"ocamlopt\"\
|
2014-12-27 00:44:46 -08:00
|
|
|
\n -ocamloptflags <opt> Pass <opt> to ocamlopt\
|
2012-02-10 08:15:24 -08:00
|
|
|
\n -o <name> Generated OCaml library is named <name>.cma or <name>.cmxa\
|
2011-07-20 02:17:07 -07:00
|
|
|
\n -oc <name> Generated C library is named dll<name>.so or lib<name>.a\
|
|
|
|
\n -rpath <dir> Same as -dllpath <dir>\
|
|
|
|
\n -R<dir> Same as -rpath\
|
|
|
|
\n -verbose Print commands before executing them\
|
|
|
|
\n -v same as -verbose\
|
|
|
|
\n -version Print version and exit\
|
|
|
|
\n -vnum Print version number and exit\
|
|
|
|
\n -Wl,-rpath,<dir> Same as -dllpath <dir>\
|
|
|
|
\n -Wl,-rpath -Wl,<dir> Same as -dllpath <dir>\
|
|
|
|
\n -Wl,-R<dir> Same as -dllpath <dir>\
|
|
|
|
\n"
|
2001-10-30 01:31:01 -08:00
|
|
|
|
|
|
|
let command cmd =
|
|
|
|
if !verbose then (print_string "+ "; print_string cmd; print_newline());
|
|
|
|
Sys.command cmd
|
|
|
|
|
|
|
|
let scommand cmd =
|
|
|
|
if command cmd <> 0 then exit 2
|
|
|
|
|
|
|
|
let safe_remove s =
|
|
|
|
try Sys.remove s with Sys_error _ -> ()
|
|
|
|
|
2001-12-20 22:54:52 -08:00
|
|
|
let make_set l =
|
|
|
|
let rec merge l = function
|
|
|
|
[] -> List.rev l
|
|
|
|
| p :: r -> if List.mem p l then merge l r else merge (p::l) r
|
|
|
|
in
|
|
|
|
merge [] l
|
|
|
|
|
2001-10-30 01:31:01 -08:00
|
|
|
let make_rpath flag =
|
|
|
|
if !rpath = [] || flag = ""
|
|
|
|
then ""
|
2001-12-20 22:54:52 -08:00
|
|
|
else flag ^ String.concat ":" (make_set !rpath)
|
2001-10-30 01:31:01 -08:00
|
|
|
|
|
|
|
let make_rpath_ccopt flag =
|
2010-01-20 08:26:46 -08:00
|
|
|
if !rpath = [] || flag = ""
|
2001-10-30 01:31:01 -08:00
|
|
|
then ""
|
2001-12-20 22:54:52 -08:00
|
|
|
else "-ccopt " ^ flag ^ String.concat ":" (make_set !rpath)
|
2001-10-30 01:31:01 -08:00
|
|
|
|
|
|
|
let prefix_list pref l =
|
|
|
|
List.map (fun s -> pref ^ s) l
|
|
|
|
|
2004-04-15 09:06:39 -07:00
|
|
|
let prepostfix pre name post =
|
|
|
|
let base = Filename.basename name in
|
|
|
|
let dir = Filename.dirname name in
|
|
|
|
Filename.concat dir (pre ^ base ^ post)
|
|
|
|
;;
|
|
|
|
|
2007-11-06 07:16:56 -08:00
|
|
|
let transl_path s =
|
|
|
|
match Sys.os_type with
|
|
|
|
| "Win32" ->
|
2014-04-29 04:56:17 -07:00
|
|
|
let s = Bytes.of_string s in
|
2010-01-20 08:26:46 -08:00
|
|
|
let rec aux i =
|
2014-04-29 04:56:17 -07:00
|
|
|
if i = Bytes.length s || Bytes.get s i = ' ' then s
|
|
|
|
else begin
|
|
|
|
if Bytes.get s i = '/' then Bytes.set s i '\\';
|
|
|
|
aux (i + 1)
|
|
|
|
end
|
|
|
|
in Bytes.to_string (aux 0)
|
2007-11-06 07:16:56 -08:00
|
|
|
| _ -> s
|
|
|
|
|
2001-10-30 01:31:01 -08:00
|
|
|
let build_libs () =
|
|
|
|
if !c_objs <> [] then begin
|
|
|
|
if !dynlink then begin
|
|
|
|
let retcode = command
|
2014-12-27 00:44:46 -08:00
|
|
|
(Printf.sprintf "%s %s -o %s %s %s %s %s %s"
|
2016-01-15 08:40:50 -08:00
|
|
|
Config.mkdll
|
2014-12-27 00:44:46 -08:00
|
|
|
(if !debug then "-g" else "")
|
2016-01-15 08:40:50 -08:00
|
|
|
(prepostfix "dll" !output_c Config.ext_dll)
|
2007-11-15 05:21:15 -08:00
|
|
|
(String.concat " " !c_objs)
|
|
|
|
(String.concat " " !c_opts)
|
|
|
|
(String.concat " " !ld_opts)
|
|
|
|
(make_rpath mksharedlibrpath)
|
|
|
|
(String.concat " " !c_libs)
|
|
|
|
)
|
|
|
|
in
|
2001-10-30 01:31:01 -08:00
|
|
|
if retcode <> 0 then if !failsafe then dynlink := false else exit 2
|
|
|
|
end;
|
2016-01-15 08:40:50 -08:00
|
|
|
safe_remove (prepostfix "lib" !output_c Config.ext_lib);
|
2001-10-30 01:31:01 -08:00
|
|
|
scommand
|
2016-01-15 08:40:50 -08:00
|
|
|
(mklib (prepostfix "lib" !output_c Config.ext_lib)
|
2007-02-07 02:31:36 -08:00
|
|
|
(String.concat " " !c_objs) "");
|
2001-10-30 01:31:01 -08:00
|
|
|
end;
|
|
|
|
if !bytecode_objs <> [] then
|
|
|
|
scommand
|
2015-09-11 04:58:31 -07:00
|
|
|
(sprintf "%s -a %s %s %s -o %s.cma %s %s -dllib -l%s -cclib -l%s \
|
|
|
|
%s %s %s %s"
|
2007-11-06 07:16:56 -08:00
|
|
|
(transl_path !ocamlc)
|
2014-12-27 00:44:46 -08:00
|
|
|
(if !debug then "-g" else "")
|
2001-10-30 01:31:01 -08:00
|
|
|
(if !dynlink then "" else "-custom")
|
2014-12-27 00:44:46 -08:00
|
|
|
(String.concat " " !ocamlc_opts)
|
2001-10-30 01:31:01 -08:00
|
|
|
!output
|
2001-11-06 08:04:00 -08:00
|
|
|
(String.concat " " !caml_opts)
|
2001-10-30 01:31:01 -08:00
|
|
|
(String.concat " " !bytecode_objs)
|
2007-02-07 02:31:36 -08:00
|
|
|
(Filename.basename !output_c)
|
|
|
|
(Filename.basename !output_c)
|
2001-10-30 01:31:01 -08:00
|
|
|
(String.concat " " (prefix_list "-ccopt " !c_opts))
|
2007-02-07 02:31:36 -08:00
|
|
|
(make_rpath_ccopt byteccrpath)
|
2002-01-04 06:15:51 -08:00
|
|
|
(String.concat " " (prefix_list "-cclib " !c_libs))
|
|
|
|
(String.concat " " !caml_libs));
|
2001-10-30 01:31:01 -08:00
|
|
|
if !native_objs <> [] then
|
|
|
|
scommand
|
2014-12-27 00:44:46 -08:00
|
|
|
(sprintf "%s -a %s %s -o %s.cmxa %s %s -cclib -l%s %s %s %s %s"
|
2007-11-06 07:16:56 -08:00
|
|
|
(transl_path !ocamlopt)
|
2014-12-27 00:44:46 -08:00
|
|
|
(if !debug then "-g" else "")
|
|
|
|
(String.concat " " !ocamlopt_opts)
|
2001-10-30 01:31:01 -08:00
|
|
|
!output
|
2001-11-06 08:04:00 -08:00
|
|
|
(String.concat " " !caml_opts)
|
2001-10-30 01:31:01 -08:00
|
|
|
(String.concat " " !native_objs)
|
2007-02-07 02:31:36 -08:00
|
|
|
(Filename.basename !output_c)
|
2001-10-30 01:31:01 -08:00
|
|
|
(String.concat " " (prefix_list "-ccopt " !c_opts))
|
2007-02-07 02:31:36 -08:00
|
|
|
(make_rpath_ccopt nativeccrpath)
|
2002-01-04 06:15:51 -08:00
|
|
|
(String.concat " " (prefix_list "-cclib " !c_libs))
|
|
|
|
(String.concat " " !caml_libs))
|
2001-10-30 01:31:01 -08:00
|
|
|
|
|
|
|
let _ =
|
|
|
|
try
|
|
|
|
parse_arguments Sys.argv;
|
|
|
|
build_libs()
|
|
|
|
with
|
2002-02-13 02:10:18 -08:00
|
|
|
| Bad_argument "" ->
|
|
|
|
prerr_string usage; exit 0
|
2001-10-30 01:31:01 -08:00
|
|
|
| Bad_argument s ->
|
|
|
|
prerr_endline s; prerr_string usage; exit 4
|
|
|
|
| Sys_error s ->
|
|
|
|
prerr_string "System error: "; prerr_endline s; exit 4
|
|
|
|
| x ->
|
|
|
|
raise x
|