Skip to content

Commit 474a472

Browse files
authored
Add swift-bootstrap command (#5860)
This command can be used to bootstrap SwiftPM via CMake without requiring us to build a full-blown SwiftPM using CMake.
1 parent 01f85fe commit 474a472

File tree

6 files changed

+45
-9
lines changed

6 files changed

+45
-9
lines changed

Package.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ let package = Package(
385385
dependencies: ["Commands"],
386386
exclude: ["CMakeLists.txt"]
387387
),
388+
.executableTarget(
389+
/** Builds SwiftPM itself for bootstrapping (minimal version of `swift-build`) */
390+
name: "swift-bootstrap",
391+
dependencies: ["CoreCommands"],
392+
exclude: ["CMakeLists.txt"]
393+
),
388394
.executableTarget(
389395
/** Runs package tests */
390396
name: "swift-test",

Sources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_subdirectory(PackageRegistry)
2727
add_subdirectory(SPMBuildCore)
2828
add_subdirectory(SPMLLBuild)
2929
add_subdirectory(SourceControl)
30+
add_subdirectory(swift-bootstrap)
3031
add_subdirectory(swift-build)
3132
add_subdirectory(swift-package)
3233
add_subdirectory(swift-run)

Sources/Workspace/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ target_link_libraries(Workspace PUBLIC
3232
PackageGraph
3333
PackageLoading
3434
PackageModel
35+
PackageRegistry
3536
SourceControl)
3637
target_link_libraries(PackageLoading PUBLIC
3738
$<$<NOT:$<PLATFORM_ID:Darwin>>:Foundation>)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This source file is part of the Swift open source project
2+
#
3+
# Copyright (c) 2022 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
add_executable(swift-bootstrap
10+
main.swift)
11+
target_link_libraries(swift-bootstrap PRIVATE
12+
CoreCommands)

Sources/swift-bootstrap/main.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import CoreCommands
14+
15+
SwiftMinimalBuildTool.main()

Utilities/bootstrap

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def install_binary(args, binary, dest_dir, ignored_patterns=[]):
483483
# Build functions
484484
# -----------------------------------------------------------
485485

486-
def build_with_cmake(args, cmake_args, source_path, build_dir):
486+
def build_with_cmake(args, cmake_args, ninja_args, source_path, build_dir):
487487
"""Runs CMake if needed, then builds with Ninja."""
488488
cache_path = os.path.join(build_dir, "CMakeCache.txt")
489489
if args.reconfigure or not os.path.isfile(cache_path) or not args.swiftc_path in open(cache_path).read():
@@ -514,7 +514,8 @@ def build_with_cmake(args, cmake_args, source_path, build_dir):
514514
if args.verbose:
515515
ninja_cmd.append("-v")
516516

517-
call(ninja_cmd, cwd=build_dir, verbose=args.verbose)
517+
518+
call(ninja_cmd + ninja_args, cwd=build_dir, verbose=args.verbose)
518519

519520
def build_llbuild(args):
520521
"""Builds LLBuild using CMake."""
@@ -541,7 +542,7 @@ def build_llbuild(args):
541542
flags.append("-DSQLite3_INCLUDE_DIR=%s/usr/include" % args.sysroot)
542543

543544
args.source_dirs["llbuild"] = get_llbuild_source_path(args)
544-
build_with_cmake(args, flags, args.source_dirs["llbuild"], args.build_dirs["llbuild"])
545+
build_with_cmake(args, flags, [], args.source_dirs["llbuild"], args.build_dirs["llbuild"])
545546

546547
def build_dependency(args, target_name, common_cmake_flags = [], non_darwin_cmake_flags = []):
547548
note("Building " + target_name)
@@ -554,11 +555,11 @@ def build_dependency(args, target_name, common_cmake_flags = [], non_darwin_cmak
554555
else:
555556
cmake_flags += non_darwin_cmake_flags
556557

557-
build_with_cmake(args, cmake_flags, args.source_dirs[target_name], args.build_dirs[target_name])
558+
build_with_cmake(args, cmake_flags, [], args.source_dirs[target_name], args.build_dirs[target_name])
558559

559560
def add_rpath_for_cmake_build(args, rpath):
560-
"Adds the given rpath to the CMake-built swift-build"
561-
swift_build = os.path.join(args.bootstrap_dir, "bin/swift-build")
561+
"Adds the given rpath to the CMake-built swift-bootstrap"
562+
swift_build = os.path.join(args.bootstrap_dir, "bin/swift-bootstrap")
562563
add_rpath_cmd = ["install_name_tool", "-add_rpath", rpath, swift_build]
563564
note(' '.join(add_rpath_cmd))
564565
subprocess.call(add_rpath_cmd, stderr=subprocess.PIPE)
@@ -594,7 +595,7 @@ def build_swiftpm_with_cmake(args):
594595
cmake_flags.append("-DCMAKE_C_FLAGS=-target %s%s" % (get_build_target(args), g_macos_deployment_target))
595596
cmake_flags.append("-DCMAKE_OSX_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target)
596597

597-
build_with_cmake(args, cmake_flags, args.project_root, args.bootstrap_dir)
598+
build_with_cmake(args, cmake_flags, ["swift-bootstrap", "PackageDescription", "PackagePlugin"], args.project_root, args.bootstrap_dir)
598599

599600
if args.llbuild_link_framework:
600601
add_rpath_for_cmake_build(args, args.build_dirs["llbuild"])
@@ -620,9 +621,9 @@ def build_swiftpm_with_swiftpm(args, integrated_swift_driver):
620621
]
621622

622623
if args.bootstrap:
623-
note("Building SwiftPM (with a freshly built swift-build)")
624+
note("Building SwiftPM (with a freshly built swift-bootstrap)")
624625
swiftpm_args.append("SWIFTPM_CUSTOM_LIBS_DIR=" + os.path.join(args.bootstrap_dir, "pm"))
625-
swiftpm_args.append(os.path.join(args.bootstrap_dir, "bin/swift-build"))
626+
swiftpm_args.append(os.path.join(args.bootstrap_dir, "bin/swift-bootstrap"))
626627
else:
627628
note("Building SwiftPM (with a prebuilt swift-build)")
628629
swiftpm_args.append(os.path.join(os.path.split(args.swiftc_path)[0], "swift-build"))

0 commit comments

Comments
 (0)