Skip to content

Commit 5eefa67

Browse files
committed
Print environment variables that were specified when executing commands
1 parent cdcc924 commit 5eefa67

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

Utilities/build-script-helper.py

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import shutil
99
import subprocess
1010
import sys
11-
from typing import Dict, List, Optional
11+
from typing import Dict, List
1212

1313

1414
# -----------------------------------------------------------------------------
@@ -27,23 +27,41 @@ def escapeCmdArg(arg: str) -> str:
2727
return arg
2828

2929

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:
3144
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')
3454

3555
# -----------------------------------------------------------------------------
3656
# SwiftPM wrappers
3757

3858

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:
4060
"""
4161
Return the path of the directory that contains the binaries produced by this package.
4262
"""
4363
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()
4765

4866

4967
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)
132150
'swift test' invocation.
133151
"""
134152

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+
}
139159
# Use local dependencies (i.e. checked out next sourcekit-lsp).
140160
if not args.no_local_deps:
141161
env['SWIFTCI_USE_LOCAL_DEPS'] = "1"
@@ -155,8 +175,6 @@ def get_swiftpm_environment_variables(swift_exec: str, args: argparse.Namespace)
155175
if args.action == 'test' and not args.skip_long_tests:
156176
env['SOURCEKIT_LSP_ENABLE_LONG_TESTS'] = '1'
157177

158-
env['SWIFT_EXEC'] = '%sc' % (swift_exec)
159-
160178
return env
161179

162180

@@ -165,19 +183,19 @@ def build_single_product(product: str, swift_exec: str, args: argparse.Namespace
165183
Build one product in the package
166184
"""
167185
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)
169187
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)
171189

172190

173191
def run_tests(swift_exec: str, args: argparse.Namespace) -> None:
174192
"""
175193
Run all tests in the package
176194
"""
177195
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)
179197

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)
181199
tests = os.path.join(bin_path, 'sk-tests')
182200
print('Cleaning ' + tests)
183201
shutil.rmtree(tests, ignore_errors=True)
@@ -188,23 +206,23 @@ def run_tests(swift_exec: str, args: argparse.Namespace) -> None:
188206
'--disable-testable-imports',
189207
'--test-product', 'SourceKitLSPPackageTests'
190208
] + swiftpm_args
191-
check_call(cmd, env=env, verbose=args.verbose)
209+
check_call(cmd, additional_env=additional_env, verbose=args.verbose)
192210

193211

194212
def install_binary(exe: str, source_dir: str, install_dir: str, verbose: bool) -> None:
195213
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)
197215

198216

199217
def install(swift_exec: str, args: argparse.Namespace) -> None:
200218
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)
202220

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)
204222
swiftpm_args += ['-Xswiftc', '-no-toolchain-stdlib-rpath']
205223
check_call([
206224
swift_exec, 'build'
207-
] + swiftpm_args, env=env)
225+
] + swiftpm_args, additional_env=additional_env)
208226

209227
if not args.install_prefixes:
210228
args.install_prefixes = [args.toolchain]

0 commit comments

Comments
 (0)