May 13, 2021

Automate framework generation for a modular iOS project

Suppose you have a modular project and you need to ship the frameworks for different projects of your workspace to clients. Now, also take into consideration that some of your clients want to have iphoneos frameworks and some want the fat binaries i.e., universal frameworks.The usual approach is to build the projects for ‘any iOS device’ in Xcode and then send the frameworks, but this is only good if they want iphoneos release. The main problem starts to come if the clients want a universal build, here you have to lipo the executables and then copy the swift modules and also modify the headers for each and every framework, now assume if you have 14 such frameworks, how much frustrating a release would be for you.At our company, I faced the same issue and came up with a script that solves this problem, let’s breakdown the problem statement and then solve the atomic sub-problems.__Problem 1: What shall be my folder structure?__![Folder structure](//images.ctfassets.net/urdv1oztwvyo/33KdvCXHhCKUGYCWtRIPgO/b9097182243003ea69f23fd0148b174a/1_gpvYi5azAg9Pi8pOdSW3cw.png)I have created 3 folders ‘Build’, ‘BuildSim’, and ‘BuildUni’, the names are self-explanatory where Build is for iphoneos frameworks. I have also kept the script file at the same level.__Problem 2: How do I build the frameworks from command line?__![Config variables](//images.ctfassets.net/urdv1oztwvyo/2o4mQH49MYzDgm4353X5kC/0e1f94e287c9584be99f6264bb1f691b/1_j99XaGAbBLRlmI7Zk7OjxA.png)First we define some config variables that we can change as per our build’s requirement.One issue with my project is, it has to be compiled in a certain order. So. we’ve created different methods to handle this.![Methods for building the frameworks](//images.ctfassets.net/urdv1oztwvyo/6J3u50uj9MZqlTUo99wAy1/2fb5fd4822331357f3dfc06d2caf27dc/1_PkaWSyT8ox3ZMzmXn-HYeg.png)We basically make use of the xcodebuild command and specify our parameters to build the project.One issue that I found, while building for iphonesimulator from command line: It was not generating the executable, neither the *-swift.h headers were generated for the frameworks. So, before running this script I just run the project on the simulator through Xcode (as I’d also have to verify the project before releasing it).__Problem 3: How do I create universal frameworks?__Once the script generates the frameworks for iphoneos, and as the frameworks are already generated for the simulator. I use the method below, to copy the iphoneos and iphonesimulator frameworks from Derived Data.![Image 1](//images.ctfassets.net/urdv1oztwvyo/1uUUQAdYtckYe8oZakl6tQ/442f6fb529cdadaaaa62ee88ac2fbee5/1_SeQ0GqozBnqzKX4iji3VoQ.png)Let’s now do some magic!![Image 2](//images.ctfassets.net/urdv1oztwvyo/bMOqm6gsObwWcYZLyBpbr/3eae617c312ad4b225158f47b4280679/1_Tw3SANuDfvYpYXQBHq5bww.png)First we copy all the iphoneos frameworks from Build directory to the BuildUni directory, this gives us the executable, headers, and swift modules of the iphoneos frameworks.Second, we copy all the swift modules from the simulator frameworks in BuildSim folder to the swiftmodules inside the frameworks in BuildUni folder. Now all our frameworks inside BuildUni folder has the swift modules of both iphoneos and iphonesimulator. Neat! 😃Third, we make use of lipo command to create the fat binaries, it basically combines all the frameworks present in the two input binaries and saves it as one binary at the output location. Almost done! 🍭Now we need to edit the headers:The header for our frameworks inside BuildUni currently looks like this, it has #if 0 and then starts with #elif (same as of the iphoneos framework), we basically need to change the #if 0 with #if TARGET_ OS _SIMULATOR and inside this if condition we need to paste all the content of the header of the iphonesimulator framework.![Image 3](//images.ctfassets.net/urdv1oztwvyo/13voxPdTyG8AlJpuFle7Pz/b751cf518c2489f9437992b742dbceaa/1_LxKttg1_g8iH6Q1v0bK9JQ.png)Now this is our final nail in the coffin!![Image 4](//images.ctfassets.net/urdv1oztwvyo/7CPMhJehIcbzTr9AnEQO22/155f129a0dc8cf835515fd7b1a468781/1_TsR2TotFMZ6N2SGTQPObNA.png)First, we remove the 1st line from the universal framework’s header (the #if 0 line)Now, we create an intermediate file to handle the operations, as the output redirection operator (>>) appends the contents.In this blank file, we first insert #if TARGET_ O S_SIMULATORAfter that, we append all the content of the header from the simulator framework to the intermediate file.Now, we append the content of the header from the universal framework (Remember, we had removed the first line from this) to the intermediate file.Almost done, now just some cleanup. We now delete the header file inside universal framework, then move the intermedate file inside the universal framework’s header and rename it to be of the same name as the framework. After that we delete backup files that were created with sed. As, the sed command for BSD based OS (sed -i ‘.bak’ ‘1d’ “file”), is slightly different than Linux (sed -i ‘1d’ “file”) and requires to specify a backup extension.Now you have iphoneos, iphonesimulator and universal frameworks, all created in less than 30mins (for my workspace of 14 projects), which with manual effort used to easily take at least 4 hours!The complete script: https://snippet.host/pprs

RELATED ARTICLES

No items found.
Written by
Full Name
Published on
22 January 2021

Get a free consultation on Segmentation strategy and
Ecosystem practices

Drop us a line.
Thank you! We'll be in touch soon.
Oops! Something went wrong while submitting the form.
Schedule a demo
Let’s Connect
We’ve received your request. We’ll be in touch soon.
Oops! Something went wrong while submitting the form.
Thank you! You can download the PDF now.
Download PDF
Oops! Something went wrong while submitting the form.