~ubuntu-branches/ubuntu/gutsy/lablgl/gutsy

« back to all changes in this revision

Viewing changes to src/build.ml.in

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2006-05-15 21:23:56 UTC
  • mfrom: (2.1.5 dapper)
  • Revision ID: james.westby@ubuntu.com-20060515212356-n5ue8ekfgxcsrm5g
Tags: 1.02-2
* Rebuild with OCaml 3.09.2.
* Updated standards version to 3.7.2, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(* $Id: build.ml.in,v 1.2 2005/10/17 08:38:42 garrigue Exp $ *)
 
2
(* A script to build lablGL libraries *)
 
3
 
 
4
open StdLabels
 
5
 
 
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 *)
 
10
 
 
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
 
15
    let next = cur+1 in
 
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 [])
 
21
 
 
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@ "
 
28
 
 
29
(* Hack to check for mingw *)
 
30
let () =
 
31
  try
 
32
    let ic = open_in "../Makefile.config" in
 
33
    while true do
 
34
      let s = input_line ic in
 
35
      match split ~sep:[' ';'\t';'='] s with
 
36
        "CCOMPTYPE" :: cc :: _ -> ccomp_type := cc
 
37
      | _ -> ()
 
38
    done
 
39
  with _ -> ()
 
40
 
 
41
(*
 
42
let gtk_libs =
 
43
  if !ccomp_type = "msvc" then gtk_libs else
 
44
  let libs =
 
45
    List.map (split gtk_libs) ~f:
 
46
      (fun nm ->
 
47
        if Filename.check_suffix nm ".lib" then "-l"^Filename.chop_extension nm^".dll"
 
48
        else nm)
 
49
  in String.concat " " libs
 
50
*)
 
51
 
 
52
let exe cmd args =
 
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)
 
57
 
 
58
let may_remove f =
 
59
  if Sys.file_exists f then Sys.remove f
 
60
 
 
61
let byte () =
 
62
  List.iter (lablgl_mls @ togl_mls @ glut_mls) ~f:
 
63
    begin fun file ->
 
64
      if Sys.file_exists (file ^ ".mli") then exe !ocamlc ["-c"; file^".mli"];
 
65
      exe !ocamlc ["-c"; file^".ml"]
 
66
    end;
 
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
 
75
    end
 
76
 
 
77
let native () =
 
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"))
 
88
    end
 
89
 
 
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)
 
94
  end
 
95
 
 
96
let () =
 
97
  try
 
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)";
 
103
      exit 2
 
104
    end;
 
105
    byte ();
 
106
    if arg = "opt" || arg <> "byte" then begin
 
107
      try native () with
 
108
        Failure err ->
 
109
          prerr_endline ("Native build failed: " ^ err);
 
110
          prerr_endline "You can still use the bytecode version"
 
111
    end;
 
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"
 
115
    end else begin
 
116
      List.iter ["liblablgl"; "libtogl"; "liblablglut"] ~f:(rename ~ext2:".a" ~ext1:".lib");
 
117
      prerr_endline "Now ready to use on an OCaml Mingw port"
 
118
    end
 
119
  with Failure err ->
 
120
    prerr_endline ("Bytecode failed: " ^ err)