configure.ml 5.31 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#use "topfind";;
#require "findlib";;
open Printf

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
 mliface               support for the CDuce/OCaml interface
 pxp_wlex              use wlexers for parsing utf8 with PXP [default: false]
 expat                 support for the expat XML parser
 curl                  support for the libcurl library
 netclient             support for the netclient library

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]

 --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;
    "expat", ref `auto;
    "curl", ref `auto;
    "netclient", ref `auto;
    "pxp_wlex", ref `no ]
  
let vars = 
  [ "prefix", ref "/usr/local";
    "bindir", ref "";
    "mandir", ref "";

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

    "sessiondir", ref "/tmp/cduce_sessions" ]
	
let fatal s = printf "*** Fatal error: %s\n" s; exit 1
let warning s = printf "* Warning: %s\n" s

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 () =
  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"

let check_feature f p =
  printf "%s: " f;
  match !(List.assoc f features) with
    | `no -> 
	printf "disabled\n"; false
    | `yes -> 
	printf "checking... ";
	if p () 
	then (printf "ok\n"; true) 
	else (printf "failed !\n"; fatal "Required feature is not available")
    | `auto -> 
	printf "autodetecting... ";
	if p () 
	then (printf "enabled\n"; true)
	else (printf "disabled\n"; false)

let check_pkg p () =
  try ignore (Findlib.package_directory p); true
  with Fl_package_base.No_such_package (_,_) -> false

let need_pkg p =
  printf "Checking for package %s... " p;
  if not (check_pkg p ()) 
  then (printf "failed !\n"; fatal "Required package is not available")
  else (printf "ok\n")

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)


let native = 
  check_feature "ocamlopt" 
122
    (fun () -> Sys.command "ocamlfind ocamlopt 2> /dev/null" = 0)
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
let ml_interface = check_feature "mliface" (fun () -> Sys.file_exists "oCaml_all.cma")
let expat = check_feature "expat" (check_pkg "expat")
let curl = check_feature "curl" (check_pkg "curl")
let netclient = check_feature "curl" (check_pkg "curl")
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"
let wprefix = dir "wprefix"
let cgidir = dir ~def:(wprefix^"/cgi-bin") "cgidir"
let htmldir = dir ~def:(wprefix^"/html") "htmldir"
let sessiondir = dir "sessiondir"

let exe = match Sys.os_type with "Cygwin" -> "exe" | _ -> ""

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

let required_packages = 
  ["camlp4"; "num"; 
   "pcre"; "ulex"; "cgi"; "netstring";
   "pxp-engine"; "pxp-lex-iso88591"
  ]

let () =
  List.iter need_pkg required_packages;
  if not pxp_wlex then need_pkg "pxp-lex-utf8";

  print_endline "Creating Makefile.conf...";

  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;
  fprintf out "ML_INTERFACE=%b\n" ml_interface;
  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;
  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