Build SPM from github

[MacOS 10.13.3, Xcode 9.2]
I’m having difficulties finding info on how to build the SPM from source locally, either from the commandline or Xcode.
What would be a generic build command? I have a variety of build tools available here.

@nap you can check this doc

@Aniket965: Thanks, but it’s not working.
My cloned repo is up to date, but the instructions on the page you linked don’t work.
Update: I don’t have Xcode 9.3beta.

../swift/utils/build-script -R --llbuild --swiftpm

build-script does not exist in the repo.

When I tried the Xcode alternative, Utilities/bootstrap --generate-xcodeproj produced the following error:

    --- bootstrap: note: building stage1
    Compile Swift Module 'PackageDescription4' (9 sources)
    Compile Swift Module 'PackageDescription' (6 sources)
    Compile Swift Module 'POSIX' (11 sources)
    Link POSIX
    Compile Swift Module 'Basic' (37 sources)
    /Users/user/src/swift/SPM/swift-package-manager/Sources/PackageDescription/Version+StringLiteralConvertible.swift:52:34: error: value of type '[String]' has no member 'compactMap'
            let requiredComponents = requiredStringComponents.compactMap({ Int($0) }).filter({ $0 >= 0 })
                                     ^~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~
    /Users/user/src/swift/SPM/swift-package-manager/Sources/PackageDescription4/Version+StringLiteralConvertible.swift:53:14: error: value of type '[String]' has no member 'compactMap'
                .map(String.init)
    ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
    --- bootstrap: error: build failed with exit status 1

I have cloned the SPM repo, do I also need to clone the Swift repo?
If so, does the SPM repo need to be nested inside the Swift repo? If so, where?

Yes you have to build swift source first , but it will take around 40 mins to build it… there is a better alternative to use which is Using trunk snapshot you can download from here.

and follow the instruction here


with that said, if you want to build from source it will be automatically cloned inside the swift source use this resource

@Aniket965 thanks again for your help.

But this is not going so well from my perspective. I just want to build the package manager, not the whole of Swift. Moreover, starting next week, I need to have a working Xcode 9.2 install (from the App Store) on my machine and I’m not prepared to take the risk of installing new things because I don’t have the time to repair my computer should something go wrong.

Since I already have Xcode 9.2 installed, and therefore all the necessary supporting files, I’m going to try checking out swift-4.0.3-RELEASE in the SPM repo and hopefully I will be able to build it.

Ok, that approach seems to be working.
So far I have been able to generate the Xcode project successfully.

Hi @nap, I’ve not checked but you should be able to use the trunk snapshot method with Xcode 9.2. We usually require a new version of Xcode if there are breaking changes in the SDK. That said, keeping multiple versions of Xcode should not cause any issue with your computer. Using the latest available Xcode is the most correct way to build the project. You might not even require the trunk snapshot if you use the latest beta!

Building swift-4.0.3-RELEASE tag is fine if you just want to explore how SwiftPM works but beware that some parts of the code might have changed or even removed.

Feel free to ask more question!

1 Like

@Aciid : Thanks, but I’ve already proven the concept I wanted by checking out the swift-4.0.3-RELEASE tag. I was able to build it without problems.

1 Like

Suppose I’ve done a full swift-source build, the instructions in Development.md aren’t super clear how to go ahead and use that then. I can run spm with its long path, but what would the steps be to set it up properly and to be able to rebuild it?

@alper : If you look into the /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin path (which is where my Xcode from the App Store is located), you will find all the same build targets (and more), which you can (but probably not such a good idea) replace with your built versions. I suppose you could also use the xcode-select function and xcrun --find swift, which I’ve only used before when I’ve had multiple versions of Xcode in the past.

Yes, xcrun finds the swift toolchain I have installed… but how do I set it to the version I've just built?

Try using xcode-select with a path. xcode-select --help will give you some info.

That won't take any of the directories I have here: /Users/alper/Documents/swift-source/build/Ninja-ReleaseAssert/swiftpm-macosx-x86_64/x86_64-apple-macosx10.10/release/swift-package

man xcode-select indicates that the --switch (or -s) requires a path to the xcode app folder. So, maybe you need to install, which SPM does not do, or organise the files is some other way.

If you look at this sample project on github: https://github.com/rhx/SwiftHelloGtk, you will find build.sh that calls app-bundle.sh, which produces an app.

Note: This example was created before Swift 4 came out, so be careful using the clean.sh script because it deletes the .build folder instead of invoking swift package clean.)

I’m not sure if this will help you, but it’s the best I can suggest.

swift-package is just one build product out of a whole set. Obviously an xcode toolchain will have all the other stuff to go with it. Maybe you could copy your Xcode.app to another name, say myXcode.app and then replace the individual files in it?

That doesn't really sound very workable. Maybe I should just alias the built swift-package, but that doesn't really seem like an ideal solution.

Not much of a difference except that if you make a symlink to your version, you will (a) remove the original one, so make a backup of it before you do, and (b) you will be affecting your production version of Xcode. I think it would be safer to make a copy, if you have the space on your hard-disk.

For this suggestion to work, you would need to install the correct Xcode (beta) version for the Swift version you’ve built.

It is possible to build an entire toolchain using the presets in Swift’s build script however that takes a long time and is unnecessary for iterating on the package manager. It is convenient to create aliases for the swiftpm products in the build directory to test the built swiftpm with other packages. These are some of the aliases that I use:

alias sb='/Users/ankit/workspace/swift.org/swiftpm/.build/x86_64-apple-macosx10.10/debug/swift-build'
alias st='/Users/ankit/workspace/swift.org/swiftpm/.build/x86_64-apple-macosx10.10/debug/swift-test'
alias sp='/Users/ankit/workspace/swift.org/swiftpm/.build/x86_64-apple-macosx10.10/debug/swift-package'
alias sr='/Users/ankit/workspace/swift.org/swiftpm/.build/x86_64-apple-macosx10.10/debug/swift-run'
alias spx='/Users/ankit/workspace/swift.org/swiftpm/.build/x86_64-apple-macosx10.10/debug/swift-package generate-xcodeproj'
1 Like