8
8
import shutil
9
9
import subprocess
10
10
import sys
11
- from typing import Dict, List, Optional
11
+ from typing import Dict, List
12
12
13
13
14
14
# -----------------------------------------------------------------------------
@@ -27,23 +27,41 @@ def escapeCmdArg(arg: str) -> str:
27
27
return arg
28
28
29
29
30
- def check_call(cmd: List[str], env: Optional[Dict[str, str]], cwd: Optional[str] = None, verbose: bool = False):
30
+ def print_cmd(cmd: List[str], additional_env: Dict[str, str]) -> None:
31
+ env_str = " ".join([f"{key}={escapeCmdArg(str(value))}" for (key, value) in additional_env.items()])
32
+ command_str = " ".join([escapeCmdArg(str(arg)) for arg in cmd])
33
+ print(f"{env_str} {command_str}")
34
+
35
+
36
+ def env_with_additional_env(additional_env: Dict[str, str]) -> Dict[str, str]:
37
+ env = dict(os.environ)
38
+ for (key, value) in additional_env.items():
39
+ env[key] = str(value)
40
+ return env
41
+
42
+
43
+ def check_call(cmd: List[str], additional_env: Dict[str, str] = {}, verbose: bool = False) -> None:
31
44
if verbose:
32
- print(" ".join([escapeCmdArg(arg) for arg in cmd]))
33
- return subprocess.check_call(cmd, cwd=cwd, env=env, stderr=subprocess.STDOUT)
45
+ print_cmd(cmd=cmd, additional_env=additional_env)
46
+
47
+ subprocess.check_call(cmd, env=env_with_additional_env(additional_env), stderr=subprocess.STDOUT)
48
+
49
+
50
+ def check_output(cmd: List[str], additional_env: Dict[str, str] = {}, verbose: bool = False) -> str:
51
+ if verbose:
52
+ print_cmd(cmd=cmd, additional_env=additional_env)
53
+ return subprocess.check_output(cmd, env=env_with_additional_env(additional_env), stderr=subprocess.STDOUT, encoding='utf-8')
34
54
35
55
# -----------------------------------------------------------------------------
36
56
# SwiftPM wrappers
37
57
38
58
39
- def swiftpm_bin_path(swift_exec: str, swiftpm_args: List[str], env: Optional[ Dict[str, str] ], verbose: bool = False) -> str:
59
+ def swiftpm_bin_path(swift_exec: str, swiftpm_args: List[str], additional_env: Dict[str, str], verbose: bool = False) -> str:
40
60
"""
41
61
Return the path of the directory that contains the binaries produced by this package.
42
62
"""
43
63
cmd = [swift_exec, 'build', '--show-bin-path'] + swiftpm_args
44
- if verbose:
45
- print(" ".join([escapeCmdArg(arg) for arg in cmd]))
46
- return subprocess.check_output(cmd, env=env, universal_newlines=True).strip()
64
+ return check_output(cmd, additional_env=additional_env, verbose=verbose).strip()
47
65
48
66
49
67
def get_build_target(swift_exec: str, args: argparse.Namespace) -> str:
@@ -132,10 +150,12 @@ def get_swiftpm_environment_variables(swift_exec: str, args: argparse.Namespace)
132
150
'swift test' invocation.
133
151
"""
134
152
135
- env = dict(os.environ)
136
- # Set the toolchain used in tests at runtime
137
- env['SOURCEKIT_TOOLCHAIN_PATH'] = args.toolchain
138
- env['INDEXSTOREDB_TOOLCHAIN_BIN_PATH'] = args.toolchain
153
+ env = {
154
+ # Set the toolchain used in tests at runtime
155
+ 'SOURCEKIT_TOOLCHAIN_PATH': args.toolchain,
156
+ 'INDEXSTOREDB_TOOLCHAIN_BIN_PATH': args.toolchain,
157
+ 'SWIFT_EXEC': f'{swift_exec}c'
158
+ }
139
159
# Use local dependencies (i.e. checked out next sourcekit-lsp).
140
160
if not args.no_local_deps:
141
161
env['SWIFTCI_USE_LOCAL_DEPS'] = "1"
@@ -155,8 +175,6 @@ def get_swiftpm_environment_variables(swift_exec: str, args: argparse.Namespace)
155
175
if args.action == 'test' and not args.skip_long_tests:
156
176
env['SOURCEKIT_LSP_ENABLE_LONG_TESTS'] = '1'
157
177
158
- env['SWIFT_EXEC'] = '%sc' % (swift_exec)
159
-
160
178
return env
161
179
162
180
@@ -165,19 +183,19 @@ def build_single_product(product: str, swift_exec: str, args: argparse.Namespace
165
183
Build one product in the package
166
184
"""
167
185
swiftpm_args = get_swiftpm_options(swift_exec, args)
168
- env = get_swiftpm_environment_variables(swift_exec, args)
186
+ additional_env = get_swiftpm_environment_variables(swift_exec, args)
169
187
cmd = [swift_exec, 'build', '--product', product] + swiftpm_args
170
- check_call(cmd, env=env , verbose=args.verbose)
188
+ check_call(cmd, additional_env=additional_env , verbose=args.verbose)
171
189
172
190
173
191
def run_tests(swift_exec: str, args: argparse.Namespace) -> None:
174
192
"""
175
193
Run all tests in the package
176
194
"""
177
195
swiftpm_args = get_swiftpm_options(swift_exec, args)
178
- env = get_swiftpm_environment_variables(swift_exec, args)
196
+ additional_env = get_swiftpm_environment_variables(swift_exec, args)
179
197
180
- bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env )
198
+ bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, additional_env=additional_env )
181
199
tests = os.path.join(bin_path, 'sk-tests')
182
200
print('Cleaning ' + tests)
183
201
shutil.rmtree(tests, ignore_errors=True)
@@ -188,23 +206,23 @@ def run_tests(swift_exec: str, args: argparse.Namespace) -> None:
188
206
'--disable-testable-imports',
189
207
'--test-product', 'SourceKitLSPPackageTests'
190
208
] + swiftpm_args
191
- check_call(cmd, env=env , verbose=args.verbose)
209
+ check_call(cmd, additional_env=additional_env , verbose=args.verbose)
192
210
193
211
194
212
def install_binary(exe: str, source_dir: str, install_dir: str, verbose: bool) -> None:
195
213
cmd = ['rsync', '-a', os.path.join(source_dir, exe), install_dir]
196
- check_call(cmd, env=None, verbose=verbose)
214
+ check_call(cmd, verbose=verbose)
197
215
198
216
199
217
def install(swift_exec: str, args: argparse.Namespace) -> None:
200
218
swiftpm_args = get_swiftpm_options(swift_exec, args)
201
- env = get_swiftpm_environment_variables(swift_exec, args)
219
+ additional_env = get_swiftpm_environment_variables(swift_exec, args)
202
220
203
- bin_path = swiftpm_bin_path(swift_exec, swiftpm_args, env )
221
+ bin_path = swiftpm_bin_path(swift_exec, swiftpm_args=swiftpm_args, additional_env=additional_env )
204
222
swiftpm_args += ['-Xswiftc', '-no-toolchain-stdlib-rpath']
205
223
check_call([
206
224
swift_exec, 'build'
207
- ] + swiftpm_args, env=env )
225
+ ] + swiftpm_args, additional_env=additional_env )
208
226
209
227
if not args.install_prefixes:
210
228
args.install_prefixes = [args.toolchain]
0 commit comments