configure.ml 8.15 KB
Newer Older
1
(*
2
3
#use "topfind";;
#require "findlib";;
4
*)
5

6
7
open Printf

8
9
let not_distrib = Sys.file_exists "Makefile.distrib"

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
let usage () =
  print_string "\
Configuration of CDuce.

Usage: ./configure [OPTION]...

Defaults for the options are specified in brackets.

Options:
 --help                display this help and exit

Optional features:
 --with-FEATURE        force support for FEATURE [default: autodetect]
 --without-FEATURE     disable support for FEATURE [default: autodetect]

 Available features:
 ocamlopt              use ocamlopt instead of ocamlc to build CDuce
 pxp_wlex              use wlexers for parsing utf8 with PXP [default: false]
28
 pxp                   support for the PXP XML parser
29
30
31
32
 expat                 support for the expat XML parser
 curl                  support for the libcurl library
 netclient             support for the netclient library

33
OCaml/CDuce interface:
34
 --mliface=DIR         build the interface with the OCaml sources in DIR
35

36
37
38
39
Installation directories:
 --prefix=PREFIX       install files in PREFIX [/usr/local]
 --bindir=DIR          install user executables in DIR [PREFIX/bin]
 --mandir=DIR          install man documentation in DIR [PREFIX/man]
40
41
42
 --docdir=DIR          install the rest of the doc in DIR [PREFIX/doc/cduce]
";
if not_distrib then print_string "
43
44
45
46
47
48
49
50
51
52
 --wprefix=WPREFIX     root directory of the web-server [/var/www]
 --cgidir=DIR          install the cgi-bin interpreter in DIR [WPREFIX/cgi-bin]
 --htmldir=DIR         install the website in DIR [WPREFIX/html]
 --sessiondir=DIR      store the open sessions of the cgi-bin in DIR 
                       [/tmp/cduce_sessions]
"

let features = 
  [ "ocamlopt", ref `auto;
    "mliface", ref `auto;
53
    "pxp", ref `auto;
54
55
56
57
58
59
60
61
62
    "expat", ref `auto;
    "curl", ref `auto;
    "netclient", ref `auto;
    "pxp_wlex", ref `no ]
  
let vars = 
  [ "prefix", ref "/usr/local";
    "bindir", ref "";
    "mandir", ref "";
63
    "docdir", ref "";
64
65
66
67
68

    "wprefix", ref "/var/www";
    "cgidir", ref "";
    "htmldir", ref "";

69
70
71
    "sessiondir", ref "/tmp/cduce_sessions";
    "mliface", ref ""
  ]
72
73


74
let src_dirs = ["/usr/src"; "/usr/local/src"; "/tmp"]
75
76
77
78
	
let fatal s = printf "*** Fatal error: %s\n" s; exit 1
let warning s = printf "* Warning: %s\n" s

79
80
81
82
83
84
85
86
87
88
89
let log s =
  let oc = open_out_gen [ Open_append; Open_creat ] 0o600 "configure.log" in
  output_string oc s;
  output_char oc '\n';
  close_out oc;
  flush stdout

let command s =
  log ("==> " ^ s);
  Sys.command (sprintf "%s 2>> configure.log" s) = 0

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let start_with s p =
  let ls = String.length s and lp = String.length p in
  if (ls >= lp) && (String.sub s 0 lp = p)
  then Some (String.sub s lp (ls - lp)) else None
    
let parse_arg s =
  if s = "--help" then (usage (); exit 0)
  else
    match start_with s "--with-" with
      | Some f -> (List.assoc f features) := `yes
      | None ->
    match start_with s "--without-" with
      | Some f -> (List.assoc f features) := `no
      | None ->
    let i = String.index s '=' in
    if i < 2 then raise Not_found;
    let n = String.sub s 2 (i - 2) in
    let v = String.sub s (succ i) (String.length s - i - 1) in
    (List.assoc n vars) := v

let () =
111
  print_endline "Configuring CDuce for compilation...\n";
112
113
114
115
116
117
  (try for i = 1 to Array.length Sys.argv - 1 do parse_arg Sys.argv.(i) done
  with Not_found -> usage (); fatal "Incorrect command line");
  (try Sys.remove "configure.log" with Sys_error _ -> ());
  ignore (Sys.command "uname -a >> configure.log")

let print s = print_string s; flush stdout
118
119
120
121
122

let check_feature f p =
  printf "%s: " f;
  match !(List.assoc f features) with
    | `no -> 
123
	print "disabled\n"; false
124
    | `yes -> 
125
	print "checking... ";
126
	if p () 
127
128
	then (print "ok\n"; true) 
	else (print "failed !\n"; fatal "Required feature is not available")
129
    | `auto -> 
130
	print "autodetecting... ";
131
	if p () 
132
133
134
135
136
	then (print "enabled\n"; true)
	else (print "disabled\n"; false)

let native = 
  check_feature "ocamlopt" (fun () -> command "ocamlfind ocamlopt")
137
138

let check_pkg p () =
139
140
141
142
143
144
  try
(*    ignore (Findlib.package_property
      [ (if native then "native" else "bytecode") ]
      p "archive"); *)
    command 
      (sprintf 
145
	 "ocamlfind ocaml%s -package %s -linkpkg -o configure.try && rm -f configure.try"
146
147
148
	 (if native then "opt" else "c")
	 p)
  with Not_found -> false
149
150

let need_pkg p =
151
  printf "Checking for package %s... " p; flush stdout;
152
  if not (check_pkg p ()) 
153
154
  then (print "failed !\n"; fatal "Required package is not available")
  else (print "ok\n")
155
156
157
158
159
160
161
162
163

let dir ?def d = 
  let s = !(List.assoc d vars) in
  if s <> "" then s
  else match def with 
    | Some x -> x 
    | None -> fatal (sprintf "%s cannot be empty" d)


164
165
let exe = match Sys.os_type with 
  | "Cygwin" ->
166
      print "Cygwin detected... executable will have .exe extension"; ".exe" 
167
168
  | _ -> ""

169
170
171
172
173
174
175
176
177
178
179
let check_mliface dir =
  log ("Looking for ocaml modules in " ^ dir);
  let file = if native then "types.cmx" else "types.cmo" in
  if Sys.file_exists (Filename.concat dir file) 
  then `flat
  else
    if Sys.file_exists 
      (Filename.concat (Filename.concat dir "typing") file) then
	`tree
    else
      `not_found
180

181
let ocaml_stdlib () =
182
183
184
  if (Sys.command "ocamlc -where > ocaml_stdlib" <> 0) then
    fatal "Can't run ocamlc to get OCaml standard library path";
  let ic = open_in "ocaml_stdlib" in
185
186
  let s = input_line ic in
  close_in ic;
187
  Sys.remove "ocaml_stdlib";
188
  s
189

190
let ml_interface = 
191
  let dir1 = !(List.assoc "mliface" vars) in
192
193
  let dirs = 
    let std = ocaml_stdlib () in
194
195
196
197
    Filename.concat (Filename.chop_suffix std "std-lib") "compiler-lib" :: 
          (* GODI *)
    Filename.concat std "compiler-libs" :: (* Debian *)
    src_dirs in
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  let dirs = if dir1 = "" then dirs else dir1 :: dirs in
  print "Looking for ocaml compiler modules ...";
  let rec loop = function
    | [] -> 
	print "not found\n";
	`no
    | d::dirs ->
	match check_mliface d with
	  | `flat -> print ("flat model: " ^ d ^ "\n"); `flat d
	  | `tree -> print ("tree model: " ^ d ^ "\n"); `tree d
	  | `not_found -> loop dirs
  in
  loop dirs

212
  
213
let pxp = check_feature "pxp" (check_pkg "pxp")
214
215
let expat = check_feature "expat" (check_pkg "expat")
let curl = check_feature "curl" (check_pkg "curl")
216
let netclient = check_feature "netclient" (check_pkg "netclient")
217
218
219
220
let pxp_wlex = check_feature "pxp_wlex" (check_pkg "pxp-wlex-utf8")
let prefix = dir "prefix"
let bindir = dir ~def:(prefix^"/bin") "bindir"
let mandir = dir ~def:(prefix^"/man") "mandir"
221
let docdir = dir ~def:(prefix^"/doc/cduce") "docdir"
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
let wprefix = dir "wprefix"
let cgidir = dir ~def:(wprefix^"/cgi-bin") "cgidir"
let htmldir = dir ~def:(wprefix^"/html") "htmldir"
let sessiondir = dir "sessiondir"

let curl,netclient = 
  match curl,netclient with
    | true,true -> 
	warning "Both netclient and curl are available. Will use curl.";
	true,false
    | false,false ->
	warning "No package for loading external URLs.";
	false,false
    | c,n -> c,n

237
238
239
240
let pxp,expat = 
  match pxp,expat with
    | true,true -> 
	warning "Both PXP and expat are available. Will build both and use expat by default.";
241
	true,true
242
243
244
245
246
    | false,false ->
	warning "No package for parsing XML documents.";
	false,false
    | c,n -> c,n

247
let required_packages = ["camlp4"; "num"; "pcre"; "ulex"; "cgi"; "netstring"]
248
249
250

let () =
  List.iter need_pkg required_packages;
251
252
  if pxp then (
    need_pkg "pxp-engine";
253
    need_pkg "pxp-lex-iso88591";
254
255
    if not pxp_wlex then need_pkg "pxp-lex-utf8";
  );
256

257
  print "Creating Makefile.conf...\n";
258
259
260
261

  let out = open_out "Makefile.conf" in
  fprintf out "# This file has been generated by the configure script\n";
  fprintf out "NATIVE=%b\n" native;
262
263
264
265
  (match ml_interface with
    | `no -> fprintf out "ML_INTERFACE=false\n"
    | `flat d -> fprintf out "ML_INTERFACE=flat\nML_MODULES=%s\n" d
    | `tree d -> fprintf out "ML_INTERFACE=tree\nML_MODULES=%s\n" d);
266
  fprintf out "PXP=%b\n" pxp;
267
268
269
270
271
272
  fprintf out "EXPAT=%b\n" expat;
  fprintf out "CURL=%b\n" curl;
  fprintf out "NETCLIENT=%b\n" netclient;
  fprintf out "PXP_WLEX=%b\n" pxp_wlex;
  fprintf out "BINDIR=%s\n" bindir;
  fprintf out "MANDIR=%s\n" mandir;
273
  fprintf out "DOCDIR=%s\n" docdir;
274
275
276
277
278
279
  fprintf out "CGI_DIR=%s\n" cgidir;
  fprintf out "HTML_DIR=%s\n" htmldir;
  fprintf out "SESSION_DIR=%s\n" sessiondir;
  fprintf out "EXE=%s\n" exe;
  fprintf out "PROFILE=false\n";
  close_out out