<?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.2.Final</version>
  </parent>
  <artifactId>netty-tcnative-openssl-static</artifactId>
  <packaging>jar</packaging>

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

  <properties>
    <opensslBuildDir>${project.build.directory}/openssl-${opensslVersion}</opensslBuildDir>
    <linkStatic>true</linkStatic>
    <!-- Don't deploy this artifact to Maven Central -->
    <maven.deploy.skip>true</maven.deploy.skip>
  </properties>

  <build>
    <plugins>
      <!-- Add the OpenSSL version to the manifest. -->
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <configuration>
          <instructions>
            <Apr-Version>${aprVersion}</Apr-Version>
            <OpenSSL-Version>${opensslVersion}</OpenSSL-Version>
          </instructions>
        </configuration>
      </plugin>

      <!-- Configure the distribution statically linked against OpenSSL and APR -->
      <plugin>
        <groupId>org.fusesource.hawtjni</groupId>
        <artifactId>maven-hawtjni-plugin</artifactId>
        <executions>
          <execution>
            <id>build-native-lib</id>
            <goals>
              <goal>generate</goal>
              <goal>build</goal>
            </goals>
            <phase>compile</phase>
            <configuration>
              <name>netty-tcnative</name>
              <nativeSourceDirectory>${project.basedir}/src/main/c</nativeSourceDirectory>
              <libDirectory>${nativeLibOnlyDir}</libDirectory>
              <forceAutogen>${forceAutogen}</forceAutogen>
              <forceConfigure>${forceConfigure}</forceConfigure>
              <windowsBuildTool>msbuild</windowsBuildTool>
              <configureArgs>
                <configureArg>--with-ssl=${sslHome}</configureArg>
                <configureArg>--with-apr=${aprHome}</configureArg>
              </configureArgs>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <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>
                <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="${os.detected.classifier}" type="jar" />
              </target>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <profiles>
    <profile>
      <id>build-openssl-windows</id>
      <activation>
        <os>
          <family>windows</family>
        </os>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>build-openssl</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <target>
                    <!-- Add the ant tasks from ant-contrib -->
                    <taskdef resource="net/sf/antcontrib/antcontrib.properties" />

                    <!-- Download the openssl source. -->
                    <get  src="http://192.168.2.16:8090/download/o/openssl/openssl-${opensslVersion}.tar.gz"  dest="${project.build.directory}/openssl-${opensslVersion}.tar.gz"   verbose="on"   />
                    <checksum file="${project.build.directory}/openssl-${opensslVersion}.tar.gz" algorithm="SHA-256" property="${opensslSha256}" verifyProperty="isEqual" />
                    <gunzip src="${project.build.directory}/openssl-${opensslVersion}.tar.gz" dest="${project.build.directory}/" />
                    <untar src="${project.build.directory}/openssl-${opensslVersion}.tar" dest="${project.build.directory}/" />

                    <!-- Build for the correct platform -->
                    <pathconvert property="sslHomePath" targetos="windows">
                      <path location="${sslHome}" />
                    </pathconvert>
                    <if>
                      <equals arg1="${archBits}" arg2="32" />
                      <then>
                        <echo message="Building OpenSSL for Win32" />
                        <exec executable="perl" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true">
                          <arg line="Configure VC-WIN32 --prefix=${sslHomePath}" />
                        </exec>
                        <exec executable="${opensslBuildDir}/ms/do_ms.bat" failonerror="true" dir="${opensslBuildDir}" />
                        <exec executable="${opensslBuildDir}/ms/do_nasm.bat" failonerror="true" dir="${opensslBuildDir}" />
                        <exec executable="nmake" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true">
                          <arg line="-f ms\nt.mak all install" />
                        </exec>
                      </then>
                      <else>
                        <echo message="Building OpenSSL for Win64" />
                        <exec executable="perl" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true">
                          <arg line="Configure VC-WIN64A --prefix=${sslHome}" />
                        </exec>
                        <exec executable="${opensslBuildDir}/ms/do_win64a.bat" failonerror="true" dir="${opensslBuildDir}" />
                        <exec executable="nmake" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true">
                          <arg line="-f ms\nt.mak all install" />
                        </exec>
                      </else>
                    </if>
                  </target>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>build-openssl-linux</id>
      <activation>
        <os>
          <family>unix</family>
        </os>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>build-openssl</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <target>
                    <!-- Download the openssl source. -->
                    <get  src="http://192.168.2.16:8090/download/o/openssl/openssl-${opensslVersion}.tar.gz"  dest="${project.build.directory}/openssl-${opensslVersion}.tar.gz"   verbose="on"   />
                    <!-- Use the tar command (rather than the untar ant task) in order to preserve file permissions. -->
                    <exec executable="tar" failonerror="true" dir="${project.build.directory}/" resolveexecutable="true">
                      <arg line="xfvz openssl-${opensslVersion}.tar.gz" />
                    </exec>

                    <mkdir dir="${sslHome}" />
                    <exec executable="config" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true">
                      <arg line="-O3 -fno-omit-frame-pointer -fPIC no-ssl2 no-ssl3 no-shared no-comp -DOPENSSL_NO_HEARTBEATS --prefix=${sslHome} --openssldir=${sslHome}" />
                    </exec>
                    <exec executable="make" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true">
                      <arg value="depend" />
                    </exec>
                    <exec executable="make" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true" />
                    <exec executable="make" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true">
                      <arg value="install" />
                    </exec>
                  </target>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    <profile>
      <id>build-openssl-mac</id>
      <activation>
        <os>
          <family>mac</family>
        </os>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
              <execution>
                <id>build-openssl</id>
                <phase>generate-sources</phase>
                <goals>
                  <goal>run</goal>
                </goals>
                <configuration>
                  <target>
                    <!-- Download the openssl source. -->
                    <get  src="http://192.168.2.16:8090/download/o/openssl/openssl-${opensslVersion}.tar.gz"  dest="${project.build.directory}/openssl-${opensslVersion}.tar.gz"   verbose="on"   />
                    <!-- Use the tar command (rather than the untar ant task) in order to preserve file permissions. -->
                    <exec executable="tar" failonerror="true" dir="${project.build.directory}/" resolveexecutable="true">
                      <arg line="xfvz openssl-${opensslVersion}.tar.gz" />
                    </exec>

                    <mkdir dir="${sslHome}" />
                    <exec executable="Configure" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true">
                      <arg line="darwin64-x86_64-cc -O3 -fno-omit-frame-pointer -fPIC no-ssl2 no-ssl3 no-shared no-comp -DOPENSSL_NO_HEARTBEATS --prefix=${sslHome} --openssldir=${sslHome}" />
                    </exec>
                    <exec executable="make" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true">
                      <arg value="depend" />
                    </exec>
                    <exec executable="make" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true" />
                    <exec executable="make" failonerror="true" dir="${opensslBuildDir}" resolveexecutable="true">
                      <arg value="install" />
                    </exec>
                  </target>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>
