<?xml version="1.0" encoding="UTF-8"?>
<!--
The contents of this file are subject to the terms of the Common Development
and Distribution License (the License). You may not use this file except in
compliance with the License.

You can obtain a copy of the License at http://www.netbeans.org/cddl.html
or http://www.netbeans.org/cddl.txt.

When distributing Covered Code, include this CDDL Header Notice in each file
and include the License file at http://www.netbeans.org/cddl.txt.
If applicable, add the following below the CDDL Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"

The Original Software is NetBeans. The Initial Developer of the Original
Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
Microsystems, Inc. All Rights Reserved.
-->
<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}/jre/lib/rt.jar">
            <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"/>
                <!-- XXX probably cannot use <available resource="..." classpath="${nbjdk.bootclasspath}"/> since would always find it in this VM -->
            </and>
        </condition>
        <!-- Cannot use classname="...": "SecurityException: Prohibited package name: java.lang" from ClassLoader.defineClass from AntClassLoader2.defineClassFromData -->
        <!-- XXX will match spuriously if host JDK is newer than target JDK -->
        <available property="have-jdk-1.4" resource="java/lang/CharSequence.class" classpath="${nbjdk.bootclasspath}"/>
        <available property="have-jdk-1.5" resource="java/lang/StringBuilder.class" classpath="${nbjdk.bootclasspath}"/>
        <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}</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="/Library/Java/Home">
            <available file="/Library/Java/Home" type="dir"/>
        </condition>
        <!-- Fallback? -->
        <property name="nbjdk.home" location="${java.home.parent}"/>
        <available property="have-jdk-1.4" classname="java.lang.CharSequence"/>
        <available property="have-jdk-1.5" classname="java.lang.StringBuilder"/>
        <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>-->
    </target>
    
    <target name="-assure-jdk-1.4" depends="-jdk-init" unless="have-jdk-1.4">
        <fail>You need JDK 1.4 or higher to build NetBeans.</fail>
    </target>

    <target name="-assure-jdk-1.5" depends="-jdk-init" unless="have-jdk-1.5">
        <fail>You need JDK 1.5 or higher to build NetBeans.</fail>
    </target>

</project>
