<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>nd4j-backend-impls</artifactId>
        <groupId>org.eclipse.deeplearning4j</groupId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nd4j-minimizer</artifactId>
    <packaging>jar</packaging>

    <name>nd4j-minimizer</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <javacpp.build.output.path>${project.build.directory}/classes/org/nd4j/linalg/minimal/bindings/${javacpp.platform}${javacpp.platform.extension}</javacpp.build.output.path>
        <!-- Java 9 module name does not allow native-->
        <module.name>nd4j.minimal</module.name>
        <libnd4j.functrace>OFF</libnd4j.functrace>
        <javacpp.compiler.options>-Wl,--no-undefined</javacpp.compiler.options>


    </properties>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.deeplearning4j</groupId>
            <artifactId>nd4j-cpu-backend-common</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.deeplearning4j</groupId>
            <artifactId>nd4j-minimizer-preset</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>


    <profiles>


        <profile>
            <id>functrace</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>libnd4j.functrace</name>
                    <value>ON</value>
                </property>

            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.bytedeco</groupId>
                        <artifactId>javacpp</artifactId>
                        <version>${javacpp.version}</version>
                        <dependencies>
                            <dependency>
                                <groupId>org.eclipse.deeplearning4j</groupId>
                                <artifactId>nd4j-minimizer-preset</artifactId>
                                <version>${project.version}</version>
                            </dependency>
                            <dependency>
                                <groupId>org.eclipse.deeplearning4j</groupId>
                                <artifactId>nd4j-native-api</artifactId>
                                <version>${project.version}</version>
                            </dependency>
                        </dependencies>
                        <configuration>
                            <properties>${javacpp.platform.properties}</properties>
                            <propertyKeysAndValues>
                                <property>
                                    <name>libnd4j.functrace</name>
                                    <value>${libnd4j.functrace}</value>
                                </property>
                                <property>
                                    <name>platform.root</name>
                                    <value>${javacpp.platform.root}</value>
                                </property>
                                <property>
                                    <name>platform.compiler</name>
                                    <value>${javacpp.platform.compiler}</value>
                                </property>
                                <property>
                                    <name>platform.sysroot</name>
                                    <value>${javacpp.platform.sysroot}</value>
                                </property>
                                <property>
                                    <name>platform.extension</name>
                                    <value>${javacpp.platform.extension}</value>
                                </property>
                            </propertyKeysAndValues>
                            <classPaths>
                                <classPath>${project.build.outputDirectory}</classPath>
                            </classPaths>

                            <includePaths>
                                <includePath>${libnd4jhome}/blasbuild/cpu/include</includePath>
                                <includePath>${libnd4jhome}/blasbuild/cpu/flatbuffers-src/include/
                                </includePath>
                                <includePath>${libnd4jhome}/blas</includePath>
                                <includePath>${libnd4jhome}/include</includePath>
                                <includePath>${libnd4jhome}/include/helpers</includePath>
                                <includePath>${libnd4jhome}/include/array</includePath>
                                <includePath>${libnd4jhome}/include/cnpy</includePath>
                                <includePath>${libnd4jhome}/include/execution</includePath>
                                <includePath>${libnd4jhome}/include/exceptions</includePath>
                                <includePath>${libnd4jhome}/include/graph</includePath>
                                <includePath>${libnd4jhome}/include/indexing</includePath>
                                <includePath>${libnd4jhome}/include/memory</includePath>
                            </includePaths>
                            <linkPaths>
                                <linkPath>${libnd4jhome}/blasbuild/cpu/blas</linkPath>
                            </linkPaths>
                            <resourcePath>${libnd4jhome}/blasbuild/cpu/blas</resourcePath>
                            <buildResources>
                                <buildResource>/${javacpp.platform.library.path}/</buildResource>
                                <buildResource>/lib/</buildResource>
                                <buildResource>/</buildResource>
                                <buildResource>${libnd4jhome}/blasbuild/cpu/blas</buildResource>
                            </buildResources>
                            <includeResources>
                                <includeResource>/${javacpp.platform.library.path}/include/
                                </includeResource>
                            </includeResources>
                            <linkResources>
                                <linkResource>/${javacpp.platform.library.path}/</linkResource>
                                <linkResource>/${javacpp.platform.library.path}/lib/</linkResource>
                            </linkResources>

                            <compilerOptions>
                                <compilerOption>${javacpp.compiler.options}</compilerOption>
                                <!-- This is the main function. Tells gcc to instrument every function with an enter and exit function.-->
                                <compilerOption>-finstrument-functions</compilerOption>
                                <!-- Adds debug information in to the generated .so file. Needed for symbol decoding.  -->
                                <compilerOption>-rdynamic</compilerOption>
                                <!-- Need these 2 libraries including bin utils for handling address decoding-->
                                <compilerOption>-ldl</compilerOption>
                                <compilerOption>-lbfd</compilerOption>
                                <compilerOption>-O0</compilerOption>
                                <!-- Only used for this function. We use inline static file handles only available in c++ 17-->
                                <compilerOption>-std=c++17</compilerOption>
                                <!-- Need to pass this to gcc in order to enable the right functions to appear in cmake.  -->
                                <compilerOption>-DSD_GCC_FUNCTRACE=ON</compilerOption>


                            </compilerOptions>
                        </configuration>
                        <executions>
                            <execution>
                                <id>javacpp-validate</id>
                                <phase>validate</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>javacpp-parser</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                                <configuration>
                                    <skip>${javacpp.parser.skip}</skip>
                                    <outputDirectory>${project.build.sourceDirectory}</outputDirectory>
                                    <classOrPackageName>org.nd4j.presets.minimal.Nd4jMinimalPresets
                                    </classOrPackageName>
                                </configuration>
                            </execution>
                            <execution>
                                <id>javacpp-compiler</id>
                                <phase>process-classes</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                                <configuration>
                                    <skip>${javacpp.compiler.skip}</skip>
                                    <classOrPackageName>org.nd4j.linalg.minimal.bindings.Nd4jMinimal</classOrPackageName>
                                    <copyLibs>true</copyLibs>
                                    <copyResources>true</copyResources>
                                    <configDirectory>${project.build.directory}/classes/META-INF/native-image/${javacpp.platform}${javacpp.platform.extension}/</configDirectory>
                                    <outputDirectory>${javacpp.build.output.path}</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>


        <profile>
            <id>non-functrace</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <property>
                    <name>libnd4j.functrace</name>
                    <value>OFF</value>
                </property>

            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.bytedeco</groupId>
                        <artifactId>javacpp</artifactId>
                        <version>${javacpp.version}</version>
                        <dependencies>
                            <dependency>
                                <groupId>org.eclipse.deeplearning4j</groupId>
                                <artifactId>nd4j-minimizer-preset</artifactId>
                                <version>${project.version}</version>
                            </dependency>
                            <dependency>
                                <groupId>org.eclipse.deeplearning4j</groupId>
                                <artifactId>nd4j-native-api</artifactId>
                                <version>${project.version}</version>
                            </dependency>
                        </dependencies>
                        <configuration>
                            <properties>${javacpp.platform.properties}</properties>
                            <propertyKeysAndValues>
                                <property>
                                    <name>platform.root</name>
                                    <value>${javacpp.platform.root}</value>
                                </property>
                                <property>
                                    <name>platform.compiler</name>
                                    <value>${javacpp.platform.compiler}</value>
                                </property>
                                <property>
                                    <name>platform.sysroot</name>
                                    <value>${javacpp.platform.sysroot}</value>
                                </property>
                                <property>
                                    <name>platform.extension</name>
                                    <value>${javacpp.platform.extension}</value>
                                </property>
                            </propertyKeysAndValues>
                            <classPaths>
                                <classPath>${project.build.outputDirectory}</classPath>
                            </classPaths>

                            <includePaths>
                                <includePath>${libnd4jhome}/blasbuild/cpu/include</includePath>
                                <includePath>${libnd4jhome}/blasbuild/cpu/flatbuffers-src/include/
                                </includePath>
                                <includePath>${libnd4jhome}/blas</includePath>
                                <includePath>${libnd4jhome}/include</includePath>
                                <includePath>${libnd4jhome}/include/helpers</includePath>
                                <includePath>${libnd4jhome}/include/array</includePath>
                                <includePath>${libnd4jhome}/include/cnpy</includePath>
                                <includePath>${libnd4jhome}/include/execution</includePath>
                                <includePath>${libnd4jhome}/include/exceptions</includePath>
                                <includePath>${libnd4jhome}/include/graph</includePath>
                                <includePath>${libnd4jhome}/include/indexing</includePath>
                                <includePath>${libnd4jhome}/include/memory</includePath>
                                <includePath>${libnd4jhome}/include/performance</includePath>
                            </includePaths>
                            <linkPaths>
                                <linkPath>${libnd4jhome}/blasbuild/cpu/blas</linkPath>
                            </linkPaths>
                            <resourcePath>${libnd4jhome}/blasbuild/cpu/blas</resourcePath>
                            <buildResources>
                                <buildResource>/${javacpp.platform.library.path}/</buildResource>
                                <buildResource>/lib/</buildResource>
                                <buildResource>/</buildResource>
                                <buildResource>${libnd4jhome}/blasbuild/cpu/blas</buildResource>
                            </buildResources>
                            <includeResources>
                                <includeResource>/${javacpp.platform.library.path}/include/
                                </includeResource>
                            </includeResources>
                            <linkResources>
                                <linkResource>/${javacpp.platform.library.path}/</linkResource>
                                <linkResource>/${javacpp.platform.library.path}/lib/</linkResource>
                            </linkResources>
                        </configuration>
                        <executions>
                            <execution>
                                <id>javacpp-validate</id>
                                <phase>validate</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                            </execution>
                            <execution>
                                <id>javacpp-parser</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                                <configuration>
                                    <skip>${javacpp.parser.skip}</skip>
                                    <outputDirectory>${project.build.sourceDirectory}</outputDirectory>
                                    <classOrPackageName>org.nd4j.presets.minimal.Nd4jMinimalPresets
                                    </classOrPackageName>
                                </configuration>
                            </execution>
                            <execution>
                                <id>javacpp-compiler</id>
                                <phase>process-classes</phase>
                                <goals>
                                    <goal>build</goal>
                                </goals>
                                <configuration>
                                    <skip>${javacpp.compiler.skip}</skip>
                                    <classOrPackageName>org.nd4j.linalg.minimal.bindings.Nd4jMinimal</classOrPackageName>
                                    <copyLibs>true</copyLibs>
                                    <copyResources>true</copyResources>
                                    <configDirectory>${project.build.directory}/classes/META-INF/native-image/${javacpp.platform}${javacpp.platform.extension}/</configDirectory>
                                    <outputDirectory>${javacpp.build.output.path}</outputDirectory>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <build>
        <plugins>

            <plugin>
                <groupId>org.moditect</groupId>
                <artifactId>moditect-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-jar</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifestEntries>
                                    <Automatic-Module-Name>${module.name}</Automatic-Module-Name>
                                </manifestEntries>
                            </archive>
                            <excludes>
                                <exclude>
                                    org/nd4j/linalg/minimal/bindings/${javacpp.platform}${javacpp.platform.extension}/*
                                </exclude>
                                <exclude>lib/**</exclude>
                                <exclude>*.dll</exclude>
                                <exclude>*.dylib</exclude>
                                <exclude>*.so</exclude>
                                <exclude>*.vso</exclude>
                                <exclude>META-INF/native-image/${javacpp.platform}${javacpp.platform.extension}/</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>${javacpp.platform}${javacpp.platform.extension}</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifestEntries>
                                    <Automatic-Module-Name>${module.name}.binary</Automatic-Module-Name>
                                </manifestEntries>
                            </archive>
                            <classifier>${javacpp.platform}${javacpp.platform.extension}</classifier>
                            <skipIfEmpty>true</skipIfEmpty>
                            <includes>
                                <include>
                                    org/nd4j/linalg/minimal/bindings/${javacpp.platform}${javacpp.platform.extension}/*
                                </include>
                                <include>lib/**</include>
                                <include>META-INF/native-image/${javacpp.platform}${javacpp.platform.extension}/</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <executions>
                    <execution>
                        <id>javacpp-parser</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <skipMain>${javacpp.parser.skip}</skipMain>
                            <includes>
                                <include>org/nd4j/linalg/minimal/bindings/*.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-deploy</id>
                        <phase>none</phase>
                    </execution>

                    <execution>
                        <id>java-classes</id>
                        <phase>deploy</phase>
                        <configuration>
                            <packaging>jar</packaging>
                            <artifactId>${project.artifactId}</artifactId>
                            <groupId>${project.groupId}</groupId>
                            <version>${project.version}</version>
                            <sources>${project.build.directory}/nd4j-native-${project.version}.jar</sources>
                        </configuration>
                    </execution>

                    <execution>
                        <id>native-deps</id>
                        <phase>deploy</phase>
                        <configuration>
                            <packaging>jar</packaging>
                            <artifactId>${project.artifactId}</artifactId>
                            <groupId>${project.groupId}</groupId>
                            <version>${project.version}</version>
                            <classifier>${javacpp.platform}</classifier>
                            <sources>${project.build.directory}/nd4j-native-${project.version}-${javacpp.platform}.jar</sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

    <profiles>
        <!-- profile only enabled for compat which does not have a matching classifier for dependencies-->
        <profile>
            <id>compat-profile</id>
            <activation>
                <property>
                    <name>javacpp.platform.extension</name>
                    <!-- Using the maven build extension (see ./mvn/extensions.xml) use regex to check for any value that isn't empty-->
                    <value>/.+/</value>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>javacpp</artifactId>
                    <version>${javacpp.version}</version>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>default-javacpp</id>
            <activation>
                <property>
                    <name>!javacpp.platform.extension</name>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.bytedeco</groupId>
                    <artifactId>javacpp</artifactId>
                    <classifier>${dependency.platform}</classifier>
                </dependency>
            </dependencies>
        </profile>


        <profile>
            <id>android-arm-32-output</id>
            <activation>
                <property>
                    <name>javacpp.platform</name>
                    <value>android-arm</value>
                </property>
            </activation>
            <properties>
                <javacpp.build.output.path>${project.build.directory}/classes/lib/armeabi-v7a</javacpp.build.output.path>
            </properties>
        </profile>
        <profile>
            <id>android-arm-64-output</id>
            <activation>
                <property>
                    <name>javacpp.platform</name>
                    <value>android-arm64</value>
                </property>
            </activation>
            <properties>
                <javacpp.build.output.path>${project.build.directory}/classes/lib/arm64-v8a</javacpp.build.output.path>
            </properties>
        </profile>

        <profile>
            <id>android-x86-output</id>
            <activation>
                <property>
                    <name>javacpp.platform</name>
                    <value>android-x86</value>
                </property>
            </activation>
            <properties>
                <javacpp.build.output.path>${project.build.directory}/classes/lib/x86</javacpp.build.output.path>
            </properties>
        </profile>

        <profile>
            <id>android-x86_64-output</id>
            <activation>
                <property>
                    <name>javacpp.platform</name>
                    <value>android-x86_64</value>
                </property>
            </activation>
            <properties>
                <javacpp.build.output.path>${project.build.directory}/classes/lib/x86_64</javacpp.build.output.path>
            </properties>
        </profile>


        <profile>
            <id>avx2</id>
            <activation>
                <property>
                    <name>libnd4j.extension</name>
                    <value>avx2</value>
                </property>
            </activation>
            <properties>
                <javacpp.platform.extension>-avx2</javacpp.platform.extension>
            </properties>
        </profile>
        <profile>
            <id>avx512</id>
            <activation>
                <property>
                    <name>libnd4j.extension</name>
                    <value>avx512</value>
                </property>
            </activation>
            <properties>
                <javacpp.platform.extension>-avx512</javacpp.platform.extension>
            </properties>
        </profile>
        <profile>
            <!--
            Windows note:
               Do NOT use the gcc compiler in usr/ always use the one in mingw/bin
               Another note on this particular gcc: ensure the path is setup in msys2 with /c/msys64/mingw64/bin
               prefixed. If the one in USR is used, errors like error: 'RTLD_LAZY' was not declared in this scope
               may show up.
            -->
            <id>mingw</id>
            <activation>
                <os>
                    <family>windows</family>
                </os>
                <property>
                    <name>!javacpp.platform</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.bytedeco</groupId>
                        <artifactId>javacpp</artifactId>
                        <configuration>
                            <properties>${javacpp.platform}-mingw</properties>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <!--
          Windows note:
             Do NOT use the gcc compiler in usr/ always use the one in mingw/bin
             Another note on this particular gcc: ensure the path is setup in msys2 with /c/msys64/mingw64/bin
             prefixed. If the one in USR is used, errors like error: 'RTLD_LAZY' was not declared in this scope
             may show up.
      -->
            <id>mingw-windows-platform</id>
            <activation>
                <os>
                    <family>windows</family>
                </os>
                <property>
                    <name>javacpp.platform</name>
                    <value>windows-x86_64</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.bytedeco</groupId>
                        <artifactId>javacpp</artifactId>
                        <configuration>
                            <properties>${javacpp.platform}-mingw</properties>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>libnd4j-set</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <property>
                    <name>env.LIBND4J_HOME</name>
                </property>
            </activation>
            <properties>
                <libnd4jhome>${env.LIBND4J_HOME}</libnd4jhome>
            </properties>
        </profile>

        <profile>
            <id>libnd4j-assembly</id>
            <activation>
                <property>
                    <name>libnd4j-assembly</name>
                </property>
            </activation>
            <properties>
                <libnd4jhome>${project.build.directory}/libnd4j/</libnd4jhome>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.eclipse.deeplearning4j</groupId>
                    <artifactId>libnd4j</artifactId>
                    <version>${project.version}</version>
                    <type>zip</type>
                    <classifier>${javacpp.platform}${javacpp.platform.extension}</classifier>
                </dependency>
            </dependencies>
            <build>
                <plugins>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <version>3.1.2</version>
                        <executions>
                            <execution>
                                <id>unpack</id>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>unpack</goal>
                                </goals>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>org.eclipse.deeplearning4j</groupId>
                                            <artifactId>libnd4j</artifactId>
                                            <version>${project.version}</version>
                                            <type>zip</type>
                                            <classifier>
                                                ${javacpp.platform}${javacpp.platform.extension}
                                            </classifier>
                                            <overWrite>true</overWrite>
                                            <outputDirectory>${project.build.directory}
                                            </outputDirectory>
                                        </artifactItem>
                                    </artifactItems>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>
