1
(* $Id: build.ml.in,v 1.2 2005/10/17 08:38:42 garrigue Exp $ *)
2
(* A script to build lablGL libraries *)
6
let ocamlc = ref "ocamlc.opt"
7
let ocamlopt = ref "ocamlopt.opt"
8
let flags = ref "-w s -I +labltk"
9
let ccomp_type = ref "msvc" (* "msvc" for MSVC++, "cc" for Mingw. Attempt ot autodetect *)
11
let split ?(sep = [' ';'\t';'\r';'\n']) s =
12
let len = String.length s in
13
let rec loop last cur acc =
14
if cur > len then acc else
16
if cur = len || List.mem s.[cur] sep then
17
if cur > last then loop next next (String.sub s ~pos:last ~len:(cur-last) :: acc)
18
else loop next next acc
19
else loop last next acc
20
in List.rev (loop 0 0 [])
22
let lablgl_mls = split "@LABLGL_MLS@"
23
let togl_mls = split "@TOGL_MLS@"
24
let glut_mls = split "@GLUT_MLS@"
25
let gl_libs = "@GLLIBS@"
26
let tk_libs = "@TKLIBS@ "
27
let glut_libs = "@GLUTLIBS@ "
29
(* Hack to check for mingw *)
32
let ic = open_in "../Makefile.config" in
34
let s = input_line ic in
35
match split ~sep:[' ';'\t';'='] s with
36
"CCOMPTYPE" :: cc :: _ -> ccomp_type := cc
43
if !ccomp_type = "msvc" then gtk_libs else
45
List.map (split gtk_libs) ~f:
47
if Filename.check_suffix nm ".lib" then "-l"^Filename.chop_extension nm^".dll"
49
in String.concat " " libs
53
let cmd = String.concat " " (cmd :: !flags :: args) in
54
print_endline cmd; flush stdout;
55
let err = Sys.command cmd in
56
if err > 0 then failwith ("error "^string_of_int err)
59
if Sys.file_exists f then Sys.remove f
62
List.iter (lablgl_mls @ togl_mls @ glut_mls) ~f:
64
if Sys.file_exists (file ^ ".mli") then exe !ocamlc ["-c"; file^".mli"];
65
exe !ocamlc ["-c"; file^".ml"]
67
List.iter ["lablgl", lablgl_mls, "";
68
"togl", togl_mls, tk_libs;
69
"lablglut", glut_mls, glut_libs]
70
~f:begin fun (lib, mls,libs) ->
71
let cmos = List.map mls ~f:(fun nm -> nm ^".cmo") in
72
exe !ocamlc (["-a -o"; lib^".cma"; "-cclib -l"^lib; "-dllib -l"^lib;
73
"-cclib \""^libs^gl_libs^"\""] @ cmos);
74
List.iter cmos ~f:may_remove
78
List.iter (lablgl_mls @ togl_mls @ glut_mls) ~f:
79
(fun file -> exe !ocamlopt ["-c"; file^".ml"]);
80
List.iter ["lablgl", lablgl_mls, "";
81
"togl", togl_mls, tk_libs;
82
"lablglut", glut_mls, glut_libs]
83
~f:begin fun (lib, mls,libs) ->
84
let cmxs = List.map mls ~f:(fun nm -> nm ^".cmx") in
85
exe !ocamlopt (["-a -o"; lib^".cmxa"; "-cclib -l"^lib;
86
"-cclib \""^libs^gl_libs^"\""] @ cmxs);
87
List.iter mls ~f:(fun nm -> may_remove (nm ^ ".obj"); may_remove (nm ^ ".o"))
90
let rename ~ext1 ~ext2 file =
91
if Sys.file_exists (file^ext1) && not (Sys.file_exists (file^ext2)) then begin
92
prerr_endline ("Renaming "^file^ext1^" to "^file^ext2);
93
Sys.rename (file^ext1) (file^ext2)
98
let arg = if Array.length Sys.argv > 1 then Sys.argv.(1) else "" in
99
if arg <> "" && arg <> "byte" && arg <> "opt" then begin
100
prerr_endline "ocaml build.ml [ byte | opt ]";
101
prerr_endline " byte build bytecode library only";
102
prerr_endline " opt build both bytecode and native (default)";
106
if arg = "opt" || arg <> "byte" then begin
109
prerr_endline ("Native build failed: " ^ err);
110
prerr_endline "You can still use the bytecode version"
112
if !ccomp_type = "msvc" then begin
113
List.iter ["liblablgl"; "libtogl"; "liblablglut"] ~f:(rename ~ext1:".a" ~ext2:".lib");
114
prerr_endline "Now ready to use on an OCaml MSVC port"
116
List.iter ["liblablgl"; "libtogl"; "liblablglut"] ~f:(rename ~ext2:".a" ~ext1:".lib");
117
prerr_endline "Now ready to use on an OCaml Mingw port"
120
prerr_endline ("Bytecode failed: " ^ err)