Pkcs11Interop / empty-pkcs11

PKCS#11 library with the simplest possible implementation
Apache License 2.0
20 stars 10 forks source link

dotnet6.0-maccatalyst: Linking fails on native reference dylib #2

Closed angelru closed 4 months ago

angelru commented 2 years ago

Hello,

I can't get your library to work for MacCatalyst

More info: https://github.com/dotnet/maui/issues/9808 https://github.com/xamarin/xamarin-macios/issues/15044

mmoraga commented 2 years ago

just edit the macOS Makefile to include the necessary flags for catalyst and you'll get a fat binary to use for the catalyst platform.

https://github.com/Pkcs11Interop/empty-pkcs11/blob/2d4de20cbbb14ac251fc5e43f1f12728b5d377ba/build/osx/Makefile

angelru commented 2 years ago

just edit the macOS Makefile to include the necessary flags for catalyst and you'll get a fat binary to use for the catalyst platform.

https://github.com/Pkcs11Interop/empty-pkcs11/blob/2d4de20cbbb14ac251fc5e43f1f12728b5d377ba/build/osx/Makefile

but in which line do I add this?

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 commented 2 years ago

try changing

CC= gcc
ARCH_FLAGS= -m32

to

CC= clang
ARCH_FLAGS= -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

but you'll have to play around with it yourself a bit

jariq commented 2 years ago

@angelru could you please help me to understand the root cause of this issue by providing more high-level description of your problem? By high-level I mean:

angelru commented 2 years ago

@jariq Hello, the application compiles and works with Xamarin.macOS but when creating it with .NET MAUI I get this error:

  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)

The app runs on windows desktop and mac.

angelru commented 2 years ago

try changing

CC= gcc
ARCH_FLAGS= -m32

to

CC= clang
ARCH_FLAGS= -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

but you'll have to play around with it yourself a bit

Thanks, but this keeps generating the same file for me.

angelru commented 2 years ago

@mmoraga @jariq I can't get it to compile a .NET MAUI application, can you help me?

jariq commented 2 years ago

@angelru I was trying to replicate your issue in my environment over the weekend and ended up with MAUI app working on Windows and not building on macOS. Up until now I didn't have enough time to analyze why it's not building. I don't when I'll be able to continue.

angelru commented 2 years ago

@jariq Indeed, on windows it works fine, but on macOS this warning appears:

  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)

It seems that libempty-pkcs11-x64.dylib needs to be generated for Mac Catalyst, but I haven't been able to.

mmoraga commented 2 years ago

I managed to get it to build by editing the Makefile ARCH flags to:

ARCH_FLAGS= -target x86_64-apple-ios14.8-macabi -arch arm64 -arch x86_64 -isysroot `xcrun --sdk macosx --show-sdk-path` -miphoneos-version-min=14.8 -fembed-bitcode

then building a minimal maccatalyst project succeeded:

% dotnet build
MSBuild version 17.3.0+92e077650 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  Detected signing identity:

    Bundle Id: com.companyname.catalyst_pkcs
    App Id: com.companyname.catalyst_pkcs
  catalyst-pkcs -> /Users/manuel.moraga/Documents/catalyst-pkcs/bin/Debug/net6.0-maccatalyst/maccatalyst-x64/catalyst-pkcs.dll
  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.94

PR is here: https://github.com/Pkcs11Interop/empty-pkcs11/pull/3

angelru commented 2 years ago

I managed to get it to build by editing the Makefile ARCH flags to:

ARCH_FLAGS= -target x86_64-apple-ios14.8-macabi -arch arm64 -arch x86_64 -isysroot `xcrun --sdk macosx --show-sdk-path` -miphoneos-version-min=14.8 -fembed-bitcode

then building a minimal maccatalyst project succeeded:

% dotnet build
MSBuild version 17.3.0+92e077650 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  Detected signing identity:

    Bundle Id: com.companyname.catalyst_pkcs
    App Id: com.companyname.catalyst_pkcs
  catalyst-pkcs -> /Users/manuel.moraga/Documents/catalyst-pkcs/bin/Debug/net6.0-maccatalyst/maccatalyst-x64/catalyst-pkcs.dll
  Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.94

PR is here: #3

Can you show the csproj file?

mmoraga commented 2 years ago
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0-maccatalyst</TargetFramework>
    <RuntimeIdentifier>maccatalyst-x64</RuntimeIdentifier>
    <RootNamespace>catalyst_pkcs</RootNamespace>
    <OutputType>Exe</OutputType>
    <Nullable>enable</Nullable>
    <ImplicitUsings>true</ImplicitUsings>
    <SupportedOSPlatformVersion>14.2</SupportedOSPlatformVersion>
  </PropertyGroup>
  <ItemGroup>
  <NativeReference Include="libempty-pkcs11-catalyst.dylib">
      <Kind>Dynamic</Kind>
      <IsCxx>False</IsCxx>
  </NativeReference>
  </ItemGroup>
</Project>
angelru commented 2 years ago

It works very well!

angelru commented 1 year ago

@mmoraga @jariq https://github.com/Pkcs11Interop/Pkcs11Interop/issues/215

It seems that with the new versions of .NET it does not connect with the token, did you get an error?

angelru commented 1 year ago

@mmoraga Compile compiles and before it worked but now it doesn't, does it work for you?

mmoraga commented 1 year ago

I don't actually use maccatalyst so I don't really know.

angelru commented 1 year ago

@mmoraga Would you mind trying? see if you see anything?

here I put the error:

https://github.com/Pkcs11Interop/Pkcs11Interop/issues/215

angelru commented 1 year ago

@mmoraga @jariq I understand that there is no solution for now right?

angelru commented 6 months ago

is there news?

jariq commented 6 months ago

Nope. I am not currently working on anything MAUI or maccatalyst related.

jariq commented 4 months ago

Since 11586bed134d5034b6d75c7013948f33fc444c49 the macOS version of empty-pkcs11 library is built for both x86_64-apple-macos10.12 and arm64-apple-macos11 targets and then merged into a single Mach-O universal binary (empty-pkcs11.dylib) usable on both Apple silicon and Intel-based Mac computers. This version is also working in a Catalyst app on macOS so I am closing this issue now.

Unresolved Pkcs11Interop related problems with Catalyst apps will be further discussed in https://github.com/Pkcs11Interop/Pkcs11Interop/issues/215