summaryrefslogtreecommitdiff
path: root/cil/configure.in
blob: aee7ac73de3af4fe6964180153f6f0a50931b143 (plain)
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
122
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# configure.in for CIL           -*- sh -*-
# Process this file with autoconf to produce a configure script.

# Autoconf runs this through the M4 macroprocessor first; lines
# starting with "dnl" are comments to M4.  The result is a bash
# script; any text which isn't an M4/autoconf directive gets
# copied verbatim to that script.

# also, in general, watch out: the M4 quoting charactes are
# the square brackets: [ and ].  if you want to pass brackets
# to something, you can quote the brackets with more brackets.
# I don't know how to pass a single (unbalanced) bracket ..

# sm: changed this file to use '#' for comments, since that's
# just as good (since this becomes an 'sh' script)


# We must put these AC_SUBST very early, and in this order. See below where we
# define NEWLINE
AC_SUBST(CIL_FEATURES_DEFINES) 
AC_SUBST(NEWLINE)


# -------------- usual initial stuff -------------
# this simply names a file somewhere in the source tree to verify
# we're in the right directory
AC_INIT(src/cil.mli)
AC_CONFIG_HEADERS(config.h)

# sm: require a late-enough autoconf; this is the version number
# that's on manju, so I assume it's ok
AC_PREREQ(2.50)

#
# Assign here the CIL version numbers
CIL_VERSION_MAJOR=1
CIL_VERSION_MINOR=3
CIL_VERSION_REV=5
CIL_VERSION=$CIL_VERSION_MAJOR.$CIL_VERSION_MINOR.$CIL_VERSION_REV


# make sure I haven't forgotten to run autoconf
if test configure -ot configure.in; then
  AC_MSG_ERROR(configure is older than configure.in; you forgot to run autoconf)
fi

# check for C compiler; this typically finds gcc; it sets the
# variable CC to whatever it finds, which then gets substituted
# for @CC@ in output files; you have to do this even if you don't
# care about @CC@, because system feature tests later on in
# the ./configure script will expect $CC to be set right
AC_PROG_CC

AC_PROG_INSTALL


# find system type (using this macro means we must include
# the files install-sh, config.sub, and config.guess (all from
# the autoconf distribution) in our source tree!)
AC_CANONICAL_SYSTEM


# ---------------- generic functions -----------------
# debugging diagnostic; set to 'echo' to debug or 'true' for production
# (technically you're not supposed to use shell functions in
# configure scripts, because some-obscure-sh somewhere doesn't
# support them.. but they're too convenient to not use)
diagnostic() {
  #echo "$@"
  true "$@"
}

# determine if a binary is in the path
binaryExists() {
  # on cygwin, 'which' always returns success, so use 'type' instead
  if type "$1" >/dev/null 2>&1; then
    return 0
  else
    return 1
  fi
}


# -------------- portable configuration ----------------
# this specifies the root of the source tree; it's just the
# directory where ./configure runs, except on cygwin, which
# overrides this below
CILHOME=`pwd`

DEFAULT_COMPILER=_GNUCC
DEFAULT_CIL_MODE=GNUCC

# is the microsoft compiler available?
# hmm.. I think we should check the version or something, because
# sometimes people have Common Lisp's interpreter called 'cl' ..
AC_MSG_CHECKING(for msvc cl.exe (optional))
# See if CC points to the MS compiler
if "$CC" 2>&1 | grep "Microsoft" >/dev/null; then 
  AC_MSG_RESULT([found, set as default])
  HAS_MSVC=yes
  DEFAULT_COMPILER=_MSVC
  DEFAULT_CIL_MODE=MSVC
else
  if cl 2>&1 | grep "Microsoft" >/dev/null ;then
     AC_MSG_RESULT(found)
     HAS_MSVC=yes
  else
     AC_MSG_RESULT(not found)
     HAS_MSVC=no
  fi
fi

# is ocaml available?
# needed binaries: ocamllex ocamlyacc ocamldep ocamlopt ocamlc
ocamlDownloadInstructions="
      OCaml can be downloaded from http://caml.inria.fr/ocaml/.
      After downloading and unpacking the source distribution, in the ocaml
      directory, do
        ./configure
        make world
        make opt
        make install
      Then come back here and re-run ./configure."

# required major/minor. 
# required major/minor
reqMaj=3
reqMin=08
knownMaj=3
knownMin=09
AC_MSG_CHECKING(ocaml version is at least $reqMaj.$reqMin)
if binaryExists ocamlc; then
  # what version?
  ver=`ocamlc -v | grep version | sed 's/^.*version //'`
  diagnostic "ver is $ver"
  # major: anything before the .
  major=`echo $ver | sed 's/\..*$//'`
  diagnostic "major is $major"
  # minor: numbers after the .
  # (the outer level of bracket-quotation protects the inner brackets)
  [minor=`echo $ver | sed 's/^[^.]*\.\([0-9][0-9]*\).*$/\1/'`]
  diagnostic "minor is $minor"

  # I would think autoconf would already have a facility for doing
  # these kinds of major/minor version checks, but I can't find it
  if test $major -gt $reqMaj -o $major -ge $reqMaj -a $minor -ge $reqMin; then
    AC_MSG_RESULT([version is $ver, ok])

    # sm: added this test when we found that CCured needed to be changed
    # a little when 3.06 came out (it had previously worked with 3.04)
    if test "$major" -gt $knownMaj -o "$major" -ge $knownMaj -a "$minor" -gt $knownMin; then
      AC_MSG_WARN([Your ocaml version is $ver, but the latest version this program
                   is known to work with is $knownMaj.$knownMin.  If you have
                   trouble compiling, please try using an earlier version
                   or see if there is a later version of this program.])
    fi
  else
    AC_MSG_ERROR([
      I found OCaml version $ver; this program requires at least $reqMaj.$reqMin.
      Please download a newer OCaml distribution.
      $ocamlDownloadInstructions
    ])
  fi

  # check for existence of other binaries
  AC_MSG_CHECKING(existence of related ocaml tools)
  if binaryExists ocamllex && \
     binaryExists ocamlyacc && \
     binaryExists ocamldep && \
     binaryExists ocamlopt; then
    AC_MSG_RESULT(ok)
  else
    AC_MSG_ERROR([
      At least one of ocamllex, ocamlyacc, ocamldep or ocamlopt is missing.
      In particular, ocamlopt requires you to "make opt" when building
      OCaml from source.  Please make sure all these tools are built and
      in the path.
    ])
  fi
else
  AC_MSG_ERROR([
      The "ocamlc" OCaml compiler was not found in the path: $PATH.

      Most of this program is written in the OCaml language, so its compiler
      is required.
      $ocamlDownloadInstructions
  ])
fi

#
# ------------------- Perl ----------------
#
AC_MSG_CHECKING([for Perl])
  if ! binaryExists perl; then
    AC_MSG_ERROR([
      perl not found.
      You need perl version 5.6.1 or later for CIL.
      You can get perl at http://www.cpan.org/src/index.html .
    ])
  fi

  # sm: oh how nice it would be to just say "use English;
  # print($PERL_VERSION)", but that appears broken on 5.6.1.. so I'm
  # trying to say "caret right-bracket", but then that would run afoul
  # of autoconf's quoting characters, so I use the "quadrigraph" @:>@
  # to stand for right-bracket.  what a mess.
  perlver=`perl -e 'print($@:>@);'`
  if perl -e "exit( $perlver >= 5.006001 );"; then
    AC_MSG_ERROR([
      Found perl version $perlver, but at least 5.6.1 is required.
      You can get a newer perl at http://www.cpan.org/src/index.html .
    ])
  fi

  perlport=`perl -e "print $^O;"`
  case "$perlport" in
    cygwin)
        ;;
    MSWin32) # ActivePerl
        ;;
    linux)
        ;;
    freebsd)
        ;;
    openbsd)
        ;;
    darwin) # Mac OS X
        ;;
    solaris)
	;;
    *)
      AC_MSG_ERROR([
        Unsupported Perl port $perlport -- sorry.
        cygwin, MSWin32 (ActivePerl), linux, freebsd, openbsd, darwin,
        and solaris are the supported ports.
      ])
  esac
AC_MSG_RESULT([found version $perlver, port $perlport])

        # The cygwin port has some bugs in the File::Spec module
if test "$perlport" = "cygwin" ;then
  AC_MSG_CHECKING([for known cygwin Perl bug in File::Spec])
  perlfixres=[`perl -e '
      use File::Spec;
      if(File::Spec->file_name_is_absolute("C:/test")) {
        print "no bug found"; exit 0;
      } else {
        print "bug";
        foreach $d (@INC) {
           if(-f "$d/File/Spec/Unix.pm") {
              open(IN, "<$d/File/Spec/Unix.pm");
              open(OUT, ">$d/File/Spec/Unix.pm.fixed") 
                       || die "Cannot open $d/File/Spec/Unix.pm.fixed";
              while(<IN>) {
                if($_ =~ m|sub file_name_is_absolute|) {
                   print OUT $_;
                   print OUT scalar(<IN>);
                   print OUT <<EOF;
   if(\\$^O eq \"cygwin\") { 
       return scalar(\\$file =~ m{^(\\[a-z\\]:)?\\[\\\\\\\\/\\]}is);
};
EOF
                   next;
                }
                print OUT $_;
              }
              close(OUT);
              close(IN);
              system("mv -f $d/File/Spec/Unix.pm.fixed $d/File/Spec/Unix.pm");
           }
        }
      }
  '`]
  # See if it was indeed fixed
  if test "$perlfixres" = "bug" ;then
     perlfixres=`perl -e '
      use File::Spec;
      if(File::Spec->file_name_is_absolute("C:/test")) {
        print "bug fixed"; exit 0;
      } else {
        print "cannot fix bug"; exit 1;
      }'`
  fi
  if test "x$perlfixres" = "x" ;then
      AC_MSG_ERROR([
        Cannot run perl
      ])
  elif test "$perlfixres" = "cannot fix bug" ;then
      AC_MSG_ERROR([
        Found a bug but cannot fix it.
      ])
  else
     AC_MSG_RESULT([$perlfixres])
 fi
fi

# 
# Now setup the performance counters
#
AC_MSG_CHECKING(if performance counters are usable)
# Create a C file from src/perfcount.c.in
rm -f ./cycles.exe
if gcc -DCONFIGURATION_ONLY \
       -x c ocamlutil/perfcount.c.in -lm -o ./cycles.exe >/dev/null 2>&1; then

   if CYCLES_PER_USEC=`./cycles.exe 2>&1` ;then
     AC_MSG_RESULT([ok ($CYCLES_PER_USEC cycles per us)])
   else
     # Print what we got 
     AC_MSG_RESULT([no ($CYCLES_PER_USEC)])
     CYCLES_PER_USEC=0
   fi
else
   CYCLES_PER_USEC=0
   AC_MSG_RESULT([no (cannot compile perfcount.c)])
fi
rm -f ./cycles.exe

# If we are on Linux and we use performance counters try to get
# the processor speed from /proc/cpuinfo
if test "$CYCLES_PER_USEC" != "0" ;then
     case "$target" in
        # linux
        *86*linux*)
             AC_MSG_CHECKING(if /proc/cpuinfo has processor speed)
             cpuinfo=`cat /proc/cpuinfo 2>/dev/null | grep "cpu MHz"`
             [procspeed=`echo $cpuinfo | sed 's/^.*[^0-9]\([0-9]\+\.[0-9]\+\).*$/\1/g'`]
             if test "$procspeed"!="" ;then
                CYCLES_PER_USEC=$procspeed
                AC_MSG_RESULT([got $CYCLES_PER_USEC cycles per us])
             else
                AC_MSG_RESULT(no)
             fi   
             ;;
        *)      
             ;;
      esac
      # Now set HAS_PERFCOUNT
      HAS_PERFCOUNT=1
else
      HAS_PERFCOUNT=0
fi

# additional tools we might check for:
#   - gnu make

#
# -------------------- GCC --------------
#

AC_MSG_CHECKING([for gcc version])
AC_CHECK_TYPE(__builtin_va_list,
              HAVE_BUILTIN_VA_LIST=true,
              HAVE_BUILTIN_VA_LIST=false)
AC_MSG_CHECKING([if __thread is a keyword])
AC_COMPILE_IFELSE([int main(int __thread) { return 0; }],
                  THREAD_IS_KEYWORD=false,
                  THREAD_IS_KEYWORD=true)
AC_MSG_RESULT($THREAD_IS_KEYWORD)

# Does gcc add underscores to identifiers to make assembly labels?
# (I think MSVC always does)
AC_MSG_CHECKING([if gcc adds underscores to assembly labels.])
AC_LINK_IFELSE([int main() { __asm__("jmp _main"); }],
                  UNDERSCORE_NAME=true,
                  UNDERSCORE_NAME=false)
AC_MSG_RESULT($UNDERSCORE_NAME)


# ----------- some stuff 'autoscan' put here --------------
# (autoscan is part of the autoconf distribution)

# checks for header files
AC_HEADER_STDC
AC_CHECK_HEADERS(stdlib.h strings.h sys/time.h unistd.h wchar.h)

# checks for typedefs, structures, and compiler characteristics
AC_C_CONST
AC_C_INLINE
AC_HEADER_TIME

# checks for library functions; more autoscan stuff
AC_FUNC_MEMCMP
AC_CHECK_FUNCS(mkdir select socket __sysv_signal)



# ----------- platform-specific code -------------
# $target is typically processor-vendor-os
case "$target" in
  # linux
  *86*linux*|*86*freebsd*|*86*openbsd*|*86*darwin*)
    AC_MSG_RESULT(configuring for linux/x86)

    ARCHOS=x86_LINUX
    ;;

  # Mac OS X
  *powerpc*darwin*)
    AC_MSG_RESULT(configuring for powerpc/darwin, which we treat like linux/x86)

    ARCHOS=ppc_DARWIN
    ;;

  # cygwin
  *86*cygwin*)
    AC_MSG_RESULT(configuring for Cygwin on win32/x86)

    ARCHOS=x86_WIN32

    # override CILHOME; even on cygwin we want forward slashes
    # sm: I folded this into what I hope will be the only
    # case-analysis of machine type
    CILHOME=`cygpath -wa "$CILHOME"  | sed -e "s/\\\\\/\\//g"`
    CC=`which $CC`
    CC=`cygpath -wa "$CC"  | sed -e "s/\\\\\/\\//g"`
    ;;

  # Solaris
  *sparc*solaris*)
    AC_MSG_RESULT(configuring for SPARC/Solaris)

    ARCHOS=sparc_SOLARIS
    ;;

  *)
    AC_MSG_ERROR([
      Unsupported platform $target -- sorry.
      ./configure supports these platforms:
         on x86:     Linux, Win32(with Cygwin), freeBSD, openBSD, and Mac OS X
         on PowerPC: Mac OS X 
         on SPARC:   Solaris
    ])
    ;;
esac

# Make the object directory if not already present
AC_CHECK_FILE(obj/$ARCHOS,, AC_MSG_RESULT(creating obj/$ARCHOS);
                            mkdir -p obj/$ARCHOS)
   
AC_MSG_CHECKING([delete the obj/$ARCHOS/feature_config.ml and obj/$ARCHOS/machdep.ml file])
rm -f obj/$ARCHOS/machdep.ml
rm -f obj/.depend/machdep.d
rm -f obj/$ARCHOS/feature_config.ml
rm -f obj/.depend/feature_config.d
AC_MSG_RESULT([done])

# We will use substitution variables whose definition contains newlines. The
# problem is that when config.status runs, it wants to break the series of
# substitution commands for sed into fragments based on line count. We could
# be unlucky and have config.status break the series of substitution in the
# middle of a variable that contains newlines. So, we first create a single
# variable called NEWLINE whose definition is a carriage return. This means
# that there will be exactly one opportunity for this error to happen (in the
# definition of NEWLINE). The occurrence of AC_SUBST for NEWLINE must occur
# after those of the variables that use it! And we want to put all of these
# very early on, to make sure that they are not around the place when the file
# bets broken.

NEWLINE="\\
"


#
# CIL/CCured features
#
#
   
# Set the defaults


# Give a space-separated list of features with the defaults
features="blockinggraph=no rand=no arithabs=no zrapp=no"

AC_ARG_WITH(blockinggraph,
    AC_HELP_STRING([--with-blockinggraph],
                   [enable the blocking graph feature]))
AC_ARG_WITH(rand,
    AC_HELP_STRING([--with-rand],
                   [enable the randomized value numbering]))
AC_ARG_WITH(arithabs,
    AC_HELP_STRING([--with-arithabs],
                   [enable the arithmetic abstraction]))
AC_ARG_WITH(zrapp,
    AC_HELP_STRING([--with-zrapp],
                   [enable the zrapp pretty-printer]))

# Smalloc.ml is distributed by {matth,nks}@cs.berkeley.edu as part of Scrash.
features="$features smalloc=no"

# cqualann.ml is used by Matt Harren.  Please ignore.
features="$features cqualann=no"

# Now add any features specified in the command-line

features="$features $EXTRAFEATURES"

for f_val in $features
do
   # If there is no =, then we default to yes
   if ! (echo $f_val | grep "=" >/dev/null) ;then f_val="$f_val=yes"; fi
   # echo "Testing feature $f_val"
   f=`echo $f_val | sed -e s%=.*$%%`
   AC_MSG_CHECKING(whether to use CIL feature $f)
   # default value from "features"
   defval=`echo $f_val | sed -e s%^.*=%%`
   # current value
   getcurval="echo \${with_$f:=$defval}"
   curval=`eval $getcurval`
   AC_MSG_RESULT($curval)
   if test $curval = yes ;then
      CIL_FEATURES="$CIL_FEATURES $f"
   fi
done

## Now produce the CIL_FEATURES_DEFINES
CIL_FEATURES_DEFINES=""
# Convert to upper case
for f in `echo $CIL_FEATURES | tr a-z A-Z`
do
   CIL_FEATURES_DEFINES="${CIL_FEATURES_DEFINES}@NEWLINE@export USE_$f=yes"
done


# ----------------- finish up -------------------
# names of the variables that get substituted in files; for example,
# write @ARCHOS@ somewhere in a written file to get it substituted
AC_SUBST(ARCHOS)
AC_SUBST(CILHOME)
AC_SUBST(HAS_MSVC)
AC_SUBST(DEFAULT_COMPILER)
AC_SUBST(DEFAULT_CIL_MODE)
AC_SUBST(CIL_VERSION_MAJOR)
AC_SUBST(CIL_VERSION_MINOR)
AC_SUBST(CIL_VERSION_REV)
AC_SUBST(CIL_VERSION)
AC_SUBST(CYCLES_PER_USEC)
AC_SUBST(HAS_PERFCOUNT)
AC_SUBST(HAVE_BUILTIN_VA_LIST)
AC_SUBST(THREAD_IS_KEYWORD)
AC_SUBST(UNDERSCORE_NAME)
AC_SUBST(EXTRAFEATURES)
AC_SUBST(EXTRASRCDIRS)

# finish the configure script and generate various files; ./configure
# will apply variable substitutions to <filename>.in to generate <filename>;
# I find it useful to mark generated files as read-only so I don't
# accidentally edit them (and them lose my changes when ./configure
# runs again); I had originally done the chmod after AC_OUTPUT, but
# the problem is then the chmod doesn't run inside ./config.status

# MY_AC_CONFIG_FILES(filename)
# do AC_CONFIG_FILES(filename, chmod a-w filename)
define([MY_AC_CONFIG_FILES],
[{
  if test -f [$1].in; then
    AC_CONFIG_FILES([$1], chmod a-w [$1])
  else
    true
    #echo "skipping [$1] because it's not in this distribution"
  fi
}])
define([MY_AC_CONFIG_EXE_FILES],
[{
  if test -f [$1].in; then
    AC_CONFIG_FILES([$1], [chmod a-w,a+x $1])
  else
    true
    #echo "skipping [$1] because it's not in this distribution"
  fi
}])

MY_AC_CONFIG_FILES(Makefile)
MY_AC_CONFIG_FILES(cil.spec)
MY_AC_CONFIG_FILES(config.mk)
MY_AC_CONFIG_FILES(test/Makefile)
MY_AC_CONFIG_EXE_FILES(bin/cilly.bat)
MY_AC_CONFIG_EXE_FILES(bin/patcher.bat)
MY_AC_CONFIG_FILES(bin/CilConfig.pm)
MY_AC_CONFIG_FILES(doc/index.html)
MY_AC_CONFIG_FILES(doc/header.html)
MY_AC_CONFIG_FILES(ocamlutil/perfcount.c)

AC_OUTPUT()

# show the user what the variables have been set to
cat <<EOF

CIL configuration:
  architecture/OS:            ARCHOS             $ARCHOS
  source tree root:           CILHOME            $CILHOME
  (optional) cl.exe found:    HAS_MSVC           $HAS_MSVC
  gcc to use                  CC                 $CC
  default compiler            DEFAULT_COMPILER   $DEFAULT_COMPILER
  CIL version                 CIL_VERSION        $CIL_VERSION
  CIL features                CIL_FEATURES       $CIL_FEATURES
  Extra source directories    EXTRASRCDIRS       $EXTRASRCDIRS
  Cycles per microsecond      CYCLES_PER_USEC    $CYCLES_PER_USEC
EOF