Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

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. @segiddins CocoaPods, Bundler, RubyGems Mobile Developer Experience @ Square Exploring

    Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 2
  2. 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
  3. main.c extern void printMyName(char *); int main(int argc, char **argv)

    { printMyName("segiddins"); } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 5
  4. #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
  5. // header.h #ifndef __HEADER_H #define __HEADER_H ... #endif // #ifndef

    __HEADER_H Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 7
  6. UsefulFramework.modulemap / module.modulemap framework module UsefulFramework { umbrella header "UsefulFramework.h"

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

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

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

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

    module * { export * } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 15
  11. Module Maps, the hard part Lessons from CocoaPods Exploring Clang

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

    Textual Headers • ... • Automatic Module Map Discovery • Relative Paths Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 19
  13. 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
  14. Private Headers framework module UsefulFramework { umbrella header "UsefulFramework.h" private

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

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

    blocks, !altivec } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 25
  17. 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
  18. 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
  19. Relative Paths framework module UsefulFramework { header "UsefulFramework.h" } Exploring

    Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 29
  20. But my Swift code doesn't have headers... Exploring Clang Modules

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

    Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 32
  22. 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
  23. Extended Module Maps module ModuleName.Swift { header "ModuleName-Swift.h" requires objc

    } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 35
  24. 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
  25. 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
  26. 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
  27. 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