<?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>
    <artifactId>natives</artifactId>
    <groupId>com.nukkitx</groupId>
    <version>1.0.3</version>
    <name>Natives</name>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <jni.classifier>${os.detected.name}-${os.detected.arch}</jni.classifier>

        <exe.make>make</exe.make>
        <exe.compiler>gcc</exe.compiler>
        <exe.archiver>ar</exe.archiver>
        <nativeLibName>libnukkit-natives</nativeLibName>
        <nativeIncludeDir>${project.basedir}/src/main/c</nativeIncludeDir>
        <nativeJarWorkdir>${project.build.directory}/native-jar-work</nativeJarWorkdir>
        <nativeObjsOnlyDir>${project.build.directory}/native-objs-only</nativeObjsOnlyDir>
        <nativeLibOnlyDir>${project.build.directory}/native-lib-only</nativeLibOnlyDir>
        <defaultJarFile>${project.build.directory}/${project.build.finalName}.jar</defaultJarFile>
        <nativeJarFile>${project.build.directory}/${project.build.finalName}-${jni.classifier}.jar</nativeJarFile>
    </properties>

    <distributionManagement>
        <repository>
            <id>releases</id>
            <name>nukkitx-releases</name>
            <url>https://repo.nukkitx.com/release</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <name>nukkitx-snapshots</name>
            <url>https://repo.nukkitx.com/snapshot</url>
        </snapshotRepository>
    </distributionManagement>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.2</version>
            </extension>
        </extensions>

        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <compilerVersion>11</compilerVersion>
                    <fork>true</fork>
                    <debug>true</debug>
                    <optimize>true</optimize>
                    <showDeprecation>true</showDeprecation>
                    <showWarnings>true</showWarnings>
                    <compilerArgument>-Xlint:-options</compilerArgument>
                    <meminitial>256m</meminitial>
                    <maxmem>1024m</maxmem>
                    <excludes>
                        <exclude>**/package-info.java</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Automatic-Module-Name>com.nukkitx.natives</Automatic-Module-Name>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>mac</id>
            <activation>
                <os>
                    <family>mac</family>
                </os>
            </activation>
            <properties>
                <exe.compiler>clang</exe.compiler>
                <jni.platform>darwin</jni.platform>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <!-- Build the additional JAR that contains the native library. -->
                            <execution>
                                <id>native-jar</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <copy todir="${nativeJarWorkdir}">
                                            <zipfileset src="${defaultJarFile}"/>
                                        </copy>
                                        <copy todir="${nativeJarWorkdir}" includeEmptyDirs="false">
                                            <zipfileset dir="${nativeLibOnlyDir}"/>
                                            <regexpmapper handledirsep="yes" from="^(?:[^/]+/)*([^/]+)$"
                                                          to="META-INF/native/lib/\1"/>
                                        </copy>
                                        <copy todir="${nativeJarWorkdir}" includeEmptyDirs="false">
                                            <zipfileset dir="${nativeIncludeDir}"/>
                                            <regexpmapper handledirsep="yes" from="^(?:[^/]+/)*([^/]+).h$"
                                                          to="META-INF/native/include/\1.h"/>
                                        </copy>
                                        <jar destfile="${nativeJarFile}"
                                             manifest="${nativeJarWorkdir}/META-INF/MANIFEST.MF"
                                             basedir="${nativeJarWorkdir}" index="true"
                                             excludes="META-INF/MANIFEST.MF,META-INF/INDEX.LIST"/>
                                        <attachartifact file="${nativeJarFile}" classifier="${jni.classifier}"
                                                        type="jar"/>
                                    </target>
                                </configuration>
                            </execution>
                            <!-- invoke the make file to build a static library -->
                            <execution>
                                <id>build-native-lib</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <exec executable="${exe.make}" failonerror="true" resolveexecutable="true">
                                            <env key="CC" value="${exe.compiler}"/>
                                            <env key="AR" value="${exe.archiver}"/>
                                            <env key="LIB_DIR" value="${nativeLibOnlyDir}"/>
                                            <env key="OBJ_DIR" value="${nativeObjsOnlyDir}"/>
                                            <env key="JNI_PLATFORM" value="${jni.platform}"/>
                                            <env key="CFLAGS"
                                                 value="-O3 -Werror -Wno-attributes -fPIC -fno-omit-frame-pointer -Wunused-variable -fvisibility=hidden"/>
                                            <env key="LDFLAGS" value="-Wl,--no-as-needed -lrt"/>
                                            <env key="LIB_NAME" value="${nativeLibName}"/>
                                            <!-- support for __attribute__((weak_import)) by the linker was added in 10.2 so ensure we
                                                 explicitly set the target platform. Otherwise we may get fatal link errors due to weakly linked
                                                 methods which are not expected to be present on MacOS (e.g. accept4). -->
                                            <env key="MACOSX_DEPLOYMENT_TARGET" value="10.2"/>
                                        </exec>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>linux</id>
            <activation>
                <os>
                    <family>linux</family>
                </os>
            </activation>
            <properties>
                <jni.platform>linux</jni.platform>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <!-- Build the additional JAR that contains the native library. -->
                            <execution>
                                <id>native-jar</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <copy todir="${nativeJarWorkdir}">
                                            <zipfileset src="${defaultJarFile}"/>
                                        </copy>
                                        <copy todir="${nativeJarWorkdir}" includeEmptyDirs="false">
                                            <zipfileset dir="${nativeLibOnlyDir}"/>
                                            <regexpmapper handledirsep="yes" from="^(?:[^/]+/)*([^/]+)$"
                                                          to="META-INF/native/lib/\1"/>
                                        </copy>
                                        <copy todir="${nativeJarWorkdir}" includeEmptyDirs="false">
                                            <zipfileset dir="${nativeIncludeDir}"/>
                                            <regexpmapper handledirsep="yes" from="^(?:[^/]+/)*([^/]+).h$"
                                                          to="META-INF/native/include/\1.h"/>
                                        </copy>
                                        <jar destfile="${nativeJarFile}"
                                             manifest="${nativeJarWorkdir}/META-INF/MANIFEST.MF"
                                             basedir="${nativeJarWorkdir}" index="true"
                                             excludes="META-INF/MANIFEST.MF,META-INF/INDEX.LIST"/>
                                        <attachartifact file="${nativeJarFile}" classifier="${jni.classifier}"
                                                        type="jar"/>
                                    </target>
                                </configuration>
                            </execution>
                            <!-- invoke the make file to build a static library -->
                            <execution>
                                <id>build-native-lib</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <exec executable="${exe.make}" failonerror="true" resolveexecutable="true">
                                            <env key="CC" value="${exe.compiler}"/>
                                            <env key="AR" value="${exe.archiver}"/>
                                            <env key="LIB_DIR" value="${nativeLibOnlyDir}"/>
                                            <env key="OBJ_DIR" value="${nativeObjsOnlyDir}"/>
                                            <env key="JNI_PLATFORM" value="${jni.platform}"/>
                                            <env key="CFLAGS"
                                                 value="-O3 -Werror -Wno-attributes -fPIC -fno-omit-frame-pointer -Wunused-variable -fvisibility=hidden"/>
                                            <env key="LDFLAGS" value="-Wl,--no-as-needed -lrt"/>
                                            <env key="LIB_NAME" value="${nativeLibName}"/>
                                        </exec>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>freebsd</id>
            <activation>
                <os>
                    <family>unix</family>
                    <name>freebsd</name>
                </os>
            </activation>
            <properties>
                <exe.compiler>clang</exe.compiler>
                <exe.make>gmake</exe.make>
                <jni.platform>freebsd</jni.platform>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <!-- Build the additional JAR that contains the native library. -->
                            <execution>
                                <id>native-jar</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <copy todir="${nativeJarWorkdir}">
                                            <zipfileset src="${defaultJarFile}"/>
                                        </copy>
                                        <copy todir="${nativeJarWorkdir}" includeEmptyDirs="false">
                                            <zipfileset dir="${nativeLibOnlyDir}"/>
                                            <regexpmapper handledirsep="yes" from="^(?:[^/]+/)*([^/]+)$"
                                                          to="META-INF/native/lib/\1"/>
                                        </copy>
                                        <copy todir="${nativeJarWorkdir}" includeEmptyDirs="false">
                                            <zipfileset dir="${nativeIncludeDir}"/>
                                            <regexpmapper handledirsep="yes" from="^(?:[^/]+/)*([^/]+).h$"
                                                          to="META-INF/native/include/\1.h"/>
                                        </copy>
                                        <jar destfile="${nativeJarFile}"
                                             manifest="${nativeJarWorkdir}/META-INF/MANIFEST.MF"
                                             basedir="${nativeJarWorkdir}" index="true"
                                             excludes="META-INF/MANIFEST.MF,META-INF/INDEX.LIST"/>
                                        <attachartifact file="${nativeJarFile}" classifier="${jni.classifier}"
                                                        type="jar"/>
                                    </target>
                                </configuration>
                            </execution>
                            <!-- invoke the make file to build a static library -->
                            <execution>
                                <id>build-native-lib</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <exec executable="${exe.make}" failonerror="true" resolveexecutable="true">
                                            <env key="CC" value="${exe.compiler}"/>
                                            <env key="AR" value="${exe.archiver}"/>
                                            <env key="LIB_DIR" value="${nativeLibOnlyDir}"/>
                                            <env key="OBJ_DIR" value="${nativeObjsOnlyDir}"/>
                                            <env key="JNI_PLATFORM" value="${jni.platform}"/>
                                            <env key="CFLAGS"
                                                 value="-O3 -Werror -Wno-attributes -fPIC -fno-omit-frame-pointer -Wunused-variable -fvisibility=hidden"/>
                                            <env key="LDFLAGS" value="-Wl,--no-as-needed -lrt"/>
                                            <env key="LIB_NAME" value="${nativeLibName}"/>
                                        </exec>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>openbsd</id>
            <activation>
                <os>
                    <family>unix</family>
                    <name>openbsd</name>
                </os>
            </activation>
            <properties>
                <exe.compiler>clang</exe.compiler>
                <exe.make>gmake</exe.make>
                <jni.platform>openbsd</jni.platform>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <!-- Build the additional JAR that contains the native library. -->
                            <execution>
                                <id>native-jar</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <copy todir="${nativeJarWorkdir}">
                                            <zipfileset src="${defaultJarFile}"/>
                                        </copy>
                                        <copy todir="${nativeJarWorkdir}" includeEmptyDirs="false">
                                            <zipfileset dir="${nativeLibOnlyDir}"/>
                                            <regexpmapper handledirsep="yes" from="^(?:[^/]+/)*([^/]+)$"
                                                          to="META-INF/native/lib/\1"/>
                                        </copy>
                                        <copy todir="${nativeJarWorkdir}" includeEmptyDirs="false">
                                            <zipfileset dir="${nativeIncludeDir}"/>
                                            <regexpmapper handledirsep="yes" from="^(?:[^/]+/)*([^/]+).h$"
                                                          to="META-INF/native/include/\1.h"/>
                                        </copy>
                                        <jar destfile="${nativeJarFile}"
                                             manifest="${nativeJarWorkdir}/META-INF/MANIFEST.MF"
                                             basedir="${nativeJarWorkdir}" index="true"
                                             excludes="META-INF/MANIFEST.MF,META-INF/INDEX.LIST"/>
                                        <attachartifact file="${nativeJarFile}" classifier="${jni.classifier}"
                                                        type="jar"/>
                                    </target>
                                </configuration>
                            </execution>
                            <!-- invoke the make file to build a static library -->
                            <execution>
                                <id>build-native-lib</id>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <exec executable="${exe.make}" failonerror="true" resolveexecutable="true">
                                            <env key="CC" value="${exe.compiler}"/>
                                            <env key="AR" value="${exe.archiver}"/>
                                            <env key="LIB_DIR" value="${nativeLibOnlyDir}"/>
                                            <env key="OBJ_DIR" value="${nativeObjsOnlyDir}"/>
                                            <env key="JNI_PLATFORM" value="${jni.platform}"/>
                                            <env key="CFLAGS"
                                                 value="-O3 -Werror -Wno-attributes -fPIC -fno-omit-frame-pointer -Wunused-variable -fvisibility=hidden"/>
                                            <env key="LDFLAGS" value="-Wl,--no-as-needed -lrt"/>
                                            <env key="LIB_NAME" value="${nativeLibName}"/>
                                        </exec>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>