Skip to content

Commit

Permalink
refactor musl detection
Browse files Browse the repository at this point in the history
      <!-- musl detection notes -->
      <!-- https://github.com/dotnet/runtime/blob/a50ba0669353893ca8ade8568b0a7d210b5a425f/src/mono/llvm/llvm-init.proj\#L7 -->
      <!-- https://github.com/dotnet/runtime/blob/a50ba0669353893ca8ade8568b0a7d210b5a425f/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs\#L78 -->
      <!-- https://learn.microsoft.com/en-us/visualstudio/msbuild/exec-task\?view\=vs-2022 -->
      <!-- dotnet restore slow in container -->
      <!-- https://github.com/NuGet/Home/issues/13062\#issuecomment-1845202196 -->

    <!-- Check musl shared lib exists -->
    <!-- if musl libsexist check if musl is the loaded libc -->
    <!-- glibc hosts could have musl cross libs installed, in the standard musl location -->
    <!-- use ldd on a well known binary such as /bin/sh and grep for musl -->
    <!-- note ldd may not be available on all musl targets -->
    <!-- Main fallback behaviour is to default to glibc flavour, ensuring miminal impact on existing supported targets -->
  • Loading branch information
YOU54F committed May 28, 2024
1 parent c73118e commit e9f3fa6
Showing 1 changed file with 88 additions and 80 deletions.
168 changes: 88 additions & 80 deletions src/PactNet/PactNet.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk" InitialTargets="build_libs">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -9,87 +9,95 @@
</PropertyGroup>

<Import Project="../NuGet.targets" />
<Target Name="build_libs">
<!-- musl detection notes -->
<!-- https://github.com/dotnet/runtime/blob/a50ba0669353893ca8ade8568b0a7d210b5a425f/src/mono/llvm/llvm-init.proj#L7 -->
<!-- https://github.com/dotnet/runtime/blob/a50ba0669353893ca8ade8568b0a7d210b5a425f/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs#L78 -->
<!-- https://learn.microsoft.com/en-us/visualstudio/msbuild/exec-task?view=vs-2022 -->
<!-- dotnet restore slow in container -->
<!-- https://github.com/NuGet/Home/issues/13062#issuecomment-1845202196 -->

<PropertyGroup>
<IsWindows>False</IsWindows>
<IsLinux>False</IsLinux>
<IsOSX>False</IsOSX>
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'True'">True</IsWindows>
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'True'">True</IsLinux>
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'True'">True</IsOSX>
<IsArm64>False</IsArm64>
<IsArm64 Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">True</IsArm64>
<IsLinuxX64 Condition="'$(IsLinux)' == 'True' And '$(IsArm64)' == 'False'">True</IsLinuxX64>
<IsLinuxArm64 Condition="'$(IsLinux)' == 'True' And '$(IsArm64)' == 'True'">True</IsLinuxArm64>
<IsLinuxMuslX64LibFound Condition="$([System.IO.File]::Exists('/lib/libc.musl-x86_64.so.1')) == 'True'">True</IsLinuxMuslX64LibFound>
<IsLinuxMuslArm64LibFound Condition="$([System.IO.File]::Exists('/lib/libc.musl-aarch64.so.1')) == 'True'">True</IsLinuxMuslArm64LibFound>
<!-- <IsLinuxMuslX64 Condition="'$(IsLinux)' == 'True' And $(IsLinuxMuslX64LibFound) == 'True' And '$(IsArm64)' == 'False'">True</IsLinuxMuslX64>
<IsLinuxMuslArm64 Condition="'$(IsLinux)' == 'True' And $(IsLinuxMuslArm64LibFound) == 'True' And '$(IsArm64)' == 'True'">True</IsLinuxMuslArm64> -->

<!-- Testing -->
<IsLinuxMuslLoaded><Exec Command="/usr/bin/ldd /bin/sh | grep musl"/>True</IsLinuxMuslLoaded>
<IsLinuxMuslX64 Condition="'$(IsLinux)' == 'True' And $(IsLinuxMuslLoaded) == 'True' And '$(IsArm64)' == 'False'">True</IsLinuxMuslX64>
<IsLinuxMuslArm64 Condition="'$(IsLinux)' == 'True' And $(IsLinuxMuslLoaded) == 'True' And '$(IsArm64)' == 'True'">True</IsLinuxMuslArm64>


<!-- musl detection notes -->
<!-- https://github.com/dotnet/runtime/blob/a50ba0669353893ca8ade8568b0a7d210b5a425f/src/mono/llvm/llvm-init.proj#L7 -->
<!-- https://github.com/dotnet/runtime/blob/a50ba0669353893ca8ade8568b0a7d210b5a425f/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.Unix.cs#L78 -->
<!-- https://learn.microsoft.com/en-us/visualstudio/msbuild/exec-task?view=vs-2022 -->
</PropertyGroup>

<ItemGroup>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\windows\x86_64\pact_ffi.dll">
<Link>pact_ffi.dll</Link>
<PackagePath>runtimes/win-x64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsWindows)'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\x86_64\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-x64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsLinuxX64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\aarch64\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-arm64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsLinuxArm64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\x86_64-musl\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-x64-musl/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsLinuxMuslX64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\aarch64-musl\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-arm64-musl/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsLinuxMuslArm64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\osx\x86_64\libpact_ffi.dylib">
<Link>libpact_ffi.dylib</Link>
<PackagePath>runtimes/osx-x64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsOSX)' == 'True' And '$(IsArm64)' == 'False'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\osx\aarch64-apple-darwin\libpact_ffi.dylib">
<Link>libpact_ffi.dylib</Link>
<PackagePath>runtimes/osx-arm64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsOSX)' == 'True' And '$(IsArm64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
</ItemGroup>
<!-- Check musl shared lib exists -->
<!-- if musl libsexist check if musl is the loaded libc -->
<!-- glibc hosts could have musl cross libs installed, in the standard musl location -->
<!-- use ldd on a well known binary such as /bin/sh and grep for musl -->
<!-- note ldd may not be available on all musl targets -->
<!-- Main fallback behaviour is to default to glibc flavour, ensuring miminal impact on existing supported targets -->
<PropertyGroup>
<IsLinux>False</IsLinux>
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'True'">True</IsLinux>
<IsLinuxMuslX64LibFound Condition="$([System.IO.File]::Exists('/lib/libc.musl-x86_64.so.1')) == 'True'">True</IsLinuxMuslX64LibFound>
<IsLinuxMuslArm64LibFound Condition="$([System.IO.File]::Exists('/lib/libc.musl-aarch64.so.1')) == 'True'">True</IsLinuxMuslArm64LibFound>
</PropertyGroup>
<!-- only run this check if linux and the musl shared libs were found -->
<Exec Command="ldd /bin/sh | grep musl" ConsoleToMSBuild="true" StandardOutputImportance="low" Condition="$(IsLinux) == 'True' And ($(IsLinuxMuslX64LibFound) == 'True' Or $(IsLinuxMuslArm64LibFound) == 'True')">
<Output TaskParameter="ExitCode" PropertyName="IsMuslDetected"/>
</Exec>
<PropertyGroup>
<IsWindows>False</IsWindows>
<IsOSX>False</IsOSX>
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'True'">True</IsWindows>
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'True'">True</IsOSX>
<IsArm64>False</IsArm64>
<IsArm64 Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">True</IsArm64>
<IsLinuxX64 Condition="'$(IsLinux)' == 'True' And '$(IsArm64)' == 'False'">True</IsLinuxX64>
<IsLinuxArm64 Condition="'$(IsLinux)' == 'True' And '$(IsArm64)' == 'True'">True</IsLinuxArm64>
<IsLinuxMuslX64 Condition="'$(IsLinux)' == 'True' And $(IsLinuxMuslLoaded) == 'True' And '$(IsArm64)' == 'False'">True</IsLinuxMuslX64>
<IsLinuxMuslArm64 Condition="'$(IsLinux)' == 'True' And $(IsLinuxMuslLoaded) == 'True' And '$(IsArm64)' == 'True'">True</IsLinuxMuslArm64>
</PropertyGroup>

<ItemGroup>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\windows\x86_64\pact_ffi.dll">
<Link>pact_ffi.dll</Link>
<PackagePath>runtimes/win-x64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsWindows)'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\x86_64\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-x64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsLinuxX64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\aarch64\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-arm64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsLinuxArm64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\x86_64-musl\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-x64-musl/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsLinuxMuslX64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\linux\aarch64-musl\libpact_ffi.so">
<Link>libpact_ffi.so</Link>
<PackagePath>runtimes/linux-arm64-musl/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsLinuxMuslArm64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\osx\x86_64\libpact_ffi.dylib">
<Link>libpact_ffi.dylib</Link>
<PackagePath>runtimes/osx-x64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsOSX)' == 'True' And '$(IsArm64)' == 'False'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\osx\aarch64-apple-darwin\libpact_ffi.dylib">
<Link>libpact_ffi.dylib</Link>
<PackagePath>runtimes/osx-arm64/native</PackagePath>
<Pack>true</Pack>
<CopyToOutputDirectory Condition="'$(IsOSX)' == 'True' And '$(IsArm64)' == 'True'">PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
</ItemGroup>
</Target>
<ItemGroup>
<Content Include="$(MSBuildProjectDirectory)\..\..\build\PactNet.targets">
<PackagePath>build/net462/</PackagePath>
Expand Down

0 comments on commit e9f3fa6

Please sign in to comment.