Coccinelle release-1.0.0-rc11
[bpt/coccinelle.git] / configure
1 #!/usr/bin/perl
2
3 # Author: Yoann Padioleau
4
5 ######################################################################
6 # Prelude
7 ######################################################################
8
9 # Yes I know about autoconf ... and autoconf sux.
10
11 # assume standard: diff
12 # assume standard: perl
13
14 #TODO check python 2.5 ?
15
16 # need latex and hevea if want to compile the documentation
17
18 #old: --with-menhir=/path/to/menhirLib or `ocamlfind query menhirLib`
19
20 my $project =
21 "coccinelle";
22 my $projectcmdline =
23 "spatch -sp_file demos/simple.cocci demos/simple.c -o /tmp/new_simple.c";
24
25 ######################################################################
26 # Options
27 ######################################################################
28
29 my $prefix="/usr/local";
30 my $python=1;
31 my $pcre=1;
32 my $noocamlscripting=0;
33 my $opt=".opt";
34 my $tarzan=1;
35 my $pydir="pycaml";
36 my $menhirdir="menhirlib";
37 my $sexpdir="ocamlsexp";
38 my $python_cmd="python";
39
40 # generic ocaml libraries (set entries to 0 to disable by default)
41 # 0: disabled, 1: use global library, >1: use local library
42 my %libs=
43 ( "pycaml" => 1
44 , "menhirLib" => 1
45 , "sexplib" => 1
46 , "pcre" => 1
47 , "dynlink" => 0
48 , "extlib" => 0
49 );
50
51 # add additional dependencies that some of these libraries may have
52 # these will then be added automatically
53 # (we don't really make use of this here)
54 my %deps=
55 ( # "pycaml" => ["str"] # example
56 # "sexplib" => ["unix", "bigarray", "num"]
57 );
58
59 local $_ = join ' ', @ARGV;
60
61 # Parse options
62 /-h/ || /--help/ and die "usage: $0 [--prefix=path] [--without-FEATURE] [--no-opt]\n
63 \t--without-python\tDisable scripting in Python
64 \t--without-ocaml\t\tDisable scripting in Ocaml
65 \t--without-pcre\t\tDisable regexp with PCRE
66 \t--with-pyton=PATH\t\tSpecify path to python executable
67 \t--no-opt\t\tDo not use the optimimized version of OCaml
68 \t--opt\t\t\tUse the optimimized version of OCaml\n\n";
69 /--prefix=([^ ]*)/ and $prefix = $1;
70 /--without-pcre/ and $pcre = 0;
71 /--without-python/ and $python = 0;
72 /--without-ocaml/ and $noocamlscripting = 1;
73 /--no-opt/ and $opt = "";
74 /--opt/ and $opt = ".opt";
75 /--with-python=([^ ]*)/ and $python_cmd = $1;
76
77 # todo: also print these options as part of the help message...
78 foreach my $lib (keys %libs) {
79 /--with-$lib/ and $libs{$lib} = 1;
80 /--with-local-$lib/ and $libs{$lib} = 2;
81 /--without-$lib/ and $libs{$lib} = 0;
82 }
83
84 #tarzan by default (used by score file parsing and now also for sexp_ast_c.ml)
85
86
87 # extend the feature set based on their dependencies
88 my $changedLibs = 1;
89 while($changedLibs) {
90 $changedLibs = 0;
91 foreach $dep (keys %deps) {
92 if ($libs{$dep}) {
93 my $reqs = $deps{$dep};
94 foreach $req (@$reqs) {
95 if (!$libs{$req}) {
96 $libs{$req} = 1;
97 $changedLibs = 1;
98 }
99 }
100 }
101 }
102 }
103
104 if (!$python) {
105 $libs{'pycaml'} = 0;
106 }
107
108 #if($ARGV[0] =~ "--prefix=(.*)") {
109 # $prefix = $1;
110 #}
111 #if($ARGV[1] =~ "--without-python") {
112 # $python = 0;
113 #}
114
115 my $src="$prefix/share/$project";
116
117 ######################################################################
118 # Side effects
119 ######################################################################
120
121
122 ######################################################################
123 # Helpers
124 ######################################################################
125 #BEGIN { die "need Perl 5 or greater" if $] < 5 ; }
126
127 #use Common;
128 sub pr2 { print STDERR "@_\n" }
129 sub cat {
130 my ($what) = @_;
131 my @list;
132 open(TMP, $what);
133 while(<TMP>) { push @list, "$_"; }
134 \@list;
135 }
136 sub notb { !$_[0] }
137 sub mapf { my ($f, $xs) = @_; [ map { &$f($_) } @{$xs} ] }
138 sub plural { my ($e) = @_; if ($e > 1) { "s" } else { "" } }
139
140 sub check_config { my ($command, $expect, $msggood, $msgbad) = @_;
141 my $error = 0;
142
143 my $full = cat($command);
144 my $res = join(" ", @{$full});
145 # pr2 $res;
146 if(notb($res =~ $expect)) { $error++; pr2 "!!!! $msgbad !!!!"; }
147 else { pr2 $msggood }
148 return $error;
149 }
150
151 ######################################################################
152 # Let's go
153 ######################################################################
154 pr2 "Checking your configuration.\n";
155
156 my $error = 0;
157
158 #---------------------------------------------------------------------
159 # Compilers and runtimes
160 #---------------------------------------------------------------------
161
162 $error +=
163 check_config("echo \"1;;\\n\" | ocaml |",
164 "Objective(.*) 3.\(09|1[0-9]\)",
165 "OCaml (the wonderful language) is present.",
166 "The program ocaml is missing or is not a good version. We need at least 3.09",
167 );
168
169 unless ($noocamlscripting) {
170 $noocamlscripting = check_config("echo \"1;;\\n\" | ocaml |",
171 "Objective(.*) 3.1[1-9]",
172 "OCaml >= 3.11 is present. OCaml scripting activated.",
173 "OCaml scripting disabled. We need at least OCaml 3.11",
174 );
175 }
176 my $ocamlprefix = $noocamlscripting ? "no" : "yes";
177
178 if ($opt eq ".opt") {
179 my $opt_check1 = `ocamlc.opt -version 2>/dev/null`;
180 if($opt_check1 =~ /[0-9\.]+/) {
181 pr2 "ocamlc.opt is present.";
182 }
183 else {
184 $opt="";
185 pr2 "ocamlc.opt not found";
186 }
187
188 my $opt_check2 = `ocamlopt.opt -version 2>/dev/null`;
189 if($opt_check2 =~ /[0-9\.]+/) {
190 pr2 "ocamlopt.opt is present.";
191 }
192 else {
193 $opt="";
194 pr2 "ocamlopt.opt not found";
195 }
196
197 my $opt_check3 = `ocamldep.opt -version 2>/dev/null`;
198 if($opt_check3 =~ /[0-9\.]+/) {
199 pr2 "ocamldep.opt is present.";
200 }
201 else {
202 $opt="";
203 pr2 "ocamldep.opt not found";
204 }
205
206 my $opt_check4 = `ocamllex.opt -version 2>/dev/null`;
207 if($opt_check4 =~ /[0-9\.]+/) {
208 pr2 "ocamllex.opt is present.";
209 }
210 else {
211 $opt="";
212 pr2 "ocamllex.opt not found";
213 }
214
215 if($opt eq "") {
216 pr2 "At least one native OCaml tool have not been found.";
217 pr2 "Desactivation of all native OCaml tools for compilation.";
218 }
219 }
220
221 #we have cached the result of menhir in the tgz we build.
222
223 #$error +=
224 # check_config("menhir --version |",
225 # "menhir, version 20071212",
226 ## "menhir, version 2006.*",
227 # "Menhir (the parser generator) is present.",
228 # "The program menhir is missing or is not a good version.",
229 # );
230
231
232 #---------------------------------------------------------------
233 # Developers tools
234 #---------------------------------------------------------------
235
236 pr2 "";
237
238 $error += check_config(
239 "make -v 2>&1 |grep Make|",
240 "GNU Make [^0-9]*3\.[0-9]+.*", #version 3.79.1, 3.81
241 "make (gnu version) is present.",
242 "The program gnu make is missing or is not a good version.
243 We need 3.XX",
244 );
245
246 #---------------------------------------------------------------------
247 # More developers tools
248 #---------------------------------------------------------------------
249
250 #---------------------------------------------------------------------
251 # Librairies
252 #---------------------------------------------------------------------
253
254 # Python dev
255 my $PY_VERSION;
256 if($python) {
257 if(check_config(
258 "$python_cmd --version 2>&1 |",
259 '^Python [23]\.',
260 "$python_cmd is present",
261 "$python_cmd is missing or is not a good version."
262 ))
263 {
264 $python=0;
265 }
266 if($python) {
267 my $PY_PREFIX = `$python_cmd pycaml/getprefix.py | tr -d '\n'`;
268 $PY_VERSION = `$python_cmd pycaml/getversion.py | tr -d '\n'`;
269 my $python_hdr = "$PY_PREFIX/include/python$PY_VERSION/Python.h";
270 if(check_config(
271 "ls $python_hdr 2> /dev/null | ",
272 $python_hdr,
273 "Python.h found",
274 "Python.h missing - You need to install python-dev"
275 ))
276 {
277 $python=0
278 }
279 }
280 }
281
282 my $ocamlfind=!check_config(
283 "ocamlfind ocamlc -version 2> /dev/null | ",
284 '^[0-9\.]+',
285 "ocamlfind is present",
286 "ocamlfind is missing -- Switch to local library copies."
287 );
288
289 # pycaml. Binding between Python and OCaml
290 my $syspydir=`ocamlfind query pycaml 2> /dev/null | xargs echo -n`;
291
292 if($syspydir) {
293 pr2 "'pycaml' library is present in $syspydir";
294 if ($libs{'pycaml'} > 1) {
295 pr2 "... but a local copy of 'pycaml' will be used.";
296 }
297 else {
298 $pydir=$syspydir;
299 }
300 } else {
301 pr2 "'pycaml' library is not present. A local copy will be used.";
302 }
303
304 # menhirLib. Runtime system for the parsers generated with menhir
305 my $sysmenhirdir=`ocamlfind query menhirLib 2> /dev/null | xargs echo -n`;
306
307 if($sysmenhirdir) {
308 $menhirdir=$sysmenhirdir;
309 pr2 "'menhirLib' library is present in $menhirdir";
310 } else {
311 pr2 "'menhirLib' library is not present. A local copy will be used.";
312 }
313
314 # sexplib.
315 my $syssexpdir=`ocamlfind query sexplib 2> /dev/null | xargs echo -n`;
316
317 if($syssexpdir) {
318 $sexpdir=$syssexpdir;
319 pr2 "'sexplib' library is present in $sexpdir";
320 } else {
321 pr2 "'sexplib' library is not present. A local copy will be used.";
322 }
323
324 # Check if libpcre-ocaml is available
325 # when pcre feature is enabled.
326 my $pcredir="";
327 if($pcre) {
328 $pcredir=`ocamlfind query pcre 2> /dev/null | xargs echo -n `;
329 if(check_config(
330 "echo -n $pcredir | ",
331 '^/.*$',
332 "'pcre-ocaml' library is present in $pcredir",
333 "'pcre-ocaml' library is not present - You need to install libpcre-ocaml-dev"
334 ))
335 {
336 $pcre=0
337 }
338 }
339
340 ######################################################################
341 # Printing of library selection
342 ######################################################################
343
344
345 pr2 "----------------------------------------------------------------------";
346 pr2 "Library configuration:";
347
348 my %libPaths;
349 foreach my $lib (keys %libs) {
350 my $isSelected = $libs{$lib};
351 if ($isSelected) {
352 my $libdir = `ocamlfind query $lib 2> /dev/null | xargs echo -n`;
353 if ($libdir && $libs{$lib} <= 1) {
354 $libs{$lib} = 1;
355 $libPaths{$lib} = $libdir;
356 }
357 else {
358 if (-d $lib) { # test if local directory exists
359 $libs{$lib} = 2; # use local version
360 $libPaths{$lib} = $lib;
361 } else {
362 $libs{$lib} = 0; # disable the feature
363 $libPaths{$lib} = "";
364 }
365 }
366 }
367
368 my $choice = $libs{$lib};
369 if ($choice == 0) {
370 pr2 "$lib: disabled";
371 } elsif ($choice == 1) {
372 pr2 "$lib: global version in $libPaths{$lib}";
373 } else {
374 pr2 "$lib: local version selected";
375 }
376 }
377
378 ######################################################################
379 # Generate config files (platform/portability issues)
380 ######################################################################
381
382
383 ######################################################################
384 # Generate globals files (features issues)
385 ######################################################################
386
387 ######################################################################
388 # Diagnostic
389 ######################################################################
390
391 if($error) {
392 pr2 "
393 ----------------------------------------------------------------------
394 !!!! There seems to have problem, we have found $error missing package" .
395 plural($error) . ".
396 " . (($error > 1) ? "Some of those packages" : "This package") .
397 " may be installed by picking " . ($error > 1 ? "them" : "it") .
398 " in $project-dependencies.tgz available
399 on the $project website. !!!!
400 ----------------------------------------------------------------------
401 ";
402 } else {
403
404 pr2
405 "----------------------------------------------------------------------
406
407 All seems fine for $project.
408
409 To compile $project type:
410 \$ make depend
411 \$ make all
412
413 Or alternatively, for the optimized version:
414 \$ make all.opt
415 If you want both, you could use:
416 \$ make world
417
418
419 To install type:
420 \$ make install
421
422 Then, to test $project simply type:
423 \$ $projectcmdline
424
425 ";
426
427 if($python) {
428 pr2
429 "To use the python SmPL feature you may have to set some environment variables.
430 However, they are automatically set by the front-end installed in $prefix/bin.
431 For bash do:
432 export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:$prefix/lib
433 export PYTHONPATH=\$PYTHONPATH:$src/python"
434 }
435
436 pr2 "----------------------------------------------------------------------";
437 }
438
439 ######################################################################
440 # Generating the configuration
441 ######################################################################
442
443 pr2 " Compilation configuration\n";
444
445 pr2 "$project target prefix: $prefix (you can use --prefix to override it)";
446 pr2 "Generating Makefile.config";
447 open(CONFIG, ">Makefile.config");
448 print CONFIG "# autogenerated by configure
449
450 #
451 INSTALL_PROGRAM?=install -c -m 755
452 INSTALL_LIB?= install -c -m 755
453 INSTALL_DATA?= install -c -m 644
454
455 # Where to install the binary
456 BINDIR=$prefix/bin
457
458 # Where to install the man pages
459 MANDIR=$prefix/man
460
461 # Where to install the lib
462 LIBDIR=$prefix/lib
463
464 # Where to install the configuration files
465 SHAREDIR=$src
466
467 BASH_COMPLETION_DIR=/etc/bash_completion.d
468
469 # Features
470 FEATURE_PYTHON=$python
471 FEATURE_TARZAN=$tarzan
472 ";
473
474 if ($python) {
475 print CONFIG "PYVER=$PY_VERSION\n";
476 print CONFIG "PYCMD=$python_cmd\n";
477 }
478
479 if($pcre) {
480 print CONFIG "
481 PCREDIR=$pcredir"
482 }
483
484 print CONFIG "
485 PYCAMLDIR=$pydir
486 MENHIRDIR=$menhirdir
487 SEXPDIR=$sexpdir
488 DYNLINK=$ocamlprefix
489
490 # The OPTBIN variable is here to allow to use ocamlc.opt instead of
491 # ocaml, when it is available, which speeds up compilation. So
492 # if you want the fast version of the ocaml chain tools, set this var
493 # or setenv it to \".opt\" in your startup script.
494 OPTBIN=$opt
495 ";
496
497 #
498 # Generate variables for handling the ocaml libraries
499 #
500
501 my @sellibs = ();
502 my @makelibs = ();
503 my @cleanlibs = ();
504 my @deplibs = ();
505 my @lnklibs = ();
506 my @optlnklibs = ();
507 my @inclibs = ();
508 my @flagslibs = ();
509 my @featurelibs = ();
510 my @pathlibs = ();
511
512 sub strVar {
513 return "\$(" . $_[0] . "_" . $_[1] . ")";
514 }
515
516 foreach $lib (keys %libs) {
517 my $sel = $libs{$lib};
518 my $path = $libPaths{$lib};
519 my $pathvar = strVar("PATH", $lib);
520
521 if ($sel == 1) { # global version
522 push(@lnklibs, strVar("GLOBAL", $lib));
523 push(@optlnklibs, strVar("GLOBALOPT", $lib));
524 } elsif ($sel > 1) { # local version
525 push(@lnklibs, strVar("LOCAL", $lib));
526 push(@optlnklibs, strVar("LOCALOPT", $lib));
527 if (-e "$path/Makefile") { # does it have a Makefile?
528 push(@makelibs, $pathvar);
529 push(@cleanlibs, $pathvar);
530 push(@deplibs, $pathvar);
531 }
532 }
533
534 if ($sel >= 1) { # indifferent of global/local
535 push(@sellibs, $lib);
536 push(@inclibs, $pathvar);
537 push(@flagslibs, strVar("FLAGS", $lib));
538
539 push(@featurelibs, "FEATURE_$lib=1");
540 push(@pathlibs, "PATH_$lib=$path");
541 }
542 }
543
544 my $strSellibs = join(" ", @sellibs);
545 my $strMakelibs = join(" ", @makelibs);
546 my $strCleanlibs = join(" ", @cleanlibs);
547 my $strDeplibs = join(" ", @deplibs);
548 my $strLnklibs = join(" ", @lnklibs);
549 my $strOptlnklibs = join(" ", @optlnklibs);
550 my $strInclibs = join(" ", @inclibs);
551 my $strFlagslibs = join(" ", @flagslibs);
552 my $strFeatures = join("\n", @featurelibs);
553 my $strPaths = join("\n", @pathlibs);
554
555 print CONFIG "
556 # selected libraries
557 $strFeatures
558
559 # library paths
560 $strPaths
561
562 # collected config vars
563 SELLIBS=$strSellibs
564 MAKELIBS=$strMakelibs
565 CLEANLIBS=$strCleanlibs
566 DEPLIBS=$strDeplibs
567 LNKLIBS=$strLnklibs
568 OPTLNKLIBS=$strOptlnklibs
569 INCLIBS=$strInclibs
570 FLAGSLIBS=$strFlagslibs
571
572 # selected ocaml modules
573 ";
574
575 sub repl {
576 my $src = $_[0];
577 my $dest = $_[1];
578 my $key = $_[2];
579 my $val = ucfirst($_[3]);
580 my $cmd = "sed -e 's|${key}|${val}|g' ${src} > ${dest}";
581 if (-e $dest) {
582 unlink($dest);
583 }
584 `$cmd`;
585 }
586
587 my $pythonprefix = $python ? "yes" : "no";
588 pr2 "Support for python scripting: $pythonprefix";
589 repl('python/pycocci.ml.in', 'python/pycocci.ml', '@PYCOCCI_FILE@', "${pythonprefix}_pycocci");
590 print CONFIG "PYCOCCI_FILE=${pythonprefix}_pycocci.ml\n";
591 # `cd python; ln -sf ${pythonprefix}_pycocci.ml pycocci.ml; `;
592 `cd python; make depend`;
593
594 pr2 "Support for ocaml scripting: $ocamlprefix";
595 repl('ocaml/prepare_ocamlcocci.ml.in', 'ocaml/prepare_ocamlcocci.ml', '@OCAMLCOCCI_FILE@', "${ocamlprefix}_prepare_ocamlcocci");
596 print CONFIG "OCAMLCOCCI_FILE=${ocamlprefix}_prepare_ocamlcocci.ml\n";
597 # `cd ocaml; ln -sf ${ocamlprefix}_prepare_ocamlcocci.ml prepare_ocamlcocci.ml;`;
598 `cd ocaml; make depend`;
599
600 my $pcresuffix = $pcre ? "pcre" : "str";
601 pr2 "Support for regexp: $pcresuffix";
602 repl('globals/regexp.ml.in', 'globals/regexp.ml', '@REGEXP_FILE@', "Regexp_${pcresuffix}");
603 print CONFIG "REGEXP_FILE=regexp_${pcresuffix}.ml";
604 # `cd globals; ln -sf regexp_${pcresuffix}.ml regexp.ml; touch regexp.ml;`;
605 `cd globals; make depend`;
606
607 pr2 "Modifying globals/config.ml";
608 my $version = `cat ./version | tr -d '\n'`;
609 `echo -n "$version" > ./version`; # make sure that there are no newlines after version
610 my $command1 = "sed -i -e 's|^let version = \".*\"\$|let version = \"$version\"|g' globals/config.ml.in"; # substitute version
611 my $command2 = "perl -p -e 's#Not_found.\*#Not_found->\\\"$src\\\"#' globals/config.ml.in > globals/config.ml";
612 `$command1`;
613 `$command2`;
614
615 pr2 "----------------------------------------------------------------------";
616 pr2 " Runtime dependency\n";
617
618 if ($ocamlfind) {
619 pr2 "Support for external ocaml library in ocaml scripting: yes (ocamlfind found)";
620 }
621 else {
622 pr2 "Support for external ocaml library in ocaml scripting: no";
623 pr2 "!!!!!!! WARNING !!!!!!! ocamlfind may be required at runtime";
624 }
625
626 pr2 "";