<?xml version="1.0" encoding="UTF-8"?>
<project name="freenet" default="package" basedir=".">
	<description>
Freenet is free software that lets you publish and retrieve information without
fear of censorship. To achieve this, the network is entirely decentralized, and
all actions are anonymous. Without anonymity, there can never be true freedom
of speech, and without decentralization the network would be vulnerable to attack.

This file is to build Freenet entirely from source. The builder is responsible
for satisfying all library dependencies, e.g. via override.properties. Package
maintainers may prefer to use this instead of build.xml.
	</description>

	<!-- =================================================================== -->
	<!-- Global properties                                                   -->
	<!-- =================================================================== -->

	<!-- user overrides (properties are immutable, so set overrides first) -->
	<property file="override.properties"/>
	<property file="build.properties"/>
	<property file="dependencies.properties"/>

	<property name="version.src" value="freenet/node/Version.java" />
	<property name="version.make" value="freenet/node/Version.class" />

	<path id="lib.path">
		<fileset dir="${lib.contrib.dir}" includes="${lib.contrib.jars}"/>
		<fileset dir="${lib.dir}" includes="${lib.jars}"/>
		<fileset dir="/usr/share/java" includes="${lib.jars}" erroronmissingdir="false"/>
	</path>
	<path id="libtest.path">
		<path refid="lib.path"/>
		<fileset dir="${lib.dir}" includes="${libtest.jars}"/>
		<fileset dir="/usr/share/java" includes="${libtest.jars}" erroronmissingdir="false"/>
		<pathelement path="${main.make}"/>
		<pathelement path="${test.make}"/>
	</path>

	<property name="gjs.dst" value="${main.src}/freenet/clients/http/staticfiles/freenetjs"/>
	<property name="gjs.dir" value="generator/js"/>
	<path id="gjs.lib.path">
		<fileset dir="${gjs.dir}" includes="lib/*.jar"/>
		<pathelement location="${gjs.dir}/war/WEB-INF/classes"/>
		<fileset dir="${lib.dir}" includes="${gjs.lib.jars}"/>
	</path>

	<path id="pmd.classpath">
		<fileset dir="${pmd.dir}" erroronmissingdir="false" includes="lib/*.jar"/>
	</path>

	<property environment="env"/>
	<property name="keystore" value="${env.jarsignerStoreLocation}"/>
	<property name="keystore_alias" value="${env.jarsignerAlias}"/>
	<property name="keystore_pass" value="${env.jarsignerStorePassword}"/>
	<property name="key_pass" value="${env.jarsignerCodeSigningKeyPassword}"/>

	<property name="jar.dst" value="${main.dst}/freenet.jar"/>

	<assertions><enable/></assertions>

	<!-- set version string based on git-describe -->
	<exec executable="git" failifexecutionfails="false"
		errorProperty="git.errror" outputproperty="git.describe" resultproperty="git.result">
		<arg value="describe" />
		<arg value="--always" />
		<arg value="--abbrev=4" />
		<arg value="--dirty" />
	</exec>
	<condition property="git.revision" value="${git.describe}" else="@unknown@">
		<and>
			<equals arg1="${git.result}" arg2="0" />
			<isset property="git.describe" />
		</and>
	</condition>

	<!-- =================================================================== -->
	<!-- Miscellaneous                                                       -->
	<!-- =================================================================== -->

	<target name="dist" depends="clean-all, all" description="clean-build everything"/>

	<target name="sign" description="Sign the distribution" depends="package">
		<echo message="Signing the distribution"/>
		<signjar jar="${jar.dst}" strict="false" tsaurl="http://timestamp.comodoca.com/rfc3161" alias="${keystore_alias}" keystore="${keystore}" storepass="${keystore_pass}" keypass="${key_pass}" sigalg="SHA256withRSA" digestalg="SHA-256"/>
		<!-- TODO: Should have strict="true", but it was introduced in Java 7, which we cannot yet require. -->
		<verifyjar jar="${jar.dst}" alias="${keystore_alias}" keystore="${keystore}" storepass="${keystore_pass}" keypass="${key_pass}"/>
	</target>

	<target name="all" depends="package, doc, findbugs, pmd, cpd" description="build everything, incl. docs, GWT js"/>

	<target name="clean-all" depends="clean, clean-doc, clean-gjs" description="clean all build products, incl. docs, GWT js"/>

	<target name="help" description="display help on parameters">
		<echo message="For main build targets, see `ant -p`"/>
		<echo message=""/>
		<echo message="Test parameters (-Dtest.PARAM=true)"/>
		<echo message="  skip        Skip all tests"/>
		<echo message="  verbose     Report additional information"/>
		<echo message="  benchmark   Run benchmark tests"/>
		<echo message="  extensive   Run extensive tests"/>
		<echo message=""/>
		<echo message="Misc parameters (-DPARAM=VALUE)"/>
		<echo message="  javac.args  Command line arguments to pass to javac"/>
		<echo message=""/>
	</target>

	<target name="findbugs" depends="package" unless="${findbugs.skip}">
		<antcall target="libdep-findbugs"/>
		<echo message="Running Findbugs"/>
		<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpath="${findbugs.path}"/>

		<findbugs home="${findbugs.dir}"
		output="xml"
		outputFile="${test.dst}/findbugs.xml"
		warningsProperty="findbugs.module.warnings"
		failOnError="true"
		jvmargs="-Xmx1536m -XX:MaxPermSize=384m -XX:ReservedCodeCacheSize=128m" >
		<auxClasspath refid="lib.path" />
		<sourcePath path="${main.src}" />
		<class location="${jar.dst}" />
		</findbugs>
	</target>

	<target name="pmd" depends="package" unless="${pmd.skip}">
		<antcall target="libdep-pmd"/>
		<echo message="Running PMD"/>
		<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath"/>
		<pmd rulesetfiles="rulesets/java/design.xml" encoding="utf-8">
			<formatter type="xml" toFile="${test.dst}/pmd.xml"/>
			<fileset dir="${main.src}">
				<include name="**/*.java"/>
			</fileset>
		</pmd>
	</target>

	<target name="cpd" depends="package" unless="${cpd.skip}">
		<antcall target="libdep-cpd"/>
		<echo message="Running CPD"/>
		<taskdef name="cpd" classname="net.sourceforge.pmd.cpd.CPDTask" classpathref="pmd.classpath"/>
		<cpd minimumTokenCount="100" format="xml" outputFile="${test.dst}/cpd.xml">
			<fileset dir="${main.src}">
				<include name="**/*.java"/>
			</fileset>
		</cpd>	
	</target>

	<target name="debug">
		<echoproperties/>
	</target>

	<!-- =================================================================== -->
	<!-- Library dependencies                                                -->
	<!-- =================================================================== -->

	<!--
	currently we only check for the most common cases of missing libraries.
	this does result in less clear error messages for more obscure setups, e.g.
	if you have debian's gwt-dev.jar but not commons-collections3.jar. however,
	to cover all scenarios would be a pain.
	-->

	<target name="env">
		<available property="lib.contrib.present" classname="freenet.node.ExtVersion" classpathref="lib.path"/>
		<available property="lib.bouncycastle.present" classname="org.bouncycastle.crypto.signers.HMacDSAKCalculator" classpathref="lib.path"/>
		<available property="lib.pebble.present" classname="com.mitchellbosecke.pebble.PebbleEngine" classpathref="lib.path"/>
		<available property="lib.junit.present" classname="org.junit.runners.JUnit4" classpathref="libtest.path"/>
		<available property="lib.hamcrest.present" classname="org.hamcrest.SelfDescribing" classpathref="libtest.path"/>
		<available property="lib.findbugs.present" classname="edu.umd.cs.findbugs.anttask.FindBugsTask" classpath="${findbugs.path}"/>
		<available property="lib.pmd.present" classname="net.sourceforge.pmd.ant.PMDTask" classpathref="pmd.classpath"/>
		<available property="lib.cpd.present" classname="net.sourceforge.pmd.cpd.CPDTask" classpathref="pmd.classpath"/>
		<available property="gjs.lib.gwt-user.present" classname="com.google.gwt.user.client.Window" classpathref="gjs.lib.path"/>
		<available property="gjs.lib.gwt-dev.present" classname="com.google.gwt.dev.About" classpathref="gjs.lib.path"/>
		<condition property="gjs.lib.gwt.present">
			<and>
				<isset property="gjs.lib.gwt-user.present"/>
				<isset property="gjs.lib.gwt-dev.present"/>
			</and>
		</condition>
	</target>

	<target name="libdep-ext" depends="env" unless="lib.contrib.present">
		<fail message="freenet-ext not available"/>
	</target>

	<target name="libdep-bc" depends="env" unless="lib.bouncycastle.present">
		<fail message="Bouncycastle not available (or not recent enough); try https://www.bouncycastle.org/download/${bc.jar}"/>
	</target>

	<target name="libdep-pebble" depends="env" unless="lib.pebble.present">
		<fail message="Pebble template engine not available"/>
	</target>

	<!-- only required for testing -->
    <!--
    <target name="libdep-junit" depends="env" unless="lib.junit.present">
		<fail message="JUnit4 not available"/>
	    </target>

	<target name="libdep-hamcrest" depends="env" unless="lib.hamcrest.present">
		<fail message="Hamcrest-core not available"/>
	</target>
    -->

	<target name="libdep-findbugs" depends="env" unless="lib.findbugs.present">
		<fail message="findbugs not available"/>
	</target>

	<target name="libdep-pmd" depends="env" unless="lib.pmd.present">
		<fail message="PMD not available"/>
	</target>

	<target name="libdep-cpd" depends="env" unless="lib.cpd.present">
		<fail message="CPD not available"/>
	</target>

	<target name="libdep-gwt-user" depends="env" unless="gjs.lib.gwt-user.present">
		<fail message="GWT-user not present"/>
	</target>

	<target name="libdep-gwt-dev" depends="env" unless="gjs.lib.gwt-dev.present">
		<fail message="GWT-dev not present"/>
	</target>

	<!-- =================================================================== -->
	<!-- Standard build                                                      -->
	<!-- =================================================================== -->

	<target name="init">
		<mkdir dir="${main.make}"/>
		<mkdir dir="${main.dst}"/>
		<mkdir dir="${test.make}"/>
		<mkdir dir="${test.dst}"/>
	</target>

	<target name="dep" depends="ensure-ext, ensure-bc, ensure-pebble, ensure-gjs"/>

	<target name="check-version-file">
		<uptodate property="version.uptodate"
		  targetfile="${main.make}/${version.src}"
		  srcfile="${main.src}/${version.src}"/>
	</target>

	<target name="build-version-file" depends="check-version-file" unless="version.uptodate">
		<!-- Create the Version file with patched revision number in ${main.make} -->
		<copy file="${main.src}/${version.src}" tofile="${main.make}/${version.src}" overwrite="true" />
		<delete file="${main.make}/${version.make}" quiet="true" />
		<replace file="${main.make}/${version.src}">
			<replacefilter token="@custom@" value="${git.revision}"/>
		</replace>
		<echo message="Updated build version to ${git.revision} in ${main.make}/${version.src}"/>
	</target>

	<target name="build" depends="init, dep, build-version-file">
		<!-- Create the time stamp -->
		<tstamp/>
		<!-- Create the build directory structure used by compile -->
		<javac srcdir="${main.src}" destdir="${main.make}" debug="on" source="1.8" target="1.8" includeAntRuntime="false" encoding="UTF-8">
			<compilerarg line="${javac.args}"/>
			<classpath refid="lib.path"/>
			<!-- tell javac to find Version.java in ${main.make}, not ${main.src} -->
			<sourcepath><pathelement path="${main.make}"/></sourcepath>
			<!-- following a very temporary list of files to be build -->
			<include name="**/*.java"/>
			<exclude name="**/package-info.java"/>
			<exclude name="${version.src}"/>
		</javac>

		<!-- Force compile of Version.java in case compile of ${main.src} didn't trigger it -->
		<javac srcdir="${main.make}" destdir="${main.make}" debug="on" source="1.8" target="1.8" includeAntRuntime="false" encoding="UTF-8">
			<compilerarg line="${javac.args}"/>
			<classpath refid="lib.path"/>
			<include name="${version.src}"/>
		</javac>

		<!-- Copy web interface static files to the build dir -->
		<copy todir="${main.make}/freenet/clients/http/staticfiles">
			<fileset dir="${main.src}/freenet/clients/http/staticfiles"/>
		</copy>
		<!-- Copy translation files to the build dir -->
		<copy todir="${main.make}/freenet/l10n">
			<fileset dir="${main.src}/freenet/l10n">
				<include name="freenet.l10n.*.properties"/>
				<include name="iso-*.tab"/>
			</fileset>
		</copy>
		<!-- Copy the dependencies.properties file -->
		<copy todir="${main.make}" file="dependencies.properties"/>
	</target>

	<target name="package-only" depends="build">
		<property name="lib.contrib.loc" location="${lib.contrib.dir}" />
		<property name="lib.loc" location="${lib.dir}" />
		<pathconvert property="lib.jars.package" refid="lib.path" pathsep=" ">
			<map from="${lib.contrib.loc}/" to=""/>
			<map from="${lib.loc}/" to=""/>
		</pathconvert>
		<jar jarfile="${jar.dst}" basedir="${main.make}">
			<manifest>
				<attribute name="Permissions" value="all-permissions"/>
				<attribute name="Application-Name" value="Freenet REference Daemon"/>

				<attribute name="Required-Ext-Version" value="29"/>
				<attribute name="Recommended-Ext-Version" value="29"/>
				<section name="common">
					<attribute name="Specification-Title" value="Freenet"/>
					<attribute name="Specification-Version" value="0.7.5"/>
					<attribute name="Specification-Vendor" value="freenetproject.org"/>
					<attribute name="Implementation-Title" value="Freenet"/>
					<attribute name="Implementation-Version" value="0.7.5 ${git.revision}"/>
					<attribute name="Implementation-Vendor" value="freenetproject.org"/>
				</section>
			</manifest>
			<exclude name="${version.src}"/>
		</jar>
	</target>

	<target name="package" depends="unit, package-only" description="build standard binary packages (Freenet daemon)"/>

	<target name="unit-build" depends="build" unless="${test.skip}">
		<antcall target="libdep-junit"/>
		<antcall target="libdep-hamcrest"/>
		<javac srcdir="${test.src}" destdir="${test.make}" debug="on" source="1.8" target="1.8" includeAntRuntime="false" encoding="UTF-8">
			<compilerarg line="${javac.args}"/>
			<classpath refid="libtest.path"/>
			<include name="**/*.java"/>
			<exclude name="*.java"/>
		</javac>
		<copy todir="${test.make}/freenet/client/filter/png" overwrite="true">
			<fileset dir="${test.src}/freenet/client/filter/png"/>
		</copy>
		<copy todir="${test.make}/freenet/client/filter/bmp" overwrite="true">
			<fileset dir="${test.src}/freenet/client/filter/bmp"/>
		</copy>
		<copy todir="${test.make}/freenet/client/filter/mp3" overwrite="true">
			<fileset dir="${test.src}/freenet/client/filter/mp3"/>
		</copy>
		<copy todir="${test.make}/freenet/client/filter/gif" overwrite="true">
			<fileset dir="${test.src}/freenet/client/filter/gif"/>
		</copy>
		<copy todir="${test.make}/freenet/crypt/ciphers/rijndael-gladman-test-data" overwrite="true">
			<fileset dir="${test.src}/freenet/crypt/ciphers/rijndael-gladman-test-data"/>
		</copy>
		<copy todir="${test.make}/freenet/l10n" overwrite="true">
			<fileset dir="${test.src}/freenet/l10n" includes="*.properties"/>
		</copy>
	</target>

	<target name="unit" depends="unit-build" unless="${test.skip}">
		<junit printsummary="yes" haltonfailure="${test.haltonfailure}" logfailedtests="yes" showoutput="yes" filtertrace="no" fork="on" forkmode="perTest" maxmemory="256m" dir="${test.dst}">
			<classpath refid="libtest.path"/>
			<formatter type="plain" usefile="false"/>
			<formatter type="xml" if="${test.xml_output}"/>
			<formatter classname="org.apache.tools.ant.taskdefs.optional.junit.TearDownOnVmCrash" usefile="false"/>
			<test if="test.class" name="${test.class}" todir="${test.dst}"/>
			<batchtest unless="test.class" fork="yes" todir="${test.dst}">
				<fileset dir="${test.make}" includes="**/*Test.class"/>
			</batchtest>
			<sysproperty key="test.verbose" value="${test.verbose}"/>
			<sysproperty key="test.benchmark" value="${test.benchmark}"/>
			<sysproperty key="test.extensive" value="${test.extensive}"/>
			<!-- TODO source needs to be edited too; the old variables were "benchmark" and "extensiveTesting" -->
			<assertions><enable/></assertions>
		</junit>
	</target>

	<target name="clean" description="clean standard build products">
		<delete dir="${main.make}"/>
		<delete dir="${main.dst}"/>
		<delete dir="${test.make}"/>
		<delete dir="${test.dst}"/>
	</target>

	<!-- =================================================================== -->
	<!-- Dependencies (contrib, ie. freenet-ext.jar)                         -->
	<!-- =================================================================== -->

	<target name="ensure-ext" depends="libdep-ext">
		<!-- clean build does nothing, since this is external library. -->
	</target>

	<target name="ensure-bc" depends="libdep-bc">
		<!-- clean build does nothing, since this is external library. -->
	</target>

	<target name="ensure-pebble" depends="libdep-pebble">
		<!-- clean build does nothing, since this is external library. -->
	</target>

	<!-- =================================================================== -->
	<!-- Generate GWT code                                                   -->
	<!-- =================================================================== -->

	<target name="env-gjs">
		<available property="gjs.present" file="${gjs.dst}" type="dir"/>
	</target>

	<target name="ensure-gjs" depends="env-gjs" unless="gjs.present">
		<antcall target="build-gjs"/>
	</target>

	<target name="build-gjs" depends="libdep-gwt-user, libdep-gwt-dev">
		<ant antfile="build.xml" dir="${gjs.dir}">
			<reference refid="gjs.lib.path" torefid="project.class.path"/>
		</ant>
	</target>

	<target name="clean-gjs">
		<ant antfile="build.xml" dir="${gjs.dir}" target="deleteGenerated"/>
	</target>

	<!-- =================================================================== -->
	<!-- Documentation                                                       -->
	<!-- =================================================================== -->

	<target name="init-doc" unless="${doc.skip}">
		<mkdir dir="${doc.api}"/>
	</target>

	<target name="javadoc" depends="init-doc, dep" unless="${doc.skip}">
		<javadoc classpathref="lib.path" sourcepath="${main.src}" destdir="${doc.api}" use="true">
			<link href="http://docs.oracle.com/javase/6/docs/api/"/>
		</javadoc>
	</target>

	<target name="doc" depends="javadoc" description="build documentation">
		<!-- currently freenet has no other documentation apart from javadocs -->
	</target>

	<target name="clean-doc" unless="${doc.skip}">
		<delete dir="${doc.api}"/>
	</target>

</project>
