Skip to content

Commit 268d83c

Browse files
committed
[Build-Script-Helper] Build and install TSC as a shared library instead of static
It is shared by SwiftDriver and SwiftOptions and building it as a static lib causes them to have common symbols. This does mean that we have to isntall TSC libraries into the toolchain as well
1 parent d8e0dfb commit 268d83c

File tree

1 file changed

+65
-11
lines changed

1 file changed

+65
-11
lines changed

Utilities/build-script-helper.py

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ def delete_rpath(rpath, binary, verbose):
116116
if verbose:
117117
print(stdout)
118118

119+
def add_rpath(rpath, binary, verbose):
120+
cmd = ['install_name_tool', '-add_rpath', rpath, binary]
121+
if verbose:
122+
print(' '.join(cmd))
123+
installToolProcess = subprocess.Popen(cmd,
124+
stdout=subprocess.PIPE,
125+
stderr=subprocess.PIPE)
126+
stdout, stderr = installToolProcess.communicate()
127+
if installToolProcess.returncode != 0:
128+
print('install_name_tool command failed: ')
129+
print(stderr)
130+
if verbose:
131+
print(stdout)
132+
119133
def should_test_parallel():
120134
if platform.system() == 'Linux':
121135
distro = platform.linux_distribution()
@@ -209,18 +223,23 @@ def install_executables(args, build_dir, universal_bin_dir, toolchain_bin_dir):
209223
# Fixup rpaths
210224
for arch in macos_target_architectures:
211225
exe_bin_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
212-
'swift-driver', 'bin', exe)
213-
help_bin_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
214-
'swift-driver', 'bin', 'swift-help')
226+
'swift-driver', 'bin', exe)
215227
driver_lib_dir_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
216228
'swift-driver', 'lib')
229+
tsc_lib_dir_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
230+
'swift-tools-support-core', 'lib')
217231
delete_rpath(driver_lib_dir_path, exe_bin_path, args.verbose)
218-
# Only swift-driver requires libllbuild
232+
delete_rpath(tsc_lib_dir_path, exe_bin_path, args.verbose)
233+
234+
# Only swift-driver relies libllbuild
219235
if exe == 'swift-driver':
220236
llbuild_lib_dir_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
221237
'llbuild', 'lib')
222238
delete_rpath(llbuild_lib_dir_path, exe_bin_path, args.verbose)
223239

240+
# Point to the installed toolchain's lib
241+
add_rpath('@executable_path/../lib/swift/macosx', exe_bin_path, args.verbose)
242+
224243
# Merge the multiple architecture binaries into a universal binary and install
225244
output_bin_path = os.path.join(universal_bin_dir, exe)
226245
lipo_cmd = ['lipo']
@@ -234,7 +253,7 @@ def install_executables(args, build_dir, universal_bin_dir, toolchain_bin_dir):
234253
install_binary(exe, universal_bin_dir, toolchain_bin_dir, args.verbose)
235254

236255
def install_libraries(args, build_dir, universal_lib_dir, toolchain_lib_dir):
237-
# Fixup the rpath for libSwiftDriver (libSwiftOptions does not link against these libraries)
256+
# Fixup the llbuild and swift-driver rpath for libSwiftDriver
238257
for arch in macos_target_architectures:
239258
lib_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
240259
'swift-driver', 'lib', 'libSwiftDriver' + shared_lib_ext)
@@ -245,18 +264,54 @@ def install_libraries(args, build_dir, universal_lib_dir, toolchain_lib_dir):
245264
delete_rpath(driver_lib_dir_path, lib_path, args.verbose)
246265
delete_rpath(llbuild_lib_dir_path, lib_path, args.verbose)
247266

267+
toolchain_relative_llbuild_rpath = os.path.join()
268+
add_rpath('@executable_path/../lib/swift/macosx', exe_bin_path, args.verbose)
269+
270+
# Fixup the TSC rpath for libSwiftDriver and libSwiftOptions
271+
for lib in ['libSwiftDriver', 'libSwiftOptions']:
272+
for arch in macos_target_architectures:
273+
lib_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
274+
'swift-driver', 'lib', lib + shared_lib_ext)
275+
tsc_lib_dir_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
276+
'swift-tools-support-core', 'lib')
277+
delete_rpath(tsc_lib_dir_path, lib_path, args.verbose)
278+
248279
# Install the libSwiftDriver and libSwiftOptions shared libraries into the toolchain lib
249280
for lib in ['libSwiftDriver', 'libSwiftOptions']:
250-
dylib_file = lib + shared_lib_ext
251-
output_dylib_path = os.path.join(universal_lib_dir, dylib_file)
281+
shared_lib_file = lib + shared_lib_ext
282+
output_dylib_path = os.path.join(universal_lib_dir, shared_lib_file)
252283
lipo_cmd = ['lipo']
253284
for arch in macos_target_architectures:
254285
input_lib_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
255-
'swift-driver', 'lib', dylib_file)
286+
'swift-driver', 'lib', shared_lib_file)
256287
lipo_cmd.append(input_lib_path)
257288
lipo_cmd.extend(['-create', '-output', output_dylib_path])
258289
subprocess.check_call(lipo_cmd)
259-
install_binary(dylib_file, universal_lib_dir, toolchain_lib_dir, args.verbose)
290+
install_binary(shared_lib_file, universal_lib_dir, toolchain_lib_dir, args.verbose)
291+
292+
for lib in ['libTSCBasic', 'libTSCLibc', 'libTSCUtility']:
293+
shared_lib_file = lib + shared_lib_ext
294+
output_dylib_path = os.path.join(universal_lib_dir, shared_lib_file)
295+
lipo_cmd = ['lipo']
296+
for arch in macos_target_architectures:
297+
input_lib_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
298+
'swift-tools-support-core', 'lib', shared_lib_file)
299+
lipo_cmd.append(input_lib_path)
300+
lipo_cmd.extend(['-create', '-output', output_dylib_path])
301+
subprocess.check_call(lipo_cmd)
302+
install_binary(shared_lib_file, universal_lib_dir, toolchain_lib_dir, args.verbose)
303+
304+
def install_library(args, lib_name, ):
305+
shared_lib_file = lib + shared_lib_ext
306+
output_dylib_path = os.path.join(universal_lib_dir, shared_lib_file)
307+
lipo_cmd = ['lipo']
308+
for arch in macos_target_architectures:
309+
input_lib_path = os.path.join(build_dir, arch + '-apple-macos' + macos_deployment_target,
310+
'swift-driver', 'lib', shared_lib_file)
311+
lipo_cmd.append(input_lib_path)
312+
lipo_cmd.extend(['-create', '-output', output_dylib_path])
313+
subprocess.check_call(lipo_cmd)
314+
install_binary(shared_lib_file, universal_lib_dir, toolchain_lib_dir, args.verbose)
260315

261316
def install_binary_swift_modules(args, build_dir, toolchain_lib_dir):
262317
# The common subpath from a project's build directory to where its build products are found
@@ -363,8 +418,7 @@ def build_tsc_using_cmake(args, target, swiftc_exec, cmake_target_dir, base_cmak
363418
print('Building TSC for target: %s' % target)
364419
tsc_source_dir = os.path.join(os.path.dirname(args.package_path), 'swift-tools-support-core')
365420
tsc_build_dir = os.path.join(cmake_target_dir, 'swift-tools-support-core')
366-
tsc_flags = base_cmake_flags + ['-DBUILD_SHARED_LIBS=OFF']
367-
cmake_build(args, swiftc_exec, tsc_flags, tsc_source_dir, tsc_build_dir)
421+
cmake_build(args, swiftc_exec, base_cmake_flags, tsc_source_dir, tsc_build_dir)
368422

369423
def build_yams_using_cmake(args, target, swiftc_exec, cmake_target_dir, base_cmake_flags):
370424
print('Building Yams for target: %s' % target)

0 commit comments

Comments
 (0)