netty5/transport-native-unix-common/pom.xml
Norman Maurer 7fb62a93b8
Ensure we only register native methods once (#10876)
Motivation:

We need to ensure we only register native methods once as otherwise we may end up in an "invalid" state. The problem here was that before it was basically the responsibility the user of transport-native-unix-common to register the methods. This is error prone as there may be multiple users of these on the classpath at the same time.

Modifications:

- Provide a way to init native lib without register the native methods of the provided classes. This is needed to be able to re-use functionality which is exposed to our internal native code
- Use flatten plugin to correctly resolve classifier and so have the correct dependency
- Call Unix.* method to ensure we register the methods correctly once
- Include native lib as well in the native jars of unix-common

Result:

Be able to have multiple artifacts of the classpath that depends on the unix-common. Related to https://github.com/netty/netty-incubator-transport-io_uring/issues/15
2020-12-18 10:37:49 +01:00

309 lines
12 KiB
XML

<?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:
~
~ https://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 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.netty</groupId>
<artifactId>netty-parent</artifactId>
<version>4.1.57.Final-SNAPSHOT</version>
</parent>
<artifactId>netty-transport-native-unix-common</artifactId>
<name>Netty/Transport/Native/Unix/Common</name>
<packaging>jar</packaging>
<description>
Static library which contains common unix utilities.
</description>
<properties>
<javaModuleName>io.netty.transport.unix.common</javaModuleName>
<exe.make>make</exe.make>
<exe.compiler>gcc</exe.compiler>
<exe.archiver>ar</exe.archiver>
<nativeLibName>libnetty-unix-common</nativeLibName>
<nativeIncludeDir>${project.basedir}/src/main/c</nativeIncludeDir>
<jniUtilCheckoutDir>${project.build.directory}/netty-jni-util</jniUtilCheckoutDir>
<jniUtilIncludeDir>${project.build.directory}/netty-jni-util/src/c</jniUtilIncludeDir>
<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>
<generatedSourcesDir>${project.build.directory}/generated-sources</generatedSourcesDir>
<cflags>-O3 -Werror -Wno-attributes -fPIC -fno-omit-frame-pointer -Wunused-variable -fvisibility=hidden -I${project.basedir}/src/main/c</cflags>
<ldflags>-Wl,--no-as-needed -lrt</ldflags>
<nativeLibArch>${os.detected.arch}</nativeLibArch>
<build.native>false</build.native>
</properties>
<build>
<plugins>
<!-- Download the netty-jni-util source -->
<plugin>
<artifactId>maven-scm-plugin</artifactId>
<version>1.11.2</version>
<executions>
<execution>
<id>get-netty-jni-util</id>
<phase>generate-sources</phase>
<goals>
<goal>checkout</goal>
</goals>
<configuration>
<checkoutDirectory>${jniUtilCheckoutDir}</checkoutDirectory>
<connectionType>developerConnection</connectionType>
<developerConnectionUrl>scm:git:https://github.com/netty/netty-jni-util.git</developerConnectionUrl>
<scmVersion>0.0.1</scmVersion>
<scmVersionType>tag</scmVersionType>
<skipCheckoutIfExists>true</skipCheckoutIfExists>
</configuration>
</execution>
</executions>
</plugin>
<!-- Also include c files in source jar -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${nativeIncludeDir}</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>mac</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<properties>
<build.native>true</build.native>
<exe.compiler>clang</exe.compiler>
<jni.platform>darwin</jni.platform>
</properties>
</profile>
<profile>
<id>linux</id>
<activation>
<os>
<family>linux</family>
</os>
</activation>
<properties>
<jni.platform>linux</jni.platform>
</properties>
</profile>
<profile>
<id>linux-aarch64</id>
<properties>
<!-- use aarch_64 as this is also what os.detected.arch will use on an aarch64 system -->
<nativeLibArch>aarch_64</nativeLibArch>
<jni.classifier>${os.detected.name}-aarch_64</jni.classifier>
<jni.platform>linux</jni.platform>
<exe.compiler>aarch64-linux-gnu-gcc</exe.compiler>
<exe.archiver>aarch64-linux-gnu-ar</exe.archiver>
</properties>
</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>
</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>
</profile>
<profile>
<id>build.native</id>
<activation>
<os>
<family>!windows</family>
</os>
</activation>
<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}" />
<!-- Only one level as we don't want to include the internal headers-->
<regexpmapper handledirsep="yes" from="^([^/]+).h$" to="META-INF/native/include/\1.h" />
</copy>
<copy todir="${nativeJarWorkdir}" includeEmptyDirs="false">
<zipfileset dir="${jniUtilIncludeDir}" />
<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="${cflags}" />
<env key="LDFLAGS" value="${ldflags}" />
<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>
<execution>
<id>copy-src</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- Copy all of the c code -->
<delete dir="${generatedSourcesDir}" quiet="true" />
<copy todir="${generatedSourcesDir}/c" verbose="true">
<fileset dir="${project.basedir}/src/main/c" />
</copy>
<copy todir="${generatedSourcesDir}/c" verbose="true">
<fileset dir="${jniUtilIncludeDir}" />
</copy>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.fusesource.hawtjni</groupId>
<artifactId>maven-hawtjni-plugin</artifactId>
<executions>
<execution>
<id>build-native-lib</id>
<configuration>
<name>netty_transport_native_unix_${os.detected.name}_${nativeLibArch}</name>
<nativeSourceDirectory>${generatedSourcesDir}</nativeSourceDirectory>
<libDirectory>${project.build.outputDirectory}</libDirectory>
<!-- We use Maven's artifact classifier instead.
This hack will make the hawtjni plugin to put the native library
under 'META-INF/native' rather than 'META-INF/native/${platform}'. -->
<platform>.</platform>
<forceConfigure>true</forceConfigure>
<forceAutogen>true</forceAutogen>
<configureArgs>
<arg>MACOSX_DEPLOYMENT_TARGET=10.2</arg>
<arg>CFLAGS=${cflags}</arg>
</configureArgs>
</configuration>
<goals>
<goal>generate</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-buffer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>