Skip to content

Commit d415523

Browse files
committed
Merge pull request #2595 from rintaro/build-script-workspace
[build-script] Factor out calculation of args.build_subdir
2 parents 69ca588 + 00617f9 commit d415523

File tree

3 files changed

+243
-57
lines changed

3 files changed

+243
-57
lines changed

utils/build-script

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ from swift_build_support import migration # noqa (E402)
4848
import swift_build_support.tar # noqa (E402)
4949
import swift_build_support.targets # noqa (E402)
5050
from swift_build_support.cmake import CMake # noqa (E402)
51+
from swift_build_support.workspace import Workspace # noqa(E402)
52+
from swift_build_support.workspace import compute_build_subdir # noqa(E402)
5153

5254

5355
# A strict parser for bools (unlike Python's `bool()`, where
@@ -1203,69 +1205,21 @@ details of the setups of other systems or automated environments.""")
12031205
]
12041206

12051207
if args.build_subdir is None:
1206-
# Create a name for the build directory.
1207-
args.build_subdir = args.cmake_generator.replace(" ", "_")
1208-
cmark_build_dir_label = args.cmark_build_variant
1209-
if args.cmark_assertions:
1210-
cmark_build_dir_label += "Assert"
1211-
1212-
llvm_build_dir_label = args.llvm_build_variant
1213-
if args.llvm_assertions:
1214-
llvm_build_dir_label += "Assert"
1215-
1216-
swift_build_dir_label = args.swift_build_variant
1217-
if args.swift_assertions:
1218-
swift_build_dir_label += "Assert"
1219-
if args.swift_analyze_code_coverage != "false":
1220-
swift_build_dir_label += "Coverage"
1221-
1222-
swift_stdlib_build_dir_label = args.swift_stdlib_build_variant
1223-
if args.swift_stdlib_assertions:
1224-
swift_stdlib_build_dir_label += "Assert"
1225-
1226-
# FIXME: mangle LLDB build configuration into the directory name.
1227-
if (llvm_build_dir_label == swift_build_dir_label and
1228-
llvm_build_dir_label == swift_stdlib_build_dir_label and
1229-
llvm_build_dir_label == cmark_build_dir_label):
1230-
# Use a simple directory name if all projects use the same build
1231-
# type.
1232-
args.build_subdir += "-" + llvm_build_dir_label
1233-
elif (llvm_build_dir_label != swift_build_dir_label and
1234-
llvm_build_dir_label == swift_stdlib_build_dir_label and
1235-
llvm_build_dir_label == cmark_build_dir_label):
1236-
# Swift build type differs.
1237-
args.build_subdir += "-" + llvm_build_dir_label
1238-
args.build_subdir += "+swift-" + swift_build_dir_label
1239-
elif (llvm_build_dir_label == swift_build_dir_label and
1240-
llvm_build_dir_label != swift_stdlib_build_dir_label and
1241-
llvm_build_dir_label == cmark_build_dir_label):
1242-
# Swift stdlib build type differs.
1243-
args.build_subdir += "-" + llvm_build_dir_label
1244-
args.build_subdir += "+stdlib-" + swift_stdlib_build_dir_label
1245-
elif (llvm_build_dir_label == swift_build_dir_label and
1246-
llvm_build_dir_label == swift_stdlib_build_dir_label and
1247-
llvm_build_dir_label != cmark_build_dir_label):
1248-
# cmark build type differs.
1249-
args.build_subdir += "-" + llvm_build_dir_label
1250-
args.build_subdir += "+cmark-" + cmark_build_dir_label
1251-
else:
1252-
# We don't know how to create a short name, so just mangle in all
1253-
# the information.
1254-
args.build_subdir += "+cmark-" + cmark_build_dir_label
1255-
args.build_subdir += "+llvm-" + llvm_build_dir_label
1256-
args.build_subdir += "+swift-" + swift_build_dir_label
1257-
args.build_subdir += "+stdlib-" + swift_stdlib_build_dir_label
1208+
args.build_subdir = compute_build_subdir(args)
12581209

1259-
build_dir = os.path.join(SWIFT_BUILD_ROOT, args.build_subdir)
1210+
workspace = Workspace(
1211+
source_root=SWIFT_SOURCE_ROOT,
1212+
build_root=os.path.join(SWIFT_BUILD_ROOT, args.build_subdir))
12601213

1261-
if args.clean and os.path.isdir(build_dir):
1262-
shutil.rmtree(build_dir)
1214+
if args.clean and os.path.isdir(workspace.build_root):
1215+
shutil.rmtree(workspace.build_root)
12631216

12641217
cmake = CMake(args=args,
12651218
toolchain=toolchain)
12661219

12671220
build_script_impl_args = [
1268-
"--build-dir", build_dir,
1221+
"--workspace", workspace.source_root,
1222+
"--build-dir", workspace.build_root,
12691223
"--install-prefix", os.path.abspath(args.install_prefix),
12701224
"--host-target", args.host_target,
12711225
"--stdlib-deployment-targets",
@@ -1287,7 +1241,6 @@ details of the setups of other systems or automated environments.""")
12871241
args.swift_analyze_code_coverage).lower(),
12881242
"--cmake-generator", args.cmake_generator,
12891243
"--build-jobs", str(args.build_jobs),
1290-
"--workspace", SWIFT_SOURCE_ROOT,
12911244
"--common-cmake-options=%s" % ' '.join(
12921245
pipes.quote(opt) for opt in cmake.common_options()),
12931246
"--build-args=%s" % ' '.join(
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# swift_build_support/workspace.py ------------------------------*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See http://swift.org/LICENSE.txt for license information
9+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
# ----------------------------------------------------------------------------
12+
"""
13+
Represent whole source tree and the build directory
14+
"""
15+
# ----------------------------------------------------------------------------
16+
17+
import os.path
18+
19+
20+
class Workspace(object):
21+
def __init__(self, source_root, build_root):
22+
self.source_root = source_root
23+
self.build_root = build_root
24+
25+
def source_dir(self, path):
26+
return os.path.join(self.source_root, path)
27+
28+
def build_dir(self, deployment_target, product):
29+
return os.path.join(self.build_root,
30+
'%s-%s' % (product, deployment_target))
31+
32+
33+
def compute_build_subdir(args):
34+
# Create a name for the build directory.
35+
build_subdir = args.cmake_generator.replace(" ", "_")
36+
37+
cmark_build_dir_label = args.cmark_build_variant
38+
if args.cmark_assertions:
39+
cmark_build_dir_label += "Assert"
40+
41+
llvm_build_dir_label = args.llvm_build_variant
42+
if args.llvm_assertions:
43+
llvm_build_dir_label += "Assert"
44+
45+
swift_build_dir_label = args.swift_build_variant
46+
if args.swift_assertions:
47+
swift_build_dir_label += "Assert"
48+
if args.swift_analyze_code_coverage != "false":
49+
swift_build_dir_label += "Coverage"
50+
51+
swift_stdlib_build_dir_label = args.swift_stdlib_build_variant
52+
if args.swift_stdlib_assertions:
53+
swift_stdlib_build_dir_label += "Assert"
54+
55+
# FIXME: mangle LLDB build configuration into the directory name.
56+
if (llvm_build_dir_label == swift_build_dir_label and
57+
llvm_build_dir_label == swift_stdlib_build_dir_label and
58+
llvm_build_dir_label == cmark_build_dir_label):
59+
# Use a simple directory name if all projects use the same build
60+
# type.
61+
build_subdir += "-" + llvm_build_dir_label
62+
elif (llvm_build_dir_label != swift_build_dir_label and
63+
llvm_build_dir_label == swift_stdlib_build_dir_label and
64+
llvm_build_dir_label == cmark_build_dir_label):
65+
# Swift build type differs.
66+
build_subdir += "-" + llvm_build_dir_label
67+
build_subdir += "+swift-" + swift_build_dir_label
68+
elif (llvm_build_dir_label == swift_build_dir_label and
69+
llvm_build_dir_label != swift_stdlib_build_dir_label and
70+
llvm_build_dir_label == cmark_build_dir_label):
71+
# Swift stdlib build type differs.
72+
build_subdir += "-" + llvm_build_dir_label
73+
build_subdir += "+stdlib-" + swift_stdlib_build_dir_label
74+
elif (llvm_build_dir_label == swift_build_dir_label and
75+
llvm_build_dir_label == swift_stdlib_build_dir_label and
76+
llvm_build_dir_label != cmark_build_dir_label):
77+
# cmark build type differs.
78+
build_subdir += "-" + llvm_build_dir_label
79+
build_subdir += "+cmark-" + cmark_build_dir_label
80+
else:
81+
# We don't know how to create a short name, so just mangle in all
82+
# the information.
83+
build_subdir += "+cmark-" + cmark_build_dir_label
84+
build_subdir += "+llvm-" + llvm_build_dir_label
85+
build_subdir += "+swift-" + swift_build_dir_label
86+
build_subdir += "+stdlib-" + swift_stdlib_build_dir_label
87+
88+
return build_subdir
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# tests/test_workspace.py ---------------------------------------*- python -*-
2+
#
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See http://swift.org/LICENSE.txt for license information
9+
# See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
#
11+
# ----------------------------------------------------------------------------
12+
13+
import argparse
14+
import itertools
15+
import os
16+
import shutil
17+
import tempfile
18+
import unittest
19+
20+
from swift_build_support.workspace import (
21+
Workspace,
22+
compute_build_subdir,
23+
)
24+
25+
26+
class WorkspaceTestCase(unittest.TestCase):
27+
28+
def test_workspace(self):
29+
tmpdir1 = os.path.realpath(tempfile.mkdtemp())
30+
tmpdir2 = os.path.realpath(tempfile.mkdtemp())
31+
os.makedirs(os.path.join(tmpdir1, 'foo'))
32+
33+
workspace = Workspace(source_root=tmpdir1,
34+
build_root=tmpdir2)
35+
36+
self.assertEqual(workspace.source_root, tmpdir1)
37+
self.assertEqual(workspace.build_root, tmpdir2)
38+
39+
# source_dir
40+
self.assertEqual(workspace.source_dir('foo'),
41+
os.path.join(tmpdir1, 'foo'))
42+
43+
# build_dir
44+
self.assertEqual(workspace.build_dir('target', 'product'),
45+
os.path.join(tmpdir2, 'product-target'))
46+
47+
shutil.rmtree(tmpdir1)
48+
shutil.rmtree(tmpdir2)
49+
50+
51+
class ComputeBuildSubdirTestCase(unittest.TestCase):
52+
53+
def create_basic_args(self, generator, variant, assertions):
54+
return argparse.Namespace(
55+
cmake_generator=generator,
56+
cmark_build_variant=variant,
57+
llvm_build_variant=variant,
58+
swift_build_variant=variant,
59+
swift_stdlib_build_variant=variant,
60+
swift_analyze_code_coverage="false",
61+
cmark_assertions=assertions,
62+
llvm_assertions=assertions,
63+
swift_assertions=assertions,
64+
swift_stdlib_assertions=assertions)
65+
66+
def test_Ninja_ReleaseAssert(self):
67+
# build-script -R
68+
args = self.create_basic_args(
69+
"Ninja", variant="Release", assertions=True)
70+
self.assertEqual(compute_build_subdir(args),
71+
"Ninja-ReleaseAssert")
72+
73+
def test_Ninja_Release(self):
74+
# build-script -R --no-assertions
75+
args = self.create_basic_args(
76+
"Ninja", variant="Release", assertions=False)
77+
self.assertEqual(compute_build_subdir(args),
78+
"Ninja-Release")
79+
80+
def test_Ninja_Release_stdlib_ReleaseAssert(self):
81+
# build-script -R --no-assertions --swift-stdlib-assertions
82+
args = self.create_basic_args(
83+
"Ninja", variant="Release", assertions=False)
84+
args.swift_stdlib_assertions = True
85+
self.assertEqual(compute_build_subdir(args),
86+
"Ninja-Release+stdlib-ReleaseAssert")
87+
88+
def test_Ninja_mixed(self):
89+
# build-script -R --no-assertions
90+
# --llvm-build-variant=RelWithDebInfo
91+
# --swift-analyze-code-coverage="merged"
92+
# --swift-stdlib-assertions
93+
args = self.create_basic_args(
94+
"Ninja", variant="Release", assertions=False)
95+
args.llvm_build_variant = "RelWithDebInfo"
96+
args.swift_analyze_code_coverage = "merged"
97+
args.swift_stdlib_assertions = True
98+
self.assertEqual(compute_build_subdir(args),
99+
"Ninja+cmark-Release+llvm-RelWithDebInfo"
100+
"+swift-ReleaseCoverage+stdlib-ReleaseAssert")
101+
102+
def test_Unix_Makefiles_ReleaseAssert(self):
103+
# build-script -R -m
104+
args = self.create_basic_args(
105+
"Unix Makefiles", variant="Release", assertions=True)
106+
self.assertEqual(compute_build_subdir(args),
107+
"Unix_Makefiles-ReleaseAssert")
108+
109+
def test_all_combinations_are_unique(self):
110+
productions = itertools.product(
111+
["Release", "Debug"], # cmark_build_variant
112+
["Release", "Debug"], # llvm_build_variant
113+
["Release", "Debug"], # swift_build_variant
114+
["Release", "Debug"], # swift_stdlib_build_variant
115+
["false", "true"], # swift_analyze_code_coverage
116+
[True, False], # cmark_assertions
117+
[True, False], # llvm_assertions
118+
[True, False], # swift_assertions
119+
[True, False], # swift_stdlib_assertions
120+
)
121+
keys = [
122+
"cmark_build_variant",
123+
"llvm_build_variant",
124+
"swift_build_variant",
125+
"swift_stdlib_build_variant",
126+
"swift_analyze_code_coverage",
127+
"cmark_assertions",
128+
"llvm_assertions",
129+
"swift_assertions",
130+
"swift_stdlib_assertions",
131+
]
132+
133+
def generate():
134+
for c in productions:
135+
args = argparse.Namespace(cmake_generator="Ninja")
136+
for key, val in zip(keys, c):
137+
setattr(args, key, val)
138+
yield compute_build_subdir(args)
139+
140+
seen = set()
141+
for line in generate():
142+
self.assertIsInstance(line, str)
143+
self.assertNotIn(line, seen)
144+
seen.add(line)
145+
self.assertEqual(len(seen), 1 << 9) # Iterated all productions.

0 commit comments

Comments
 (0)