Exploring Clang Modules

Exploring Clang Modules

Given at try! Swift Tokyo 2018

4d6be90af74894fd132fb06dacec04d7?s=128

Samuel E. Giddins

March 01, 2018
Tweet

Transcript

  1. 2.

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

    Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 2
  2. 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
  3. 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
  4. 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
  5. 7.

    // header.h #ifndef __HEADER_H #define __HEADER_H ... #endif // #ifndef

    __HEADER_H Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 7
  6. 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
  7. 12.

    UsefulFramework.modulemap framework module UsefulFramework { umbrella header "UsefulFramework.h" export *

    module * { export * } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 12
  8. 13.

    UsefulFramework.modulemap framework module UsefulFramework { umbrella header "UsefulFramework.h" export *

    module * { export * } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 13
  9. 14.

    UsefulFramework.modulemap framework module UsefulFramework { umbrella header "UsefulFramework.h" export *

    module * { export * } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 14
  10. 15.

    UsefulFramework.modulemap framework module UsefulFramework { umbrella header "UsefulFramework.h" export *

    module * { export * } } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 15
  11. 17.
  12. 18.

    Module Maps, the hard part Lessons from CocoaPods Exploring Clang

    Modules – Samuel Giddins @ try! Swift Tokyo 2018 18
  13. 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
  14. 20.
  15. 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
  16. 23.

    Private Headers framework module UsefulFramework { umbrella header "UsefulFramework.h" private

    header "PrivateHashMapImpl.h" } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 23
  17. 24.

    Textual Headers framework module UsefulFramework { umbrella header "UsefulFramework.h" textual

    header "AssertMacros.h" } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 24
  18. 25.

    Requires framework module UsefulFramework { umbrella header "UsefulFramework.h" requires objc_arc,

    blocks, !altivec } Exploring Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 25
  19. 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
  20. 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
  21. 29.

    Relative Paths framework module UsefulFramework { header "UsefulFramework.h" } Exploring

    Clang Modules – Samuel Giddins @ try! Swift Tokyo 2018 29
  22. 31.

    But my Swift code doesn't have headers... Exploring Clang Modules

    – Samuel Giddins @ try! Swift Tokyo 2018 31
  23. 32.

    But my Swift code doesn't have headers... It does !

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

    Extended Module Maps module ModuleName.Swift { header "ModuleName-Swift.h" requires objc

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