Skip to content

Commit

Permalink
Version the native library (#127)
Browse files Browse the repository at this point in the history
Use LibZipSharp MAJOR.MINOR version in the native library name.  

This is to avoid issues when two or more versions of the LibZipSharp
nuget are found and their native libraries have the same name but
different ABI, which can result in runtime problems when a function
expected by the managed code is not found in the native library.

From this point on, whenever any function is added or removed in
the native library, at least the MINOR version number must be bumped.
More serious ABI changes will require also MAJOR version number bump.

Co-authored-by: Dean Ellis <dellis1972@googlemail.com>
  • Loading branch information
grendello and dellis1972 authored Apr 27, 2023
1 parent d5499c2 commit 45f4c1d
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 102 deletions.
43 changes: 37 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@ endif()
#
# Read product version
#
file(STRINGS "LibZipSharp.props" LZS_PRODUCT_VERSION_XML REGEX "^[ \t]*<_LibZipSharpAssemblyVersion>(.*)</_LibZipSharpAssemblyVersion>")
string(REGEX REPLACE "^[ \t]*<_LibZipSharpAssemblyVersion>([0-9.]+).*</_LibZipSharpAssemblyVersion>" "\\1" LZS_VERSION "${LZS_PRODUCT_VERSION_XML}")
file(STRINGS "LibZipSharp.props" LZS_PRODUCT_VERSION_XML_MAJOR REGEX "^[ \t]*<_LibZipSharpAssemblyVersionMajor>(.*)</_LibZipSharpAssemblyVersionMajor>")
file(STRINGS "LibZipSharp.props" LZS_PRODUCT_VERSION_XML_MINOR REGEX "^[ \t]*<_LibZipSharpAssemblyVersionMinor>(.*)</_LibZipSharpAssemblyVersionMinor>")
file(STRINGS "LibZipSharp.props" LZS_PRODUCT_VERSION_XML_PATCH REGEX "^[ \t]*<_LibZipSharpAssemblyVersionPatch>(.*)</_LibZipSharpAssemblyVersionPatch>")

string(REGEX REPLACE "^[ \t]*<_LibZipSharpAssemblyVersionMajor>([0-9.]+).*</_LibZipSharpAssemblyVersionMajor>" "\\1" LZS_VERSION_MAJOR "${LZS_PRODUCT_VERSION_XML_MAJOR}")
string(REGEX REPLACE "^[ \t]*<_LibZipSharpAssemblyVersionMinor>([0-9.]+).*</_LibZipSharpAssemblyVersionMinor>" "\\1" LZS_VERSION_MINOR "${LZS_PRODUCT_VERSION_XML_MINOR}")
string(REGEX REPLACE "^[ \t]*<_LibZipSharpAssemblyVersionPatch>([0-9.]+).*</_LibZipSharpAssemblyVersionPatch>" "\\1" LZS_VERSION_PATCH "${LZS_PRODUCT_VERSION_XML_PATCH}")

set(LZS_VERSION "${LZS_VERSION_MAJOR}.${LZS_VERSION_MINOR}.${LZS_VERSION_PATCH}")

if(WIN32)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded" CACHE STRING "Statically link MSVC runtime library")
Expand Down Expand Up @@ -516,17 +523,31 @@ else()
${LZS_CXX_FLAGS}
)

set(LT_VERSION "${CMAKE_PROJECT_VERSION_MAJOR}.${CMAKE_PROJECT_VERSION_MINOR}.${CMAKE_PROJECT_VERSION_PATCH}")
set(LT_VERSION_SHORT "${CMAKE_PROJECT_VERSION_MAJOR}.${CMAKE_PROJECT_VERSION_MINOR}")
set(LT_RELEASE "${CMAKE_PROJECT_VERSION_MAJOR}-${CMAKE_PROJECT_VERSION_MINOR}")
set(NATIVE_LIB_OUTPUT_NAME "${PROJECT_NAME}-${LT_RELEASE}")

if(APPLE)
set_target_properties(
${PROJECT_NAME}
PROPERTIES
OSX_ARCHITECTURES "${LZS_OSX_ARCHITECTURES}"
)
OUTPUT_NAME "${NATIVE_LIB_OUTPUT_NAME}"
)
elseif(UNIX)
set_target_properties(
${PROJECT_NAME}
PROPERTIES
VERSION ${LT_VERSION}
SOVERSION ${LT_RELEASE}
OUTPUT_NAME "${NATIVE_LIB_OUTPUT_NAME}"
)
elseif(WIN32)
set_target_properties(
${PROJECT_NAME}
PROPERTIES
OUTPUT_NAME "lib${PROJECT_NAME}"
OUTPUT_NAME "lib${NATIVE_LIB_OUTPUT_NAME}"
)
endif()

Expand Down Expand Up @@ -629,7 +650,7 @@ else()
)

#
# Place debug info in separate files, if possible
# Place debug info in separate files, if possible, and get rid of symlinks
#
if(IS_LINUX OR IS_ANDROID)
if(IS_LINUX)
Expand All @@ -647,6 +668,16 @@ else()
COMMAND ${OBJCOPY} --only-keep-debug "$<TARGET_FILE_NAME:${PROJECT_NAME}>" "$<TARGET_FILE_NAME:${PROJECT_NAME}>.debug"
COMMAND ${STRIP} --strip-debug --strip-unneeded "$<TARGET_FILE_NAME:${PROJECT_NAME}>"
COMMAND ${OBJCOPY} --add-gnu-debuglink="$<TARGET_FILE_NAME:${PROJECT_NAME}>.debug" "$<TARGET_FILE_NAME:${PROJECT_NAME}>"
)
)

add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
WORKING_DIRECTORY "$<TARGET_FILE_DIR:${PROJECT_NAME}>"
COMMAND rm lib${PROJECT_NAME}-${LT_RELEASE}.so
COMMAND rm lib${PROJECT_NAME}-${LT_RELEASE}.so.${LT_RELEASE}
COMMAND ln lib${PROJECT_NAME}-${LT_RELEASE}.so.${LT_VERSION} lib${PROJECT_NAME}-${LT_RELEASE}.so.${LT_VERSION_SHORT}
COMMAND ln lib${PROJECT_NAME}-${LT_RELEASE}.so.${LT_VERSION} lib${PROJECT_NAME}-${LT_RELEASE}.so
)
endif()
endif()
16 changes: 8 additions & 8 deletions LibZipSharp.UnitTest/LibZipSharp.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@

<ItemGroup>
<ProjectReference Include="..\LibZipSharp\libZipSharp.csproj" Condition="'$(ReferenceNuget)' != 'True'" />
<None Include="$(_NativeBuildDir)\lib\Darwin\libZipSharpNative.dylib" Condition="'$(ReferenceNuget)' != 'True' And Exists ('$(_NativeBuildDir)\lzsbuild\lib\Darwin\libZipSharpNative.dylib')">
<Link>libZipSharpNative.dylib</Link>
<None Include="$(_NativeBuildDir)\lib\Darwin\$(_NativeLibraryBaseName).dylib" Condition="'$(ReferenceNuget)' != 'True' And Exists ('$(_NativeBuildDir)\lzsbuild\lib\Darwin\$(_NativeLibraryBaseName).dylib')">
<Link>$(_NativeLibraryBaseName).dylib</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(_NativeBuildDir)\lib\win64\RelWithDebInfo\libZipSharpNative.dll" Condition="'$(ReferenceNuget)' != 'True' And Exists ('$(_NativeBuildDir)\lib\win64\RelWithDebInfo\libZipSharpNative.dll')">
<Link>libZipSharpNative.dll</Link>
<None Include="$(_NativeBuildDir)\lib\win64\RelWithDebInfo\$(_NativeLibraryBaseName).dll" Condition="'$(ReferenceNuget)' != 'True' And Exists ('$(_NativeBuildDir)\lib\win64\RelWithDebInfo\$(_NativeLibraryBaseName).dll')">
<Link>$(_NativeLibraryBaseName).dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(_NativeBuildDir)\lib\Linux\libZipSharpNative.so" Condition="'$(ReferenceNuget)' != 'True' And Exists ('$(_NativeBuildDir)\lib\Linux\libZipSharpNative.so')">
<Link>libZipSharpNative.so</Link>
<None Include="$(_NativeBuildDir)\lib\Linux\$(_NativeLibraryBaseName).so" Condition="'$(ReferenceNuget)' != 'True' And Exists ('$(_NativeBuildDir)\lib\Linux\$(_NativeLibraryBaseName).so')">
<Link>$(_NativeLibraryBaseName).so</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(_NativeBuildDir)\lib\Linux\libZipSharpNative.so.debug" Condition="'$(ReferenceNuget)' != 'True' And Exists ('$(_NativeBuildDir)\lib\Linux\libZipSharpNative.so.debug')">
<Link>libZipSharpNative.so.debug</Link>
<None Include="$(_NativeBuildDir)\lib\Linux\$(_NativeLibraryBaseName).so.$(_LibZipSharpAssemblyVersionMajor).$(_LibZipSharpAssemblyVersionMinor).$(_LibZipSharpAssemblyVersionPatch).debug" Condition="'$(ReferenceNuget)' != 'True' And Exists ('$(_NativeBuildDir)\lib\Linux\$(_NativeLibraryBaseName).so.$(_LibZipSharpAssemblyVersionMajor).$(_LibZipSharpAssemblyVersionMinor).$(_LibZipSharpAssemblyVersionPatch).debug')">
<Link>$(_NativeLibraryBaseName).so.$(_LibZipSharpAssemblyVersionMajor).$(_LibZipSharpAssemblyVersionMinor).$(_LibZipSharpAssemblyVersionPatch).debug</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packaged_resources">
Expand Down
8 changes: 7 additions & 1 deletion LibZipSharp.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<_LibZipSharpAssemblyVersion>3.0.0</_LibZipSharpAssemblyVersion>
<_LibZipSharpAssemblyVersionMajor>3</_LibZipSharpAssemblyVersionMajor>
<_LibZipSharpAssemblyVersionMinor>0</_LibZipSharpAssemblyVersionMinor>
<_LibZipSharpAssemblyVersionPatch>0</_LibZipSharpAssemblyVersionPatch>
<_LibZipSharpAssemblyVersion>$(_LibZipSharpAssemblyVersionMajor).$(_LibZipSharpAssemblyVersionMinor).$(_LibZipSharpAssemblyVersionPatch)</_LibZipSharpAssemblyVersion>
<_NativeLibraryVersionForName>$(_LibZipSharpAssemblyVersionMajor)-$(_LibZipSharpAssemblyVersionMinor)</_NativeLibraryVersionForName>
<_NativeLibraryBaseName>libZipSharpNative-$(_NativeLibraryVersionForName)</_NativeLibraryBaseName>

<!--
Nuget Version. You can append things like -alpha-1 etc to this value.
But always leave the $(_LibZipSharpAssemblyVersion) value at the start.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
<LibZipSharpBundleAllNativeLibraries Condition=" '$(LibZipSharpBundleAllNativeLibraries)' == '' ">false</LibZipSharpBundleAllNativeLibraries>
</PropertyGroup>
<ItemGroup>
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\win-x64\native\libZipSharpNative.*">
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\win-x64\native\@LibraryBaseFileName@.*">
<Link>x64\%(FileName)%(Extension)</Link>
</_LibZipNativeLibs>
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\win-arm64\native\libZipSharpNative.*">
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\win-arm64\native\@LibraryBaseFileName@.*">
<Link>arm64\%(FileName)%(Extension)</Link>
</_LibZipNativeLibs>
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\win-x86\native\libZipSharpNative.*">
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\win-x86\native\@LibraryBaseFileName@.*">
<Link>x86\%(FileName)%(Extension)</Link>
</_LibZipNativeLibs>
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\osx\native\libZipSharpNative.dylib">
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\osx\native\@LibraryBaseFileName@.dylib">
<Link>%(FileName)%(Extension)</Link>
</_LibZipNativeLibs>
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\linux-x64\native\libZipSharpNative.*">
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\linux-x64\native\@LibraryBaseFileName@.so">
<Link>%(FileName)%(Extension)</Link>
</_LibZipNativeLibs>
<_LibZipNativeLibs Include="$(MSBuildThisFileDirectory)..\runtimes\linux-x64\native\@LibraryBaseFileName@.so.@LibraryVersionMajor@.@LibraryVersionMinor@.@LibraryVersionPatch@.debug">
<Link>%(FileName)%(Extension)</Link>
</_LibZipNativeLibs>
<None Include="@(_LibZipNativeLibs)" Condition="'$(LibZipSharpBundleAllNativeLibraries)' == 'True'">
Expand Down
7 changes: 7 additions & 0 deletions LibZipSharp/Xamarin.Tools.Zip/Constants.cs.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Xamarin.Tools.Zip
{
static class Constants
{
public const string ZIP_LIBNAME = "@LibraryBaseFileName@";
}
}
Loading

0 comments on commit 45f4c1d

Please sign in to comment.