Search code examples
moduleswiftxcode6

What is Swift Compiler - Search Paths Import Paths in Xcode 6 Building Settings?


In Xcode 6 (Beta), there is Swift Compiler - Search Paths, Import Paths. What does it do?


Solution

  • Just to share what I have discovered during days of connecting dots. Short answer, Import Search Path SWIFT_INCLUDE_PATHS specifies where Swift finds and imports modules.

    Modules and Semantic Import

    What are modules? Modules improve access to the API of software libraries by replacing the textual preprocessor inclusion model with a more robust, more efficient semantic model. From the user’s perspective, the code looks only slightly different, because one uses an import declaration rather than a #include preprocessor directive like this:

    import std.io
    

    It was first announced in Nov 2012 by Apple at LLVM DevMeeting. You can still find the Doug Gregor’s talk here (Video and PDF). At WWDC 2013, Semantic Import was introduced on along with iOS 7, the @import was just for it. So it was determinant that module to be part of new language Swift. Documentation of modules can be found here.

    Example - How to import

    To have a taste, below are steps to create an example app project with HTML Tidy library module.

    • Create a Swift project (OS X or iOS) in Xcode 6

    • Create a module.map file, and place it in a directory. E.g. /Users/vladof/module/

       module tidy [system] {
           header "/usr/include/tidy/tidy.h"
           header "/usr/include/tidy/buffio.h"
           link "tidy"
           export *
       }
      
    • Go Build Settings, set Swift Compiler - Search Paths > Import Paths to the directory that you put the module.map file in. /Users/vladof/module in my case. Then you can use import tidy and leverage useful APIs of HTML Tidy library, even in Swift REPL.

    • Import

       import tidy
      
    • Example code

       var input: CString = "<node>upper case node</node>"
       var tdoc: TidyDoc = tidyCreate() // Initialize "document"
       var rc: Int32 = -1
       var ok = tidyOptSetBool(tdoc, TidyUpperCaseTags, yes) // Convert tags to upper cases
       ok = tidyOptSetBool(tdoc, TidyXmlTags, yes) // Convert to XML
      
       if ok.value == 1 {
           rc = tidyParseString(tdoc, input) // Parse the input
           if rc >= 0 {
               rc = tidyCleanAndRepair(tdoc) // Tidy it up
           }
           if rc >= 0 {
               rc = tidySaveStdout(tdoc) // Pretty print to console
           }
       }
      
    • Print

       <NODE>upper case node</NODE>
      

    Conclude

    Also I have experimented with curl module. In fact some APIs are not imported as I test, e.g. curl_easy_setopt(), let’s hope they catch up in near future. But I am positive this opens a door for Swift developers.