FastText.NetWrapper icon indicating copy to clipboard operation
FastText.NetWrapper copied to clipboard

Problems with native libraries in a service published as Portable

Open VictorIranzo opened this issue 2 years ago • 0 comments

First of all, congratulations for your library. It's very nice.

We are consuming your NuGet package from one of our microservices that is responsible of doing some AI staff. We have a problem when this service is published as portable. The FastText files are not copied to the corresponding runtime folder. The same can happen if the microservice is published for a specific runtime identifier, such as win-x64.

At the moment, as a workaround we have added the following code that copies the files after publishing the service.

  <!-- This is a workaround to copy the FastText native files to the runtimes folder when the service is published as Portable. -->
  <Target Name="PostPublishPortableCopyFastTextNativeFiles" AfterTargets="AfterPublish" Condition="'$(RuntimeIdentifier)' == ''">
    <Warning Text="This project's platform is set to '$(PlatformTarget)', but it requires a native DLLs. Defaulting to 'x64' binaries. Explicitly choose a platform in the project's Build properties to remove this warning."
             Condition="'$(PlatformTarget)' != 'x86' And '$(PlatformTarget)' != 'x64'" />

    <PropertyGroup>
      <FastTextPlatformTarget>$(PlatformTarget)</FastTextPlatformTarget>
      <FastTextPlatformTarget Condition="'$(PlatformTarget)' != 'x86' And '$(PlatformTarget)' != 'x64'">x64</FastTextPlatformTarget>
    </PropertyGroup>

    <PropertyGroup>
      <FastTextWinFilesOutputPath>$(PublishDir)runtimes\win-$(FastTextPlatformTarget)\native\</FastTextWinFilesOutputPath>
      <FastTextLinuxFilesOutputPath>$(PublishDir)runtimes\linux-$(FastTextPlatformTarget)\native\</FastTextLinuxFilesOutputPath>
      <FastTextOsxFilesOutputPath>$(PublishDir)runtimes\osx-$(FastTextPlatformTarget)\native\</FastTextOsxFilesOutputPath>
    </PropertyGroup>

    <Message Text="FastText.NetWrapper Post Publish Portable Message" Importance="high"/>
    <Message Text="RuntimeIdentifier: Portable" Importance="high"/>
    <Message Text="PlatformTarget: $(FastTextPlatformTarget)" Importance="high"/>
    <Message Text="FastTextWinFilesOutputPath: $(FastTextWinFilesOutputPath)" Importance="high"/>
    <Message Text="FastTextLinuxFilesOutputPath: $(FastTextLinuxFilesOutputPath)" Importance="high"/>
    <Message Text="FastTextOsxFilesOutputPath: $(FastTextOsxFilesOutputPath)" Importance="high"/>

    <ItemGroup>
      <FastTextWinFiles Include="$(MSBuildThisFileDirectory)**\fasttext.dll" />
      <FastTextLinuxFiles Include="$(MSBuildThisFileDirectory)**\libfasttext.so" />
      <FastTextOsxFiles Include="$(MSBuildThisFileDirectory)**\libfasttext.dylib" />
    </ItemGroup>

    <Message Text="Copying FastText native files to the runtimes folder..." Importance="high"/>
    <Copy SourceFiles="@(FastTextWinFiles)" DestinationFolder="$(FastTextWinFilesOutputPath)" />
    <Copy SourceFiles="@(FastTextLinuxFiles)" DestinationFolder="$(FastTextLinuxFilesOutputPath)" />
    <Copy SourceFiles="@(FastTextOsxFiles)" DestinationFolder="$(FastTextOsxFilesOutputPath)" />

    <ItemGroup>
      <FastTextWinFilesToDelete Include="$(PublishDir)fasttext.dll" />
      <FastTextLinuxFilesToDelete Include="$(PublishDir)libfasttext.so" />
      <FastTextOsxFilesToDelete Include="$(PublishDir)libfasttext.dylib" />
    </ItemGroup>

    <Message Text="Deleting FastText native files from directory that contains only Portable libraries..." Importance="high"/>
    <Delete Files="@(FastTextWinFilesToDelete)" />
    <Delete Files="@(FastTextLinuxFilesToDelete)" />
    <Delete Files="@(FastTextOsxFilesToDelete)" />
  </Target>

  <!-- This is a workaround to delete the FastText native files that not match the specified runtime. -->
  <Target Name="PostPublishRuntimeDeleteFastTextNativeFilesThatNotMatchSpecifiedRuntime" AfterTargets="AfterPublish" Condition="'$(RuntimeIdentifier)' != ''">
    <Message Text="FastText.NetWrapper Post Publish Runtime Message" Importance="high"/>
    <Message Text="RuntimeIdentifier: $(RuntimeIdentifier)" Importance="high"/>

    <ItemGroup>
      <FastTextWinFilesToDelete Include="$(PublishDir)fasttext.dll" />
      <FastTextLinuxFilesToDelete Include="$(PublishDir)libfasttext.so" />
      <FastTextOsxFilesToDelete Include="$(PublishDir)libfasttext.dylib" />
    </ItemGroup>

    <Message Text="Deleting FastText native files that not match the specified runtime..." Importance="high"/>
    <Delete Files="@(FastTextWinFilesToDelete)" Condition="!$(RuntimeIdentifier.StartsWith('win'))" />
    <Delete Files="@(FastTextLinuxFilesToDelete)" Condition="!$(RuntimeIdentifier.StartsWith('linux'))" />
    <Delete Files="@(FastTextOsxFilesToDelete)" Condition="!$(RuntimeIdentifier.StartsWith('osx'))" />
  </Target>

We create the issue only to report our solution. Perhaps our fix will be interesting for you.

Thank you in advance.

VictorIranzo avatar Mar 25 '22 11:03 VictorIranzo