Skip to content

Commit 31bcaed

Browse files
authored
Merge pull request #198 from Rostepher/use-os-path
[NFC] Update build-script.py to use os.path to manipulate path string rather than raw string concatenation.
2 parents 1de6063 + 75db796 commit 31bcaed

File tree

1 file changed

+55
-37
lines changed

1 file changed

+55
-37
lines changed

build-script.py

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,22 @@
1515
# Constants
1616

1717
PACKAGE_DIR = os.path.dirname(os.path.realpath(__file__))
18-
WORKSPACE_DIR = os.path.realpath(PACKAGE_DIR + '/..')
18+
WORKSPACE_DIR = os.path.dirname(PACKAGE_DIR)
1919

20-
INCR_TRANSFER_ROUNDTRIP_EXEC = \
21-
WORKSPACE_DIR + '/swift/utils/incrparse/incr_transfer_round_trip.py'
20+
LLVM_DIR = os.path.join(WORKSPACE_DIR, 'llvm-project', 'llvm')
21+
SWIFT_DIR = os.path.join(WORKSPACE_DIR, 'swift')
2222

23-
GYB_EXEC = os.path.join(WORKSPACE_DIR, 'swift', 'utils', 'gyb')
23+
INCR_TRANSFER_ROUNDTRIP_EXEC = os.path.join(
24+
SWIFT_DIR, 'utils', 'incrparse', 'incr_transfer_round_trip.py')
2425

25-
LIT_EXEC = WORKSPACE_DIR + '/llvm-project/llvm/utils/lit/lit.py'
26+
GYB_EXEC = os.path.join(
27+
SWIFT_DIR, 'utils', 'gyb')
2628

27-
GROUP_INFO_PATH = PACKAGE_DIR + '/utils/group.json'
29+
LIT_EXEC = os.path.join(
30+
LLVM_DIR, 'utils', 'lit', 'lit.py')
31+
32+
GROUP_INFO_PATH = os.path.join(
33+
PACKAGE_DIR, 'utils', 'group.json')
2834

2935
BASE_KIND_FILES = {
3036
'Decl': 'SyntaxDeclNodes.swift',
@@ -126,20 +132,24 @@ def generate_single_gyb_file(gyb_exec, gyb_file, output_file_name, destination,
126132
[] if add_source_locations else ['--line-directive='])
127133

128134
# Generate the new file
129-
check_call([gyb_exec] +
130-
[gyb_file] +
131-
['-o', temp_files_dir + '/' + output_file_name] +
132-
line_directive_flags +
133-
additional_gyb_flags,
134-
verbose=verbose)
135+
gyb_command = [
136+
gyb_exec, gyb_file,
137+
'-o', os.path.join(temp_files_dir, output_file_name),
138+
]
139+
gyb_command += line_directive_flags
140+
gyb_command += additional_gyb_flags
141+
142+
check_call(gyb_command, verbose=verbose)
135143

136144
# Copy the file if different from the file already present in
137145
# gyb_generated
138-
check_call(['rsync'] +
139-
['--checksum'] +
140-
[temp_files_dir + '/' + output_file_name] +
141-
[destination + '/' + output_file_name],
142-
verbose=verbose)
146+
rsync_command = [
147+
'rsync', '--checksum',
148+
os.path.join(temp_files_dir, output_file_name),
149+
os.path.join(destination, output_file_name),
150+
]
151+
152+
check_call(rsync_command, verbose=verbose)
143153

144154

145155
def generate_gyb_files(gyb_exec, verbose, add_source_locations,
@@ -149,8 +159,8 @@ def generate_gyb_files(gyb_exec, verbose, add_source_locations,
149159
check_gyb_exec(gyb_exec)
150160
check_rsync()
151161

152-
swiftsyntax_sources_dir = os.path.join(PACKAGE_DIR, 'Sources',
153-
'SwiftSyntax')
162+
swiftsyntax_sources_dir = os.path.join(
163+
PACKAGE_DIR, 'Sources', 'SwiftSyntax')
154164
temp_files_dir = tempfile.gettempdir()
155165

156166
if destination is None:
@@ -189,11 +199,13 @@ def generate_gyb_files(gyb_exec, verbose, add_source_locations,
189199
if not gyb_file.endswith('.gyb'):
190200
continue
191201

202+
gyb_file_path = os.path.join(swiftsyntax_sources_dir, gyb_file)
203+
192204
# Slice off the '.gyb' to get the name for the output file
193205
output_file_name = gyb_file[:-4]
194206

195207
generate_single_gyb_file(gyb_exec,
196-
swiftsyntax_sources_dir + '/' + gyb_file,
208+
gyb_file_path,
197209
output_file_name, destination,
198210
temp_files_dir, add_source_locations,
199211
additional_gyb_flags=[],
@@ -202,7 +214,8 @@ def generate_gyb_files(gyb_exec, verbose, add_source_locations,
202214
for base_kind in BASE_KIND_FILES:
203215
output_file_name = BASE_KIND_FILES[base_kind]
204216

205-
gyb_file = swiftsyntax_sources_dir + '/SyntaxNodes.swift.gyb.template'
217+
gyb_file = os.path.join(
218+
swiftsyntax_sources_dir, 'SyntaxNodes.swift.gyb.template')
206219

207220
generate_single_gyb_file(gyb_exec, gyb_file, output_file_name,
208221
template_destination, temp_files_dir,
@@ -271,8 +284,9 @@ def build(self, product_name, module_group_path=''):
271284
# Testing
272285

273286
def verify_generated_files(gyb_exec, verbose):
274-
user_generated_dir = os.path.join(PACKAGE_DIR, 'Sources', 'SwiftSyntax',
275-
'gyb_generated')
287+
user_generated_dir = os.path.join(
288+
PACKAGE_DIR, 'Sources', 'SwiftSyntax', 'gyb_generated')
289+
276290
self_generated_dir = tempfile.mkdtemp()
277291
generate_gyb_files(gyb_exec, verbose=verbose,
278292
add_source_locations=False,
@@ -346,7 +360,7 @@ def find_lit_test_helper_exec(toolchain, build_dir, release):
346360
swiftpm_call.extend(['--show-bin-path'])
347361

348362
bin_dir = subprocess.check_output(swiftpm_call, stderr=subprocess.STDOUT)
349-
return bin_dir.strip() + '/lit-test-helper'
363+
return os.path.join(bin_dir.strip(), 'lit-test-helper')
350364

351365

352366
def run_lit_tests(toolchain, build_dir, release, filecheck_exec, verbose):
@@ -361,7 +375,7 @@ def run_lit_tests(toolchain, build_dir, release, filecheck_exec, verbose):
361375
release=release)
362376

363377
lit_call = [LIT_EXEC]
364-
lit_call.extend([PACKAGE_DIR + '/lit_tests'])
378+
lit_call.append(os.path.join(PACKAGE_DIR, 'lit_tests'))
365379

366380
if filecheck_exec:
367381
lit_call.extend(['--param', 'FILECHECK=' + filecheck_exec])
@@ -431,22 +445,26 @@ def check_and_sync(file_path, install_path):
431445

432446

433447
def install(build_dir, dylib_dir, swiftmodule_base_name, stdlib_rpath):
434-
dylibPath = build_dir + '/libSwiftSyntax.dylib'
435-
modulePath = build_dir + '/SwiftSyntax.swiftmodule'
436-
docPath = build_dir + '/SwiftSyntax.swiftdoc'
448+
dylib_name = get_installed_dylib_name()
449+
450+
dylib_path = os.path.join(build_dir, dylib_name)
451+
module_path = os.path.join(build_dir, 'SwiftSyntax.swiftmodule')
452+
doc_path = os.path.join(build_dir, 'SwiftSyntax.swiftdoc')
453+
437454
# users should find the dylib as if it's a part of stdlib.
438-
change_id_rpath('@rpath/' + get_installed_dylib_name(), dylibPath)
455+
change_id_rpath(os.path.join('@rpath', dylib_name), dylib_path)
456+
439457
# we don't wanna hard-code the stdlib dylibs into rpath.
440-
delete_rpath(stdlib_rpath, dylibPath)
441-
check_and_sync(file_path=dylibPath,
442-
install_path=dylib_dir + '/' + get_installed_dylib_name())
458+
delete_rpath(stdlib_rpath, dylib_path)
459+
check_and_sync(file_path=dylib_path,
460+
install_path=os.path.join(dylib_dir, dylib_name))
461+
443462
# Optionally install .swiftmodule
444463
if swiftmodule_base_name:
445464
module_dest = swiftmodule_base_name + '.swiftmodule'
446465
doc_dest = swiftmodule_base_name + '.swiftdoc'
447-
check_and_sync(file_path=modulePath, install_path=module_dest)
448-
check_and_sync(file_path=docPath, install_path=doc_dest)
449-
return
466+
check_and_sync(file_path=module_path, install_path=module_dest)
467+
check_and_sync(file_path=doc_path, install_path=doc_dest)
450468

451469

452470
# -----------------------------------------------------------------------------
@@ -606,10 +624,10 @@ def main():
606624
if not args.build_dir:
607625
fatal_error('Must specify build directory to copy from')
608626
if args.release:
609-
build_dir = args.build_dir + '/release'
627+
build_dir = os.path.join(args.build_dir, 'release')
610628
else:
611629
# will this ever happen?
612-
build_dir = args.build_dir + '/debug'
630+
build_dir = os.path.join(args.build_dir, 'debug')
613631
stdlib_rpath = os.path.join(
614632
args.toolchain, 'usr', 'lib', 'swift', 'macosx')
615633
install(build_dir=build_dir, dylib_dir=args.dylib_dir,

0 commit comments

Comments
 (0)