<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright (C) 2018 Universal Character/Graphics display library
  ~
  ~ This program is free software: you can redistribute it and/or modify
  ~ it under the terms of the GNU Lesser General Public License as
  ~ published by the Free Software Foundation, either version 3 of the
  ~ License, or (at your option) any later version.
  ~
  ~ This program 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 Lesser Public License for more details.
  ~
  ~ You should have received a copy of the GNU General Lesser Public
  ~ License along with this program.  If not, see
  ~ <http://www.gnu.org/licenses/lgpl-3.0.html>.
  -->

<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">
    <parent>
        <artifactId>ucg-display</artifactId>
        <groupId>com.ibasco.ucgdisplay</groupId>
        <version>1.2.2-alpha</version>
        <relativePath>../../../pom.xml</relativePath>
    </parent>

    <artifactId>ucgd-native-graphics</artifactId>
    <packaging>jar</packaging>
    <modelVersion>4.0.0</modelVersion>
    <name>${project.prefix} Native :: Graphics</name>

    <properties>
        <main.basedir>${project.parent.basedir}</main.basedir>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.ibasco.ucgdisplay</groupId>
            <artifactId>ucgd-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>uk.org.lidalia</groupId>
            <artifactId>sysout-over-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.scijava</groupId>
            <artifactId>native-lib-loader</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
    </dependencies>

    <profiles>
        <!-- Cross-compile profile (can only be ran under linux os) -->
        <profile>
            <id>cross-compile</id>
            <properties>
                <current-os>linux</current-os>
                <current-arch>amd64</current-arch>
                <crossCompile>true</crossCompile>
                <build-target>native-build-cc-all</build-target>
            </properties>
            <build>
                <plugins>
                    <!-- Enforcer: Make sure this can only be run from the Linux OS -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-enforcer-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>enforce-os</id>
                                <goals>
                                    <goal>enforce</goal>
                                </goals>
                                <configuration>
                                    <rules>
                                        <requireOS>
                                            <name>Linux</name>
                                            <family>unix</family>
                                            <arch>amd64</arch>
                                        </requireOS>
                                    </rules>
                                    <failFast>true</failFast>
                                    <fail>true</fail>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <!-- BUILD PROFILE: Linux - x86_64/amd64 -->
        <profile>
            <id>build-linux-amd64</id>
            <activation>
                <os>
                    <name>linux</name>
                    <family>unix</family>
                    <arch>amd64</arch>
                </os>
            </activation>
            <properties>
                <current-os>linux</current-os>
                <current-arch>amd64</current-arch>
                <build-target>native-build-linux-x86_64</build-target>
            </properties>
        </profile>

        <!-- BUILD PROFILE: Linux - arm -->
        <profile>
            <id>build-linux-arm</id>
            <activation>
                <os>
                    <name>linux</name>
                    <family>unix</family>
                    <arch>arm</arch>
                </os>
            </activation>
            <properties>
                <current-os>linux</current-os>
                <current-arch>arm</current-arch>
                <build-target>native-build-linux-arm</build-target>
            </properties>
        </profile>

        <!-- BUILD PROFILE: Windows - x86_64/amd64 -->
        <profile>
            <id>build-windows-amd64</id>
            <activation>
                <os>
                    <family>windows</family>
                    <arch>amd64</arch>
                </os>
            </activation>
            <properties>
                <current-os>windows</current-os>
                <current-arch>amd64</current-arch>
                <build-target>native-build-windows-x86_64</build-target>
            </properties>
        </profile>

        <!-- BUILD PROFILE: Mac OSX - x86_64/amd64 -->
        <profile>
            <id>build-mac-amd64</id>
            <activation>
                <os>
                    <family>unix</family>
                    <arch>x86_64</arch>
                    <name>mac os x</name>
                </os>
            </activation>
            <properties>
                <current-os>macosx</current-os>
                <current-arch>amd64</current-arch>
                <build-target>native-build-macosx-x86_64</build-target>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <!-- Ant-Run Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <!-- Compile native library using CMake -->
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <!-- Pass maven properties to ant -->
                                <echo>Building native GRAPHICS library (OS: ${current-os}, ARCH: ${current-arch}, TARGET: ${build-target})</echo>
                                <!--suppress UnresolvedMavenProperty -->
                                <property name="root.dir" location="../../.." />
                                <!--suppress UnresolvedMavenProperty -->
                                <ant antfile="${root.dir}/scripts/build-common.xml" target="show-prop-info" />
                                <!--suppress UnresolvedMavenProperty -->
                                <ant antfile="${root.dir}/scripts/build-graphics.xml" target="${build-target}" />
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Native Maven Plugin -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>native-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>javah</goal>
                        </goals>
                        <configuration>
                            <javahVerbose>true</javahVerbose>
                            <javahPath>${env.JAVA_HOME}/bin/javah</javahPath>
                            <javahIncludes>
                                <javahInclude>
                                    <className>com.ibasco.ucgdisplay.core.u8g2.U8g2Graphics</className>
                                    <headerName>U8g2Graphics.h</headerName>
                                </javahInclude>
                            </javahIncludes>
                            <javahOutputDirectory>${cpp.proj.dir}</javahOutputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Source Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/UpdateControllerHeader.java</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <!-- Javadoc Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/UpdateControllerHeader.java</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <!-- Clean Plugin: Remove the native build directory during clean operation -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>src/main/cpp/build</directory>
                            <followSymlinks>false</followSymlinks>
                        </fileset>
                        <fileset>
                            <directory>src/main/cpp/utils/build</directory>
                            <followSymlinks>false</followSymlinks>
                        </fileset>
                        <fileset>
                            <directory>src/main/cpp/lib</directory>
                            <followSymlinks>false</followSymlinks>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>

            <!-- Maven Exec Plugin: Update code sources -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
            </plugin>

            <!-- JAR Plugin: Include the classes and natives folder in jar packaging -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/utils/codegen/**</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <!--<addClasspath>true</addClasspath>-->
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>