Skip to content

Commit c864719

Browse files
authored
Merge pull request #154 from ahoppen/pr/unified-build
Several changes in build-script-helper.py to support building indexstore-db in a unified build
2 parents 7a214e3 + 34e4b09 commit c864719

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

.flake8

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
3+
ignore =
4+
E501,

Utilities/build-script-helper.py

+47-23
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import shutil
77
import subprocess
88
import sys
9-
from typing import Dict, List, Optional
9+
from typing import Dict, List
1010

1111
# -----------------------------------------------------------------------------
1212
# General utilities
@@ -24,23 +24,45 @@ def escapeCmdArg(arg: str) -> str:
2424
return arg
2525

2626

27-
def check_call(cmd: List[str], env: Optional[Dict[str, str]], cwd: Optional[str] = None, verbose: bool = False):
27+
def print_cmd(cmd: List[str], additional_env: Dict[str, str]) -> None:
28+
env_str = " ".join([f"{key}={escapeCmdArg(str(value))}" for (key, value) in additional_env.items()])
29+
command_str = " ".join([escapeCmdArg(str(arg)) for arg in cmd])
30+
print(f"{env_str} {command_str}")
31+
32+
33+
def env_with_additional_env(additional_env: Dict[str, str]) -> Dict[str, str]:
34+
env = dict(os.environ)
35+
for (key, value) in additional_env.items():
36+
env[key] = str(value)
37+
return env
38+
39+
40+
def check_call(cmd: List[str], additional_env: Dict[str, str] = {}, verbose: bool = False) -> None:
41+
if verbose:
42+
print_cmd(cmd=cmd, additional_env=additional_env)
43+
44+
subprocess.check_call(cmd, env=env_with_additional_env(additional_env), stderr=subprocess.STDOUT)
45+
46+
47+
def check_output(cmd: List[str], additional_env: Dict[str, str] = {}, capture_stderr: bool = True, verbose: bool = False) -> str:
2848
if verbose:
29-
print(" ".join([escapeCmdArg(arg) for arg in cmd]))
30-
return subprocess.check_call(cmd, cwd=cwd, env=env, stderr=subprocess.STDOUT)
49+
print_cmd(cmd=cmd, additional_env=additional_env)
50+
if capture_stderr:
51+
stderr = subprocess.STDOUT
52+
else:
53+
stderr = subprocess.DEVNULL
54+
return subprocess.check_output(cmd, env=env_with_additional_env(additional_env), stderr=stderr, encoding='utf-8')
3155

3256
# -----------------------------------------------------------------------------
3357
# SwiftPM wrappers
3458

3559

36-
def swiftpm_bin_path(swift_exec: str, swiftpm_args: List[str], env: Optional[Dict[str, str]], verbose: bool = False) -> str:
60+
def swiftpm_bin_path(swift_exec: str, swiftpm_args: List[str], additional_env: Dict[str, str], verbose: bool = False) -> str:
3761
"""
3862
Return the path of the directory that contains the binaries produced by this package.
3963
"""
4064
cmd = [swift_exec, 'build', '--show-bin-path'] + swiftpm_args
41-
if verbose:
42-
print(" ".join([escapeCmdArg(arg) for arg in cmd]))
43-
return subprocess.check_output(cmd, env=env, universal_newlines=True).strip()
65+
return check_output(cmd, additional_env=additional_env, capture_stderr=False, verbose=verbose).strip()
4466

4567
# -----------------------------------------------------------------------------
4668
# Build indexstore-db
@@ -52,10 +74,13 @@ def get_swiftpm_options(args: argparse.Namespace) -> List[str]:
5274
"""
5375
swiftpm_args = [
5476
'--package-path', args.package_path,
55-
'--build-path', args.build_path,
77+
'--scratch-path', args.build_path,
5678
'--configuration', args.configuration,
5779
]
5880

81+
if args.multiroot_data_file:
82+
swiftpm_args += ['--multiroot-data-file', args.multiroot_data_file]
83+
5984
if args.verbose:
6085
swiftpm_args += ['--verbose']
6186

@@ -82,9 +107,11 @@ def get_swiftpm_environment_variables(args: argparse.Namespace) -> Dict[str, str
82107
'swift test' invocation.
83108
"""
84109

85-
env = dict(os.environ)
86-
# Set the toolchain used in tests at runtime
87-
env['INDEXSTOREDB_TOOLCHAIN_BIN_PATH'] = args.toolchain
110+
env = {
111+
# Set the toolchain used in tests at runtime
112+
'INDEXSTOREDB_TOOLCHAIN_BIN_PATH': args.toolchain,
113+
'SWIFTCI_USE_LOCAL_DEPS': '1',
114+
}
88115

89116
if args.ninja_bin:
90117
env['NINJA_BIN'] = args.ninja_bin
@@ -106,36 +133,32 @@ def build(swift_exec: str, args: argparse.Namespace) -> None:
106133
Build one product in the package
107134
"""
108135
swiftpm_args = get_swiftpm_options(args)
109-
env = get_swiftpm_environment_variables(args)
110-
cmd = [swift_exec, 'build'] + swiftpm_args
111-
check_call(cmd, env=env, verbose=args.verbose)
136+
additional_env = get_swiftpm_environment_variables(args)
137+
cmd = [swift_exec, 'build', '--product', 'IndexStoreDBPackageTests'] + swiftpm_args
138+
check_call(cmd, additional_env=additional_env, verbose=args.verbose)
112139

113140

114141
def run_tests(swift_exec: str, args: argparse.Namespace) -> None:
115142
"""
116143
Run all tests in the indexstore-db package
117144
"""
118145
swiftpm_args = get_swiftpm_options(args)
119-
env = get_swiftpm_environment_variables(args)
146+
additional_env = get_swiftpm_environment_variables(args)
120147

121-
bin_path = swiftpm_bin_path(swift_exec=swift_exec, swiftpm_args=swiftpm_args, env=env, verbose=args.verbose)
148+
bin_path = swiftpm_bin_path(swift_exec=swift_exec, swiftpm_args=swiftpm_args, additional_env=additional_env, verbose=args.verbose)
122149
tests = os.path.join(bin_path, 'isdb-tests')
123150
print('Cleaning ' + tests)
124151
shutil.rmtree(tests, ignore_errors=True)
125152

126153
cmd = [swift_exec, 'test', '--parallel', '--test-product', 'IndexStoreDBPackageTests'] + swiftpm_args
127-
check_call(cmd, env=env, verbose=args.verbose)
154+
check_call(cmd, additional_env=additional_env, verbose=args.verbose)
128155

129156

130157
def handle_invocation(swift_exec: str, args: argparse.Namespace) -> None:
131158
"""
132159
Depending on the action in 'args', build the package or run tests.
133160
"""
134161
if args.action == 'build':
135-
# Workaround for incremental build bug in swiftpm.
136-
print('Cleaning ' + args.build_path)
137-
shutil.rmtree(args.build_path, ignore_errors=True)
138-
139162
build(swift_exec, args)
140163
elif args.action == 'test':
141164
run_tests(swift_exec, args)
@@ -156,7 +179,8 @@ def add_common_args(parser):
156179
parser.add_argument('--sanitize', action='append', help='build using the given sanitizer(s) (address|thread|undefined)')
157180
parser.add_argument('--sanitize-all', action='store_true', help='build using every available sanitizer in sub-directories of build path')
158181
parser.add_argument('--verbose', '-v', action='store_true', help='enable verbose output')
159-
182+
parser.add_argument('--multiroot-data-file', help='path to an Xcode workspace to create a unified build of all of Swift\'s SwiftPM projects')
183+
160184
parser = argparse.ArgumentParser(description='Build along with the Swift build-script.')
161185

162186
if sys.version_info >= (3, 7, 0):

0 commit comments

Comments
 (0)