<?xml version="1.0" encoding="UTF-8"?>
<!--

    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.

-->
<project name="jdk" basedir=".">

    <description>
        Permits selection of a JDK to use when building and running NetBeans.
        For details on usage, see apisupport/harness/release/README (in sources)
        or harness/README (in binaries). Cf. issue #63541.
    </description>

    <target name="-jdk-pre-preinit">
        <condition property="nbjdk.active-or-nbjdk.home">
            <or>
                <and>
                    <isset property="nbjdk.active"/>
                    <not>
                        <equals arg1="${nbjdk.active}" arg2="default"/>
                    </not>
                </and>
                <and>
                    <isset property="nbjdk.home"/>
                    <not>
                        <isset property="nbjdk.home.defaulted"/>
                    </not>
                </and>
            </or>
        </condition>
    </target>

    <target name="-jdk-preinit" depends="-jdk-pre-preinit" if="nbjdk.active-or-nbjdk.home" xmlns:common="http://nbbuild.netbeans.org/jdk.xml">
        <macrodef name="property" uri="http://nbbuild.netbeans.org/jdk.xml">
            <attribute name="name"/>
            <attribute name="value"/>
            <sequential>
                <property name="@{name}" value="${@{value}}"/>
            </sequential>
        </macrodef>
        <common:property name="nbjdk.home" value="platforms.${nbjdk.active}.home"/>
        <common:property name="nbjdk.javac.tmp" value="platforms.${nbjdk.active}.javac"/>
        <condition property=".exe" value=".exe">
            <os family="windows"/> <!-- #72467 -->
        </condition>
        <property name=".exe" value=""/>
        <condition property="nbjdk.javac" value="${nbjdk.home}/bin/javac${.exe}">
            <equals arg1="${nbjdk.javac.tmp}" arg2="$${platforms.${nbjdk.active}.javac}"/>
        </condition>
        <property name="nbjdk.javac" value="${nbjdk.javac.tmp}"/>
        <common:property name="nbjdk.java.tmp" value="platforms.${nbjdk.active}.java"/>
        <condition property="nbjdk.java" value="${nbjdk.home}/bin/java${.exe}">
            <equals arg1="${nbjdk.java.tmp}" arg2="$${platforms.${nbjdk.active}.java}"/>
        </condition>
        <property name="nbjdk.java" value="${nbjdk.java.tmp}"/>
        <common:property name="nbjdk.javadoc.tmp" value="platforms.${nbjdk.active}.javadoc"/>
        <condition property="nbjdk.javadoc" value="${nbjdk.home}/bin/javadoc${.exe}">
            <equals arg1="${nbjdk.javadoc.tmp}" arg2="$${platforms.${nbjdk.active}.javadoc}"/>
        </condition>
        <property name="nbjdk.javadoc" value="${nbjdk.javadoc.tmp}"/>
        <common:property name="nbjdk.bootclasspath.tmp" value="platforms.${nbjdk.active}.bootclasspath"/>
        <condition property="nbjdk.bootclasspath" value="${nbjdk.home}/../Classes/classes.jar:${nbjdk.home}/../Classes/jsse.jar:${nbjdk.home}/../Classes/jce.jar:${nbjdk.home}/../Classes/ui.jar:${nbjdk.home}/../Classes/laf.jar:${nbjdk.home}/../Classes/sunrsasign.jar:${nbjdk.home}/../Classes/charsets.jar">
            <and>
                <equals arg1="${nbjdk.bootclasspath.tmp}" arg2="$${platforms.${nbjdk.active}.bootclasspath}"/>
                <available file="${nbjdk.home}/../Classes/classes.jar" type="file"/>
            </and>
        </condition>
        <pathconvert property="nbjdk.bootclasspath.standard">
            <path>
                <fileset dir="${nbjdk.home}/jre/lib" erroronmissingdir="false" includes="*.jar"/>
            </path>
        </pathconvert>
        <condition property="nbjdk.bootclasspath" value="${nbjdk.bootclasspath.standard}">
            <equals arg1="${nbjdk.bootclasspath.tmp}" arg2="$${platforms.${nbjdk.active}.bootclasspath}"/>
        </condition>
        <property name="nbjdk.bootclasspath" value="${nbjdk.bootclasspath.tmp}"/>
        <condition property="nbjdk.valid">
            <and>
                <available file="${nbjdk.home}" type="dir"/>
                <available file="${nbjdk.javac}" type="file"/>
                <available file="${nbjdk.java}" type="file"/>
                <available file="${nbjdk.javadoc}" type="file"/>
                <or>
                    <available classname="java.lang.Object" classpath="${nbjdk.bootclasspath}" ignoresystemclasses="true"/>
                    <available file="${nbjdk.home}/bin/jmod${.exe}"/>
                </or>
            </and>
        </condition>
        <available property="have-jdk-1.4" classname="java.lang.CharSequence" classpath="${nbjdk.bootclasspath}" ignoresystemclasses="true"/>
        <available property="have-jdk-1.5" classname="java.lang.StringBuilder" classpath="${nbjdk.bootclasspath}" ignoresystemclasses="true"/>
        <available property="have-jdk-1.6" classname="java.util.ServiceLoader" classpath="${nbjdk.bootclasspath}" ignoresystemclasses="true"/>
        <available property="have-jdk-1.7" classname="java.lang.ReflectiveOperationException" classpath="${nbjdk.bootclasspath}" ignoresystemclasses="true"/>
        <available property="have-jdk-1.8" classname="java.lang.FunctionalInterface" classpath="${nbjdk.bootclasspath}" ignoresystemclasses="true"/>
        <available property="have-jdk-1.9" file="${nbjdk.home}/bin/jmod${.exe}"/>
        <echo level="verbose">nbjdk.active=${nbjdk.active} nbjdk.home=${nbjdk.home} nbjdk.java=${nbjdk.java} nbjdk.javac=${nbjdk.javac} nbjdk.javadoc=${nbjdk.javadoc} nbjdk.bootclasspath=${nbjdk.bootclasspath} nbjdk.valid=${nbjdk.valid} have-jdk-1.4=${have-jdk-1.4} have-jdk-1.5=${have-jdk-1.5} have-jdk-1.6=${have-jdk-1.6} have-jdk-1.7=${have-jdk-1.7} have-jdk-1.8=${have-jdk-1.8} have-jdk-1.9=${have-jdk-1.9}</echo>
    </target>

    <target name="-jdk-warn" depends="-jdk-preinit" if="nbjdk.active-or-nbjdk.home" unless="nbjdk.valid">
        <property name="jdkhome.presumed" location="${java.home}/.."/>
        <echo level="warning">Warning: nbjdk.active=${nbjdk.active} or nbjdk.home=${nbjdk.home} is an invalid Java platform; ignoring and using ${jdkhome.presumed}</echo>
    </target>
    
    <target name="-jdk-presetdef-basic" depends="-jdk-preinit" if="nbjdk.valid" unless="jdk.presetdef.basic.done">
        <!-- Have to eval these props now, not when used, because of usages of <ant ... inheritall="false"> -->
        <!-- (cf. http://issues.apache.org/bugzilla/show_bug.cgi?id=38040) -->
        <macrodef name="javac-presetdef">
            <attribute name="javacval"/>
            <sequential>
                <presetdef name="javac">
                    <javac fork="yes" executable="@{javacval}"/>
                </presetdef>
            </sequential>
        </macrodef>
        <javac-presetdef javacval="${nbjdk.javac}"/>
        <macrodef name="java-presetdef">
            <attribute name="javaval"/>
            <sequential>
                <presetdef name="java">
                    <java fork="yes" jvm="@{javaval}"/>
                </presetdef>
            </sequential>
        </macrodef>
        <java-presetdef javaval="${nbjdk.java}"/>
        <macrodef name="javadoc-presetdef">
            <attribute name="javadocval"/>
            <sequential>
                <presetdef name="javadoc">
                    <javadoc executable="@{javadocval}"/>
                </presetdef>
            </sequential>
        </macrodef>
        <javadoc-presetdef javadocval="${nbjdk.javadoc}"/>
        <macrodef name="junit-presetdef">
            <attribute name="javaval"/>
            <sequential>
                <presetdef name="junit">
                    <junit fork="yes" jvm="@{javaval}"/>
                </presetdef>
            </sequential>
        </macrodef>
        <junit-presetdef javaval="${nbjdk.java}"/>
        <property name="jdk.presetdef.basic.done" value="true"/>
    </target>
    
    <target name="-jdk-presetdef-nbjpdastart" depends="-jdk-preinit" if="nbjdk.valid" unless="jdk.presetdef.nbjpdastart.done">
        <macrodef name="nbjpdastart-presetdef">
            <attribute name="bootcpval"/>
            <sequential>
                <presetdef name="nbjpdastart">
                    <nbjpdastart>
                        <bootclasspath>
                            <path path="@{bootcpval}"/>
                        </bootclasspath>
                    </nbjpdastart>
                </presetdef>
            </sequential>
        </macrodef>
        <nbjpdastart-presetdef bootcpval="${nbjdk.bootclasspath}"/>
        <property name="jdk.presetdef.nbjpdastart.done" value="true"/>
    </target>
    
    <target name="-jdk-presetdef-nbprofiledirect" depends="-jdk-preinit" if="nbjdk.valid" unless="jdk.presetdef.nbprofiledirect.done">
        <!-- XXX disable for now, but could be considered:
        <macrodef name="nbprofiledirect-presetdef">
            <attribute name="javaval"/>
            <sequential>
                <presetdef name="nbprofiledirect">
                    <nbprofiledirect jvmproperty="@{javaval}"/>
                </presetdef>
            </sequential>
        </macrodef>
        <nbprofiledirect-presetdef javaval="${nbjdk.java}"/>
        -->
        <property name="jdk.presetdef.nbprofiledirect.done" value="true"/>
    </target>
    
    <target name="-jdk-default" unless="nbjdk.active-or-nbjdk.home">
        <!-- Define fallback values of some things. -->
        <property name="java.home.parent" location="${java.home}/.."/>
        <condition property="nbjdk.home" value="${java.home.parent}">
            <available file="${java.home.parent}/lib/tools.jar" type="file"/>
        </condition>
        <condition property="nbjdk.home" value="${java.home}">
            <available file="${java.home}/lib/tools.jar" type="file"/>
        </condition>

        <!-- Mac OS X -->
        <condition property="nbjdk.home" value="${java.home}">
            <available file="${java.home}/../Classes/classes.jar" type="file"/>
        </condition>

        <!-- Fallback? -->
        <property name="nbjdk.home" location="${java.home}"/>
        <available property="have-jdk-1.4" classname="java.lang.CharSequence"/>
        <available property="have-jdk-1.5" classname="java.lang.StringBuilder"/>
        <available property="have-jdk-1.6" classname="java.util.ServiceLoader"/>
        <available property="have-jdk-1.7" classname="java.lang.ReflectiveOperationException"/>
        <available property="have-jdk-1.8" classname="java.lang.FunctionalInterface"/>
        <available property="have-jdk-1.9" classname="java.util.zip.CRC32C"/>
        <property name="nbjdk.home.defaulted" value="true"/>
    </target>

    <target name="-jdk-init" depends="-jdk-preinit,-jdk-warn,-jdk-presetdef-basic,-jdk-default">
        <!-- Will be bogus on Mac OS X, but doesn't matter - Classes/classes.jar should have this anyway -->
        <property name="tools.jar" location="${nbjdk.home}/lib/tools.jar"/>
        <!--<echo level="info">Using JDK: ${nbjdk.home}</echo>-->
        <condition property=".exe" value=".exe">
            <os family="windows"/> <!-- #72467 -->
        </condition>
        <property name=".exe" value=""/>
        <condition property="test.nbjdk.java" value="${test.nbjdk.home}/bin/java${.exe}" else="${nbjdk.home}/bin/java${.exe}">
            <and>
                <isset property="test.nbjdk.home" />
                <available file="${test.nbjdk.home}/bin/java${.exe}" type="file"/>
            </and>
        </condition>
    </target>
    
</project>
