Skip to content

Instantly share code, notes, and snippets.

@tonisuter
Last active July 10, 2017 02:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonisuter/1734db055a56ce81bd9f4a69f0e49b01 to your computer and use it in GitHub Desktop.
Save tonisuter/1734db055a56ce81bd9f4a69f0e49b01 to your computer and use it in GitHub Desktop.
Testing libSwiftPM
import Basic
import PackageGraph
import PackageLoading
import Workspace
class MyWorkspaceDelegate: WorkspaceDelegate {
func packageGraphWillLoad(currentGraph: PackageGraph, dependencies: AnySequence<ManagedDependency>, missingURLs: Set<String>) {
print("packageGraphWillLoad")
}
func fetching(repository: String) {
print("fetching \(repository)")
}
func cloning(repository: String) {
print("cloning \(repository)")
}
func checkingOut(repository: String, at reference: String) {
print("checking out \(repository)")
}
func removing(repository: String) {
print("removing \(repository)")
}
func warning(message: String) {
print("warning \(message)")
}
func managedDependenciesDidUpdate(_ dependencies: AnySequence<ManagedDependency>) {
print("managedDependenciesDidUpdate")
}
}
struct MyManifestResourceProvider: ManifestResourceProvider {
var swiftCompiler = AbsolutePath("/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift")
var libDir = AbsolutePath("/Users/tonisuter/Projects/swift-package-manager/.build/lib/swift/pm")
}
func printManifest(pkgPath: AbsolutePath) {
let manifestLoader = ManifestLoader(resources: MyManifestResourceProvider())
let workspace = Workspace(
dataPath: pkgPath.appending(component: ".build"),
editablesPath: pkgPath.appending(component: "Packages"),
pinsFile: pkgPath.appending(component: "Package.pins"),
manifestLoader: manifestLoader,
delegate: MyWorkspaceDelegate()
)
let diagnosticsEngine = DiagnosticsEngine()
let manifests = workspace.loadRootManifests(packages: [pkgPath], diagnostics: diagnosticsEngine)
if let manifest = manifests.first {
print(manifest)
} else {
for diagnostic in diagnosticsEngine.diagnostics {
print(diagnostic.localizedDescription)
}
}
}
printManifest(pkgPath: AbsolutePath("/Users/tonisuter/Downloads/MyProj"))
@tonisuter
Copy link
Author

My file structure looks like this and additionally, there's a regular, empty SwiftPM project at the path /Users/tonisuter/Downloads/MyProj:

.
├── include
│   ├── Basic.swiftmodule
│   ├── Build.swiftmodule
│   ├── POSIX.swiftmodule
│   ├── PackageDescription.swiftmodule
│   ├── PackageDescription4.swiftmodule
│   ├── PackageGraph.swiftmodule
│   ├── PackageLoading.swiftmodule
│   ├── PackageModel.swiftmodule
│   ├── SourceControl.swiftmodule
│   ├── Utility.swiftmodule
│   ├── Workspace.swiftmodule
│   ├── Xcodeproj.swiftmodule
│   ├── clibc.h
│   ├── libc.swiftmodule
│   └── module.modulemap
├── lib
│   └── libSwiftPM.dylib
└── main.swift

And I am using the following command to compile and run main.swift:

xcrun swift -I ./include -L ./lib -lSwiftPM -module-link-name SwiftPM main.swift

However, when I run the command I get the following error:

manifest parse error(s):
/Users/tonisuter/Downloads/MyProj/Package.swift:3:8: error: no such module 'PackageDescription'
import PackageDescription
       ^

@tonisuter
Copy link
Author

The problem was that I used the libSwiftPM library from the master branch, but the libDir path was set to /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm (i.e., the folder that contains the libPackageDescription.dylib of the current version of Xcode). Instead, I had to execute the following command in the Swift Package Manager repository:

<Path-To-SwiftPM-Repository>/Utilities/bootstrap --libswiftpm-library-dir "/tmp/libSwiftPM/install/dir/include" --libswiftpm-modules-dir "/tmp/libSwiftPM/install/dir/lib"

Finally, the libDir path needs to be set to <Path-To-SwiftPM-Repository>/.build/lib/swift/pm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment