Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M1 support #1620

Merged
merged 14 commits into from
Sep 3, 2021
Merged

M1 support #1620

merged 14 commits into from
Sep 3, 2021

Conversation

Kylmakalle
Copy link
Contributor

@Kylmakalle Kylmakalle commented Jul 19, 2021

While tdlib can be built for arm64 devices, it still lacks support of arm64 simulators and macOS targets. That means we're required to use EXCLUDED_ARCHS = arm64 in XCode development and even use Apple Rosetta on M1 macs which makes some people struggle (#1555, tdlight-team/tdlight-java#28) building tdlib.

This PR introduces arm64 support for macOS & simulator targets

  • Patch OpenSSL with separate simulators target (cause lipo can't merge multiple platforms with same arch).
    Results in DOUBLED time to build openSSL for non macOS platforms 😞
  • Patch OpenSSL with macOS arm64 target without any asm optimizations.
    Any optimizations possible only for OpenSSL 1.1.1 which is another TODO
  • Added arm64 in tdlib iOS.cmake destinations for simulators
  • Added arm64 option in build.sh for cmake macOS targets
  • Change build scripts to provide separate binaries and linking for devices and simulators separately
  • Combine result dylib's to .xcframework
  • @levlam Describe -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" for macOS build in main readme.md
  • (Optional) Provide test project that will cover initial framework implementation

@Kylmakalle Kylmakalle changed the title M1 support WIP: M1 support Jul 19, 2021
@levlam
Copy link
Contributor

levlam commented Jul 21, 2021

CMake has basic framework generation support: https://cmake.org/cmake/help/v3.5/prop_tgt/FRAMEWORK.html#prop_tgt:FRAMEWORK.

But I'm not sure, whether it could be useful for multi-arch builds.

@Kylmakalle
Copy link
Contributor Author

Kylmakalle commented Jul 21, 2021

CMake has basic framework generation support:
But I'm not sure, whether it could be useful for multi-arch builds.

That's not our option for tdlib unfortunately, we still unable to link and pack it in one framework. Here's note in CMake Gitlab thread which suggests to move on xcframeworks as well.

@Kylmakalle
Copy link
Contributor Author

Kylmakalle commented Jul 21, 2021

I assume that I've reached this PR goal.
Now we're just replacing bare dylibs with xcframeworks that includes arm64 to be able to run tdlib on M1 nativelly.
🛠 Full build example on Github Actions

This PR does not optimize arm code, library packaging, linking, headers and so on.
I would rather make another PR for Readme.md and link my standalone repo that provides pre-build binary in modern Swift Package and App example to quickly jump into development.

@Kylmakalle Kylmakalle marked this pull request as ready for review July 21, 2021 22:00
@Kylmakalle Kylmakalle changed the title WIP: M1 support M1 support Jul 21, 2021
@levlam
Copy link
Contributor

levlam commented Jul 26, 2021

Currently, with this patch applied we have problems with ordinary build on macOS without the example usage, because brew-installed OpenSSL supports only one architecture.

@levlam
Copy link
Contributor

levlam commented Jul 26, 2021

Probably, CMAKE_OSX_ARCHITECTURES shouldn't be changed in the main CMakeLists.txt, only passed in build.sh as another argument to cmake on "macOS" platform.

@Kylmakalle
Copy link
Contributor Author

Kylmakalle commented Aug 2, 2021

Moved -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" as custom flag for build.sh
🛠 Github Actions Build

@levlam Upon accepting this MR, add docs to main readme please.
Cause binary will not be build for arm64 by default on Intel (x86_64) CPUs.
You can phrase it as you like or even use GitHub "edit as maintainer" thing.

@Kylmakalle
Copy link
Contributor Author

Kylmakalle commented Aug 2, 2021

P.S. I've faced same issue in short investigation with brew-installed openssl. Will dive a bit more deeply.

@Kylmakalle
Copy link
Contributor Author

Kylmakalle commented Aug 3, 2021

Universal pre-built OpenSSL binary installation can be achieved with MacPorts

sudo port install openssl
...
--->  Attempting to fetch openssl-1.1.1k_0.darwin_20.x86_64.tbz2 from https://packages.macports.org/openssl

file /opt/local/bin/openssl   
/opt/local/bin/openssl: Mach-O 64-bit executable x86_64

using +universal flag which brew lacks of due to removal in 2017 (current build script)

sudo port install openssl
...
--->  Attempting to fetch openssl-1.1.1k_0+universal.darwin_20.arm64-x86_64.tbz2 from https://packages.macports.org/openssl

file /opt/local/bin/openssl   
/opt/local/bin/openssl: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64:Mach-O 64-bit executable arm64]
/opt/local/bin/openssl (for architecture x86_64):	Mach-O 64-bit executable x86_64
/opt/local/bin/openssl (for architecture arm64):	Mach-O 64-bit executable arm64

Make sure to install OpenSSL version >= 1.1.1i cause it officially supports arm64

P.S. I also prefer brew more but this missing flag sucks :)

@levlam levlam merged commit ab4736d into tdlib:master Sep 3, 2021
@levlam
Copy link
Contributor

levlam commented Sep 3, 2021

Thanks @Kylmakalle for these awesome changes! Sorry that merging this took so long.

Kylmakalle added a commit to Swiftgram/TDLibFramework that referenced this pull request Sep 4, 2021
Adapted Changes in tdlib tdlib/td#1620
@sapoepsilon
Copy link

@levlam Would you mind adding this page and changing this to Swiftgram in README, since this wrapper is not updated anymore. It could have saved me about 2 hours if I knew it was there. I had to manually find it through issues. Thank you!

@Kylmakalle Thanks for the update! I was able to build the TDLib with your instructions on M1. Is there any difference between using your wrapper for M1 and using the TDLib?

@Kylmakalle
Copy link
Contributor Author

@sapoepsilon TDLibKit is a Swift wrapper with pre-generated classes, types and a little "sugar". TDLibFramework is just a pre-built fat xcframework binary. Both now based on recent TDLib commits with M1 support as well. More info in each project READMEs.

@levlam
Copy link
Contributor

levlam commented Sep 12, 2021

@sapoepsilon This is a merged pull request, so all changes are present in the master branch. Swiftgram/TDLibKit has just been added to the list of examples as you suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants