Xcode New Build System for Speedy Swift Builds
Note: This post was originally published on XCBlog, read original post for better graphics and without missing any content here
With Xcode 9, Apple announced the new build system at WWDC 2017. The new Xcode build system is written from scratch and purely in Swift. However, for some reason, it’s not enabled by default and available as the preview. Apple said that they will make it default in the later version of Xcode. It means we have to accept this build system in future without any questioning. In this short post, we will see how to use Xcode New build system to speed up the Swift build time of iOS apps.
Xcode New Build System
Yes. The “Xcode New Build System” that’s what they call it, they couldn’t think of the proper name to this build system. In order to know more about Xcode new build system DON’T watch this WWDC 2017 session “What’s New in Swift” at which new build is announced. The person literally spent only 40 seconds to talk about new build system so there is nothing to learn there, instead, google about it or keep reading this post. Xcode new build system is available in Xcode 9+. As an iOS Engineer, you might be pressing CMD+B or CMD+R hundreds of time during the day and watch Xcode compiling the stuff for a long time. Currently, Xcode uses the “Standard Build System” by default which is the bit slow in compiling the Swift projects. In order to improve the compile and build time of the Swift projects, Apple has launched this brand new “New Build System” which promises to be advance in the area including the performance and dependency management. As per the Apple documentation on What’s New Xcode 9, new build system should be able to
- Provide higher reliability
- Catch project configuration bugs
- Improve the overall build performance
How to enable New Build System
There are multiple ways we can enable new build system for our iOS project.
It’s the great idea to enable the new build system from Xcode itself so that we can use all the features while developing apps. With, Xcode 9, it’s fairly easy to enable the new build setting. You might have Xcode project or Xcode workspace for your project, In Xcode, Select File -> Project/Workspace Setting. You will see a Build System option to select the new build system as shown below
One this is enabled, you should see the blue hammer.
From Command Line
If you are building an iOS project from the command line using the xcodebuild then we have to pass additional parameter -UseNewBuildSystem=YES will also force the new build system. If you are building project with Fastlane Gym then I am not sure this additional flag is implemented in the Gym.
As per this confusing Tweet from engineering manage from Apple who manages new build system project. We can also enable new build system using the flag in Xcode 9.2+
$ defaults write com.apple.dt.Xcode BuildSystemScheduleInherentlyParallelCommandsExclusively -bool NO
What Changed in Xcode
When you enabled the ‘New Build System’ the only change you will see in the workspace or project setting is
It just adds the one key to the project settings and that’s all. The new system doesn’t make any changes to build settings or recommend you to make any changes. It means moving to new build system is fairly easy without the need to learn new build settings. We get the benefit of supposedly faster, brand new and modern build system without touching the build settings.
New Build System Under The Hood
As we have seen that, there isn’t anything changed in the Xcode build setting, you might be wondering where is this faster Swift build magic happens. Basically, the new build system is written on top of the swift-lldb project. Apple seems to have build entire framework that is shipped with Xcode and that framework make this all sort of faster Swift build magic.
Tell You Real Name of New Build System
If you look into the Xcode App and navigate to this directory
$ cd /Applications/Xcode.app/Contents/SharedFrameworks
You will see all the frameworks that shipped with Xcode. If you are not comfortable with command line, then you can reach there using Finder as well. From Finder, Go->Go To Folder and enter the above path. This ends the surprise of the real name of the New Build System. You can find the framework called XCBuild.frameworks and I hope that’s the real name of the New Build System. YAY. We found out, it’s XCBuild
It’s easy to remember, you reading XCBlog so the build system is XCBuild as simple as that. However, it’s not the same as the build tool that Facebook has built with the same name. GitHub repo for the Facebook’s xcbuild is here
The binary of the Apple’s xcbuild is located at this weird path:
The role of this binary is out of the scope of this post but you can add this to $PATH and explore the options.
Build Time Benchmark
We trust Apple but in order to verify that the new build system is really fast or it’s just bollocks, you can enable this on your own project and see the build time by yourself. It is designed to work best on Swift project so try it on the Swift project and you will be amazed at the build time improvement.
How to Perform Build Time Benchmark Test
Before you start the test, you should do this setting which shows the build time in the Xcode as soon as it finishes the building. You can enable the build time in Xcode by running
$ defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES
Now we will see the build time in the Xcode.
- Open your Swift project or workspace in Xcode. Don’t enable the new build system yet
- Clean all the derived data + clean the project and Press CMD+B
- Let the project build and take a note of the build time.
- Run the build again and note the time of the incremental builds
- Now enable the new build system from the Project/workspace setting.
- Repeat step 2 until 4
- Observer the magical improvement in the build time.
I have done, this test on some of the project and found the major improvement in the build. The new build system is average 30% faster on the clean build and 75% faster on the incremental builds. Don’t trust anyone. Just try it yourself.
In order to demo this, I have created an empty single view iOS app called ‘NewBuildSystem-Demo’. I performed the benchmark test as mentioned above and saw the amazing build time improvement.
With current standard build system, it took 12 seconds to build and clean build
However, when I enabled the new build system then it just took 5 seconds or the clean build.
The subsequent incremental builds take just 0.037s.
As you can see the new build system is lightning fast. You should enable this build system as soon as possible and see how your projects perform on the new build system.
As you might have seen that there are some issues that mentioned on the stack overflow or some other channels but most of them are related to Objective-C or Cocoapods related. It shouldn’t impact on Swift project unless you have crazy setup. Some of the popular open issues are
- New Build System reports dependency cycle in Xcode. Reported on Twitter here
- New build system resets the build number when using scripted builds. Reported on Twitter here
- Bunch of issues open in the Cocoapods Github repository.
As Apple made a significant improvement in order to speed up the Swift builds in the new build system. We should take the benefit of this feature by enabling our projects. The new builds are incredibly faster and can be enabled without changing the current build settings. In the near future, the new build system will be default so sooner or later we have to migrate our projects to the new build system. So don’t wait to enable the new build system now. Let me know what happens when you enable new build system in the comment below.
Like this post from XCBlog By XCTEQ ? You may also like some of our services like guest blogging or Mobile DevOps(CI/CD) or Test Automation. Chekout our services, open source projects on Github or Follow us on Twitter , Facebook, Youtube , LinkedIn. Download Our XCBlog iOS App to read the blogs offline.