Skip to content

Commit cedbd82

Browse files
committed
Group functions in build-script-helper.py
1 parent bbb3abc commit cedbd82

File tree

1 file changed

+96
-86
lines changed

1 file changed

+96
-86
lines changed

build-script-helper.py

Lines changed: 96 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,92 @@
1919
import os, platform
2020
import subprocess
2121

22-
def printerr(message):
22+
# -----------------------------------------------------------------------------
23+
# General utilities
24+
25+
def printerr(message: str):
2326
print(message, file=sys.stderr)
2427

25-
def main(argv_prefix = []):
26-
args = parse_args(argv_prefix + sys.argv[1:])
27-
run(args)
28+
def check_call(cmd, verbose, env=os.environ, **kwargs):
29+
if verbose:
30+
print(' '.join([escape_cmd_arg(arg) for arg in cmd]))
31+
return subprocess.check_call(cmd, env=env, stderr=subprocess.STDOUT, **kwargs)
32+
33+
def check_output(cmd, verbose, env=os.environ, capture_stderr=True, **kwargs):
34+
if verbose:
35+
print(' '.join([escape_cmd_arg(arg) for arg in cmd]))
36+
if capture_stderr:
37+
stderr = subprocess.STDOUT
38+
else:
39+
stderr = subprocess.DEVNULL
40+
return subprocess.check_output(cmd, env=env, stderr=stderr, encoding='utf-8', **kwargs)
41+
42+
def escape_cmd_arg(arg):
43+
if '"' in arg or ' ' in arg:
44+
return '"%s"' % arg.replace('"', '\\"')
45+
else:
46+
return arg
47+
48+
# -----------------------------------------------------------------------------
49+
# SwiftPM wrappers
50+
51+
def update_swiftpm_dependencies(package_path, swift_exec, build_path, env, verbose):
52+
args = [swift_exec, 'package', '--package-path', package_path, '--scratch-path', build_path, 'update']
53+
check_call(args, env=env, verbose=verbose)
54+
55+
def invoke_swift(package_path, swift_exec, action, products, build_path, multiroot_data_file, configuration, env, verbose):
56+
# Until rdar://53881101 is implemented, we cannot request a build of multiple
57+
# targets simultaneously. For now, just build one product after the other.
58+
for product in products:
59+
invoke_swift_single_product(package_path, swift_exec, action, product, build_path, multiroot_data_file, configuration, env, verbose)
60+
61+
def get_swiftpm_options(package_path, build_path, multiroot_data_file, configuration, verbose):
62+
args = [
63+
'--package-path', package_path,
64+
'--configuration', configuration,
65+
'--scratch-path', build_path
66+
]
67+
if multiroot_data_file:
68+
args += ['--multiroot-data-file', multiroot_data_file]
69+
if verbose:
70+
args += ['--verbose']
71+
if platform.system() == 'Darwin':
72+
args += [
73+
'-Xlinker', '-rpath', '-Xlinker', '/usr/lib/swift',
74+
'-Xlinker', '-rpath', '-Xlinker', '@executable_path/../lib/swift/macosx',
75+
'-Xlinker', '-rpath', '-Xlinker', '@executable_path/../lib/swift-5.5/macosx',
76+
]
77+
return args
78+
79+
def get_swiftpm_environment_variables(no_local_deps):
80+
env = dict(os.environ)
81+
if not no_local_deps:
82+
env['SWIFTCI_USE_LOCAL_DEPS'] = "1"
83+
return env
84+
85+
86+
def invoke_swift_single_product(package_path, swift_exec, action, product, build_path, multiroot_data_file, configuration, env, verbose):
87+
args = [swift_exec, action]
88+
args += get_swiftpm_options(package_path, build_path, multiroot_data_file, configuration, verbose)
89+
if action == 'test':
90+
args += [
91+
'--test-product', product,
92+
'--disable-testable-imports'
93+
]
94+
else:
95+
args += ['--product', product]
96+
97+
check_call(args, env=env, verbose=verbose)
98+
99+
def generate_xcodeproj(package_path, swift_exec, env, verbose):
100+
package_name = os.path.basename(package_path)
101+
xcodeproj_path = os.path.join(package_path, '%s.xcodeproj' % package_name)
102+
args = [swift_exec, 'package', '--package-path', package_path, 'generate-xcodeproj', '--output', xcodeproj_path]
103+
check_call(args, env=env, verbose=verbose)
104+
105+
# -----------------------------------------------------------------------------
106+
# Argument parsing
107+
28108

29109
def parse_args(args):
30110
parser = argparse.ArgumentParser(prog='build-script-helper.py')
@@ -54,6 +134,14 @@ def parse_args(args):
54134

55135
return parsed
56136

137+
def should_run_action(action_name, selected_actions):
138+
if action_name in selected_actions:
139+
return True
140+
elif "all" in selected_actions:
141+
return True
142+
else:
143+
return False
144+
57145
def run(args):
58146
package_name = os.path.basename(args.package_path)
59147

@@ -139,87 +227,9 @@ def run(args):
139227
cmd = ['rsync', '-a', os.path.join(bin_path, 'swift-format'), os.path.join(prefix, 'bin')]
140228
check_call(cmd, verbose=args.verbose)
141229

142-
def should_run_action(action_name, selected_actions):
143-
if action_name in selected_actions:
144-
return True
145-
elif "all" in selected_actions:
146-
return True
147-
else:
148-
return False
149-
150-
def update_swiftpm_dependencies(package_path, swift_exec, build_path, env, verbose):
151-
args = [swift_exec, 'package', '--package-path', package_path, '--scratch-path', build_path, 'update']
152-
check_call(args, env=env, verbose=verbose)
153-
154-
def invoke_swift(package_path, swift_exec, action, products, build_path, multiroot_data_file, configuration, env, verbose):
155-
# Until rdar://53881101 is implemented, we cannot request a build of multiple
156-
# targets simultaneously. For now, just build one product after the other.
157-
for product in products:
158-
invoke_swift_single_product(package_path, swift_exec, action, product, build_path, multiroot_data_file, configuration, env, verbose)
159-
160-
def get_swiftpm_options(package_path, build_path, multiroot_data_file, configuration, verbose):
161-
args = [
162-
'--package-path', package_path,
163-
'--configuration', configuration,
164-
'--scratch-path', build_path
165-
]
166-
if multiroot_data_file:
167-
args += ['--multiroot-data-file', multiroot_data_file]
168-
if verbose:
169-
args += ['--verbose']
170-
if platform.system() == 'Darwin':
171-
args += [
172-
'-Xlinker', '-rpath', '-Xlinker', '/usr/lib/swift',
173-
'-Xlinker', '-rpath', '-Xlinker', '@executable_path/../lib/swift/macosx',
174-
'-Xlinker', '-rpath', '-Xlinker', '@executable_path/../lib/swift-5.5/macosx',
175-
]
176-
return args
177-
178-
def get_swiftpm_environment_variables(no_local_deps):
179-
env = dict(os.environ)
180-
if not no_local_deps:
181-
env['SWIFTCI_USE_LOCAL_DEPS'] = "1"
182-
return env
183-
184-
185-
def invoke_swift_single_product(package_path, swift_exec, action, product, build_path, multiroot_data_file, configuration, env, verbose):
186-
args = [swift_exec, action]
187-
args += get_swiftpm_options(package_path, build_path, multiroot_data_file, configuration, verbose)
188-
if action == 'test':
189-
args += [
190-
'--test-product', product,
191-
'--disable-testable-imports'
192-
]
193-
else:
194-
args += ['--product', product]
195-
196-
check_call(args, env=env, verbose=verbose)
197-
198-
def generate_xcodeproj(package_path, swift_exec, env, verbose):
199-
package_name = os.path.basename(package_path)
200-
xcodeproj_path = os.path.join(package_path, '%s.xcodeproj' % package_name)
201-
args = [swift_exec, 'package', '--package-path', package_path, 'generate-xcodeproj', '--output', xcodeproj_path]
202-
check_call(args, env=env, verbose=verbose)
203-
204-
def check_call(cmd, verbose, env=os.environ, **kwargs):
205-
if verbose:
206-
print(' '.join([escape_cmd_arg(arg) for arg in cmd]))
207-
return subprocess.check_call(cmd, env=env, stderr=subprocess.STDOUT, **kwargs)
208-
209-
def check_output(cmd, verbose, env=os.environ, capture_stderr=True, **kwargs):
210-
if verbose:
211-
print(' '.join([escape_cmd_arg(arg) for arg in cmd]))
212-
if capture_stderr:
213-
stderr = subprocess.STDOUT
214-
else:
215-
stderr = subprocess.DEVNULL
216-
return subprocess.check_output(cmd, env=env, stderr=stderr, encoding='utf-8', **kwargs)
217-
218-
def escape_cmd_arg(arg):
219-
if '"' in arg or ' ' in arg:
220-
return '"%s"' % arg.replace('"', '\\"')
221-
else:
222-
return arg
230+
def main(argv_prefix = []):
231+
args = parse_args(argv_prefix + sys.argv[1:])
232+
run(args)
223233

224234
if __name__ == '__main__':
225-
main()
235+
main()

0 commit comments

Comments
 (0)