<?xml version="1.0" encoding="UTF-8"?>
<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>

    <groupId>ai.h2o</groupId>
    <artifactId>libtorch-bundle</artifactId>
    <version>1.9.0-alpha-3</version>

    <parent>
        <groupId>net.kozelka.maven</groupId>
        <artifactId>kozelka-pom</artifactId>
        <version>1.0.0</version>
        <relativePath />
    </parent>
    <properties>
        <!-- NOTE: keep project version consistent with libtorch version ! -->
        <libtorch.version>1.9.0</libtorch.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.pytorch</groupId>
            <artifactId>pytorch_java_only</artifactId>
            <version>${libtorch.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.facebook.soloader</groupId>
            <artifactId>nativeloader</artifactId>
            <version>0.10.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.scijava</groupId>
            <artifactId>native-lib-loader</artifactId>
            <version>2.4.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.32</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.32</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>embed-dependencies</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeScope>provided</includeScope>
                            <excludeArtifactIds>slf4j-api</excludeArtifactIds>
                            <overWriteIfNewer>false</overWriteIfNewer>
                            <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>ai.h2o.libtorch.LibtorchBundle</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>bundle</id>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target xmlns:if="ant:if">
                                <!-- for test, lets pass this one closer -->
                                <copy tofile="target/slf4j-api.jar" file="${org.slf4j:slf4j-api:jar}" verbose="true"/>
                                <!-- make sure that version of this project is related to the target libtorch release -->
                                <fail message="ERROR: libtorch.version '${libtorch.version}' must prefix the project version but it does not: '${project.version}'">
                                    <condition>
                                        <not>
                                            <contains string="@_${project.version}" substring="@_${libtorch.version}"/>
                                        </not>
                                    </condition>
                                </fail>

                                <mkdir dir="lib"/>
                                <copy todir="target/bundles">
                                    <fileset dir="src/main/dependencies"/>
                                </copy>

                                <!-- in release mode, let's bundle all platforms -->
                                <condition property="bundle_all">
                                    <not>
                                        <contains string="${project.version}_X" substring="-SNAPSHOT_X"/>
                                    </not>
                                </condition>

                                <!-- on Linux, bundle linux_64 -->
                                <condition property="do_linux_64">
                                    <or>
                                        <os family="unix"/>
                                        <istrue value="${bundle_all}"/>
                                    </or>
                                </condition>
                                <sequential if:set="do_linux_64">
                                    <get dest="lib" usetimestamp="true" src="https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-${libtorch.version}%2Bcpu.zip"/>
                                    <unzip src="lib/libtorch-shared-with-deps-${libtorch.version}%2Bcpu.zip" dest="target/bundles/linux_64">
                                        <patternset>
                                            <include name="libtorch/lib/libc10.so"/>
                                            <include name="libtorch/lib/libfbjni.so"/>
                                            <include name="libtorch/lib/libgomp-a34b3233.so.1"/>
                                            <include name="libtorch/lib/libpytorch_jni.so"/>
                                            <include name="libtorch/lib/libtorch.so"/>
                                            <include name="libtorch/lib/libtorch_cpu.so"/>
                                        </patternset>
                                        <globmapper from="libtorch/lib/*" to="*"/>
                                    </unzip>
                                    <zip destfile="target/bundles/linux_64.jar" basedir="target/bundles" includes="linux_64/**"/>
                                    <attachartifact file="target/bundles/linux_64.jar" classifier="linux_64" type="jar"/>
                                </sequential>

                                <!-- on Mac, bundle osx_64 -->
                                <condition property="do_osx_64">
                                    <or>
                                        <os family="mac"/>
                                        <istrue value="${bundle_all}"/>
                                    </or>
                                </condition>
                                <sequential if:set="do_osx_64">
                                    <get dest="lib" usetimestamp="true" src="https://download.pytorch.org/libtorch/cpu/libtorch-macos-${libtorch.version}.zip"/>
                                    <unzip src="lib/libtorch-macos-${libtorch.version}.zip" dest="target/bundles/osx_64">
                                        <patternset>
                                            <include name="libtorch/lib/libc10.dylib"/>
                                            <include name="libtorch/lib/libfbjni.dylib"/>
                                            <include name="libtorch/lib/libiomp5.dylib"/>
                                            <include name="libtorch/lib/libpytorch_jni.dylib"/>
                                            <include name="libtorch/lib/libtorch.dylib"/>
                                            <include name="libtorch/lib/libtorch_cpu.dylib"/>
                                        </patternset>
                                        <globmapper from="libtorch/lib/*" to="*"/>
                                    </unzip>
                                    <zip destfile="target/bundles/osx_64.jar" basedir="target/bundles" includes="osx_64/**"/>
                                    <attachartifact file="target/bundles/osx_64.jar" classifier="osx_64" type="jar"/>
                                </sequential>

                                <!-- on Windows, bundle windows_64 -->
                                <condition property="do_windows_64">
                                    <or>
                                        <os family="windows"/>
<!-- TODO: make this work on windows - for now, it's excluded from the release; see https://github.com/pkozelka/libtorch-bundle/issues/1
                                        <istrue value="${bundle_all}"/>
-->
                                    </or>
                                </condition>
                                <sequential if:set="do_windows_64">
                                    <get dest="lib" usetimestamp="true" src="https://download.pytorch.org/libtorch/cpu/libtorch-win-shared-with-deps-${libtorch.version}%2Bcpu.zip"/>
                                    <unzip src="lib/libtorch-win-shared-with-deps-${libtorch.version}%2Bcpu.zip" dest="target/bundles/windows_64">
                                        <patternset>
                                            <include name="libtorch/lib/*.dll"/>
    <!--
                                            <include name="libtorch/lib/c10.dll"/>
                                            <include name="libtorch/lib/fbjni.dll"/>
                                            <include name="libtorch/lib/libiomp5md.dll"/>
                                            <include name="libtorch/lib/pytorch_jni.dll"/>
                                            <include name="libtorch/lib/torch.dll"/>
                                            <include name="libtorch/lib/torch_cpu.dll"/>
    -->
                                        </patternset>
                                        <globmapper from="libtorch/lib/*" to="*"/>
                                    </unzip>
                                    <zip destfile="target/bundles/windows_64.jar" basedir="target/bundles" includes="windows_64/**"/>
                                    <attachartifact file="target/bundles/windows_64.jar" classifier="windows_64" type="jar"/>
                                </sequential>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M7</version>
                <configuration>
                    <additionalClasspathElements>
                        <additionalClasspathElement>target/bundles/linux_64.jar</additionalClasspathElement>
                        <additionalClasspathElement>target/bundles/windows_64.jar</additionalClasspathElement>
                        <additionalClasspathElement>target/bundles/osx_64.jar</additionalClasspathElement>
                    </additionalClasspathElements>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <licenses>
        <license>
            <name>Apache License 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>
    <scm><tag>v1.9.0-alpha-3</tag>
        <connection>scm:git:http://github.com/pkozelka/libtorch-bundle.git</connection>
        <developerConnection>scm:git:git@github.com:pkozelka/libtorch-bundle.git</developerConnection>
        <url>https://github.com/pkozelka/libtorch-bundle</url>
    </scm>
</project>
