libc : Generates a module called libc which is Glibc on linux and Darwin.C on OSX.
POSIX : This module Swiftifies the POSIX APIs so that they can be used without all the C like code inside swift.
sys : Contains APIs to interact with file system (in swift-like way).
PackageDescription : The module we import in our swift packages containing package description, target, dependencies, versions.
dep : This is the main module which clones git repos and invokes llbuild process to build the packages.
swift-build : And finally, the executable module which parses the input and drives the build process.
llbuild is “A low-level build system, used by the Swift Package Manager”. The swift-build-tool product of llbuild is used by swift package manager to compile the swift packages.
Swiftpm has a python bootstrap script which initially compiles all of swiftpm targets and generates the swiftpm executable. The bootstrapped exectuable is then used to compile the swiftpm source and generate the final swift-build product.
Lets explore a bit of bootstrap script :
Target : A python class which contains name, dependencies, flags etc for a target. As soon as it is initialized, it figures out if a target is a library or an executable by checking for main.swift
It contains a method which writes the .llbuild commands needed for this target to be built to output string buffer.
The array of Target objects are defined in a targets global variable :
When the script is run, the following options are pretty important to it but they’re all filled automatically if toolchain is installed and exported correctly.
swiftc_path : Swift compiler path
sbt_path : Swift built tool path (llbuild)
sysroot : needed on OSX (macosx sdk)
build_path : $pwd/.build dir
the build_path (where swiftpm will be finally built to) is set to .build dir and sandbox_path (where bootstrap binary will be built) is set to .build/.bootstrap
The get_swift_build_tool_path() method is used to look for the swift-build-tool (llbuild) if it is not supplied at input
create_bootstrap_files() method creates the .llbuild file which will build all targets and the executable.
It uses the write_swift_compile_commands method on target objects.
The file is created at .build/.bootstrap/build.swift-build.
Now the bootstrap script is ready to spawn swift-build-tool to compile swiftpm
process_runtime_libraries method is called on the generated PackageDescription module to check if they’re being loaded correctly or not.