<?xml version="1.0" encoding="UTF-8"?>
<!--
 Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.

 This code is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License version 2 only, as
 published by the Free Software Foundation.  Oracle designates this
 particular file as subject to the "Classpath" exception as provided
 by Oracle in the LICENSE file that accompanied this code.

 This code 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
 version 2 for more details (a copy is included in the LICENSE file that
 accompanied this code).

 You should have received a copy of the GNU General Public License version
 2 along with this work; if not, write to the Free Software Foundation,
 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

 Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 or visit www.oracle.com if you need additional information or have any
 questions.
-->

<project name="jtreg" default="build" basedir="..">

    <!-- ********** system-specific values ********************************* -->

    <property file="build.properties"/>  <!-- for user overrides -->
    <property file="make/build.properties"/>

    <!-- ********** general useful values ********************************** -->

    <property environment="env"/>

    <property name="src.bin.dir" location="src/share/bin"/>
    <property name="src.classes.dir" location="src/share/classes"/>
    <property name="src.doc.dir" location="src/share/doc"/>
    <property name="test.dir" location="test"/>
    <property name="build.dir" location="build"/>
    <property name="build.checkstyle.dir" value="${build.dir}/checkstyle"/>
    <property name="build.classes.dir" location="${build.dir}/classes" />
    <property name="dist.dir" location="dist" />
    <property name="dist.jtreg.dir" location="${dist.dir}/jtreg" />
    <property name="dist.jtreg.zip" location="${dist.dir}/jtreg.zip" />
    <property name="make.checkstyle.dir" value="make/checkstyle"/>

    <patternset id="helpset">
        <include name="*.hs"/>
        <include name="default/**"/>
        <include name="images/**"/>
    </patternset>


    <!-- ********** top level and shared targets *************************** -->

    <target name="build" depends="dist-jtreg"
        description="default build target: build jtreg for distribution"/>

    <target name="clean" description="remove all generated files">
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
    </target>

    <!-- ********** init *************************************************** -->

    <target name="-init">
        <tstamp>
            <format property="build.date" pattern="MM/dd/yyyy"/>
            <format property="build.date.time" pattern="MM/dd/yyyy hh:mm aa"/>
        </tstamp>
        <available property="javatest.ok" file="${javatest.home}/COPYRIGHT-javatest.html"/>
        <available property="jtharness.ok" file="${javatest.home}/legal/copyright.txt"/>
        <available property="junit.ok" file="${junit.jar}"/>
        <available property="testng.ok" file="${testng.jar}"/>
        <available property="jcommander.ok" file="${jcommander.jar}"/>
        <available property="asmtools.ok" file="${asmtools.jar}"/>
        <condition property="jcov.ok">
            <and>
                <available file="${jcov.home}/lib/jcov.jar"/>
                <available file="${jcov.home}/lib/jcov_network_saver.jar"/>
            </and>
        </condition>
    </target>

    <!-- ********** jtreg ************************************************** -->

    <property name="src.jtreg.help" location="${src.doc.dir}/javatest/regtest/help"/>
    <property name="build.jtreg.help" location="${build.classes.dir}/com/sun/javatest/regtest/help"/>

    <patternset id="jtreg.packages">
        <include name="com/sun/javatest/regtest/**"/>
        <include name="com/sun/javatest/diff/**"/>
        <include name="com/sun/javatest/diff/**"/>
        <include name="java/lang/JTRegModuleHelper.*"/>
        <include name="java/lang/reflect/JTRegModuleHelper.*"/>
    </patternset>

    <patternset id="jtreg.jdk11.files">
        <include name="com/sun/javatest/regtest/agent/AppletWrapper.java"/>
        <include name="com/sun/javatest/regtest/agent/GetSystemProperty.java"/>
        <include name="com/sun/javatest/regtest/agent/MainWrapper.java"/>
        <include name="com/sun/javatest/regtest/agent/RStatus.java"/>
        <include name="com/sun/javatest/regtest/agent/StringArray.java"/>
    </patternset>

    <target name="dist-jtreg"
        depends="import-javatest,import-junit,import-testng,import-asmtools,import-jcov,import-jtharness,jar-jtreg,-dist-jtreg.bin,-dist-jtreg.doc,-dist-jtreg.legal"
        description="build jtreg for distribution">
        <zip destfile="${dist.jtreg.zip}" filesOnly="true" basedir="${dist.dir}">
            <include name="jtreg/"/>
        </zip>
    </target>

    <target name="-dist-jtreg.bin" depends="jar-jtreg,-dist-jtreg.doc">
        <copy file="${src.bin.dir}/jtreg.sh" tofile="${dist.jtreg.dir}/bin/jtreg"/>
        <copy file="${src.bin.dir}/jtdiff.sh" tofile="${dist.jtreg.dir}/bin/jtdiff"/>
        <chmod dir="${dist.jtreg.dir}" perm="+x">
            <include name="bin/jtreg"/>
            <include name="bin/jtdiff"/>
        </chmod>
    </target>

    <target name="-dist-jtreg.doc" depends="-compile-jtreg.usage">
        <mkdir dir="${dist.jtreg.dir}/doc/jtreg"/>
        <copy todir="${dist.jtreg.dir}" file="${src.doc.dir}/javatest/regtest/README"/>
        <copy todir="${dist.jtreg.dir}/doc/jtreg">
            <fileset file="${src.doc.dir}/javatest/regtest/tag-spec.html"/>
            <fileset file="${build.dir}/jtreg/usage.txt"/>
        </copy>
    </target>

    <target name="-dist-jtreg.legal">
        <copy todir="${dist.jtreg.dir}" file="COPYRIGHT"/>
        <copy todir="${dist.jtreg.dir}" file="LICENSE"/>
    </target>

    <target name="jar-jtreg" depends="-init,-check-jtreg.jar" unless="jtreg.jar.ok">
        <mkdir dir="${dist.jtreg.dir}/lib"/>
        <jar destfile="${dist.jtreg.dir}/lib/jtreg.jar"
            basedir="${build.classes.dir}" >
            <manifest>
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Main-Class" value="com.sun.javatest.regtest.Main"/>
                <attribute name="Class-Path" value="javatest.jar jh.jar junit.jar"/>
                <attribute name="jtreg-Name" value="jtreg"/>
                <attribute name="jtreg-Version" value="${build.version}"/>
                <attribute name="jtreg-Milestone" value="${build.milestone}"/>
                <attribute name="jtreg-Build" value="${build.number}"/>
                <attribute name="jtreg-BuildJavaVersion" value="${java.version}"/>
                <attribute name="jtreg-BuildDate" value="${build.date.time}"/>
            </manifest>
            <include name="COPYRIGHT"/>
            <include name="buildInfo.txt"/>
            <patternset refid="jtreg.packages"/>
        </jar>
    </target>

    <target name="-check-jtreg.jar" depends="compile-jtreg">
        <dependset>
            <srcfileset dir="${build.classes.dir}">
                <patternset refid="jtreg.packages"/>
            </srcfileset>
            <targetfilelist dir="${dist.jtreg.dir}/lib" files="jtreg.jar"/>
        </dependset>
        <available property="jtreg.jar.ok" file="${dist.jtreg.dir}/lib/jtreg.jar"/>
    </target>

    <target name="compile-jtreg" depends="-compile-jtreg.classes,-compile-jtreg.resources"/>

    <target name="-compile-jtreg.classes">
        <mkdir dir="${build.classes.dir}"/>
        <javac encoding="iso-8859-1" source="1.8" target="1.8"
            debug="true" debuglevel="source,lines"
            srcdir="${src.classes.dir}"
            destdir="${build.classes.dir}"
            classpath="${build.classes.dir}:${javatest.jar}:${junit.jar}:${testng.jar}"
            includeantruntime="true">
            <compilerarg line="-Xlint:all"/>
            <patternset refid="jtreg.packages"/>
        </javac>
    </target>

    <target name="-compile-jtreg.usage" depends="-compile-jtreg.classes,-ant-jct.utils">
        <!-- generate the usage page from the command line help -->
        <!-- have to set fork=true because otherwise output redirection gets screwed up -->
        <mkdir dir="${build.dir}/jtreg"/>
        <java fork="true" failonerror="true"
            classname="com.sun.javatest.regtest.Main" classpath="${build.classes.dir}:${javatest.jar}:${junit.jar}:${jh.jar}"
            output="${build.dir}/jtreg/usage.txt">
            <jvmarg value="-Dprogram=jtreg"/>
            <arg value="-help"/>
            <arg value="all"/>
        </java>
    </target>

    <target name="-compile-jtreg.resources">
        <copy todir="${build.classes.dir}" file="COPYRIGHT"/>
        <copy todir="${build.classes.dir}">
            <fileset dir="${src.classes.dir}">
                <patternset refid="jtreg.packages"/>
                <include name="**/*.properties"/>
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>

    <!-- ********** imports ************************************************ -->

    <target name="import-javatest" depends="-init,import-javahelp" if="javatest.ok">
        <copy todir="${dist.jtreg.dir}/doc/javatest" file="${javatest.home}/doc/javatest/javatestGUI.pdf"/>
        <copy tofile="${dist.jtreg.dir}/legal/javatest/copyright.html" file="${javatest.home}/COPYRIGHT-javatest.html"/>
        <copy file="${javatest.jar}" tofile="${dist.jtreg.dir}/lib/javatest.jar"/>
    </target>

    <target name="import-junit" depends="-init" if="junit.ok">
        <copy file="${junit.jar}" tofile="${dist.jtreg.dir}/lib/junit.jar"/>
    </target>

    <target name="import-testng" depends="-init,import-jcommander" if="testng.ok">
        <copy file="${testng.jar}" tofile="${dist.jtreg.dir}/lib/testng.jar"/>
    </target>

    <target name="import-jcommander" depends="-init" if="jcommander.ok">
        <copy file="${jcommander.jar}" tofile="${dist.jtreg.dir}/lib/jcommander.jar"/>
    </target>

    <target name="import-asmtools" depends="-init" if="asmtools.ok">
        <copy file="${asmtools.jar}" tofile="${dist.jtreg.dir}/lib/asmtools.jar"/>
    </target>

    <target name="import-jcov" depends="-init" if="jcov.ok">
        <copy file="${jcov.home}/lib/jcov.jar" tofile="${dist.jtreg.dir}/lib/jcov.jar"/>
        <copy file="${jcov.home}/lib/jcov_network_saver.jar" tofile="${dist.jtreg.dir}/lib/jcov_network_saver.jar"/>
    </target>

    <target name="import-jtharness" depends="-init,import-javahelp" if="jtharness.ok">
        <copy todir="${dist.jtreg.dir}/legal/jtharness" file="${javatest.home}/legal/copyright.txt"/>
        <copy todir="${dist.jtreg.dir}/legal/jtharness" file="${javatest.home}/legal/license.txt"/>
        <copy file="${javatest.jar}" tofile="${dist.jtreg.dir}/lib/javatest.jar"/>
    </target>

    <target name="import-javahelp" unless="javatest.includes.javahelp.ok">
        <copy file="${jh.jar}" tofile="${dist.jtreg.dir}/lib/jh.jar"/>
    </target>

    <!-- ********** jct-utils ********************************************** -->

    <target name="-ant-jct.utils" depends="-compile-jct.utils">
        <taskdef name="i18ncheck" classname="com.sun.jct.utils.i18ncheck.Main$$Ant" classpath="${build.classes.dir}" />
    </target>

    <target name="-compile-jct.utils">
        <mkdir dir="${build.classes.dir}"/>
        <javac encoding="iso-8859-1" debug="true"
            srcdir="${src.classes.dir}"
            destdir="${build.classes.dir}"
            includeantruntime="true">
            <compilerarg value="-Xlint"/>
            <include name="com/sun/jct/utils/i18ncheck/**"/>
        </javac>

    </target>

    <target name="test-jtreg" depends="-test-jtreg.i18n,-test-jtreg.basic"
        description="execute the main jtreg tests"/>

    <target name="-test-jtreg.i18n" depends="dist-jtreg,-ant-jct.utils">
        <!-- have to set fork=true because otherwise output redirection gets screwed up -->
        <java fork="true" jar="${dist.jtreg.dir}/lib/jtreg.jar"
            output="${build.dir}/jtreg.i18n" >
            <sysproperty key="javatest.i18n.log" value="com.sun.javatest.regtest"/>
            <arg value="-help"/>
        </java>
        <i18ncheck patternFile="make/i18n.pat">
            <fileset dir="${src.classes.dir}/com/sun/javatest/regtest">
                <include name="**/*.properties"/>
                <include name="**/*.java"/>
            </fileset>
            <fileset file="${build.dir}/jtreg.i18n"/>
        </i18ncheck>
    </target>

    <target name="-test-jtreg.basic" depends="dist-jtreg">
        <delete dir="${build.dir}/basic"/>
        <mkdir dir="${build.dir}/basic/classes"/>
        <mkdir dir="${build.dir}/basic/report"/>
        <mkdir dir="${build.dir}/basic/work/scratch"/>
        <javac classpath="${dist.jtreg.dir}/lib/jtreg.jar"
            srcdir="${test.dir}/basic"
            destdir="${build.dir}/basic/classes"
            includes="Basic.java">
            <compilerarg value="-Xlint"/>
        </javac>
        <java classpath="${dist.jtreg.dir}/lib/jtreg.jar:${build.dir}/basic/classes" classname="Basic"
            fork="true" dir="${build.dir}/basic/work/scratch" >
            <sysproperty key="javatest.regtest.allowTrailingBuild" value="true"/>
            <arg file="${test.dir}/share/basic"></arg>
            <arg file="${build.dir}/basic/report"/>
            <arg file="${build.dir}/basic/work"/>
            <arg file="${java.home}/.."/>
            <arg value="TESTJAVAHOME=${java.home},DISPLAY=${env.DISPLAY},HOME=${user.home}"/>
            <arg value="-othervm"/>
        </java>
        <java fork="true" jar="${dist.jtreg.dir}/lib/jtreg.jar"
            output="${build.dir}/jtreg/basic/report.out">
            <arg value="-reportOnly"/>
            <arg value="-automatic"/>
            <arg value="-verbose"/>
            <arg value="-reportDir"/>
            <arg file="${build.dir}/basic/report2"/>
            <arg value="-workDir"/>
            <arg file="${build.dir}/basic/work"/>
            <arg file="${test.dir}/share/basic"></arg>
        </java>
        <exec executable="grep" input="${build.dir}/jtreg/basic/report.out">
            <arg value="-s"/>
            <arg value="Test results: passed: 92; failed: 44; error: 87"/>
        </exec>
    </target>

    <!-- checkstyle -->

    <target name="checkstyle" depends="-def-checkstyle"
        description="Generates reports for code convention violations.">
        <mkdir dir="${build.checkstyle.dir}"/>

        <checkstyle config="${make.checkstyle.dir}/checkstyle-jtreg.xml"
            failureProperty="checkstyle.failure" failOnViolation="false">
            <formatter type="xml" tofile="${build.checkstyle.dir}/checkstyle_report.xml"/>
            <fileset dir="src/share/classes/" includes="**/*.java, **/*.properties"/>
        </checkstyle>

        <!-- transform the output to a simple html -->
        <xslt in="${build.checkstyle.dir}/checkstyle_report.xml"
            out="${build.checkstyle.dir}/checkstyle_report.html"
            style="${checkstyle.home}/contrib/checkstyle-simple.xsl"/>

        <!-- transform the output to a very simple emacs friendly text file -->
        <xslt in="${build.checkstyle.dir}/checkstyle_report.xml"
            out="${build.checkstyle.dir}/checkstyle_report.tmp"
            style="${make.checkstyle.dir}/checkstyle-emacs.xsl"/>

        <!-- beautify remove extra lines -->
        <move file="${build.checkstyle.dir}/checkstyle_report.tmp"
            toFile="${build.checkstyle.dir}/checkstyle_report.emacs.txt">
            <filterchain>
                <ignoreblank/>
                <replaceregex byline="true" pattern="^File:" replace="${line.separator}File:"/>
            </filterchain>
        </move>
    </target>

    <!-- target can be invoked from an ide, the output of which can be used
         to access and fix the errors directly.-->
    <target name="checkstyle-ide" depends="checkstyle">
        <concat>
            <fileset file="${build.checkstyle.dir}/checkstyle_report.emacs.txt"/>
        </concat>
    </target>

    <target name="-check-checkstyle.home" depends="-def-check">
        <check name="checkstyle" property="checkstyle.home" marker="${checkstyle.name.version}.jar"/>
    </target>

    <target name="-def-checkstyle" unless="checkstyle.defined" depends="-check-checkstyle.home">
        <taskdef resource="checkstyletask.properties">
            <classpath>
                <pathelement location="${checkstyle.home}/${checkstyle.name.version}-all.jar"/>
            </classpath>
        </taskdef>
        <property name="checkstyle.defined" value="true"/>
    </target>

    <target name="-def-check">
        <macrodef name="check">
            <attribute name="name"/>
            <attribute name="property"/>
            <attribute name="marker" default=""/>
            <sequential>
                <fail message="Cannot locate @{name}: please set @{property} to its location">
                    <condition>
                        <not>
                            <isset property="@{property}"/>
                        </not>
                    </condition>
                </fail>
                <fail message="@{name} is not installed in ${@{property}}">
                    <condition>
                        <and>
                            <not>
                                <equals arg1="@{marker}" arg2=""/>
                            </not>
                            <not>
                                <available file="${@{property}}/@{marker}"/>
                            </not>
                        </and>
                    </condition>
                </fail>
            </sequential>
        </macrodef>
    </target>
</project>
