Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Swift Static Libraries

Building Swift Static Libraries

Samuel E. Giddins

July 13, 2018
Tweet

More Decks by Samuel E. Giddins

Other Decks in Technology

Transcript

  1. @segiddins Mobile Developer Experience @ Square CocoaPods Core Contributor 2

    — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  2. Mobile Developer Experience ⇢Owns the build system, dependency manager, interface

    with CI ⇢Maintains the (open source) tools used to build the app ⇢Keeps developers as productive as possible ⇢Cross platform: iOS & Android ⇢Develops CocoaPods features 3 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  3. CocoaPods @ Square ⇢265 total pods ⇢Swift, Objective-C ⇢Dynamic Frameworks,

    Static Libraries, Static Frameworks ⇢Monorepo & Multirepo 4 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  4. A (Brief) History of Swift Linkage ⇢SwiftyThing.framework From Xcode ⇢

    SwiftyThing (as a dynamic library) ⇢libSwiftyThing.a From Swift PM ⇢... And that's it! 5 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  5. Until Xcode 9 6 — Building Swift Static Libraries –

    Samuel Giddins @ iOS Dev Camp DC 2018
  6. Xcode 9 added the ability to build Swift into static

    libraries 7 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  7. Why static libraries? ⇢Easier to obfuscate / strip dead code

    ⇢No overhead on app launch ⇢Possible to have true single-file executables 8 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  8. Aside: This also means you can now build Swift static

    frameworks, if you like! 9 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  9. What static libraries don't fix: ⇢ABI stability ⇢Module format stability

    ⇢Clang Modules ⇢Swift compiler performance ⇢Buggy code 10 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  10. But: They did allow us to start building Swift pods

    11 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  11. So, how do these magic new libraries work? 12 —

    Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  12. Swift Static Libraries, In Practice 13 — Building Swift Static

    Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  13. Swift Static Libraries, In Practice Surprisingly simple! 1. Create an

    app / command line target 2. Create a static library target 1. Delete Objective-C files 2. Add a Swift file 3. Link the static library into the app 4. Build & Run 5. Profit‽ 14 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  14. But: Only if you don't want to use any of

    the code in the static library in another target 15 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  15. So, no profit. 16 — Building Swift Static Libraries –

    Samuel Giddins @ iOS Dev Camp DC 2018
  16. The hardest part of building Swift static libraries has been

    using them 17 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  17. Frameworks make importing Swift code easy 18 — Building Swift

    Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  18. Importing Swift Code 1. .modulemap 2. Headers 3. .swiftmodule 4.

    Generated -Swift.h Bridging Header 19 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  19. Importing Swift Code Objective-C: $ clang -F path/to/framework -f FrameworkName

    ... Swift: $ swiftc -F path/to/framework -f FrameworkName ... 20 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  20. Importing Swift Code Piece of cake with frameworks! (Seriously, frameworks

    are amazing) 21 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  21. On the other hand: Static librarys Manual integration 22 —

    Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  22. Aside: Most of this is all trivia I learned integrating

    Swift static library support into CocoaPods. If I could do it all again, I would've just made static frameworks work and call it a day. 23 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  23. Compilers Magic But constructing correct compiler arguments by hand is

    tedious and error prone and it's 2018 and no one should ever need to do it 25 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  24. ⚠ Detour into Clang Modules 26 — Building Swift Static

    Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  25. ! I gave a talk about this particular, highly painful,

    subject at try! Swift Tokyo 2018 27 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  26. Clang Modules & Swift Modules are highly related 28 —

    Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  27. For Swift to import Objective-C: ⇢Bridging Header ! ⇢Modules for

    the imported code For Objective-C to import Swift: ⇢Directly import generated -Swift.h header ⇢Import the generated module 29 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  28. Seeing a pattern here? 30 — Building Swift Static Libraries

    – Samuel Giddins @ iOS Dev Camp DC 2018
  29. Modules are the lingua franca by which Swift and Objective-C

    are meant to bridge 31 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  30. Modules are created automatically when building frameworks in Xcode 32

    — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  31. This is where the heavy lifting comes in for manually

    integrating static libraries 33 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  32. Clang: $ clang -fmodule-map-path=path/to/Library.modulemap Swift: $ swiftc -Xcc -fmodule-map-path=path/to/Library.modulemap -I

    dir/of/other/library.swiftmodule 34 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  33. This is what enables imports to work Objective-C: @import Library;

    Swift: import Library 35 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  34. Why does being able to use imports matter? ⇢It allows

    libraries to import other libraries ⇢It gets rid of buggy bridging headers ⇢Modular imports are semantic, and don't need to be tied to particular header names 36 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  35. How do I mix Swift & Objective-C in one library?

    37 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  36. How do I mix Swift & Objective-C in one library?

    Trivial with frameworks. ⇢Objective-C headers in the module map are immediately usable from Swift ⇢#import <Library/Library-Swift.h> from Objective-C 38 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  37. How do I mix Swift & Objective-C in one library?

    ⇢Pass the module map to both clang & swiftc ⇢Pass the path to the generated Objective-C interface to clang ⇢Experience all new compiler segfaults 39 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  38. Your developers were so preoccupied with whether or not they

    could, they didn't stop to think if they should. — Jurassic Park 40 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  39. Should I... ⇢Mix Swift & Objective-C? ⇢Mix static libraries &

    dynamic frameworks? ⇢Mix static libraries & static frameworks? 41 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  40. But Samuel, what if that looks like a lot of

    work? 42 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  41. Never fear! We're programmers, so we have pre-written solutions! 43

    — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  42. Pre-written solutions: ⇢Swift Package Manager ⇢CocoaPods ⇢ ❌ Xcode 44

    — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  43. In CocoaPods ⇢Released in v1.5 ⇢Mixed Swift / Objective-C libraries

    ⇢Objective-C & Swift can both import from Swift static libraries ⇢Just remove use_frameworks! ⇢You're ! good to go 46 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  44. Known Rough Edges ⇢Mixing (precompiled) bridging headers & modules ⇢Swift

    autocomplete ⇢The path of the Swift generated interface to import ⇢Too many compiler flags ⇢None of this was meant to work in this way. ⇢At all. 47 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  45. So, Samuel, if it's all so terrible, why did you

    add support in CocoaPods and start using it at work? 48 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  46. We didn't know what we were getting into. Honestly. 49

    — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018
  47. Swift is the Future 50 — Building Swift Static Libraries

    – Samuel Giddins @ iOS Dev Camp DC 2018
  48. Swift is the Future Being able to write it now,

    and write libraries with Swift, is worth the tradeoff. It's better than developing a massive hairball or ignoring the future of  platform development. 51 — Building Swift Static Libraries – Samuel Giddins @ iOS Dev Camp DC 2018