Skip to content

Commit a253a54

Browse files
committed
[5.5] Bootstrap script needs to build PackageDescription and PackagePlugin libraries universal when cross-compiling.
Also unify and clean up some of the logic by making the helper function that installs libSwiftPM be more generic, and also apply to PackageDescription and PackagePlugin. rdar://75186958 (cherry picked from commit 637fa7a)
1 parent 441f465 commit a253a54

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

Utilities/bootstrap

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ from helpers import note, error, symlink_force, mkdir_p, call, call_output
2727

2828
g_macos_deployment_target = '10.15'
2929

30+
g_shared_lib_prefix = "lib"
3031
if platform.system() == 'Darwin':
31-
g_shared_lib_ext = ".dylib"
32+
g_shared_lib_suffix = ".dylib"
3233
else:
33-
g_shared_lib_ext = ".so"
34+
g_shared_lib_suffix = ".so"
3435

3536
def main():
3637
parser = argparse.ArgumentParser(description="""
@@ -366,7 +367,7 @@ def install(args):
366367
"PackageGraph", "SPMBuildCore", "Build",
367368
"Xcodeproj", "Workspace"
368369
]
369-
install_libswiftpm_dylib(args, "SwiftPM", args.libswiftpm_install_dir, libswiftpm_modules)
370+
install_dylib(args, "SwiftPM", args.libswiftpm_install_dir, libswiftpm_modules)
370371

371372
# Install libSwiftPMDataModel if an install directory was provided.
372373
if args.libswiftpmdatamodel_install_dir:
@@ -377,82 +378,59 @@ def install(args):
377378
"PackageGraph", "SPMBuildCore",
378379
"Xcodeproj", "Workspace"
379380
]
380-
install_libswiftpm_dylib(args, "SwiftPMDataModel", args.libswiftpmdatamodel_install_dir, libswiftpmdatamodel_modules)
381+
install_dylib(args, "SwiftPMDataModel", args.libswiftpmdatamodel_install_dir, libswiftpmdatamodel_modules)
381382

383+
# Installs the SwiftPM tools and runtime support libraries.
382384
def install_swiftpm(prefix, args):
383385
# Install swiftpm binaries.
384386
for binary in ["swift-build", "swift-test", "swift-run", "swift-package", "swift-package-collection"]:
385387
dest = os.path.join(prefix, "bin")
386388
install_binary(args, binary, dest)
387389

390+
# On Darwin, also install the swiftpm-xctest-helper tool.
388391
if platform.system() == 'Darwin':
389392
dest = os.path.join(prefix, "libexec", "swift", "pm")
390393
install_binary(args, "swiftpm-xctest-helper", dest)
391394

392-
# Install PackageDescription runtime libraries.
393-
runtime_lib_dest = os.path.join(prefix, "lib", "swift", "pm")
394-
runtime_lib_src = os.path.join(args.bootstrap_dir, "pm")
395+
# Install the PackageDescription library and associated modules.
396+
dest = os.path.join(prefix, "lib", "swift", "pm", "ManifestAPI")
397+
install_dylib(args, "PackageDescription", dest, ["PackageDescription"])
395398

396-
files_to_install = ["libPackageDescription" + g_shared_lib_ext]
397-
if platform.system() == 'Darwin':
398-
files_to_install.append("PackageDescription.swiftinterface")
399-
else:
400-
files_to_install.append("PackageDescription.swiftmodule")
401-
files_to_install.append("PackageDescription.swiftdoc")
402-
403-
for file in files_to_install:
404-
src = os.path.join(runtime_lib_src, "ManifestAPI", file)
405-
dest = os.path.join(runtime_lib_dest, "ManifestAPI", file)
406-
mkdir_p(os.path.dirname(dest))
407-
408-
note("Installing %s to %s" % (src, dest))
409-
410-
file_util.copy_file(src, dest, update=1)
411-
412-
files_to_install = ["libPackagePlugin" + g_shared_lib_ext]
413-
if platform.system() == 'Darwin':
414-
files_to_install.append("PackagePlugin.swiftinterface")
415-
else:
416-
files_to_install.append("PackagePlugin.swiftmodule")
417-
files_to_install.append("PackagePlugin.swiftdoc")
418-
419-
for file in files_to_install:
420-
src = os.path.join(runtime_lib_src, "PluginAPI", file)
421-
dest = os.path.join(runtime_lib_dest, "PluginAPI", file)
422-
mkdir_p(os.path.dirname(dest))
423-
424-
note("Installing %s to %s" % (src, dest))
425-
426-
file_util.copy_file(src, dest, update=1)
399+
# Install the PackagePlugin library and associated modules.
400+
dest = os.path.join(prefix, "lib", "swift", "pm", "PluginAPI")
401+
install_dylib(args, "PackagePlugin", dest, ["PackagePlugin"])
427402

428403

429-
def install_libswiftpm_dylib(args, library_name, install_dir, module_names):
430-
# FIXME: Don't hardcode the prefix and suffix.
431-
install_binary(args, "lib" + library_name + ".dylib", install_dir)
404+
# Helper function that installs a dynamic library and a set of modules to a particular directory.
405+
def install_dylib(args, library_name, install_dir, module_names):
406+
# Install the dynamic library itself.
407+
install_binary(args, g_shared_lib_prefix + library_name + g_shared_lib_suffix, install_dir)
432408

433-
# Install the swiftmodule and swiftdoc files.
409+
# Install the swiftmodule/swiftinterface and swiftdoc files for all the modules.
434410
for module in module_names:
435-
install_binary(args, module + ".swiftmodule", install_dir)
436-
if not args.cross_compile_hosts: # When compiling for multiple arches, swiftdoc is part of the swiftmodule directory
411+
# If we're cross-compiling, we expect the .swiftmodule to be a directory that contains everything.
412+
if args.cross_compile_hosts:
413+
install_binary(args, module + ".swiftmodule", install_dir)
414+
else:
415+
# Otherwise we have either a .swiftinterface or a .swiftmodule, plus a .swiftdoc.
416+
if os.path.exists(os.path.join(args.bin_dir, module + ".swiftinterface")):
417+
install_binary(args, module + ".swiftinterface", install_dir)
418+
else:
419+
install_binary(args, module + ".swiftmodule", install_dir)
437420
install_binary(args, module + ".swiftdoc", install_dir)
438421

439-
# Install the C headers.
440-
tscclibc_include_dir = os.path.join(args.tsc_source_dir, "Sources/TSCclibc/include")
441-
tscclibc_include_dir_dest = os.path.join(install_dir, "TSCclibc")
442-
dir_util.copy_tree(tscclibc_include_dir, tscclibc_include_dir_dest)
443-
444422

423+
# Helper function that installs a single built artifact to a particular directory. The source may be either a file or a directory.
445424
def install_binary(args, binary, dest_dir):
446425
src = os.path.join(args.bin_dir, binary)
447426
dest = os.path.join(dest_dir, binary)
448427

449428
note("Installing %s to %s" % (src, dest))
450-
451429
mkdir_p(os.path.dirname(dest))
452-
if os.path.isdir(src) and args.cross_compile_hosts: # Handle swiftmodule directories if compiling for multiple arches.
453-
dir_util.copy_tree(src, dest)
430+
if os.path.isdir(src):
431+
dir_util.copy_tree(src, dest, update=1, verbose=1)
454432
else:
455-
file_util.copy_file(src, dest, update=1)
433+
file_util.copy_file(src, dest, update=1, verbose=1)
456434

457435
# -----------------------------------------------------------
458436
# Build functions
@@ -647,6 +625,7 @@ def build_swiftpm_with_swiftpm(args, integrated_swift_driver):
647625
if integrated_swift_driver:
648626
swiftpm_args.append("--use-integrated-swift-driver")
649627

628+
# Build SwiftPM, including libSwiftPM, all the command line tools, and the current variant of PackageDescription.
650629
call_swiftpm(args, swiftpm_args)
651630

652631
# Setup symlinks that'll allow using swiftpm from the build directory.
@@ -772,7 +751,7 @@ def get_swiftpm_flags(args):
772751
swift_library_rpath_prefix = "$ORIGIN/../"
773752
platform_path = None
774753
for path in args.target_info["paths"]["runtimeLibraryPaths"]:
775-
platform_path = re.search(r"(lib/swift/[^/]+)$", path)
754+
platform_path = re.search(r"(lib/swift/([^/]+))$", path)
776755
if platform_path:
777756
build_flags.extend(
778757
[
@@ -782,6 +761,15 @@ def get_swiftpm_flags(args):
782761
swift_library_rpath_prefix + platform_path.group(1),
783762
]
784763
)
764+
if platform.system() == 'Linux':
765+
build_flags.extend(
766+
[
767+
"-Xlinker",
768+
"-rpath",
769+
"-Xlinker",
770+
swift_library_rpath_prefix + '../' + platform_path.group(2),
771+
]
772+
)
785773
break
786774

787775
if not platform_path:

0 commit comments

Comments
 (0)