Skip to content

Instantly share code, notes, and snippets.

@ileitch
Last active June 16, 2018 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ileitch/3e43966cf5a59fc083fe5758087423a9 to your computer and use it in GitHub Desktop.
Save ileitch/3e43966cf5a59fc083fe5758087423a9 to your computer and use it in GitHub Desktop.

Identifying compiler arguments for SourceKit's 'indexsource' request type.

SourceKit's 'indexsource' request type requires swiftc compiler arguments in order to compile the given source file on demand (I assume). However, the exact arguments required are numerous, not documented and highly specific to the requested source file.

Advanced users of 'indexsource' do not typically attempt to construct this set of arguments manually, and instead 'spy' on Xcode in order to determine the necessary arguments. Both SourceKitten[1] and Periphery[2] rely upon parsing the output from xcodebuild to identify the appropriate swiftc call, parse the argument set, and then remove specific arguments that 'indexsource' does not appear to accept[3].

This approach is problematic for a number of reasons:

  • Building a project simply to obtain the swiftc arguments is extremely inefficient. With regard to Periphery, the xcodebuild step accounts for over 95% of total runtime.
  • xcodebuild must also perform a 'clean' build, since the output log will not include swiftc calls for targets which do not need to be rebuilt.
  • The format of the arguments as they're used in a swiftc call can be tricky to parse.
  • Using xcodebuild is a "hack", the log output may change without notice in future Xcode releases.
  • Obtaining swiftc arguments for XCTest target requires extra logic, in that xcodebuild's 'build-for-testing' action must be used instead of 'build'.

In order to provide a faster and more stable experience to our users, we would like the ability to request build plans without the need to perform an actual build. An ideal solution might look something like this:

  • For a given workspace/project, scheme and target, output an easily parsable build plan.
  • XCTest targets should be supported just as normal build targets.
  • Optionally, an additional flag would restrict the argument set to those that are compatible with 'indexsource' so that they may be used verbatim.

This generic approach would also aid other use-cases which do not intended to use the arguments with SourceKit.

  1. https://github.com/jpsim/SourceKitten
  2. https://peripheryapp.com
  3. This is not necessarily an exhaustive list, they're just the ones identified during the development of Periphery:
  • "-Xfrontend"
  • "-output-file-map"
  • "-parseable-output"
  • "-serialize-diagnostics"
  • "-incremental"
  • "-emit-dependencies"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment