<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ Copyright 2016 The Netty Project
  ~
  ~ The Netty Project licenses this file to you under the Apache License,
  ~ version 2.0 (the "License"); you may not use this file except in compliance
  ~ with the License. You may obtain a copy of the License at:
  ~
  ~   http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  ~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  ~ License for the specific language governing permissions and limitations
  ~ under the License.
  -->
<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>io.netty</groupId>
    <artifactId>netty-tcnative-parent</artifactId>
    <version>2.0.54.Final</version>
  </parent>
  <artifactId>netty-tcnative</artifactId>
  <packaging>jar</packaging>

  <name>Netty/TomcatNative [OpenSSL - Dynamic]</name>
  <description>
    A Mavenized fork of Tomcat Native which incorporates various patches. This artifact is dynamically linked
    to OpenSSL and Apache APR.
  </description>

  <properties>
    <compileLibrary>true</compileLibrary>
    <linkStatic>false</linkStatic>
    <nativeSourceDirectory />
    <skipJapicmp>true</skipJapicmp>
    <!-- We need to use the same module name for all our "native" impls as only one should be loaded -->
    <javaModuleName>io.netty.tcnative.openssl.${jniClassifier}</javaModuleName>
  </properties>

  <build>

    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <configuration>
            <archive>
              <manifestEntries>
                <Fragment-Host>io.netty.tcnative-classes</Fragment-Host>
              </manifestEntries>
            </archive>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <!--
        Set the classifier property based on the settings of the os-detector-plugin.
        Fedora-based systems use a different soname for OpenSSL than other linux distributions.
        Use a custom classifier ending in "-fedora" when building on fedora-based systems.
        Systems based on SUSE or Arch Linux build their OpenSSL library a bit differently and also
        require a custom classifier: "-suse" and "-arch".
      -->
      <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}/META-INF/native" />
                  <regexpmapper handledirsep="yes" from="^(?:[^/]+/)*([^/]+)$" to="META-INF/native/\1" />
                </copy>
                <!-- linux / osx -->
                <move todir="${nativeJarWorkdir}/META-INF/native/" flatten="true">
                  <fileset dir="${nativeJarWorkdir}/META-INF/native/" />
                  <globmapper from="libnetty_tcnative.*" to="libnetty_tcnative_${os.detected.name}_${jniArch}.*" />
                </move>
                <!-- windows-->
                <move todir="${nativeJarWorkdir}/META-INF/native/" flatten="true">
                  <fileset dir="${nativeJarWorkdir}/META-INF/native/" />
                  <globmapper from="netty_tcnative.*" to="netty_tcnative_${os.detected.name}_${jniArch}.*" />
                </move>

                <!-- Append the Bundle-NativeCode section -->
                <manifest file="${nativeJarWorkdir}/META-INF/MANIFEST.MF" mode="update">
                  <attribute name="Bundle-NativeCode" value="${tcnativeManifest}" />
                </manifest>

                <jar destfile="${nativeJarFile}" manifest="${nativeJarWorkdir}/META-INF/MANIFEST.MF" basedir="${nativeJarWorkdir}" index="true" excludes="META-INF/MANIFEST.MF,META-INF/INDEX.LIST" />
                <!-- Adjust the classifier used for different OS distributions which provide differently-built openSSL libraries -->
                <condition property="classifier" value="${jniClassifier}-fedora">
                  <isset property="os.detected.release.like.fedora" />
                </condition>
                <condition property="classifier" value="${jniClassifier}-suse">
                  <isset property="os.detected.release.like.suse" />
                </condition>
                <condition property="classifier" value="${jniClassifier}-arch">
                  <isset property="os.detected.release.like.arch" />
                </condition>
                <condition property="classifier" value="${jniClassifier}">
                  <not>
                    <or>
                      <isset property="os.detected.release.like.fedora" />
                      <isset property="os.detected.release.like.suse" />
                      <isset property="os.detected.release.like.arch" />
                    </or>
                  </not>
                </condition>
                <attachartifact file="${nativeJarFile}" classifier="${classifier}" type="jar" />
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <!-- Generate the .so/.dynlib/.dll as part of the build. -->
      <plugin>
        <groupId>org.fusesource.hawtjni</groupId>
        <artifactId>maven-hawtjni-plugin</artifactId>
        <executions>
          <execution>
            <id>build-native-lib</id>
            <configuration>
              <name>netty_tcnative</name>
              <nativeSourceDirectory>${generatedSourcesDir}/c</nativeSourceDirectory>
              <libDirectory>${nativeLibOnlyDir}</libDirectory>
              <forceAutogen>${forceAutogen}</forceAutogen>
              <forceConfigure>${forceConfigure}</forceConfigure>
              <configureArgs>
                <configureArg>--libdir=${project.build.directory}/native-build/target/lib</configureArg>
              </configureArgs>
              <windowsBuildTool>msbuild</windowsBuildTool>
            </configuration>
            <goals>
              <goal>generate</goal>
              <goal>build</goal>
            </goals>
            <phase>compile</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>netty-tcnative-classes</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

  <profiles>
    <!-- Make sure all required environment variables are present on Windows. -->
    <profile>
      <id>enforce-windows-env</id>
      <activation>
        <os>
          <family>windows</family>
        </os>
      </activation>
      <properties>
        <!-- Skip tests on windows as its not easy to load the openssl library we link against in the tests -->
        <skipTests>true</skipTests>
      </properties>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-enforcer-plugin</artifactId>
            <executions>
              <execution>
                <id>enforce-msbuild-environment</id>
                <phase>validate</phase>
                <goals>
                  <goal>enforce</goal>
                </goals>
                <configuration>
                  <rules>
                    <requireEnvironmentVariable>
                      <variableName>APR_INCLUDE_DIR</variableName>
                    </requireEnvironmentVariable>
                    <requireEnvironmentVariable>
                      <variableName>APR_LIB_DIR</variableName>
                    </requireEnvironmentVariable>
                    <requireEnvironmentVariable>
                      <variableName>OPENSSL_INCLUDE_DIR</variableName>
                    </requireEnvironmentVariable>
                    <requireEnvironmentVariable>
                      <variableName>OPENSSL_LIB_DIR</variableName>
                    </requireEnvironmentVariable>
                  </rules>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>

    <!-- Make sure all required environment variables are present on Windows. -->
    <profile>
      <id>mac-x86_64</id>
      <activation>
        <os>
          <family>mac</family>
          <arch>x86_64</arch>
        </os>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.fusesource.hawtjni</groupId>
            <artifactId>maven-hawtjni-plugin</artifactId>
            <executions>
              <execution>
                <id>build-native-lib</id>
                <configuration>
                  <name>netty_tcnative</name>
                  <nativeSourceDirectory>${generatedSourcesDir}/c</nativeSourceDirectory>
                  <libDirectory>${nativeLibOnlyDir}</libDirectory>
                  <forceAutogen>${forceAutogen}</forceAutogen>
                  <forceConfigure>${forceConfigure}</forceConfigure>
                  <configureArgs>
                    <configureArg>${macOsxDeploymentTarget}</configureArg>
                    <configureArg>--with-apr=/usr/local/opt/apr/</configureArg>
                    <configureArg>--with-ssl=/usr/local/opt/openssl@1.1/</configureArg>
                  </configureArgs>
                </configuration>
                <goals>
                  <goal>generate</goal>
                  <goal>build</goal>
                </goals>
                <phase>compile</phase>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>mac-aarch64</id>
      <activation>
        <os>
          <family>mac</family>
          <arch>aarch64</arch>
        </os>
      </activation>
      <build>
        <plugins>
          <plugin>
            <groupId>org.fusesource.hawtjni</groupId>
            <artifactId>maven-hawtjni-plugin</artifactId>
            <executions>
              <execution>
                <id>build-native-lib</id>
                <configuration>
                  <name>netty_tcnative</name>
                  <nativeSourceDirectory>${generatedSourcesDir}/c</nativeSourceDirectory>
                  <libDirectory>${nativeLibOnlyDir}</libDirectory>
                  <forceAutogen>${forceAutogen}</forceAutogen>
                  <forceConfigure>${forceConfigure}</forceConfigure>
                  <configureArgs>
                    <configureArg>${macOsxDeploymentTarget}</configureArg>
                    <configureArg>--with-apr=/opt/homebrew/opt/apr/</configureArg>
                    <configureArg>--with-ssl=/opt/homebrew/opt/openssl@1.1/</configureArg>
                  </configureArgs>
                </configuration>
                <goals>
                  <goal>generate</goal>
                  <goal>build</goal>
                </goals>
                <phase>compile</phase>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>linux-aarch64</id>

      <properties>
        <!-- use aarch_64 as this is also what os.detected.arch will use on an aarch64 system -->
        <nativeJarFile>${project.build.directory}/${project.build.finalName}-${os.detected.name}-aarch_64.jar</nativeJarFile>
        <jniClassifier>${os.detected.name}-aarch_64</jniClassifier>
        <nativeLibOsParts>${os.detected.name}_aarch_64</nativeLibOsParts>
        <jniArch>aarch_64</jniArch>
        <javaModuleNameClassifier>${os.detected.name}.aarch_64</javaModuleNameClassifier>
      </properties>

      <build>
        <pluginManagement>
          <plugins>
            <plugin>
              <artifactId>maven-jar-plugin</artifactId>
              <configuration>
                <archive>
                  <manifestEntries>
                    <Automatic-Module-Name>io.netty.tcnative.openssl.dynamic</Automatic-Module-Name>
                  </manifestEntries>
                </archive>
              </configuration>
            </plugin>
            <plugin>
              <artifactId>maven-enforcer-plugin</artifactId>
              <version>1.4.1</version>
              <dependencies>
                <!-- Provides the 'requireFilesContent' enforcer rule. -->
                <dependency>
                  <groupId>com.ceilfors.maven.plugin</groupId>
                  <artifactId>enforcer-rules</artifactId>
                  <version>1.2.0</version>
                </dependency>
              </dependencies>
            </plugin>
          </plugins>
        </pluginManagement>

        <plugins>
          <plugin>
            <artifactId>maven-enforcer-plugin</artifactId>
            <executions>
              <execution>
                <id>enforce-release-environment</id>
                <goals>
                  <goal>enforce</goal>
                </goals>
                <configuration>
                  <rules>
                    <requireProperty>
                      <regexMessage>
                        Cross compile and Release process must be performed on linux-x86_64.
                      </regexMessage>
                      <property>os.detected.classifier</property>
                      <regex>^linux-x86_64.*</regex>
                    </requireProperty>
                    <requireFilesContent>
                      <message>
                        Cross compile and Release process must be performed on RHEL 7.6 or its derivatives.
                      </message>
                      <files>
                        <file>/etc/redhat-release</file>
                      </files>
                      <content>release 7.6</content>
                    </requireFilesContent>
                    <requireProperty>
                      <property>aprArmHome</property>
                      <message>The folder of APR for aarch64 must be specified by hand. Please try -DaprArmHome=</message>
                    </requireProperty>
                    <requireProperty>
                      <property>opensslArmHome</property>
                      <message>The folder of OpenSSL for aarch64 must be specified by hand. Please try -DopensslArmHome=</message>
                    </requireProperty>
                  </rules>
                  <ignoreCache>true</ignoreCache>
                </configuration>
              </execution>
            </executions>
          </plugin>

          <!-- Generate the .so/.dynlib/.dll as part of the build. -->
          <plugin>
            <groupId>org.fusesource.hawtjni</groupId>
            <artifactId>maven-hawtjni-plugin</artifactId>
            <executions>
              <execution>
                <id>build-native-lib</id>
                <configuration>
                  <name>netty_tcnative</name>
                  <nativeSourceDirectory>${generatedSourcesDir}/c</nativeSourceDirectory>
                  <libDirectory>${nativeLibOnlyDir}</libDirectory>
                  <forceAutogen>${forceAutogen}</forceAutogen>
                  <forceConfigure>${forceConfigure}</forceConfigure>
                  <configureArgs>
                    <configureArg>--libdir=${project.build.directory}/native-build/target/lib</configureArg>
                    <configureArg>--with-apr=${aprArmHome}</configureArg>
                    <configureArg>--with-ssl=${opensslArmHome}</configureArg>
                    <configureArg>--host=aarch64-linux-gnu</configureArg>
                    <configureArg>CC=aarch64-none-linux-gnu-gcc</configureArg>
                  </configureArgs>
                </configuration>
                <goals>
                  <goal>generate</goal>
                  <goal>build</goal>
                </goals>
                <phase>compile</phase>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
