Skip to content

Commit 892f4a0

Browse files
committed
Emit a script allow self-hosting SwiftPM after bootstrap
<rdar://problem/42350010> Emit a script allow self-hosting SwiftPM after bootstrap SwiftPM allowed using the built/inferior swift-{build, package, run, test} to iterate on the package manager codebase. This is going to break now because we adopted llbuild as a link time dependency. To resolve this, the bootstrap script will emit a script that will call the swiftpm tool with the additional flags that are required to allow self-hosting. This can be removed once we have actual support for build settings.
1 parent 58313e4 commit 892f4a0

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

Documentation/Development.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,22 @@ using a [snapshot](https://swift.org/download/#releases) from swift.org.
7676

7777
## Self-hosting
7878

79-
It is possible to build the Package Manager with itself. This is useful when you
80-
want to rebuild just the sources or run a single test. Make sure you run the
81-
bootstrap script first.
79+
It is possible to build SwiftPM with itself using a special script that is
80+
emitted during bootstrapping. This is useful when you want to rebuild just the
81+
sources or run a single test. Make sure you run the bootstrap script first.
8282

8383
```sh
8484
$ cd swiftpm
8585

8686
# Rebuild just the sources.
87-
$ .build/x86_64-apple-macosx10.10/debug/swift-build
87+
$ .build/x86_64-apple-macosx10.10/debug/spm build
8888

8989
# Run a single test.
90-
$ .build/x86_64-apple-macosx10.10/debug/swift-test --filter BasicTests.GraphAlgorithmsTests/testCycleDetection
90+
$ .build/x86_64-apple-macosx10.10/debug/spm test --filter BasicTests.GraphAlgorithmsTests/testCycleDetection
9191
```
9292

93-
Note: If you make any changes to `PackageDescription` or `PackageDescription4`
94-
target, you **will** need to rebuild using the bootstrap script.
93+
Note: If you make any changes to `PackageDescription` runtime-related targets,
94+
you **will** need to rebuild using the bootstrap script.
9595

9696
## Developing using Xcode
9797

Utilities/bootstrap

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import os
3939
import pipes
4040
import platform
4141
import re
42+
import stat
4243
import shlex
4344
import shutil
4445
import subprocess
@@ -857,6 +858,46 @@ def llbuild_link_args(args):
857858
build_flags.extend(["-Xlinker", "-rpath", "-Xlinker", llbuild_libdir])
858859
return build_flags
859860

861+
def write_self_hosting_script(path, args):
862+
# Construct the build flags.
863+
swiftpm_flags = []
864+
for import_path in llbuild_import_paths(args):
865+
swiftpm_flags.extend(["-Xswiftc", "-I%s" % import_path])
866+
if args.llbuild_link_framework:
867+
swiftpm_flags.extend(["-Xswiftc", "-F%s" % args.llbuild_build_dir])
868+
swiftpm_flags.extend(llbuild_link_args(args))
869+
870+
# Construct the string from the build flags.
871+
swiftpm_flags_str = ' '.join(('"%s"' % flag) for flag in swiftpm_flags)
872+
873+
script_contents = """#!/usr/bin/env bash
874+
875+
# Helper script for self-hosting SwiftPM tools.
876+
877+
set -e
878+
879+
if [ ! -z "$SPM_VERBOSE" ]; then
880+
set -x
881+
fi
882+
883+
if [ -z "$1" ]; then
884+
echo "provide the executable name: build|test|package|run" && exit 1
885+
fi
886+
887+
__dir="$(cd "$(dirname "${{BASH_SOURCE[0]}}")" && pwd)"
888+
889+
SPM_FLAGS=({options})
890+
891+
exec ${{__dir}}/swift-$1 ${{SPM_FLAGS[@]}} "${{@:2}}" """.format(options=swiftpm_flags_str)
892+
893+
script_path = os.path.join(path, "spm")
894+
with open(script_path, "w") as f:
895+
print(script_contents, file=f)
896+
897+
st = os.stat(script_path)
898+
os.chmod(script_path, st.st_mode | stat.S_IEXEC)
899+
900+
860901
def main():
861902
parser = argparse.ArgumentParser(
862903
usage="%(prog)s [options] [clean|all|test|install]",
@@ -1194,6 +1235,9 @@ def main():
11941235
if result != 0:
11951236
error("build failed with exit status %d" % (result,))
11961237

1238+
# Write the script to allow self-hosting for development.
1239+
write_self_hosting_script(os.path.join(target_path, conf), args)
1240+
11971241
swift_package_path = os.path.join(target_path, conf, "swift-package")
11981242
swiftpm_build_dir = os.path.join(target_path, conf, "swift-build")
11991243
swift_test_path = os.path.join(target_path, conf, "swift-test")

0 commit comments

Comments
 (0)