<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<!-- MAVEN ARTIFACT INFORMATION -->
	<artifactId>pi4j-native</artifactId>
	<name>Pi4J :: JNI Native Library</name>
	<description>Pi4J JNI Native Library</description>
	<packaging>pom</packaging>
	<parent>
		<groupId>com.pi4j</groupId>
		<artifactId>pi4j-parent</artifactId>
        <version>1.2</version>
	</parent>

    <!-- BUILD PROPERTIES -->
    <properties>
        <pi4j-native-ant-build-target>build-libpi4j.so-remote</pi4j-native-ant-build-target>
    </properties>

	<!-- BUILD PROFILES -->
	<profiles>

        <!-- SPECIAL PROFILE TO DETERMINE IF RUNNING ON THE PI HARDWARE -->
        <profile>
            <id>local-compile</id>
            <activation>
                <os>
                    <name>linux</name>
                    <family>unix</family>
                    <arch>arm</arch>
                </os>
            </activation>
            <properties>
                <pi4j-native-ant-build-target>build-libpi4j.so-local</pi4j-native-ant-build-target>
            </properties>
        </profile>

		<!-- SPECIAL PROFILE TO SUPPORT CROSS-COMPILER BUILDS -->
		<profile>
			<id>cross-compile</id>
			<activation>
				<property>
					<name>env.RPI_CROSS_COMPILE</name>
					<value>true</value>
				</property>
			</activation>
			<properties>
				<pi4j-native-ant-build-target>build-libpi4j.so-cross-compile</pi4j-native-ant-build-target>
			</properties>
		</profile>

		<!-- This profile is used to dynamically rebuild the native library .H
			header files from the defined JNI jar classes. This profile should only be
			used when changes have been made to the JNI Java wrapper classes and new
			native method signatures are required. To use this profile, simply include
			the "-generate-jni-headers" command argument with the maven build command. -->
		<profile>
			<id>generate-jni-headers</id>

			<!-- SPECIAL PROFILE DEPENDENCIES -->
			<dependencies>

				<!-- we define the pi4j Java project as a dependency so that the .H header
					files can be generated based on the Java native wrapper classes -->
				<dependency>
					<groupId>com.pi4j</groupId>
					<artifactId>pi4j-core</artifactId>
					<version>${project.version}</version>
					<scope>compile</scope>
				</dependency>

			</dependencies>

			<!-- SPECIAL PROFILE BUILD INSTRUCTIONS -->
			<build>
				<plugins>

					<!-- Generate .h header files for JNI wrapper -->
					<plugin>
						<groupId>org.codehaus.mojo</groupId>
						<artifactId>native-maven-plugin</artifactId>
						<extensions>true</extensions>
						<executions>
							<execution>
								<id>javah-generate</id>
								<phase>generate-sources</phase>
								<goals>
									<goal>javah</goal>
								</goals>
								<configuration>

									<!-- trigger javah execution -->
									<javahClassNames>
                                        <javahClassName>com.pi4j.jni.AnalogInputMonitor</javahClassName>
                                        <javahClassName>com.pi4j.jni.WDT</javahClassName>
										<javahClassName>com.pi4j.jni.I2C</javahClassName>
										<javahClassName>com.pi4j.jni.Serial</javahClassName>
                                        <javahClassName>com.pi4j.jni.SerialInterrupt</javahClassName>
										<javahClassName>com.pi4j.wiringpi.Gpio</javahClassName>
										<javahClassName>com.pi4j.wiringpi.GpioInterrupt</javahClassName>
										<javahClassName>com.pi4j.wiringpi.GpioUtil</javahClassName>
										<javahClassName>com.pi4j.wiringpi.Serial</javahClassName>
										<javahClassName>com.pi4j.wiringpi.Lcd</javahClassName>
										<javahClassName>com.pi4j.wiringpi.Shift</javahClassName>
										<javahClassName>com.pi4j.wiringpi.SoftPwm</javahClassName>
										<javahClassName>com.pi4j.wiringpi.Spi</javahClassName>
										<javahClassName>com.pi4j.wiringpi.Gertboard</javahClassName>
										<javahClassName>com.pi4j.wiringpi.Nes</javahClassName>
									</javahClassNames>

									<!-- enable additional javah interface in dependencies list -->
									<javahSearchJNIFromDependencies>true</javahSearchJNIFromDependencies>

									<!-- specify the output directory for generated header files -->
									<javahOutputDirectory>src/main/native</javahOutputDirectory>

								</configuration>
							</execution>
						</executions>
					</plugin>
				</plugins>
			</build>
		</profile>
	</profiles>

	<!-- BUILD INSTRUCTIONS -->
	<build>
		<finalName>libpi4j</finalName>
		<plugins>

			<!-- GENERATE LICENSE HEADERS IN SOURCE FILES -->
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>license-maven-plugin</artifactId>
			</plugin>

			<plugin>
				<artifactId>maven-antrun-plugin</artifactId>
				<executions>

					<!-- ensure that a target directory has been created platform -->
					<execution>
						<id>generate-sources</id>
						<phase>generate-sources</phase>
						<configuration>
							<tasks>
								<mkdir dir="target" />
								<mkdir dir="target/classes" />
								<mkdir dir="target/native" />
								<mkdir dir="target/native/lib" />
							</tasks>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>

                    <!-- copy the native source files and compile them on each of the targeted platforms -->
                    <execution>
                        <id>pi4j-build-native-libraries</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name="pi4j-build-platform-libraries" >
                                <ant target="${pi4j-native-ant-build-target}" inheritRefs="true">

                                    <!-- WE REDEFINE THE FOLLOWING PROPERTIES TO ALLOW THEM TO BE OVERRIDDEN
                                         VIA SYSTEM PROPERTIES PROVIDED ON THE MAVEN COMMAND LINE -->

                                    <!-- RaspberryPi Platform Properties -->
                                    <property name="raspberrypi.name" value="${raspberrypi.name}"/>
                                    <property name="raspberrypi.host" value="${raspberrypi.host}"/>
                                    <property name="raspberrypi.port" value="${raspberrypi.port}"/>
                                    <property name="raspberrypi.user" value="${raspberrypi.user}"/>
                                    <property name="raspberrypi.password" value="${raspberrypi.password}"/>
                                    <property name="raspberrypi.directory" value="${raspberrypi.directory}"/>

                                    <!-- Odroid Platform Properties -->
                                    <property name="odroid.name" value="${odroid.name}"/>
                                    <property name="odroid.host" value="${odroid.host}"/>
                                    <property name="odroid.port" value="${odroid.port}"/>
                                    <property name="odroid.user" value="${odroid.user}"/>
                                    <property name="odroid.password" value="${odroid.password}"/>
                                    <property name="odroid.directory" value="${odroid.directory}"/>

                                    <!-- LeMaker BananaPi Platform Properties -->
                                    <property name="bananapi.name" value="${bananapi.name}"/>
                                    <property name="bananapi.host" value="${bananapi.host}"/>
                                    <property name="bananapi.port" value="${bananapi.port}"/>
                                    <property name="bananapi.user" value="${bananapi.user}"/>
                                    <property name="bananapi.password" value="${bananapi.password}"/>
                                    <property name="bananapi.directory" value="${bananapi.directory}"/>

                                    <!-- LeMaker BananaPro Platform Properties -->
                                    <property name="bananapro.name" value="${bananapro.name}"/>
                                    <property name="bananapro.host" value="${bananapro.host}"/>
                                    <property name="bananapro.port" value="${bananapro.port}"/>
                                    <property name="bananapro.user" value="${bananapro.user}"/>
                                    <property name="bananapro.password" value="${bananapro.password}"/>
                                    <property name="bananapro.directory" value="${bananapro.directory}"/>

                                    <!-- SINOVOIP-BPI Platform Properties -->
                                    <property name="bpi.name" value="${bpi.name}"/>
                                    <property name="bpi.host" value="${bpi.host}"/>
                                    <property name="bpi.port" value="${bpi.port}"/>
                                    <property name="bpi.user" value="${bpi.user}"/>
                                    <property name="bpi.password" value="${bpi.password}"/>
                                    <property name="bpi.directory" value="${bpi.directory}"/>

                                    <!-- OrangePi Platform Properties -->
                                    <property name="orangepi.name" value="${orangepi.name}"/>
                                    <property name="orangepi.host" value="${orangepi.host}"/>
                                    <property name="orangepi.port" value="${orangepi.port}"/>
                                    <property name="orangepi.user" value="${orangepi.user}"/>
                                    <property name="orangepi.password" value="${orangepi.password}"/>
                                    <property name="orangepi.directory" value="${orangepi.directory}"/>

                                    <!-- NanoPi Platform Properties -->
                                    <property name="nanopi.name" value="${nanopi.name}"/>
                                    <property name="nanopi.host" value="${nanopi.host}"/>
                                    <property name="nanopi.port" value="${nanopi.port}"/>
                                    <property name="nanopi.user" value="${nanopi.user}"/>
                                    <property name="nanopi.password" value="${nanopi.password}"/>
                                    <property name="nanopi.directory" value="${nanopi.directory}"/>

                                </ant>
                            </target>
                        </configuration>
                    </execution>

                    <!-- attach the compiled native libraries for each of the targeted platforms as artifacts -->
                    <execution>
                        <id>pi4j-attach-native-library-artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target name="pi4j-attach-platform-libraries" >
                                <taskdef  resource="net/sf/antcontrib/antcontrib.properties"
                                          classpathref="maven.plugin.classpath"/>

                                <!-- Attach 'libpi4j.so' for RasbperryPi -->
                                <if>
                                    <equals arg1="${raspberrypi.build}" arg2="true" />
                                    <then>
                                        <!-- NOTE: As of 2018-04-23 in Pi4J 1.2 SNAPSHOT builds, Pi4J no longer includes
                                           a statically linked wiringPi lib for the Raspberry Pi platform. -->
                                        <attachartifact file="${project.build.directory}/native/lib/raspberrypi/dynamic/libpi4j.so" classifier="raspberrypi-dynamic" type="so"/>
                                    </then>
                                </if>

                                <!-- Attach 'libpi4j.so' for Odroid -->
                                <if>
                                    <equals arg1="${odroid.build}" arg2="true" />
                                    <then>
                                        <attachartifact file="${project.build.directory}/native/lib/odroid/static/libpi4j.so" classifier="odroid-static" type="so"/>
                                        <attachartifact file="${project.build.directory}/native/lib/odroid/dynamic/libpi4j.so" classifier="odroid-dynamic" type="so"/>
                                    </then>
                                </if>

                                <!-- Attach 'libpi4j.so' for LeMaker BananaPi -->
                                <if>
                                    <equals arg1="${bananapi.build}" arg2="true" />
                                    <then>
                                        <attachartifact file="${project.build.directory}/native/lib/bananapi/static/libpi4j.so" classifier="bananapi-static" type="so"/>
                                        <attachartifact file="${project.build.directory}/native/lib/bananapi/dynamic/libpi4j.so" classifier="bananapi-dynamic" type="so"/>
                                    </then>
                                </if>

                                <!-- Attach 'libpi4j.so' for LeMaker BananaPro -->
                                <if>
                                    <equals arg1="${bananapro.build}" arg2="true" />
                                    <then>
                                        <attachartifact file="${project.build.directory}/native/lib/bananapro/static/libpi4j.so" classifier="bananapro-static" type="so"/>
                                        <attachartifact file="${project.build.directory}/native/lib/bananapro/dynamic/libpi4j.so" classifier="bananapro-dynamic" type="so"/>
                                    </then>
                                </if>

                                <!-- Attach 'libpi4j.so' for Sinovoip BPI (BananaPi)  -->
                                <if>
                                    <equals arg1="${bpi.build}" arg2="true" />
                                    <then>
                                        <attachartifact file="${project.build.directory}/native/lib/bpi/static/libpi4j.so" classifier="bpi-static" type="so"/>
                                        <attachartifact file="${project.build.directory}/native/lib/bpi/dynamic/libpi4j.so" classifier="bpi-dynamic" type="so"/>
                                    </then>
                                </if>

                                <!-- Attach 'libpi4j.so' for OrangePi -->
                                <if>
                                    <equals arg1="${orangepi.build}" arg2="true" />
                                    <then>
                                        <attachartifact file="${project.build.directory}/native/lib/orangepi/static/libpi4j.so" classifier="orangepi-static" type="so"/>
                                        <attachartifact file="${project.build.directory}/native/lib/orangepi/dynamic/libpi4j.so" classifier="orangepi-dynamic" type="so"/>
                                    </then>
                                </if>

                                <!-- Attach 'libpi4j.so' for NanoPi -->
                                <if>
                                    <equals arg1="${nanopi.build}" arg2="true" />
                                    <then>
                                        <attachartifact file="${project.build.directory}/native/lib/nanopi/static/libpi4j.so" classifier="nanopi-static" type="so"/>
                                        <attachartifact file="${project.build.directory}/native/lib/nanopi/dynamic/libpi4j.so" classifier="nanopi-dynamic" type="so"/>
                                    </then>
                                </if>

                            </target>
                        </configuration>
                    </execution>

				</executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.ant</groupId>
                        <artifactId>ant-jsch</artifactId>
                        <version>${ant-jsch.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>com.jcraft</groupId>
                        <artifactId>jsch</artifactId>
                        <version>${jsch.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>ant-contrib</groupId>
                        <artifactId>ant-contrib</artifactId>
                        <version>${ant-contrib.version}</version>
                    </dependency>
                </dependencies>
			</plugin>

        </plugins>
	</build>
</project>
