@@ -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 .DEBUG ,
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,6 +84,7 @@ 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 ()
64
89
args .func = args .func or build
65
90
args .func (args )
@@ -253,36 +278,47 @@ def parse_test_args(args):
253
278
254
279
def get_swiftc_path (args ):
255
280
"""Returns the path to the Swift compiler."""
281
+ logging .info ("Getting path to swiftc..." )
256
282
if args .swiftc_path :
257
283
swiftc_path = os .path .abspath (args .swiftc_path )
284
+ logging .debug ("path provided via command line argument. swiftc_path is %r" , swiftc_path )
258
285
elif os .getenv ("SWIFT_EXEC" ):
259
286
swiftc_path = os .path .realpath (os .getenv ("SWIFT_EXEC" ))
287
+ logging .info ("SWIFT_EXEC env set." )
260
288
elif platform .system () == 'Darwin' :
289
+ logging .debug ("we are on darwin, so calling `xcrun --find swiftc`" )
261
290
swiftc_path = call_output (
262
291
["xcrun" , "--find" , "swiftc" ],
263
292
stderr = subprocess .PIPE ,
264
- verbose = args .verbose
293
+ verbose = args .verbose ,
265
294
)
295
+ logging .debug ("swiftc_path is set to %r" , swiftc_path )
266
296
else :
267
297
swiftc_path = call_output (["which" , "swiftc" ], verbose = args .verbose )
298
+ logging .debug ("calling 'which swiftc'. path is %r" , swiftc_path )
268
299
269
300
if os .path .basename (swiftc_path ) == 'swift' :
270
301
swiftc_path = swiftc_path + 'c'
302
+ logging .debug ("appending to path, it is now %r" , swiftc_path )
271
303
304
+ logging .debug ("swiftc_path set to %r" , swiftc_path )
272
305
if os .path .exists (swiftc_path ):
306
+ logging .debug ("swiftc_path exists.. returning..." )
273
307
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 )
275
310
276
311
def get_tool_path (args , tool ):
277
312
"""Returns the path to the specified tool."""
313
+ logging .debug ("Searching for %s tool" , tool )
278
314
path = getattr (args , tool + "_path" , None )
279
315
if path is not None :
280
316
return os .path .abspath (path )
281
317
elif platform .system () == 'Darwin' :
282
318
return call_output (
283
319
["xcrun" , "--find" , tool ],
284
320
stderr = subprocess .PIPE ,
285
- verbose = args .verbose
321
+ verbose = args .verbose ,
286
322
)
287
323
else :
288
324
return call_output (["which" , tool ], verbose = args .verbose )
@@ -294,26 +330,30 @@ def get_build_target(args, cross_compile=False):
294
330
if cross_compile :
295
331
cross_compile_json = json .load (open (args .cross_compile_config ))
296
332
command += ['-target' , cross_compile_json ["target" ]]
333
+ logging .debug ("Running command >>> %r" , command )
297
334
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 )
299
337
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..." )
304
341
# Temporary fallback for Darwin.
305
342
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
307
346
else :
308
- error (str (e ))
347
+ logging .error ("get build targets: %s" , str (cpe ))
348
+ raise cpe
309
349
310
350
# -----------------------------------------------------------
311
351
# Actions
312
352
# -----------------------------------------------------------
313
353
314
354
def clean (args ):
315
355
"""Cleans the build artifacts."""
316
- note ("Cleaning" )
356
+ logging . info ("Cleaning" )
317
357
parse_global_args (args )
318
358
319
359
call (["rm" , "-rf" , args .build_dir ], verbose = args .verbose )
@@ -323,6 +363,7 @@ def build(args):
323
363
parse_build_args (args )
324
364
325
365
if args .bootstrap :
366
+ logging .info ("Building bootstrap" )
326
367
# Build llbuild if its build path is not passed in.
327
368
if not "llbuild" in args .build_dirs :
328
369
build_llbuild (args )
@@ -359,7 +400,7 @@ def test(args):
359
400
"""Builds SwiftPM, then tests itself."""
360
401
build (args )
361
402
362
- note ("Testing" )
403
+ logging . info ("Testing" )
363
404
parse_test_args (args )
364
405
cmd = [
365
406
os .path .join (args .bin_dir , "swift-test" )
@@ -376,7 +417,7 @@ def test(args):
376
417
return
377
418
378
419
# Build SwiftPM with the integrated driver.
379
- note ("Bootstrap with the integrated Swift driver" )
420
+ logging . info ("Bootstrap with the integrated Swift driver" )
380
421
build_swiftpm_with_swiftpm (args ,integrated_swift_driver = True )
381
422
382
423
# Test SwiftPM with the integrated driver. Only the build and
@@ -439,7 +480,7 @@ def install_swiftpm(prefix, args):
439
480
for tool in ["swift-build" , "swift-test" , "swift-run" , "swift-package-collection" , "swift-package-registry" , "swift-sdk" , "swift-experimental-sdk" ]:
440
481
src = "swift-package"
441
482
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 )
443
484
symlink_force (src , dest )
444
485
445
486
# Install the PackageDescription/CompilerPluginSupport libraries and associated modules.
@@ -488,7 +529,7 @@ def install_file(args, src, destination, destination_is_directory=True, ignored_
488
529
else :
489
530
dest = destination
490
531
491
- note ("Installing %s to %s" % ( src , dest ) )
532
+ logging . info ("Installing %s to %s" , src , dest )
492
533
if os .path .isdir (src ):
493
534
shutil .copytree (src , dest , ignore = shutil .ignore_patterns (* ignored_patterns ))
494
535
else :
@@ -522,7 +563,7 @@ def build_with_cmake(args, cmake_args, ninja_args, source_path, build_dir, cmake
522
563
] + cmake_args + [source_path ]
523
564
524
565
if args .verbose :
525
- print (' ' .join (cmd ))
566
+ logging . info (' ' .join (cmd ))
526
567
527
568
mkdir_p (build_dir )
528
569
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
540
581
541
582
def build_llbuild (args ):
542
583
"""Builds LLBuild using CMake."""
543
- note ("Building llbuild" )
584
+ logging . info ("Building llbuild" )
544
585
545
586
# Set where we are going to build llbuild for future steps to find it
546
587
args .build_dirs ["llbuild" ] = os .path .join (args .target_dir , "llbuild" )
@@ -571,7 +612,7 @@ def build_llbuild(args):
571
612
build_with_cmake (args , flags , [], args .source_dirs ["llbuild" ], args .build_dirs ["llbuild" ], cmake_env = cmake_env )
572
613
573
614
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 )
575
616
args .build_dirs [target_name ] = os .path .join (args .target_dir , target_name )
576
617
577
618
cmake_flags = common_cmake_flags
@@ -587,8 +628,8 @@ def add_rpath_for_cmake_build(args, rpath):
587
628
"Adds the given rpath to the CMake-built swift-bootstrap"
588
629
swift_build = os .path .join (args .bootstrap_dir , "bin/swift-bootstrap" )
589
630
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 )
592
633
593
634
def get_swift_backdeploy_library_paths (args ):
594
635
if platform .system () == 'Darwin' :
@@ -598,7 +639,7 @@ def get_swift_backdeploy_library_paths(args):
598
639
599
640
def build_swiftpm_with_cmake (args ):
600
641
"""Builds SwiftPM using CMake."""
601
- note ("Building SwiftPM (with CMake)" )
642
+ logging . info ("Building SwiftPM (with CMake)" )
602
643
603
644
cmake_flags = [
604
645
get_llbuild_cmake_arg (args ),
@@ -642,11 +683,11 @@ def build_swiftpm_with_swiftpm(args, integrated_swift_driver):
642
683
swiftpm_args = []
643
684
644
685
if args .bootstrap :
645
- note ("Building SwiftPM (with a freshly built swift-bootstrap)" )
686
+ logging . info ("Building SwiftPM (with a freshly built swift-bootstrap)" )
646
687
swiftpm_args .append ("SWIFTPM_CUSTOM_LIBS_DIR=" + os .path .join (args .bootstrap_dir , "pm" ))
647
688
swiftpm_args .append (os .path .join (args .bootstrap_dir , "bin/swift-bootstrap" ))
648
689
else :
649
- note ("Building SwiftPM (with a prebuilt swift-build)" )
690
+ logging . info ("Building SwiftPM (with a prebuilt swift-build)" )
650
691
swiftpm_args .append (args .swift_build_path or os .path .join (os .path .split (args .swiftc_path )[0 ], "swift-build" ))
651
692
swiftpm_args .append ("--disable-sandbox" )
652
693
@@ -680,25 +721,28 @@ def build_swiftpm_with_swiftpm(args, integrated_swift_driver):
680
721
681
722
def call_swiftpm (args , cmd , cwd = None ):
682
723
"""Calls a SwiftPM binary with the necessary environment variables and flags."""
683
-
724
+ logging . info ( "args: %r, cmd: %r, cwd: %r" , args , cmd , cwd )
684
725
args .build_target = get_build_target (args , cross_compile = (True if args .cross_compile_config else False ))
685
726
727
+ if args .verbose :
728
+ logging .debug ("build target: %r" , args .build_target )
686
729
args .platform_path = None
687
730
for path in args .target_info ["paths" ]["runtimeLibraryPaths" ]:
688
731
args .platform_path = re .search (r"(lib/swift/([^/]+))$" , path )
689
732
if args .platform_path :
690
733
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
696
739
)
740
+ raise SystemExit (1 )
697
741
698
742
full_cmd = get_swiftpm_env_cmd (args ) + cmd + get_swiftpm_flags (args )
699
743
if cwd is None :
700
744
cwd = args .project_root
701
- call (full_cmd , cwd = cwd , verbose = True )
745
+ call_output (full_cmd , cwd = cwd , stderr = True , verbose = True )
702
746
703
747
# -----------------------------------------------------------
704
748
# Build-related helper functions
@@ -727,8 +771,9 @@ def get_llbuild_source_path(args):
727
771
llbuild_path = os .path .join (args .project_root , ".." , "llbuild" )
728
772
if os .path .exists (llbuild_path ):
729
773
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 )
732
777
733
778
def get_swiftpm_env_cmd (args ):
734
779
"""Returns the environment variable command to run SwiftPM binaries."""
@@ -823,7 +868,8 @@ def get_swiftpm_flags(args):
823
868
elif cross_compile_hosts .startswith ('android-' ):
824
869
build_flags .extend (["--destination" , args .cross_compile_config ])
825
870
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 )
827
873
828
874
# Ensure we are not sharing the module cache with concurrent builds in CI
829
875
local_module_cache_path = os .path .join (args .build_dir , "module-cache" )
@@ -837,6 +883,6 @@ def get_swiftpm_flags(args):
837
883
return build_flags
838
884
839
885
if __name__ == '__main__' :
840
- log_timestamp ("start" )
886
+ logging . info ("start" )
841
887
main ()
842
- log_timestamp ("end" )
888
+ logging . info ("end" )
0 commit comments