<?xml version="1.0" encoding="UTF-8"?>
<!--
  	ANT
 
  	Copyright 2012-2024 MicroEJ Corp. All rights reserved.
	Modification and distribution is permitted under certain conditions.
  	MicroEJ Corp. PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
-->
<project name="hil">
	<dirname property="ant.dir.hil" file="${ant.file.hil}"/>
	
	<loadproperties srcfile="${ant.dir.hil}/hil.properties" prefix="hilengine"/>	
	<property name="s3.dir" value="${platform.dir}/S3"/>
	
	<import file="init.xml"/>

	<target name="mainMock/initOptions">
		<property name="com.microej.simulator.hil.debug.enabled" value="false"/>
		<property name="com.microej.simulator.hil.debug.port" value="8002"/>
	</target>

	<target name="mainMock/initDebug" depends="mainMock/initOptions" if="${com.microej.simulator.hil.debug.enabled}">
		<!-- Mock debug enabled -->
		<property name="hil.debug.line" value="-Xdebug -Xrunjdwp:transport=dt_socket,suspend=y,server=y,address=${com.microej.simulator.hil.debug.port}"/>
		<echo message="Mock debug is enabled on port ${com.microej.simulator.hil.debug.port}. To have time to attach your debug session, you may need to increase the 's3.hil.timeout' option (currently set to ${s3.hil.timeout} seconds)."></echo>
	</target>
	
	<target name="mainMock" depends="sleep.onWinXP,mainMock/initDebug"> <!-- force depends until a clean solution if found combining extension-point and parallel -->
		<!-- Mock debug disabled by default -->
		<property name="hil.debug.line" value=""/>
		
		<launchMock 
			port="${hil.main.port}"
			properties.file="${hil.main.properties.file}"
			hil.main.excludes="${overall.hil.main.excludes}"
			forceExitOnDisconnect="${hil.main.forceExitOnDisconnect}"
		>
			<mock.classpath>
				<path refid="hil.main.classpath"/>
			</mock.classpath>
		</launchMock>
	</target>
	
	<macrodef name="launchMock">
		<attribute name="name" default="main"/>
		<attribute name="port" default="${s3.hil.port}"/>
		<attribute name="connectFileout" default="${microej.io.tmpdir}/s3.terminated"/>
		<attribute name="forceExitOnDisconnect" default="true"/>
		<attribute name="properties.file" />
		
		<!-- use a default value for backward compatibility but shouldn't -->
		<attribute name="hil.main.excludes" default="${overall.hil.main.excludes}"/>
		
		<element name="mock.classpath"/>
		
		<sequential>
			<fail message="The given properties file (@{properties.file}) doesn't exists">
				<condition>
					<not><available file="@{properties.file}"/></not>
				</condition>
			</fail>
			
			<condition property="hil.verbose.arg" value="-verboseeeeeeeee" else="-verbose">
				<isset property="execution.verbose"/>
			</condition>
			
			<path id="mock.classpath.id.@{name}">
				<mock.classpath/>
			</path>
			
			<echo message="Launching Mock[@{name}] (port: @{port}, properties: @{properties.file}, classpath=${toString:mock.classpath.id.@{name}})" level="verbose"/>
			<java fork="true" failonerror="true" classname="${hilengine.classname}">
				<jvmarg line="${hil.debug.line}"/>
				<env key="${ldlibrarypath.property.name}" path="${ld.library.path}"/>
				<classpath refid="mock.classpath.id.@{name}"/>
				
				<arg line="${hil.verbose.arg}"/>

				
				<!-- Mandatory properties for an UART connection -->
				<!-- java.library.path for native libraries packaged by the jpf -->
				<!-- single quote are needed for WIN32 platforms-->
				<jvmarg line="-Djava.library.path='${jpf.library.path}'"/>
				
				
				<!-- HIL options -->
				<arg line="-port @{port}"/>
				<arg line="-connectFileout @{connectFileout}"/>
				<arg line="-forceExitOnDisconnect @{forceExitOnDisconnect}"/>
				
				<!-- properties option points to a property file containing all System properties to add to the HILEngine instance -->
				<arg value="-properties"/>
				<arg value="@{properties.file}"/>
				
				<!--
					Keep this arg line the last one since @{hil.main.excludes} can be empty.
					If so, the -properties options is not well passed to Java (at least not as expected)
				-->
				
				<!-- excludes option computed by libaries initializers (see extension-init.xml) -->
				<arg line="-excludes @{hil.main.excludes}"/>
			</java>
		</sequential>
	</macrodef>

</project>