WildCat's Blog

Build and Debug Telegram iOS in 2022

Telegram iOS has been open sourced on https://github.com/TelegramMessenger/Telegram-iOS for years.

At its early stages, it uses Buck to build, which is not very easy to set up. Now it has been migrated to Bazel. We can build it in about 20 minutes on M1 Max MacBooks.

Install Bazel (Optional)

This is an optional step to run custom Bazel build. The Python script of Telegram iOS would download Bazel automatically. If you’d like to have bazel on your Mac globally, you can install it with Homebrew:

brew install bazelisk

We prefer bazelisk over bazel because it’s easier to use, especially you need to switch between different versions of Bazel.

Clone the Project and Check Out to a Working Version

You can follow the README to clone the project first:

git clone --recursive -j8 https://github.com/TelegramMessenger/Telegram-iOS.git

Then you may need to checkout to a specific version, for example, git checkout 2a32c8718. In the latest version (702f254), there’s a broken dependency using git submodule.

Follow Other Setup steps

In the README file, there are several steps to create cache and configuration directories we need to follow.

Generate the Xcode Project

To build the project quickly, we don’t need to create our provisioning profiles. It is too time-consuming as there’s no automatic method to create these provisioning profiles.

Instead, we can skip to the last optional step to create the Xcode project. After than, we can build and run Telegram iOS on simulators. For simulator targets, we only need ad-hoc code signatures.

python3 build-system/Make/Make.py \
    --cacheDir="$HOME/telegram-bazel-cache" \
    generateProject \
    --configurationPath="$HOME/telegram-configuration" \
    --disableExtensions \
    --disableProvisioningProfiles

After running this command, you should see a new Xcode project generated and it will be opened automatically.

However, there would be a bug that bazel cannot be downloaded. You need to create the build-input directory manually:

# In Telegram-iOS
mkdir build-input

Additionally, on Intel Macs, the Bazel download script is not working. You need to download it manually:

# In Telegram-iOS
curl -L https://github.com/bazelbuild/bazel/releases/download/5.0.0/bazel-5.0.0-darwin-x86_64 --output build-input/bazel-5.0.0-x86_64
# For Apple Silicon Macs:
curl -L https://github.com/bazelbuild/bazel/releases/download/5.0.0/bazel-5.0.0-darwin-arm64 --output build-input/bazel-5.0.0-darwin-arm64

Side Notes

As of March 2022, the official documentation of Telegram’s reproducible build (https://core.telegram.org/reproducible-builds) is totally outdated. For Telegram iOS, you don’t need to install Parallels Desktop at all. Since they have migrated to Bazel, everything is much easier, even if you don’t know Bazel at all.

Telegram iOS is a great project to research and learn Bazel for iOS. If your team has a fairly large codebase, it’s time to learn bazel. Telegram iOS has over 5 million lines of code as of now, both the clean and incremental builds of it (without remote cache) is super fast.

Appendix: Total LoC of Telegram iOS

➜  Telegram-iOS git:(2a32c8718) ✗ tokei .
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
===============================================================================
 Total               22119      7328103      5315796      1186607       825700
===============================================================================

References