$30 off During Our Annual Pro Sale. View Details »

Exploring Clang Modules

Exploring Clang Modules

Given at try! Swift Tokyo 2018

Samuel E. Giddins

March 01, 2018
Tweet

More Decks by Samuel E. Giddins

Other Decks in Technology

Transcript

  1. Exploring Clang Modules Samuel Giddins 1

  2. @segiddins CocoaPods, Bundler, RubyGems Mobile Developer Experience @ Square Exploring

    Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 2
  3. Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo

    2018 3
  4. cool_function.h #define AUTHORS_NAME "segiddins" extern void printMyName(char *); main.c #include

    "cool_function.h" int main(int argc, char **argv) { printMyName(AUTHORS_NAME); } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 4
  5. main.c extern void printMyName(char *); int main(int argc, char **argv)

    { printMyName("segiddins"); } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 5
  6. #include "header.h" #define MAGIC_NUMBER 42 ... ... ... #include "header2.h"

    // also includes header.h preprocessed into: static int secretNumber = -1; static int secretNumber = 42; Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 6
  7. // header.h #ifndef __HEADER_H #define __HEADER_H ... #endif // #ifndef

    __HEADER_H Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 7
  8. #import "header.h" Exploring Clang Modules – Samuel Giddins @ try!

    Swift Tokyo 2018 8
  9. Enter Module Maps Exploring Clang Modules – Samuel Giddins @

    try! Swift Tokyo 2018 9
  10. Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo

    2018 10
  11. UsefulFramework.modulemap / module.modulemap framework module UsefulFramework { umbrella header "UsefulFramework.h"

    export * module * { export * } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 11
  12. UsefulFramework.modulemap framework module UsefulFramework { umbrella header "UsefulFramework.h" export *

    module * { export * } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 12
  13. UsefulFramework.modulemap framework module UsefulFramework { umbrella header "UsefulFramework.h" export *

    module * { export * } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 13
  14. UsefulFramework.modulemap framework module UsefulFramework { umbrella header "UsefulFramework.h" export *

    module * { export * } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 14
  15. UsefulFramework.modulemap framework module UsefulFramework { umbrella header "UsefulFramework.h" export *

    module * { export * } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 15
  16. main.swift import UsefulFramework main.m @import UsefulFramework; Exploring Clang Modules –

    Samuel Giddins @ try! Swift Tokyo 2018 16
  17. Module Maps, the hard part Exploring Clang Modules – Samuel

    Giddins @ try! Swift Tokyo 2018 17
  18. Module Maps, the hard part Lessons from CocoaPods Exploring Clang

    Modules – Samuel Giddins @ try! Swift Tokyo 2018 18
  19. • Umbrella Directories • Explicit Submodules • Private Headers •

    Textual Headers • ... • Automatic Module Map Discovery • Relative Paths Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 19
  20. Umbrella Directories framework module UsefulFramework { umbrella "HeadersDirectory" } Exploring

    Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 20
  21. Explicit Submodules Exploring Clang Modules – Samuel Giddins @ try!

    Swift Tokyo 2018 21
  22. Explicit Submodules framework module UsefulFramework { umbrella header "UsefulFramework.h" explicit

    module ForSubclassEyesOnly { header "ComplicatedTextField_subclass.h" } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 22
  23. Private Headers framework module UsefulFramework { umbrella header "UsefulFramework.h" private

    header "PrivateHashMapImpl.h" } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 23
  24. Textual Headers framework module UsefulFramework { umbrella header "UsefulFramework.h" textual

    header "AssertMacros.h" } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 24
  25. Requires framework module UsefulFramework { umbrella header "UsefulFramework.h" requires objc_arc,

    blocks, !altivec } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 25
  26. Conflicts framework module UsefulFramework { umbrella header "UsefulFramework.h" conflict PointlessFramework,

    "we do something more useful than that framework" } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 26
  27. Automatic Module Map Discovery $ tree include include !"" baking.h

    !"" cake.h !"" cookie.h !"" module.modulemap Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 27
  28. Relative Paths Exploring Clang Modules – Samuel Giddins @ try!

    Swift Tokyo 2018 28
  29. Relative Paths framework module UsefulFramework { header "UsefulFramework.h" } Exploring

    Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 29
  30. Modules & Swift Exploring Clang Modules – Samuel Giddins @

    try! Swift Tokyo 2018 30
  31. But my Swift code doesn't have headers... Exploring Clang Modules

    – Samuel Giddins @ try! Swift Tokyo 2018 31
  32. But my Swift code doesn't have headers... It does !

    Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 32
  33. swiftc Understands .swiftmodule Files Exploring Clang Modules – Samuel Giddins

    @ try! Swift Tokyo 2018 33
  34. The Mixed Framework Process 1. Compile all .swift files 2.

    Generate ModuleName-Swift.h in the (empty) .framework 3. "Extend" existing module map with .Swift submodule 4. Compile .m files Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 34
  35. Extended Module Maps module ModuleName.Swift { header "ModuleName-Swift.h" requires objc

    } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 35
  36. Same-Module Imports Superclass.m #import <ModuleName/ModuleName-Swift.h> Subclass.swift // nothing to import!

    Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 36
  37. Same-Module Imports swiftc Useful.swift -module-name UsefulFramework -import-underlying-module -emit-module -emit-objc-header -Xcc

    -fmodule-map-file="UsefulFramework.modulemap" Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 37
  38. Swift Static Libraries in CocoaPods All of the changes needed:

    https://github.com/CocoaPods/CocoaPods/pull/6966 Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 38
  39. Are Modules the Future? • Tied to headers • Swift

    module stability • Still confusing • Better than C Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 39
  40. Some Resources • Clang Docs https://clang.llvm.org/docs/Modules.html • Clang Source https://github.com/llvm-mirror/clang/blob/master/include/clang/

    Basic/Module.h • CocoaPods Source https://github.com/CocoaPods/CocoaPods/blob/master/lib/ cocoapods/generator/module_map.rb • Trial & Error Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 40
  41. @segiddins Mobile Developer Experience @ Square Exploring Clang Modules 41