Skip to content

Commit e072d55

Browse files
author
Mike Ferris
committed
Merge pull request #57 from modocache/build-script-subcommands-test
[1/2][build_script.py] Add "test" subcommand
2 parents 256139c + 2cbc592 commit e072d55

File tree

1 file changed

+130
-55
lines changed

1 file changed

+130
-55
lines changed

build_script.py

Lines changed: 130 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import glob
1414
import os
1515
import subprocess
16+
import sys
1617
import tempfile
1718

1819
SOURCE_DIR = os.path.dirname(os.path.abspath(__file__))
@@ -24,50 +25,12 @@ def run(command):
2425
note(command)
2526
subprocess.check_call(command, shell=True)
2627

27-
def main():
28-
parser = argparse.ArgumentParser(
29-
description="Builds XCTest using a Swift compiler.")
30-
parser.add_argument("--swiftc",
31-
help="path to the swift compiler",
32-
metavar="PATH",
33-
required=True)
34-
parser.add_argument("--build-dir",
35-
help="path to the output build directory. If not "
36-
"specified, a temporary directory is used",
37-
metavar="PATH",
38-
default=tempfile.mkdtemp())
39-
parser.add_argument("--swift-build-dir", help="deprecated, do not use")
40-
parser.add_argument("--arch", help="deprecated, do not use")
41-
parser.add_argument("--module-install-path",
42-
help="location to install module files",
43-
metavar="PATH",
44-
dest="module_path")
45-
parser.add_argument("--library-install-path",
46-
help="location to install shared library files",
47-
metavar="PATH",
48-
dest="lib_path")
49-
parser.add_argument("--release",
50-
help="builds for release",
51-
action="store_const",
52-
dest="build_style",
53-
const="release",
54-
default="debug")
55-
parser.add_argument("--debug",
56-
help="builds for debug (the default)",
57-
action="store_const",
58-
dest="build_style",
59-
const="debug",
60-
default="debug")
61-
parser.add_argument("--test",
62-
help="Whether to run tests after building. "
63-
"Note that you must have cloned "
64-
"https://github.com/apple/swift-llvm "
65-
"at {} in order to run this command. ".format(
66-
os.path.join(
67-
os.path.dirname(SOURCE_DIR), 'llvm')),
68-
action="store_true")
69-
args = parser.parse_args()
7028

29+
def _build(args):
30+
"""
31+
Build XCTest and place the built products in the given 'build_dir'.
32+
If 'test' is specified, also executes the 'test' subcommand.
33+
"""
7134
swiftc = os.path.abspath(args.swiftc)
7235
build_dir = os.path.abspath(args.build_dir)
7336

@@ -112,20 +75,132 @@ def main():
11275
subprocess.check_call(cmd)
11376

11477
if args.test:
115-
lit_path = os.path.join(
116-
os.path.dirname(SOURCE_DIR), 'llvm', 'utils', 'lit', 'lit.py')
117-
lit_flags = '-sv --no-progress-bar'
118-
tests_path = os.path.join(SOURCE_DIR, 'Tests', 'Functional')
119-
run('SWIFT_EXEC={swiftc} '
120-
'BUILT_PRODUCTS_DIR={built_products_dir} '
121-
'{lit_path} {lit_flags} '
122-
'{tests_path}'.format(swiftc=swiftc,
123-
built_products_dir=build_dir,
124-
lit_path=lit_path,
125-
lit_flags=lit_flags,
126-
tests_path=tests_path))
78+
# Execute main() using the arguments necessary to run the tests.
79+
main(args=["test", "--swiftc", swiftc, build_dir])
12780

12881
note('Done.')
12982

83+
84+
def _test(args):
85+
"""
86+
Test the built XCTest.so library at the given 'build_dir', using the
87+
given 'swiftc' compiler.
88+
"""
89+
# FIXME: Allow path to lit to be specified as an option, with this
90+
# path as a default.
91+
lit_path = os.path.join(
92+
os.path.dirname(SOURCE_DIR), "llvm", "utils", "lit", "lit.py")
93+
# FIXME: Allow these to be specified by the Swift build script.
94+
lit_flags = "-sv --no-progress-bar"
95+
tests_path = os.path.join(SOURCE_DIR, "Tests", "Functional")
96+
run("SWIFT_EXEC={swiftc} "
97+
"BUILT_PRODUCTS_DIR={build_dir} "
98+
"{lit_path} {lit_flags} "
99+
"{tests_path}".format(swiftc=args.swiftc,
100+
build_dir=args.build_dir,
101+
lit_path=lit_path,
102+
lit_flags=lit_flags,
103+
tests_path=tests_path))
104+
105+
106+
def main(args=sys.argv[1:]):
107+
"""
108+
The main entry point for this script. Based on the subcommand given,
109+
delegates building or testing XCTest to a sub-parser and its corresponding
110+
function.
111+
"""
112+
parser = argparse.ArgumentParser(
113+
description="Builds, tests, and installs XCTest.")
114+
subparsers = parser.add_subparsers(
115+
description="Use one of these to specify whether to build, test, "
116+
"or install XCTest. If you don't specify any of these, "
117+
"'build' is executed as a default. You may also use "
118+
"'build' to also test and install the built products. "
119+
"Pass the -h or --help option to any of the subcommands "
120+
"for more information.")
121+
122+
build_parser = subparsers.add_parser(
123+
"build",
124+
description="Build XCTest.so, XCTest.swiftmodule, and XCTest.swiftdoc "
125+
"using the given Swift compiler. This command may also "
126+
"test and install the built products.")
127+
build_parser.set_defaults(func=_build)
128+
build_parser.add_argument(
129+
"--swiftc",
130+
help="Path to the 'swiftc' compiler that will be used to build "
131+
"XCTest.so, XCTest.swiftmodule, and XCTest.swiftdoc. This will "
132+
"also be used to build the tests for those built products if the "
133+
"--test option is specified.",
134+
metavar="PATH",
135+
required=True)
136+
build_parser.add_argument(
137+
"--build-dir",
138+
help="Path to the output build directory. If not specified, a "
139+
"temporary directory is used",
140+
metavar="PATH",
141+
default=tempfile.mkdtemp())
142+
build_parser.add_argument("--swift-build-dir",
143+
help="deprecated, do not use")
144+
build_parser.add_argument("--arch", help="deprecated, do not use")
145+
build_parser.add_argument(
146+
"--module-install-path",
147+
help="Location at which to install XCTest.swiftmodule and "
148+
"XCTest.swiftdoc. This directory will be created if it doesn't "
149+
"already exist.",
150+
dest="module_path")
151+
build_parser.add_argument(
152+
"--library-install-path",
153+
help="Location at which to install XCTest.so. This directory will be "
154+
"created if it doesn't already exist.",
155+
dest="lib_path")
156+
build_parser.add_argument(
157+
"--release",
158+
help="builds for release",
159+
action="store_const",
160+
dest="build_style",
161+
const="release",
162+
default="debug")
163+
build_parser.add_argument(
164+
"--debug",
165+
help="builds for debug (the default)",
166+
action="store_const",
167+
dest="build_style",
168+
const="debug",
169+
default="debug")
170+
build_parser.add_argument(
171+
"--test",
172+
help="Whether to run tests after building. Note that you must have "
173+
"cloned https://github.com/apple/swift-llvm at {} in order to "
174+
"run this command.".format(os.path.join(
175+
os.path.dirname(SOURCE_DIR), 'llvm')),
176+
action="store_true")
177+
178+
test_parser = subparsers.add_parser(
179+
"test",
180+
description="Tests a built XCTest framework at the given path.")
181+
test_parser.set_defaults(func=_test)
182+
test_parser.add_argument(
183+
"build_dir",
184+
help="An absolute path to a directory containing the built XCTest.so "
185+
"library.",
186+
metavar="PATH")
187+
test_parser.add_argument(
188+
"--swiftc",
189+
help="Path to the 'swiftc' compiler used to build and run the tests.",
190+
required=True)
191+
192+
# Many versions of Python require a subcommand must be specified.
193+
# We handle this here: if no known subcommand (or none of the help options)
194+
# is included in the arguments, then insert the default subcommand
195+
# argument.
196+
if any([a in ["build", "test", "-h", "--help"] for a in args]):
197+
parsed_args = parser.parse_args(args=args)
198+
else:
199+
parsed_args = parser.parse_args(args=["build"] + args)
200+
201+
# Execute the function for the subcommand we've been given.
202+
parsed_args.func(parsed_args)
203+
204+
130205
if __name__ == '__main__':
131206
main()

0 commit comments

Comments
 (0)