#  Copyright (c) 1997-2010
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------
#  $Project: polymake $$Id: JavaConfigure.pm 9931 2010-11-19 19:58:46Z gawrilow $

package Java::Configure;

my $java_version;

sub proceed {
   my $java_cmd=find_java_cmd($java) or return;

   if ($DeveloperMode || -d "$InstallArch/lib/jni") {
      undef $java_native_libs;
   } else {
      # got to compile native libraries now
      my $jni_path=find_jni_headers($java_cmd) or return;
      foreach (glob "$InstallTop/apps/*/src/native") {
         m{/apps/([^/]*)/src/};
         my $app=add Core::Application($1);
         $java_native_libs=Core::CPlusPlus::private_jni_libdir($app, $jni_path);
      }
   }

   1
}

sub find_java_cmd {
   find_program($_[0], "java",
                { prompt => "the java interpreter",
                  also => sub { $ENV{JAVACMD} || $ENV{JAVA_HOME} && "$ENV{JAVA_HOME}/bin/java" },
                  check => sub {
                     ($java_version)= `$_[0] -version 2>&1` =~ /version "([\d.]+)/s;
                     my $java_version_num=eval("v$java_version");
                     $java_version_num lt v1.5
                       && "version is $java_version, while minimal required is 1.5";
                  } })
}

sub find_jni_headers {
   my $javac=shift;
   my $jni_path;
   $javac =~ s/\bjava$/javac/ or undef $javac;
   find_program($javac, "javac",
                { prompt => "the java compiler", also => sub { $ENV{JAVA_HOME} && "$ENV{JAVA_HOME}/bin/javac" },
                  check => sub {
                     my $path=$_[0];
                     my $javac_version=`$path -version 2>&1`;
                     $javac_version =~ /version "([\d.]+)/s  or
                       $javac_version =~ /javac ([\d.]+)/;
                     if ($1 ne $java_version) {
                        return "version $1 does not match the chosen Java VM ($java_version)";
                     }
                     if ($ENV{JNI_HEADERS} && -f "$ENV{JNI_HEADERS}/jni.h") {
                        $jni_path=$ENV{JNI_HEADERS};
                        return;
                     } else {
                        do {
                           if ($path =~ m|/bin/javac$|) {
                              if (-f "$`/include/jni.h") {
                                 $jni_path="$`/include";
                                 return;
                              }
                           } elsif ($path =~ m|/Commands/javac$|) {
                              # MacOS X naming convention
                              if (-f "$`/Headers/jni.h") {
                                 $jni_path="$`/Headers";
                                 return;
                              }
                           }
                        } while (-l $path and $path=readlink($path));
                     }

                     # this trick works under FreeBSD having a wrapper script for all java commands
                     local $ENV{JAVAVM_DRYRUN}=1;
                     if (`$path 2>/dev/null` =~ /^JAVA_HOME=(.*)$/m && -f "$1/include/jni.h") {
                        $jni_path="$1/include";
                        return;
                     }

                     "JNI header files (for example, jni.h) not found alongside the compiler";
                  } })
   and $jni_path;
}

1

# Local Variables:
# cperl-indent-level:3
# End:
