Skip to content

Commit

Permalink
Fix cross-compiling on macos and also ensure CFLAGS / LDFLAGS are cor…
Browse files Browse the repository at this point in the history
…rectly set (java-native-access#406)


Motivation:

Our cross-compilation on mac did not work correctly and produced a non working lib.

Modifications:

Fix cross compilation by using a custom m4 template

Result:

cross compile works as expected
  • Loading branch information
normanmaurer committed Jul 4, 2022
1 parent 9f635c8 commit 8f1ec1b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 26 deletions.
65 changes: 39 additions & 26 deletions codec-native-quic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
<generatedSourcesDir>${project.build.directory}/generated-sources</generatedSourcesDir>
<templateDir>${project.build.directory}/template</templateDir>
<cargoTarget />
<cflags>-std=gnu99 -Werror -fno-omit-frame-pointer -fvisibility=hidden -Wunused -Wno-unused-value -O3 -I${quicheHomeIncludeDir} -I${boringsslHomeIncludeDir}</cflags>
<ldflags>-L${quicheHomeBuildDir} -lquiche -L${boringsslHomeBuildDir} -lssl -lcrypto</ldflags>
<cflags />
<ldflags />
<extraCmakeFlags />
<extraCflags />
<extraCxxflags />
Expand Down Expand Up @@ -114,17 +114,15 @@
Symbol not found: ___isPlatformVersionAtLeast
-->
<macosxDeploymentTarget>10.12</macosxDeploymentTarget>
<extraCflags>-O3 -fno-omit-frame-pointer</extraCflags>
<extraCxxflags>-O3 -fno-omit-frame-pointer</extraCxxflags>
<!-- On *nix, add ASM flags to disable executable stack -->
<cmakeAsmFlags>-Wa,--noexecstack</cmakeAsmFlags>
<cmakeCFlags>${extraCflags} -DOPENSSL_C11_ATOMIC</cmakeCFlags>
<cmakeCFlags>${extraCflags} -O3 -fno-omit-frame-pointer -DOPENSSL_C11_ATOMIC</cmakeCFlags>
<!-- We need to define __STDC_CONSTANT_MACROS and __STDC_FORMAT_MACROS when building boringssl on centos 6 -->
<cmakeCxxFlags>${extraCflags} -DOPENSSL_C11_ATOMIC -Wno-error=range-loop-analysis</cmakeCxxFlags>
<cmakeCxxFlags>${extraCxxflags} -O3 -fno-omit-frame-pointer -DOPENSSL_C11_ATOMIC -Wno-error=range-loop-analysis</cmakeCxxFlags>
<libssl>libssl.a</libssl>
<libcrypto>libcrypto.a</libcrypto>
<libquiche>libquiche.a</libquiche>
<extraLdflags>-Wl,-exported_symbol,_JNI_* -Wl,-platform_version,macos,${macosxDeploymentTarget},${macosxDeploymentTarget}</extraLdflags>
<extraLdflags>-platform_version,macos,${macosxDeploymentTarget},${macosxDeploymentTarget}</extraLdflags>
<extraConfigureArg>MACOSX_DEPLOYMENT_TARGET=${macosxDeploymentTarget}</extraConfigureArg>
<bundleNativeCode>META-INF/native/lib${jniLibName}.jnilib;osname=macos;osname=macosx;processor=${os.detected.arch}</bundleNativeCode>
</properties>
Expand All @@ -136,19 +134,20 @@
<jni.classifier>osx-aarch_64</jni.classifier>
<javaModuleNameWithClassifier>osx.aarch_64</javaModuleNameWithClassifier>
<macosxDeploymentTarget>11.0</macosxDeploymentTarget>
<extraCflags>-O3 -fno-omit-frame-pointer -target arm64-apple-macos11</extraCflags>
<extraCxxflags>-O3 -fno-omit-frame-pointer -target arm64-apple-macos11</extraCxxflags>
<extraCflags>-target arm64-apple-macos11</extraCflags>
<extraCxxflags>-target arm64-apple-macos11</extraCxxflags>
<!-- On *nix, add ASM flags to disable executable stack -->
<cmakeAsmFlags>-Wa,--noexecstack -target arm64-apple-macos11</cmakeAsmFlags>
<extraCmakeFlags>-DCMAKE_SYSTEM_PROCESSOR=arm64 -DCMAKE_OSX_ARCHITECTURES=arm64</extraCmakeFlags>
<cmakeCFlags>${extraCflags} -DOPENSSL_C11_ATOMIC</cmakeCFlags>
<cmakeCFlags>${extraCflags} -O3 -fno-omit-frame-pointer -DOPENSSL_C11_ATOMIC</cmakeCFlags>
<!-- We need to define __STDC_CONSTANT_MACROS and __STDC_FORMAT_MACROS when building boringssl on centos 6 -->
<cmakeCxxFlags>${extraCflags} -DOPENSSL_C11_ATOMIC -Wno-error=range-loop-analysis</cmakeCxxFlags>
<cmakeCxxFlags>${extraCxxflags} -O3 -fno-omit-frame-pointer -DOPENSSL_C11_ATOMIC -Wno-error=range-loop-analysis</cmakeCxxFlags>
<libssl>libssl.a</libssl>
<libcrypto>libcrypto.a</libcrypto>
<libquiche>libquiche.a</libquiche>
<extraLdflags>-arch arm64 -Wl,-exported_symbol,_JNI_* -Wl,-platform_version,macos,${macosxDeploymentTarget},${macosxDeploymentTarget}</extraLdflags>
<extraLdflags>-arch arm64 -platform_version,macos,${macosxDeploymentTarget},${macosxDeploymentTarget}</extraLdflags>
<extraConfigureArg>--host=aarch64-apple-darwin</extraConfigureArg>
<extraConfigureArg2>MACOSX_DEPLOYMENT_TARGET=${macosxDeploymentTarget}</extraConfigureArg2>
<bundleNativeCode>META-INF/native/lib${jniLibName}.jnilib;osname=macos;osname=macosx;processor=aarch64</bundleNativeCode>
<!-- Don't run tests as we can't load the aarch64 lib on a x86_64 system -->
<skipTests>true</skipTests>
Expand Down Expand Up @@ -788,24 +787,39 @@
<pathconvert property="quicheHomeBuildWindowsDir" targetos="windows">
<path location="${quicheHomeBuildDir}" />
</pathconvert>

<!-- Copy and filter the template MSVC project -->
<filter token="BORINGSSL_INCLUDE_DIR" value="${boringsslHomeIncludeWindowsDir}" />
<filter token="QUICHE_INCLUDE_DIR" value="${quicheHomeIncludeWindowsDir}" />
<filter token="BORINGSSL_LIB_DIR" value="${boringsslHomeBuildWindowsDir}" />
<filter token="QUICHE_LIB_DIR" value="${quicheHomeBuildWindowsDir}" />
<filter token="QUICHE_LIB" value="${libquiche}" />
<filter token="CRYPTO_LIB" value="${libcrypto}" />
<filter token="SSL_LIB" value="${libssl}" />

<copy file="src/main/native-package/vs2010.custom.props.template" tofile="${templateDir}/vs2010.custom.props" filtering="true" overwrite="true" verbose="true" />

<!-- Copy custom.m4 to fix building library without version-suffix on Android -->
<if>
<equals arg1="${platform}" arg2="android" />
<then>
<copy file="src/main/native-package/m4/custom.m4.android.template" tofile="${templateDir}/m4/custom.m4" filtering="true" overwrite="true" verbose="true" />
</then>
<elseif>
<equals arg1="${os.detected.name}" arg2="windows" />
<then>
<!-- Copy and filter the template MSVC project -->
<filter token="BORINGSSL_INCLUDE_DIR" value="${boringsslHomeIncludeWindowsDir}" />
<filter token="QUICHE_INCLUDE_DIR" value="${quicheHomeIncludeWindowsDir}" />
<filter token="BORINGSSL_LIB_DIR" value="${boringsslHomeBuildWindowsDir}" />
<filter token="QUICHE_LIB_DIR" value="${quicheHomeBuildWindowsDir}" />
<filter token="QUICHE_LIB" value="${libquiche}" />
<filter token="CRYPTO_LIB" value="${libcrypto}" />
<filter token="SSL_LIB" value="${libssl}" />
<copy file="src/main/native-package/vs2010.custom.props.template" tofile="${templateDir}/vs2010.custom.props" filtering="true" overwrite="true" verbose="true" />
</then>
</elseif>
<else>
<!-- Copy and filter custom.m4 -->
<filter token="BORINGSSL_INCLUDE_DIR" value="${boringsslHomeIncludeDir}" />
<filter token="QUICHE_INCLUDE_DIR" value="${quicheHomeIncludeDir}" />
<filter token="BORINGSSL_LIB_DIR" value="${boringsslHomeBuildDir}" />
<filter token="QUICHE_LIB_DIR" value="${quicheHomeBuildDir}" />
<filter token="QUICHE_LIB" value="quiche" />
<filter token="CRYPTO_LIB" value="crypto" />
<filter token="SSL_LIB" value="ssl" />
<filter token="EXTRA_LDFLAGS" value="${extraLdflags}" />
<filter token="EXTRA_CFLAGS" value="${extraCflags}" />
<copy file="src/main/native-package/m4/custom.m4.template" tofile="${templateDir}/m4/custom.m4" filtering="true" overwrite="true" verbose="true" />
</else>
</if>
</target>
</configuration>
Expand Down Expand Up @@ -932,11 +946,10 @@
<windowsCustomProps>true</windowsCustomProps>
<windowsPlatformToolset>v142</windowsPlatformToolset>
<libDirectory>${nativeLibOnlyDir}</libDirectory>
<verbose>true</verbose>
<configureArgs>
<configureArg>${extraConfigureArg}</configureArg>
<configureArg>${extraConfigureArg2}</configureArg>
<configureArg>CFLAGS=${cflags}</configureArg>
<configureArg>LDFLAGS=${ldflags} ${extraLdflags}</configureArg>
<configureArg>--libdir=${project.build.directory}/native-build/target/lib</configureArg>
</configureArgs>
</configuration>
Expand Down
39 changes: 39 additions & 0 deletions codec-native-quic/src/main/native-package/m4/custom.m4.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
dnl ---------------------------------------------------------------------------
dnl Copyright 2022 The Netty Project
dnl
dnl Licensed under the Apache License, Version 2.0 (the "License");
dnl you may not use this file except in compliance with the License.
dnl You may obtain a copy of the License at
dnl
dnl http://www.apache.org/licenses/LICENSE-2.0
dnl
dnl Unless required by applicable law or agreed to in writing, software
dnl distributed under the License is distributed on an "AS IS" BASIS,
dnl WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dnl See the License for the specific language governing permissions and
dnl limitations under the License.
dnl ---------------------------------------------------------------------------

AC_DEFUN([CUSTOM_M4_SETUP],
[
dnl Ensure we only expose what we really need
case $host in
*-darwin*)
LDFLAGS="$LDFLAGS -Wl,-exported_symbol,_JNI_*"
;;
*linux*)
LDFLAGS="$LDFLAGS -Wl,--exclude-libs,ALL"
;;
*)
;;
esac

dnl Update the compiler/linker flags
CFLAGS="$CFLAGS -std=gnu99 -fvisibility=hidden -Werror -fno-omit-frame-pointer -Wunused -Wno-unused-value -O3 -I@BORINGSSL_INCLUDE_DIR@ -I@QUICHE_INCLUDE_DIR@ @EXTRA_CFLAGS@"
CXXFLAGS="$CXXFLAGS"
LDFLAGS="$LDFLAGS -L@BORINGSSL_LIB_DIR@ -lssl -lcrypto -L@QUICHE_LIB_DIR@ -lquiche @EXTRA_LDFLAGS@"
AC_SUBST(CFLAGS)
AC_SUBST(CXXFLAGS)
AC_SUBST(LDFLAGS)
])

0 comments on commit 8f1ec1b

Please sign in to comment.