Skip to content

Commit ed5575b

Browse files
authored
Merge pull request swiftlang#71 from benlangmuir/build-script-helper
[build-script] Add build-script-helper.py
2 parents 71f6025 + 1daaa24 commit ed5575b

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

Tests/SKCoreTests/ToolchainRegistryTests.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,9 @@ final class ToolchainRegistryTests: XCTestCase {
228228
XCTAssertNil(tr.default)
229229
XCTAssert(tr.toolchains.isEmpty)
230230

231-
try! setenv("SOURCEKIT_TOOLCHAIN_PATH", value: binPath.parentDirectory.asString)
232-
defer { try! setenv("SOURCEKIT_TOOLCHAIN_PATH", value: "") }
231+
try! setenv("TEST_SOURCEKIT_TOOLCHAIN_PATH_1", value: binPath.parentDirectory.asString)
233232

234-
tr.scanForToolchains(fs)
233+
tr.scanForToolchains(environmentVariables: ["TEST_SOURCEKIT_TOOLCHAIN_PATH_1"], fs)
235234

236235
guard let tc = tr.toolchains.first(where: { tc in tc.path == binPath.parentDirectory }) else {
237236
XCTFail("couldn't find expected toolchain")
@@ -256,10 +255,10 @@ final class ToolchainRegistryTests: XCTestCase {
256255
XCTAssertNil(tr.default)
257256
XCTAssert(tr.toolchains.isEmpty)
258257

259-
try! setenv("TEST_ENV_SOURCEKIT_TOOLCHAIN_PATH", value: binPath.parentDirectory.asString)
258+
try! setenv("TEST_ENV_SOURCEKIT_TOOLCHAIN_PATH_2", value: binPath.parentDirectory.asString)
260259

261260
tr.scanForToolchains(
262-
environmentVariables: ["TEST_ENV_SOURCEKIT_TOOLCHAIN_PATH"],
261+
environmentVariables: ["TEST_ENV_SOURCEKIT_TOOLCHAIN_PATH_2"],
263262
setDefault: false,
264263
fs)
265264

Utilities/build-script-helper.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)