configure.ml 8.84 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
 expat                 support for the expat XML parser
 curl                  support for the libcurl library
 netclient             support for the netclient library
32
 cgi                   support for the cgi library 
33

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

37 38 39 40
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]
41 42 43
 --docdir=DIR          install the rest of the doc in DIR [PREFIX/doc/cduce]
";
if not_distrib then print_string "
44 45 46 47 48 49 50 51 52 53
 --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;
54
    "pxp", ref `auto;
55 56 57
    "expat", ref `auto;
    "curl", ref `auto;
    "netclient", ref `auto;
58
    "cgi", ref `auto;
59 60 61 62 63 64
    "pxp_wlex", ref `no ]
  
let vars = 
  [ "prefix", ref "/usr/local";
    "bindir", ref "";
    "mandir", ref "";
65
    "docdir", ref "";
66 67 68 69 70

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

71 72 73
    "sessiondir", ref "/tmp/cduce_sessions";
    "mliface", ref ""
  ]
74 75


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

81 82 83 84 85 86 87 88 89 90 91
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

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
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 () =
113
  print_endline "Configuring CDuce for compilation...\n";
114 115 116 117 118 119
  (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
120 121 122 123 124

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

let native = 
  check_feature "ocamlopt" (fun () -> command "ocamlfind ocamlopt")
139 140

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

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

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)


166
let exe = match Sys.os_type with 
Karoline Malmkjaer's avatar
Karoline Malmkjaer committed
167 168
  | "Win32" ->
      print "Win32 detected... executable will have .exe extension\n"; ".exe" 
169
  | "Cygwin" ->
Karoline Malmkjaer's avatar
Karoline Malmkjaer committed
170
      print "Cygwin detected... executable will have .exe extension\n"; ".exe" 
171 172
  | _ -> ""

173 174 175 176
let add_icon = match Sys.os_type with 
  | "Win32" | "Cygwin" -> true
  | _ -> false

177
let check_mliface dir =
Karoline Malmkjaer's avatar
Karoline Malmkjaer committed
178
(*  Sys.file_exists (Filename.concat dir "typing/types.ml") *)
179
  Sys.file_exists (Filename.concat dir "typing/types.ml") 
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 191 192 193 194
let make_absolute dir =
  if Filename.is_relative dir 
  then Filename.concat (Sys.getcwd ()) dir
  else dir

195
let ml_interface = 
196
  let dir1 = !(List.assoc "mliface" vars) in
197
  let dirs = if dir1 = "" then [] else [ make_absolute dir1 ] in
198
  print "ocaml sources... ";
199 200
  let rec loop = function
    | [] -> 
201 202
	print "not found (the interface will not be built)\n";
	None
203
    | d::dirs ->
204 205 206
	if check_mliface d then
	  (print ("found: " ^ d ^ "\n"); Some d)
	else loop dirs
207 208
  in
  loop dirs
209
  
210
let pxp = check_feature "pxp" (check_pkg "pxp")
211 212
let expat = check_feature "expat" (check_pkg "expat")
let curl = check_feature "curl" (check_pkg "curl")
213
let netclient = check_feature "netclient" (check_pkg "netclient")
214
let cgi = check_feature "cgi" (check_pkg "netcgi1")
215 216 217 218
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"
219
let docdir = dir ~def:(prefix^"/doc/cduce") "docdir"
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
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

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

245 246
let cgi =
    if not(cgi) then
247
        (warning "No cgi package found: dtd2cduce and the web interface won't be built";
248 249 250 251
        false)
    else true

let required_packages = ["camlp4"; "num"; "pcre"; "ulex"; "netstring"]
252

253 254
let has_forpack =
  print "testing for -for-pack option: ";
Karoline Malmkjaer's avatar
Karoline Malmkjaer committed
255 256 257 258 259 260 261 262
  let comm =
    match Sys.os_type with
      | "Win32" -> "ocamlc -for-pack foo"
      | _ -> "ocamlc -for-pack foo 2> /dev/null" in
    if Sys.command comm = 0 then
      (print "available\n"; true)
    else
      (print "not available\n"; false)
263

264 265 266 267 268 269 270
let has_natdynlink =
  print "testing for native dynlink: ";
  if Sys.command "ocamlopt -o foo dynlink.cmxa && rm -f foo" = 0 then
    (printf "available\n"; true)
  else
    (print "not available\n"; false)

271 272
let () =
  List.iter need_pkg required_packages;
273 274
  if pxp then (
    need_pkg "pxp-engine";
275
    need_pkg "pxp-lex-iso88591";
276 277
    if not pxp_wlex then need_pkg "pxp-lex-utf8";
  );
278

279
  print "Creating Makefile.conf...\n";
280 281 282 283

  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;
284
  (match ml_interface with
285 286
    | Some d -> fprintf out "ML_INTERFACE=true\nOCAML_SRC=%s\n" d
    | None -> fprintf out "ML_INTERFACE=false\n");
287
  fprintf out "PXP=%b\n" pxp;
288 289 290
  fprintf out "EXPAT=%b\n" expat;
  fprintf out "CURL=%b\n" curl;
  fprintf out "NETCLIENT=%b\n" netclient;
291
  fprintf out "CGI=%b\n" cgi;
292 293 294
  fprintf out "PXP_WLEX=%b\n" pxp_wlex;
  fprintf out "BINDIR=%s\n" bindir;
  fprintf out "MANDIR=%s\n" mandir;
295
  fprintf out "DOCDIR=%s\n" docdir;
296 297 298 299
  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;
300
  fprintf out "ADDICON=%b\n" add_icon;
301
  fprintf out "PROFILE=false\n";
302
  fprintf out "FORPACK=%b\n" has_forpack;
303
  fprintf out "NATDYNLINK=%b\n" has_natdynlink;
304
  close_out out