@@ -17,13 +17,31 @@ from __future__ import print_function
17
17
18
18
import argparse
19
19
import json
20
+ import logging
20
21
import os
22
+ import pathlib
21
23
import platform
22
24
import re
23
25
import shutil
24
26
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 .INFO ,
44
+ )
27
45
g_macos_deployment_target = '12.0'
28
46
29
47
g_shared_lib_prefix = "lib"
@@ -32,6 +50,12 @@ if platform.system() == 'Darwin':
32
50
else :
33
51
g_shared_lib_suffix = ".so"
34
52
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
+
35
59
def main ():
36
60
parser = argparse .ArgumentParser (description = """
37
61
This script will build a bootstrapped copy of the Swift Package Manager, and optionally perform extra
@@ -60,7 +84,10 @@ def main():
60
84
parser_install .set_defaults (func = install )
61
85
add_build_args (parser_install )
62
86
87
+ logging .info ("sys.argv: %r" , sys .argv )
63
88
args = parser .parse_args ()
89
+ # update the root logger level based on the verbose flag
90
+ logging .getLogger ().setLevel (logging .DEBUG if args .verbose else logging .INFO )
64
91
args .func = args .func or build
65
92
args .func (args )
66
93
@@ -253,36 +280,47 @@ def parse_test_args(args):
253
280
254
281
def get_swiftc_path (args ):
255
282
"""Returns the path to the Swift compiler."""
283
+ logging .debug ("Getting path to swiftc..." )
256
284
if args .swiftc_path :
257
285
swiftc_path = os .path .abspath (args .swiftc_path )
286
+ logging .debug ("path provided via command line argument. swiftc_path is %r" , swiftc_path )
258
287
elif os .getenv ("SWIFT_EXEC" ):
259
- swiftc_path = os .path .realpath (os .getenv ("SWIFT_EXEC" ))
288
+ swiftc_path = os .getenv ("SWIFT_EXEC" )
289
+ logging .debug ("SWIFT_EXEC env set. swiftc_path set to %r" , swiftc_path )
260
290
elif platform .system () == 'Darwin' :
291
+ logging .debug ("we are on darwin, so calling `xcrun --find swiftc`" )
261
292
swiftc_path = call_output (
262
293
["xcrun" , "--find" , "swiftc" ],
263
294
stderr = subprocess .PIPE ,
264
- verbose = args .verbose
295
+ verbose = args .verbose ,
265
296
)
297
+ logging .debug ("swiftc_path is set to %r" , swiftc_path )
266
298
else :
267
299
swiftc_path = call_output (["which" , "swiftc" ], verbose = args .verbose )
300
+ logging .debug ("calling 'which swiftc'. path is %r" , swiftc_path )
268
301
269
302
if os .path .basename (swiftc_path ) == 'swift' :
270
303
swiftc_path = swiftc_path + 'c'
304
+ logging .debug ("appending to path, it is now %r" , swiftc_path )
271
305
306
+ logging .debug ("swiftc_path set to %r" , swiftc_path )
272
307
if os .path .exists (swiftc_path ):
308
+ logging .debug ("swiftc_path exists.. returning..." )
273
309
return swiftc_path
274
- error ("unable to find swiftc at %s" % swiftc_path )
310
+ logging .error ("unable to find swiftc at %s" , swiftc_path )
311
+ raise BinaryNotFound (tool = "swiftc" , path = swiftc_path )
275
312
276
313
def get_tool_path (args , tool ):
277
314
"""Returns the path to the specified tool."""
315
+ logging .debug ("Searching for %s tool" , tool )
278
316
path = getattr (args , tool + "_path" , None )
279
317
if path is not None :
280
318
return os .path .abspath (path )
281
319
elif platform .system () == 'Darwin' :
282
320
return call_output (
283
321
["xcrun" , "--find" , tool ],
284
322
stderr = subprocess .PIPE ,
285
- verbose = args .verbose
323
+ verbose = args .verbose ,
286
324
)
287
325
else :
288
326
return call_output (["which" , tool ], verbose = args .verbose )
@@ -294,26 +332,30 @@ def get_build_target(args, cross_compile=False):
294
332
if cross_compile :
295
333
cross_compile_json = json .load (open (args .cross_compile_config ))
296
334
command += ['-target' , cross_compile_json ["target" ]]
335
+ logging .debug ("Running command >>> %r" , command )
297
336
target_info_json = subprocess .check_output (command ,
298
- stderr = subprocess .PIPE , universal_newlines = True ).strip ()
337
+ stderr = subprocess .PIPE , universal_newlines = True , env = os .environ ).strip ()
338
+ logging .debug ("Command returned: %r" , target_info_json )
299
339
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 :
340
+ return args .target_info ["target" ]["unversionedTriple" if platform .system () == 'Darwin' else "triple" ]
341
+ except subprocess .CalledProcessError as cpe :
342
+ logging .debug ("Command failed..." )
304
343
# Temporary fallback for Darwin.
305
344
if platform .system () == 'Darwin' :
306
- return 'x86_64-apple-macosx'
345
+ macOS_default = 'x86_64-apple-macosx'
346
+ logging .debug ("we are on Darwin. defaulting to %r" , macOS_default )
347
+ return macOS_default
307
348
else :
308
- error (str (e ))
349
+ logging .error ("get build targets: %s" , str (cpe ))
350
+ raise cpe
309
351
310
352
# -----------------------------------------------------------
311
353
# Actions
312
354
# -----------------------------------------------------------
313
355
314
356
def clean (args ):
315
357
"""Cleans the build artifacts."""
316
- note ("Cleaning" )
358
+ logging . info ("Cleaning" )
317
359
parse_global_args (args )
318
360
319
361
call (["rm" , "-rf" , args .build_dir ], verbose = args .verbose )
@@ -323,6 +365,7 @@ def build(args):
323
365
parse_build_args (args )
324
366
325
367
if args .bootstrap :
368
+ logging .info ("Building bootstrap" )
326
369
# Build llbuild if its build path is not passed in.
327
370
if not "llbuild" in args .build_dirs :
328
371
build_llbuild (args )
@@ -359,7 +402,7 @@ def test(args):
359
402
"""Builds SwiftPM, then tests itself."""
360
403
build (args )
361
404
362
- note ("Testing" )
405
+ logging . info ("Testing" )
363
406
parse_test_args (args )
364
407
cmd = [
365
408
os .path .join (args .bin_dir , "swift-test" )
@@ -376,7 +419,7 @@ def test(args):
376
419
return
377
420
378
421
# Build SwiftPM with the integrated driver.
379
- note ("Bootstrap with the integrated Swift driver" )
422
+ logging . info ("Bootstrap with the integrated Swift driver" )
380
423
build_swiftpm_with_swiftpm (args ,integrated_swift_driver = True )
381
424
382
425
# Test SwiftPM with the integrated driver. Only the build and
@@ -439,7 +482,7 @@ def install_swiftpm(prefix, args):
439
482
for tool in ["swift-build" , "swift-test" , "swift-run" , "swift-package-collection" , "swift-package-registry" , "swift-sdk" , "swift-experimental-sdk" ]:
440
483
src = "swift-package"
441
484
dest = os .path .join (cli_tool_dest , tool )
442
- note ("Creating tool symlink from %s to %s" % ( src , dest ) )
485
+ logging . info ("Creating tool symlink from %s to %s" , src , dest )
443
486
symlink_force (src , dest )
444
487
445
488
# Install the PackageDescription/CompilerPluginSupport libraries and associated modules.
@@ -488,7 +531,7 @@ def install_file(args, src, destination, destination_is_directory=True, ignored_
488
531
else :
489
532
dest = destination
490
533
491
- note ("Installing %s to %s" % ( src , dest ) )
534
+ logging . info ("Installing %s to %s" , src , dest )
492
535
if os .path .isdir (src ):
493
536
shutil .copytree (src , dest , ignore = shutil .ignore_patterns (* ignored_patterns ))
494
537
else :
@@ -521,8 +564,7 @@ def build_with_cmake(args, cmake_args, ninja_args, source_path, build_dir, cmake
521
564
"-DCMAKE_RANLIB:PATH=%s" % (args .ranlib_path ),
522
565
] + cmake_args + [source_path ]
523
566
524
- if args .verbose :
525
- print (' ' .join (cmd ))
567
+ logging .debug (' ' .join (cmd ))
526
568
527
569
mkdir_p (build_dir )
528
570
call (cmd , cwd = build_dir , verbose = True )
@@ -540,7 +582,7 @@ def build_with_cmake(args, cmake_args, ninja_args, source_path, build_dir, cmake
540
582
541
583
def build_llbuild (args ):
542
584
"""Builds LLBuild using CMake."""
543
- note ("Building llbuild" )
585
+ logging . info ("Building llbuild" )
544
586
545
587
# Set where we are going to build llbuild for future steps to find it
546
588
args .build_dirs ["llbuild" ] = os .path .join (args .target_dir , "llbuild" )
@@ -571,7 +613,7 @@ def build_llbuild(args):
571
613
build_with_cmake (args , flags , [], args .source_dirs ["llbuild" ], args .build_dirs ["llbuild" ], cmake_env = cmake_env )
572
614
573
615
def build_dependency (args , target_name , common_cmake_flags = [], non_darwin_cmake_flags = []):
574
- note ("Building " + target_name )
616
+ logging . info ("Building dependency %s" , target_name )
575
617
args .build_dirs [target_name ] = os .path .join (args .target_dir , target_name )
576
618
577
619
cmake_flags = common_cmake_flags
@@ -587,8 +629,8 @@ def add_rpath_for_cmake_build(args, rpath):
587
629
"Adds the given rpath to the CMake-built swift-bootstrap"
588
630
swift_build = os .path .join (args .bootstrap_dir , "bin/swift-bootstrap" )
589
631
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 )
632
+ logging . info (' ' .join (add_rpath_cmd ))
633
+ subprocess .call (add_rpath_cmd , stderr = subprocess .PIPE , env = os . environ )
592
634
593
635
def get_swift_backdeploy_library_paths (args ):
594
636
if platform .system () == 'Darwin' :
@@ -598,7 +640,7 @@ def get_swift_backdeploy_library_paths(args):
598
640
599
641
def build_swiftpm_with_cmake (args ):
600
642
"""Builds SwiftPM using CMake."""
601
- note ("Building SwiftPM (with CMake)" )
643
+ logging . info ("Building SwiftPM (with CMake)" )
602
644
603
645
cmake_flags = [
604
646
get_llbuild_cmake_arg (args ),
@@ -642,11 +684,11 @@ def build_swiftpm_with_swiftpm(args, integrated_swift_driver):
642
684
swiftpm_args = []
643
685
644
686
if args .bootstrap :
645
- note ("Building SwiftPM (with a freshly built swift-bootstrap)" )
687
+ logging . info ("Building SwiftPM (with a freshly built swift-bootstrap)" )
646
688
swiftpm_args .append ("SWIFTPM_CUSTOM_LIBS_DIR=" + os .path .join (args .bootstrap_dir , "pm" ))
647
689
swiftpm_args .append (os .path .join (args .bootstrap_dir , "bin/swift-bootstrap" ))
648
690
else :
649
- note ("Building SwiftPM (with a prebuilt swift-build)" )
691
+ logging . info ("Building SwiftPM (with a prebuilt swift-build)" )
650
692
swiftpm_args .append (args .swift_build_path or os .path .join (os .path .split (args .swiftc_path )[0 ], "swift-build" ))
651
693
swiftpm_args .append ("--disable-sandbox" )
652
694
@@ -655,6 +697,7 @@ def build_swiftpm_with_swiftpm(args, integrated_swift_driver):
655
697
656
698
# Any leftover resolved file from a run without `SWIFTCI_USE_LOCAL_DEPS` needs to be deleted.
657
699
if os .path .exists ("Package.resolved" ):
700
+ logging .debug ("removing Package.resolve" )
658
701
os .remove ("Package.resolved" )
659
702
660
703
if integrated_swift_driver :
@@ -680,25 +723,27 @@ def build_swiftpm_with_swiftpm(args, integrated_swift_driver):
680
723
681
724
def call_swiftpm (args , cmd , cwd = None ):
682
725
"""Calls a SwiftPM binary with the necessary environment variables and flags."""
683
-
726
+ logging . info ( "function args: %r, cmd: %r, cwd: %r" , args , cmd , cwd )
684
727
args .build_target = get_build_target (args , cross_compile = (True if args .cross_compile_config else False ))
685
728
729
+ logging .debug ("build target: %r" , args .build_target )
686
730
args .platform_path = None
687
731
for path in args .target_info ["paths" ]["runtimeLibraryPaths" ]:
688
732
args .platform_path = re .search (r"(lib/swift/([^/]+))$" , path )
689
733
if args .platform_path :
690
734
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
735
+ else :
736
+ # this gets called if the for loop does not break
737
+ logging . error (
738
+ "the command `%s -print-target-info` didn't return a valid runtime library path" ,
739
+ args .swiftc_path
696
740
)
741
+ raise SystemExit (1 )
697
742
698
743
full_cmd = get_swiftpm_env_cmd (args ) + cmd + get_swiftpm_flags (args )
699
744
if cwd is None :
700
745
cwd = args .project_root
701
- call (full_cmd , cwd = cwd , verbose = True )
746
+ call_output (full_cmd , cwd = cwd , stderr = True , verbose = True )
702
747
703
748
# -----------------------------------------------------------
704
749
# Build-related helper functions
@@ -727,8 +772,9 @@ def get_llbuild_source_path(args):
727
772
llbuild_path = os .path .join (args .project_root , ".." , "llbuild" )
728
773
if os .path .exists (llbuild_path ):
729
774
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 )
775
+ logging .info ("clone llbuild next to swiftpm directory; see development docs: https://github.com/swiftlang/swift-package-manager/blob/master/Documentation/Contributing.md" )
776
+ logging .error ("unable to find llbuild source directory at %s" , llbuild_path )
777
+ raise BinaryNotFound (tool = "llbuild" , path = llbuild_path )
732
778
733
779
def get_swiftpm_env_cmd (args ):
734
780
"""Returns the environment variable command to run SwiftPM binaries."""
@@ -823,7 +869,8 @@ def get_swiftpm_flags(args):
823
869
elif cross_compile_hosts .startswith ('android-' ):
824
870
build_flags .extend (["--destination" , args .cross_compile_config ])
825
871
else :
826
- error ("cannot cross-compile for %s" % cross_compile_hosts )
872
+ logging .error ("cannot cross-compile for %s" , cross_compile_hosts )
873
+ raise SystemExit (1 )
827
874
828
875
# Ensure we are not sharing the module cache with concurrent builds in CI
829
876
local_module_cache_path = os .path .join (args .build_dir , "module-cache" )
@@ -837,6 +884,6 @@ def get_swiftpm_flags(args):
837
884
return build_flags
838
885
839
886
if __name__ == '__main__' :
840
- log_timestamp ("start" )
887
+ logging . info ("start" )
841
888
main ()
842
- log_timestamp ("end" )
889
+ logging . info ("end" )
0 commit comments