xamarin-macios icon indicating copy to clipboard operation
xamarin-macios copied to clipboard

dotnet6.0-macos preview: Linking fails on native reference dylib

Open mmoraga opened this issue 3 years ago • 17 comments

I have a Xamarin.mac app that uses https://github.com/Pkcs11Interop to interact with smartcards. Due to an implementation detail (https://github.com/Pkcs11Interop/Pkcs11Interop/issues/109) the library requires a dylib with required symbols to be present at build time. For this a simple library with empty implementations of the pkcs interface can be used.

Steps to Reproduce

  1. Create a new dotnet6.0-macos project
  2. Add the latest Pkcs11Interop nuget package
  3. Add a native reference to empty-pkcs11-x64.dylib
  4. Try to build the project with dotnet build

Expected Behavior

With current Xamarin.Mac, the linking succeeds and the app can be built.

Actual Behavior

The build fails at the linking stage:

  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
  Tool xcrun execution finished (exit code = 1).

  ld: library not found for -lempty-pkcs11-x64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)

Environment

Version information

=== Visual Studio Community 2019 for Mac ===

Version 8.10.23 (build 7)
Installation UUID: 66016c88-d73f-43ba-9679-9669762ac900
	GTK+ 2.24.23 (Raleigh theme)
	Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

	Package version: 612000174

=== Mono Framework MDK ===

Runtime:
	Mono 6.12.0.174 (2020-02/adf1bc4335d) (64-bit)
	Package version: 612000174

=== Roslyn (Language Service) ===

3.10.0-4.21269.26+029847714208ebe49668667c60ea5b0a294e0fcb

=== NuGet ===

Version: 5.9.0.7134

=== .NET SDK (x64) ===

SDK: /usr/local/share/dotnet/x64/sdk/6.0.103/Sdks
SDK Versions:
	6.0.201
	6.0.103
	5.0.408
	5.0.407
	5.0.406
	3.1.419
	3.1.418
	3.1.417
MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/x64/dotnet
Runtime Versions:
	6.0.3
	5.0.17
	5.0.16
	5.0.15
	3.1.25
	3.1.24
	3.1.23

=== .NET Core 3.1 SDK ===

SDK: 3.1.419

=== .NET 5.0 SDK ===

SDK: 5.0.408

=== Xamarin.Profiler ===

Version: 1.6.15.68
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Apple Developer Tools ===

Xcode 13.3.1 (20103)
Build 13E500a

=== Xamarin.Mac ===

Version: 8.8.0.3 (Visual Studio Community)
Hash: eb7f8ed35
Branch: xcode13.3
Build date: 2022-03-30 03:16:09-0400

=== Xamarin.iOS ===

Version: 15.8.0.3 (Visual Studio Community)
Hash: eb7f8ed35
Branch: xcode13.3
Build date: 2022-03-30 03:16:09-0400

=== Xamarin Designer ===

Version: 16.11.0.60
Hash: 56f9b80b0
Branch: remotes/origin/d16-11
Build date: 2021-12-15 01:44:16 UTC

=== Xamarin.Android ===

Version: 12.2.4.3 (Visual Studio Community)
Commit: xamarin-android/d17-1/fc1125e
Android SDK: /Users/manuel.moraga/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		None installed

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 30.0.4
SDK Build Tools Version: 30.0.2

Build Information: 
Mono: adf1bc4
Java.Interop: xamarin/java.interop/d17-1@7ac33610
ProGuard: Guardsquare/proguard/v7.0.1@912d149
SQLite: xamarin/sqlite/3.37.1@0893b3b
Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-1@db125a7

=== Eclipse Temurin JDK ===

Java SDK: Not Found

=== Android SDK Manager ===

Version: 16.10.0.13
Hash: 1b81df5
Branch: remotes/origin/d16-10
Build date: 2021-11-12 00:17:32 UTC

=== Android Device Manager ===

Version: 16.10.0.15
Hash: 89dcc0b
Branch: remotes/origin/d16-10
Build date: 2021-11-12 00:17:52 UTC

=== Build Information ===

Release ID: 810230007
Git revision: 7f00a2236acfde86feffbab606eefae32a9adbb3
Build date: 2022-05-05 09:46:09-04
Build branch: release-8.10

=== Operating System ===

Mac OS X 12.3.1
Darwin 21.4.0 Darwin Kernel Version 21.4.0
    Fri Mar 18 00:46:32 PDT 2022
    root:xnu-8020.101.4~15/RELEASE_ARM64_T6000 x86_64

Build Logs

msbuild.binlog.zip

Example Project (If Possible)

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0-macos</TargetFramework>
        <RootNamespace>dotnet_macos_cert</RootNamespace>
        <ApplicationId>com.example.service</ApplicationId>
        <RuntimeIdentifier>osx-x64</RuntimeIdentifier>
        <SelfContained>true</SelfContained>
        <Nullable>enable</Nullable>
        <SupportedOSPlatformVersion>10.15</SupportedOSPlatformVersion>
    	<MtouchExtraArgs>-v -v -v -v</MtouchExtraArgs>
    </PropertyGroup>
    <ItemGroup>
    <NativeReference Include="empty-pkcs11-x64.dylib">
      <Kind>Dynamic</Kind>
      <SmartLink>False</SmartLink>
    </NativeReference>
    </ItemGroup>
    <ItemGroup>
      <PackageReference Include="Pkcs11Interop" Version="5.1.2" />
    </ItemGroup>

</Project>

mmoraga avatar May 17 '22 08:05 mmoraga

Can you try adding a lib prefix to your dylib to see if that works? So that it's named libempty-pkcs11-x64.dylib.

rolfbjarne avatar May 17 '22 09:05 rolfbjarne

Hi @mmoraga. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar May 17 '22 09:05 ghost

@rolfbjarne thanks for the quick reply. That seems to build! Will run a few tests to see if it also works correctly

mmoraga avatar May 17 '22 09:05 mmoraga

Now I've properly tested it. So prepending lib to the library name fixed it. Is this by design or will you be fixing it in the final release?

mmoraga avatar May 19 '22 09:05 mmoraga

This was not intentional, but we won't fix it for the final release (too late for that). We might fix it for a later release though.

rolfbjarne avatar May 19 '22 09:05 rolfbjarne

@rolfbjarne I have the same problem with .NET MAUI, in xamarin forms macos this solution works fine.

angelru avatar Aug 31 '22 09:08 angelru

@angelru you're saying that adding a "lib" prefix to your native library doesn't work? If so, it's a different bug, which means a new issue should be filed.

rolfbjarne avatar Aug 31 '22 10:08 rolfbjarne

@rolfbjarne Captura In a Xamarin project this works fine.

angelru avatar Aug 31 '22 10:08 angelru

@angelru yes, that's a different issue. I'm not sure what's going on, but I believe that should be filed in https://github.com/dotnet/maui/issues.

rolfbjarne avatar Aug 31 '22 10:08 rolfbjarne

@angelru yes, that's a different issue. I'm not sure what's going on, but I believe that should be filed in https://github.com/dotnet/maui/issues.

Thanks

angelru avatar Aug 31 '22 10:08 angelru

@rolfbjarne

 ld: building for Mac Catalyst, but linking in dylib built for macOS, file 'obj/Debug/net6.0-maccatalyst/maccatalyst-x64/nativelibraries/libempty-pkcs11-x64.dylib' for architecture x86_64
        clang: error: linker command failed with exit code 1 (use -v to see invocation)

I really don't know how to fix this, is it .NET MAUI or Pkcs11Interop?

angelru avatar Aug 31 '22 10:08 angelru

It's probably Pkcs11Interop, but if you get a binary buildlog, I could have a look.

rolfbjarne avatar Aug 31 '22 11:08 rolfbjarne

@rolfbjarne msbuild.zip

angelru avatar Aug 31 '22 12:08 angelru

@angelru you're including Libs/libempty-pkcs11-x64.dylib in your main project file:

<NativeReference Include="Libs/libempty-pkcs11-x64.dylib">
	<Kind>Dynamic</Kind>
	<SmartLink>False</SmartLink>
</NativeReference>

that has to be a library built for Mac Catalyst. You'll have to contact the people you got the library from, and ask them for a library built for Mac Catalyst (a library built for macOS does not work).

rolfbjarne avatar Aug 31 '22 12:08 rolfbjarne

@angelru you're including Libs/libempty-pkcs11-x64.dylib in your main project file:

<NativeReference Include="Libs/libempty-pkcs11-x64.dylib">
	<Kind>Dynamic</Kind>
	<SmartLink>False</SmartLink>
</NativeReference>

that has to be a library built for Mac Catalyst. You'll have to contact the people you got the library from, and ask them for a library built for Mac Catalyst (a library built for macOS does not work).

https://github.com/Pkcs11Interop/empty-pkcs11/issues/2

angelru avatar Aug 31 '22 12:08 angelru

@angelru try building the library with catalyst target as seen here: https://developer.apple.com/forums/thread/122571

clang -target x86_64-apple-ios-macabi \
-arch arm64 -arch x86_64 \
-isysroot `xcrun --sdk macosx --show-sdk-path` \
-miphoneos-version-min=13.0 \
-fembed-bitcode \
main.c

mmoraga avatar Aug 31 '22 12:08 mmoraga

@angelru try building the library with catalyst target as seen here: https://developer.apple.com/forums/thread/122571

clang -target x86_64-apple-ios-macabi \
-arch arm64 -arch x86_64 \
-isysroot `xcrun --sdk macosx --show-sdk-path` \
-miphoneos-version-min=13.0 \
-fembed-bitcode \
main.c

I don't know very well where I have to put those commands, in the build.sh?

angelru avatar Aug 31 '22 12:08 angelru