1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import print_function
4
+
5
+ import argparse
6
+ import os
7
+ import platform
8
+ import subprocess
9
+ import sys
10
+
11
+ def swiftpm (action , swift_exec , swiftpm_args , env = None ):
12
+ cmd = [swift_exec , action ] + swiftpm_args
13
+ print (' ' .join (cmd ))
14
+ subprocess .check_call (cmd , env = env )
15
+
16
+ def get_swiftpm_options (args ):
17
+ swiftpm_args = [
18
+ '--package-path' , args .package_path ,
19
+ '--build-path' , args .build_path ,
20
+ '--configuration' , args .configuration ,
21
+ ]
22
+
23
+ if args .verbose :
24
+ swiftpm_args += ['--verbose' ]
25
+
26
+ if args .configuration == 'release' :
27
+ # Enable running tests that use @testable in release builds.
28
+ swiftpm_args += ['-Xswiftc' , '-enable-testing' ]
29
+
30
+ if platform .system () != 'Darwin' :
31
+ swiftpm_args += [
32
+ # Dispatch headers
33
+ '-Xcxx' , '-I' , '-Xcxx' ,
34
+ os .path .join (args .toolchain , 'usr' , 'lib' , 'swift' ),
35
+ ]
36
+
37
+ return swiftpm_args
38
+
39
+ def main ():
40
+ parser = argparse .ArgumentParser (description = 'Build along with the Swift build-script.' )
41
+ def add_common_args (parser ):
42
+ parser .add_argument ('--package-path' , metavar = 'PATH' , help = 'directory of the package to build' , default = '.' )
43
+ parser .add_argument ('--toolchain' , required = True , metavar = 'PATH' , help = 'build using the toolchain at PATH' )
44
+ parser .add_argument ('--build-path' , metavar = 'PATH' , default = '.build' , help = 'build in the given path' )
45
+ parser .add_argument ('--configuration' , '-c' , default = 'debug' , help = 'build using configuration (release|debug)' )
46
+ parser .add_argument ('--verbose' , '-v' , action = 'store_true' , help = 'enable verbose output' )
47
+
48
+ subparsers = parser .add_subparsers (title = 'subcommands' , dest = 'action' , metavar = 'action' )
49
+ build_parser = subparsers .add_parser ('build' , help = 'build the package' )
50
+ add_common_args (build_parser )
51
+
52
+ test_parser = subparsers .add_parser ('test' , help = 'test the package' )
53
+ add_common_args (test_parser )
54
+
55
+ args = parser .parse_args (sys .argv [1 :])
56
+
57
+ # Canonicalize paths
58
+ args .package_path = os .path .abspath (args .package_path )
59
+ args .build_path = os .path .abspath (args .build_path )
60
+ args .toolchain = os .path .abspath (args .toolchain )
61
+
62
+ if args .toolchain :
63
+ swift_exec = os .path .join (args .toolchain , 'usr' , 'bin' , 'swift' )
64
+ else :
65
+ swift_exec = 'swift'
66
+
67
+ swiftpm_args = get_swiftpm_options (args )
68
+
69
+ if args .action == 'build' :
70
+ swiftpm ('build' , swift_exec , swiftpm_args )
71
+ elif args .action == 'test' :
72
+ # Set the toolchain used in tests at runtime
73
+ env = os .environ
74
+ env ['SOURCEKIT_TOOLCHAIN_PATH' ] = args .toolchain
75
+ swiftpm ('test' , swift_exec , swiftpm_args , env )
76
+ else :
77
+ assert False , 'unknown action \' {}\' ' .format (args .action )
78
+
79
+ if __name__ == '__main__' :
80
+ main ()
0 commit comments