<?xml version="1.0" encoding="UTF-8"?>
<!--
	Ant
 
	Copyright 2024-2025 MicroEJ Corp. All rights reserved.
	This library is provided in source code for use, modification and test, subject to license terms.
	Any modification of the source code will break MicroEJ Corp. warranties on the whole library.
-->

<project name="build" default="build/platform">

    <dirname file="${ant.file.build}" property="ant.file.dir"/>
    <property name="build.dir" value="${ant.file.dir}"/>
    <property name="framework.dir" location="${ant.file.dir}/microejgenerator"/>

    <target name="build/environment" depends="build/environment/mergeLibraries">
        <!-- Properties given by build environment -->
        <property name="platform.microej" location=""/>
        <property name="platform.printablename" value="VEEPort"/>
        <tstamp>
            <format property="buildLabel" pattern="yyyyMMdd-HHmm"/>
        </tstamp>

        <property name="frontpanel.build.file"
                  value="${architecture.output.dir}/build/fragments/frontpanel/build/build.xml"/>
        <available file="${frontpanel.build.file}" type="file" property="frontpanel.installed"/>
    </target>

    <target name="build/clean" depends="build/environment">
        <delete failonerror="false">
            <fileset dir="${veeport.output.dir}"/>
        </delete>
    </target>

    <target name="build/frontpanel" description="Empty the Front Panel build build." if="${frontpanel.installed}">
        <!--
        The build of the Front Panel project was historically done in an Architecture build script.
        It has been now moved to the SDK, so this build script has to be disabled.
        Since there is no way to cleanly disable the Front Panel project build only without impacting other features,
        the build script content is erased and replaced by a minimal content which does nothing.
        -->
        <echo file="${frontpanel.build.file}" append="false"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<project name="frontpanel">
	<extension-point name="frontpanel/project/compile/requirements"/>
	<target name="compat" depends="frontpanel/project/compile/requirements" extensionOf="platform/customization">
		<!-- Empty task to keep compatibility with other scripts which use the extension point 'frontpanel/project/compile/requirements' -->
	</target>
</project>]]>
        </echo>
    </target>

    <target name="build/platform" depends="build/environment,build/clean,build/frontpanel"
            description="Generates the platform.">
        <ant antfile="${framework.dir}/MicroEJGenerator.xml"/>
        <antcall target="microej/clean"/>
    </target>

    <target name="microej/clean">
        <delete dir="${scripts.dir}"/>
    </target>

    <!--
        The System Property 'noMergeLibraries' must not be added at all if not set by the user
        since the Architecture Ant script uses 'isset' (which returns true even if the System Property is empty).
    -->
    <target name="build/environment/mergeLibraries" if="noMergeLibraries">
        <property name="noMergeLibrariesArg" value="-DnoMergeLibraries=${noMergeLibraries}"/>
    </target>

    <target name="launch" depends="build/environment/mergeLibraries">
        <path id="ant.classpath">
            <pathelement path="${classpath}"/>
        </path>
        <property name="noMergeLibrariesArg" value=""/>

        <java classname="org.apache.tools.ant.launch.Launcher" fork="true" failonerror="true">
            <classpath refid="ant.classpath"/>
            <arg value="-buildfile"></arg>
            <arg value="${build.script.path}"></arg>
            <arg value="-emacs"></arg>
            <jvmarg value="-Dbuild.dir=${build.dir}"></jvmarg>
            <jvmarg value="-Dartifacts.dir=${artifacts.dir}"></jvmarg>
            <jvmarg value="-Dproject.parent.dir=${project.parent.dir}"></jvmarg>
            <jvmarg value="-Dplatform.microej=${platform.microej}"></jvmarg>
            <jvmarg value="-Dplatform.base.dir=${platform.base.dir}"></jvmarg>
            <jvmarg value="-Dveeport.project.dir=${veeport.project.dir}"></jvmarg>
            <jvmarg value="-Darchitecture.output.dir=${architecture.output.dir}"></jvmarg>
            <jvmarg value="-Dveeport.output.dir=${veeport.output.dir}"></jvmarg>
            <jvmarg value="-Dmicroui.frontpanel.output.dir=${microui.frontpanel.output.dir}"></jvmarg>
            <jvmarg value="-Dbuild.compiler=org.eclipse.jdt.core.JDTCompilerAdapter"></jvmarg>
            <jvmarg line="${noMergeLibrariesArg}"></jvmarg>
        </java>
    </target>

</project>
