Skip to content

Commit ca70f24

Browse files
committed
Adding additional logging to troubleshoot the bootstap failure
1 parent 6fcb906 commit ca70f24

File tree

3 files changed

+112
-72
lines changed

3 files changed

+112
-72
lines changed

Utilities/bootstrap

Lines changed: 83 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,31 @@ from __future__ import print_function
1717

1818
import argparse
1919
import json
20+
import logging
2021
import os
22+
import pathlib
2123
import platform
2224
import re
2325
import shutil
2426
import subprocess
25-
from helpers import note, error, symlink_force, mkdir_p, call, call_output, log_timestamp
26-
27+
import sys
28+
from helpers import symlink_force, mkdir_p, call, call_output
29+
30+
31+
logging.basicConfig(
32+
stream=sys.stdout,
33+
format=" | ".join([
34+
f"--- {pathlib.Path(sys.argv[0]).name}", # Prefix script name to the log in an attempt to avoid confusion when parsing logs
35+
"%(asctime)s",
36+
"%(levelname)-7s",
37+
"%(threadName)s",
38+
"%(module)s",
39+
"%(funcName)s",
40+
"Line:%(lineno)d",
41+
"%(message)s",
42+
]),
43+
level=logging.DEBUG,
44+
)
2745
g_macos_deployment_target = '12.0'
2846

2947
g_shared_lib_prefix = "lib"
@@ -32,6 +50,12 @@ if platform.system() == 'Darwin':
3250
else:
3351
g_shared_lib_suffix = ".so"
3452

53+
class BinaryNotFound(BaseException):
54+
55+
def __init__(self, *, tool: str, path: pathlib.Path):
56+
super().__init__("Unable to find {tool} source directory at {path}")
57+
58+
3559
def main():
3660
parser = argparse.ArgumentParser(description="""
3761
This script will build a bootstrapped copy of the Swift Package Manager, and optionally perform extra
@@ -60,6 +84,7 @@ def main():
6084
parser_install.set_defaults(func=install)
6185
add_build_args(parser_install)
6286

87+
logging.info("sys.argv: %r", sys.argv)
6388
args = parser.parse_args()
6489
args.func = args.func or build
6590
args.func(args)
@@ -253,36 +278,47 @@ def parse_test_args(args):
253278

254279
def get_swiftc_path(args):
255280
"""Returns the path to the Swift compiler."""
281+
logging.info("Getting path to swiftc...")
256282
if args.swiftc_path:
257283
swiftc_path = os.path.abspath(args.swiftc_path)
284+
logging.debug("path provided via command line argument. swiftc_path is %r", swiftc_path)
258285
elif os.getenv("SWIFT_EXEC"):
259286
swiftc_path = os.path.realpath(os.getenv("SWIFT_EXEC"))
287+
logging.info("SWIFT_EXEC env set.")
260288
elif platform.system() == 'Darwin':
289+
logging.debug("we are on darwin, so calling `xcrun --find swiftc`")
261290
swiftc_path = call_output(
262291
["xcrun", "--find", "swiftc"],
263292
stderr=subprocess.PIPE,
264-
verbose=args.verbose
293+
verbose=args.verbose,
265294
)
295+
logging.debug("swiftc_path is set to %r", swiftc_path)
266296
else:
267297
swiftc_path = call_output(["which", "swiftc"], verbose=args.verbose)
298+
logging.debug("calling 'which swiftc'. path is %r", swiftc_path)
268299

269300
if os.path.basename(swiftc_path) == 'swift':
270301
swiftc_path = swiftc_path + 'c'
302+
logging.debug("appending to path, it is now %r", swiftc_path)
271303

304+
logging.debug("swiftc_path set to %r", swiftc_path)
272305
if os.path.exists(swiftc_path):
306+
logging.debug("swiftc_path exists.. returning...")
273307
return swiftc_path
274-
error("unable to find swiftc at %s" % swiftc_path)
308+
logging.error("unable to find swiftc at %s", swiftc_path)
309+
raise BinaryNotFound(tool="swiftc", path=swiftc_path)
275310

276311
def get_tool_path(args, tool):
277312
"""Returns the path to the specified tool."""
313+
logging.debug("Searching for %s tool", tool)
278314
path = getattr(args, tool + "_path", None)
279315
if path is not None:
280316
return os.path.abspath(path)
281317
elif platform.system() == 'Darwin':
282318
return call_output(
283319
["xcrun", "--find", tool],
284320
stderr=subprocess.PIPE,
285-
verbose=args.verbose
321+
verbose=args.verbose,
286322
)
287323
else:
288324
return call_output(["which", tool], verbose=args.verbose)
@@ -294,26 +330,30 @@ def get_build_target(args, cross_compile=False):
294330
if cross_compile:
295331
cross_compile_json = json.load(open(args.cross_compile_config))
296332
command += ['-target', cross_compile_json["target"]]
333+
logging.debug("Running command >>> %r", command)
297334
target_info_json = subprocess.check_output(command,
298-
stderr=subprocess.PIPE, universal_newlines=True).strip()
335+
stderr=subprocess.PIPE, universal_newlines=True, env=os.environ).strip()
336+
logging.debug("Command returned: %r", target_info_json)
299337
args.target_info = json.loads(target_info_json)
300-
if platform.system() == 'Darwin':
301-
return args.target_info["target"]["unversionedTriple"]
302-
return args.target_info["target"]["triple"]
303-
except Exception as e:
338+
return args.target_info["target"]["unversionedTriple" if platform.system() == 'Darwin' else "triple"]
339+
except subprocess.CalledProcessError as cpe:
340+
logging.debug("Command failed...")
304341
# Temporary fallback for Darwin.
305342
if platform.system() == 'Darwin':
306-
return 'x86_64-apple-macosx'
343+
macOS_default = 'x86_64-apple-macosx'
344+
logging.debug("we are on Darwin. defaulting to %r", macOS_default)
345+
return macOS_default
307346
else:
308-
error(str(e))
347+
logging.error("get build targets: %s", str(cpe))
348+
raise cpe
309349

310350
# -----------------------------------------------------------
311351
# Actions
312352
# -----------------------------------------------------------
313353

314354
def clean(args):
315355
"""Cleans the build artifacts."""
316-
note("Cleaning")
356+
logging.info("Cleaning")
317357
parse_global_args(args)
318358

319359
call(["rm", "-rf", args.build_dir], verbose=args.verbose)
@@ -323,6 +363,7 @@ def build(args):
323363
parse_build_args(args)
324364

325365
if args.bootstrap:
366+
logging.info("Building bootstrap")
326367
# Build llbuild if its build path is not passed in.
327368
if not "llbuild" in args.build_dirs:
328369
build_llbuild(args)
@@ -359,7 +400,7 @@ def test(args):
359400
"""Builds SwiftPM, then tests itself."""
360401
build(args)
361402

362-
note("Testing")
403+
logging.info("Testing")
363404
parse_test_args(args)
364405
cmd = [
365406
os.path.join(args.bin_dir, "swift-test")
@@ -376,7 +417,7 @@ def test(args):
376417
return
377418

378419
# Build SwiftPM with the integrated driver.
379-
note("Bootstrap with the integrated Swift driver")
420+
logging.info("Bootstrap with the integrated Swift driver")
380421
build_swiftpm_with_swiftpm(args,integrated_swift_driver=True)
381422

382423
# Test SwiftPM with the integrated driver. Only the build and
@@ -439,7 +480,7 @@ def install_swiftpm(prefix, args):
439480
for tool in ["swift-build", "swift-test", "swift-run", "swift-package-collection", "swift-package-registry", "swift-sdk", "swift-experimental-sdk"]:
440481
src = "swift-package"
441482
dest = os.path.join(cli_tool_dest, tool)
442-
note("Creating tool symlink from %s to %s" % (src, dest))
483+
logging.info("Creating tool symlink from %s to %s", src, dest)
443484
symlink_force(src, dest)
444485

445486
# Install the PackageDescription/CompilerPluginSupport libraries and associated modules.
@@ -488,7 +529,7 @@ def install_file(args, src, destination, destination_is_directory=True, ignored_
488529
else:
489530
dest = destination
490531

491-
note("Installing %s to %s" % (src, dest))
532+
logging.info("Installing %s to %s", src, dest)
492533
if os.path.isdir(src):
493534
shutil.copytree(src, dest, ignore=shutil.ignore_patterns(*ignored_patterns))
494535
else:
@@ -522,7 +563,7 @@ def build_with_cmake(args, cmake_args, ninja_args, source_path, build_dir, cmake
522563
] + cmake_args + [source_path]
523564

524565
if args.verbose:
525-
print(' '.join(cmd))
566+
logging.info(' '.join(cmd))
526567

527568
mkdir_p(build_dir)
528569
call(cmd, cwd=build_dir, verbose=True)
@@ -540,7 +581,7 @@ def build_with_cmake(args, cmake_args, ninja_args, source_path, build_dir, cmake
540581

541582
def build_llbuild(args):
542583
"""Builds LLBuild using CMake."""
543-
note("Building llbuild")
584+
logging.info("Building llbuild")
544585

545586
# Set where we are going to build llbuild for future steps to find it
546587
args.build_dirs["llbuild"] = os.path.join(args.target_dir, "llbuild")
@@ -571,7 +612,7 @@ def build_llbuild(args):
571612
build_with_cmake(args, flags, [], args.source_dirs["llbuild"], args.build_dirs["llbuild"], cmake_env=cmake_env)
572613

573614
def build_dependency(args, target_name, common_cmake_flags = [], non_darwin_cmake_flags = []):
574-
note("Building " + target_name)
615+
logging.info("Building dependency %s", target_name)
575616
args.build_dirs[target_name] = os.path.join(args.target_dir, target_name)
576617

577618
cmake_flags = common_cmake_flags
@@ -587,8 +628,8 @@ def add_rpath_for_cmake_build(args, rpath):
587628
"Adds the given rpath to the CMake-built swift-bootstrap"
588629
swift_build = os.path.join(args.bootstrap_dir, "bin/swift-bootstrap")
589630
add_rpath_cmd = ["install_name_tool", "-add_rpath", rpath, swift_build]
590-
note(' '.join(add_rpath_cmd))
591-
subprocess.call(add_rpath_cmd, stderr=subprocess.PIPE)
631+
logging.info(' '.join(add_rpath_cmd))
632+
subprocess.call(add_rpath_cmd, stderr=subprocess.PIPE, env=os.environ)
592633

593634
def get_swift_backdeploy_library_paths(args):
594635
if platform.system() == 'Darwin':
@@ -598,7 +639,7 @@ def get_swift_backdeploy_library_paths(args):
598639

599640
def build_swiftpm_with_cmake(args):
600641
"""Builds SwiftPM using CMake."""
601-
note("Building SwiftPM (with CMake)")
642+
logging.info("Building SwiftPM (with CMake)")
602643

603644
cmake_flags = [
604645
get_llbuild_cmake_arg(args),
@@ -642,11 +683,11 @@ def build_swiftpm_with_swiftpm(args, integrated_swift_driver):
642683
swiftpm_args = []
643684

644685
if args.bootstrap:
645-
note("Building SwiftPM (with a freshly built swift-bootstrap)")
686+
logging.info("Building SwiftPM (with a freshly built swift-bootstrap)")
646687
swiftpm_args.append("SWIFTPM_CUSTOM_LIBS_DIR=" + os.path.join(args.bootstrap_dir, "pm"))
647688
swiftpm_args.append(os.path.join(args.bootstrap_dir, "bin/swift-bootstrap"))
648689
else:
649-
note("Building SwiftPM (with a prebuilt swift-build)")
690+
logging.info("Building SwiftPM (with a prebuilt swift-build)")
650691
swiftpm_args.append(args.swift_build_path or os.path.join(os.path.split(args.swiftc_path)[0], "swift-build"))
651692
swiftpm_args.append("--disable-sandbox")
652693

@@ -680,25 +721,28 @@ def build_swiftpm_with_swiftpm(args, integrated_swift_driver):
680721

681722
def call_swiftpm(args, cmd, cwd=None):
682723
"""Calls a SwiftPM binary with the necessary environment variables and flags."""
683-
724+
logging.info("args: %r, cmd: %r, cwd: %r", args, cmd, cwd)
684725
args.build_target = get_build_target(args, cross_compile=(True if args.cross_compile_config else False))
685726

727+
if args.verbose:
728+
logging.debug("build target: %r", args.build_target)
686729
args.platform_path = None
687730
for path in args.target_info["paths"]["runtimeLibraryPaths"]:
688731
args.platform_path = re.search(r"(lib/swift/([^/]+))$", path)
689732
if args.platform_path:
690733
break
691-
692-
if not args.platform_path:
693-
error(
694-
"the command `%s -print-target-info` didn't return a valid runtime library path"
695-
% args.swiftc_path
734+
else:
735+
# this gets called if the for loop does not break
736+
logging.error(
737+
"the command `%s -print-target-info` didn't return a valid runtime library path",
738+
args.swiftc_path
696739
)
740+
raise SystemExit(1)
697741

698742
full_cmd = get_swiftpm_env_cmd(args) + cmd + get_swiftpm_flags(args)
699743
if cwd is None:
700744
cwd = args.project_root
701-
call(full_cmd, cwd=cwd, verbose=True)
745+
call_output(full_cmd, cwd=cwd, stderr=True, verbose=True)
702746

703747
# -----------------------------------------------------------
704748
# Build-related helper functions
@@ -727,8 +771,9 @@ def get_llbuild_source_path(args):
727771
llbuild_path = os.path.join(args.project_root, "..", "llbuild")
728772
if os.path.exists(llbuild_path):
729773
return llbuild_path
730-
note("clone llbuild next to swiftpm directory; see development docs: https://github.com/swiftlang/swift-package-manager/blob/master/Documentation/Contributing.md")
731-
error("unable to find llbuild source directory at %s" % llbuild_path)
774+
logging.info("clone llbuild next to swiftpm directory; see development docs: https://github.com/swiftlang/swift-package-manager/blob/master/Documentation/Contributing.md")
775+
logging.error("unable to find llbuild source directory at %s", llbuild_path)
776+
raise BinaryNotFound(tool="llbuild", path=llbuild_path)
732777

733778
def get_swiftpm_env_cmd(args):
734779
"""Returns the environment variable command to run SwiftPM binaries."""
@@ -823,7 +868,8 @@ def get_swiftpm_flags(args):
823868
elif cross_compile_hosts.startswith('android-'):
824869
build_flags.extend(["--destination", args.cross_compile_config])
825870
else:
826-
error("cannot cross-compile for %s" % cross_compile_hosts)
871+
logging.error("cannot cross-compile for %s", cross_compile_hosts)
872+
raise SystemExit(1)
827873

828874
# Ensure we are not sharing the module cache with concurrent builds in CI
829875
local_module_cache_path=os.path.join(args.build_dir, "module-cache")
@@ -837,6 +883,6 @@ def get_swiftpm_flags(args):
837883
return build_flags
838884

839885
if __name__ == '__main__':
840-
log_timestamp("start")
886+
logging.info("start")
841887
main()
842-
log_timestamp("end")
888+
logging.info("end")

Utilities/build-using-self

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
set -eu
1515

1616
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17+
root_dir="$(cd ${__dir}/.. && pwd)"
1718

18-
cd "${__dir}/.."
19+
cd "${root_dir}/"
1920
echo "Current directory is ${PWD}"
2021

2122
CONFIGURATION=debug
@@ -24,7 +25,7 @@ export SWIFTCI_IS_SELF_HOSTED=1
2425
# Ensure SDKROOT is configured
2526
host=$(uname)
2627
if [ "${host}" == "Darwin" ]; then
27-
export SDKROOT=$(xcrun --show-sdk-path --sdk macosx)
28+
export SDKROOT=$(xcrun --show-sdk-path --sdk macosx)
2829
fi
2930

3031
set -x
@@ -38,17 +39,21 @@ swift test -c $CONFIGURATION --parallel
3839

3940
# Run the integration tests with just built SwiftPM.
4041
export SWIFTPM_BIN_DIR=$(swift build -c $CONFIGURATION --show-bin-path)
41-
cd IntegrationTests
42-
# Perform package update in order to get the latest commits for the dependencies.
43-
swift package update
44-
$SWIFTPM_BIN_DIR/swift-test --parallel
42+
(
43+
cd ${root_dir}/IntegrationTests
44+
# Perform package update in order to get the latest commits for the dependencies.
45+
swift package update
46+
$SWIFTPM_BIN_DIR/swift-test --parallel
47+
)
4548

4649
if [ "${host}" == "Darwin" ]; then
47-
cd ..
48-
echo "Current working directory is ${PWD}"
49-
echo "Bootstrapping with the XCBuild codepath..."
50-
./Utilities/bootstrap build --release \
51-
--cross-compile-hosts macosx-arm64 \
52-
--skip-cmake-bootstrap \
53-
--swift-build-path "${SWIFTPM_BIN_DIR}/swift-build"
50+
echo "Current working directory is ${PWD}"
51+
echo "Bootstrapping with the XCBuild codepath..."
52+
${root_dir}/Utilities/bootstrap \
53+
build \
54+
--release \
55+
--verbose \
56+
--cross-compile-hosts macosx-arm64 \
57+
--skip-cmake-bootstrap \
58+
--swift-build-path "${SWIFTPM_BIN_DIR}/swift-build"
5459
fi

0 commit comments

Comments
 (0)