Skip to content

Commit f9299e7

Browse files
authored
[test] rth: Use dynamic linking to properly test backwards-deployment (#14447)
The added test worked before as well; it's to make sure I didn't break the existing behavior.
1 parent 5880e7d commit f9299e7

File tree

4 files changed

+103
-28
lines changed

4 files changed

+103
-28
lines changed

test/lit.cfg

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,14 +1006,15 @@ config.target_sil_nm= (
10061006
'%s -target %s %s'
10071007
% (config.sil_nm, config.variant_triple, mcp_opt))
10081008

1009-
config.target_resilience_test_wmo = (
1010-
('%s --target-build-swift "%s" --target-run "%s" --t %%t --S %%S --s %%s '
1011-
'--additional-compile-flags-library "-whole-module-optimization -parse-as-library"')
1012-
% (config.rth, config.target_build_swift, config.target_run))
1013-
10141009
config.target_resilience_test = (
1015-
'%s --target-build-swift "%s" --target-run "%s" --t %%t --S %%S --s %%s'
1016-
% (config.rth, config.target_build_swift, config.target_run))
1010+
'%s --target-build-swift "%s" --target-run "%s" --t %%t --S %%S --s %%s '
1011+
'--lib-prefix "%s" --lib-suffix ".%s" --target-codesign "%s"'
1012+
% (config.rth, config.target_build_swift, config.target_run, 'lib',
1013+
config.target_dylib_extension, config.target_codesign))
1014+
1015+
config.target_resilience_test_wmo = (
1016+
('%s --additional-compile-flags-library "-whole-module-optimization -parse-as-library"')
1017+
% (config.target_resilience_test))
10171018

10181019
#
10191020
# When changing substitutions, update docs/Testing.rst.

utils/rth

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@ def verbose_print_command(command):
3030

3131
class ResilienceTest(object):
3232

33-
def __init__(self, target_build_swift, target_run, tmp_dir, test_dir,
34-
test_src, additional_compile_flags_library,
33+
def __init__(self, target_build_swift, target_run, target_codesign,
34+
tmp_dir, test_dir, test_src, lib_prefix, lib_suffix,
35+
additional_compile_flags_library,
3536
no_backward_deployment):
3637
self.target_build_swift = shlex.split(target_build_swift)
3738
self.target_run = shlex.split(target_run)
39+
self.target_codesign = shlex.split(target_codesign)
3840
self.tmp_dir = tmp_dir
3941
self.test_dir = test_dir
4042
self.test_src = test_src
43+
self.lib_prefix = lib_prefix
44+
self.lib_suffix = lib_suffix
4145
self.additional_compile_flags_library = \
4246
shlex.split(additional_compile_flags_library)
4347

@@ -47,7 +51,7 @@ class ResilienceTest(object):
4751
'AFTER': self.after_dir}
4852

4953
self.lib_src_name = os.path.basename(self.test_src)[5:]
50-
self.lib_obj_name = self.lib_src_name[:-6] + '.o'
54+
self.lib_name = self.lib_src_name[:-6]
5155
self.lib_src = os.path.join(self.test_dir, 'Inputs', self.lib_src_name)
5256

5357
self.no_backward_deployment = no_backward_deployment
@@ -65,22 +69,36 @@ class ResilienceTest(object):
6569
os.makedirs(self.after_dir)
6670
os.makedirs(self.before_dir)
6771

72+
def is_apple_platform(self):
73+
return any('-apple-' in arg for arg in self.target_build_swift)
74+
6875
def compile_library(self):
6976
for config in self.config_dir_map:
70-
for emit_flag in ['-emit-library', '-emit-module']:
71-
output_obj = os.path.join(self.config_dir_map[config],
72-
self.lib_obj_name)
73-
compiler_flags = [emit_flag,
74-
'-swift-version', '4',
75-
'-Xfrontend', '-enable-resilience',
76-
'-D', config,
77-
'-c', self.lib_src,
78-
'-o', output_obj]
79-
command = self.target_build_swift + \
80-
self.additional_compile_flags_library + compiler_flags
81-
verbose_print_command(command)
82-
returncode = subprocess.call(command)
83-
assert returncode == 0, str(command)
77+
lib_file = self.lib_prefix + self.lib_name + self.lib_suffix
78+
output_dylib = os.path.join(self.config_dir_map[config],
79+
lib_file)
80+
compiler_flags = ['-emit-library', '-emit-module',
81+
'-swift-version', '4',
82+
'-Xfrontend', '-enable-resilience',
83+
'-D', config,
84+
self.lib_src,
85+
'-o', output_dylib]
86+
if self.is_apple_platform():
87+
compiler_flags += ['-Xlinker',
88+
'-install_name',
89+
'-Xlinker',
90+
os.path.join('@rpath', lib_file)]
91+
92+
command = self.target_build_swift + \
93+
self.additional_compile_flags_library + compiler_flags
94+
verbose_print_command(command)
95+
returncode = subprocess.call(command)
96+
assert returncode == 0, str(command)
97+
98+
codesign_cmd = self.target_codesign + [output_dylib]
99+
verbose_print_command(codesign_cmd)
100+
returncode = subprocess.call(codesign_cmd)
101+
assert returncode == 0, str(codesign_cmd)
84102

85103
def compile_main(self):
86104
for config in self.config_dir_map:
@@ -110,13 +128,23 @@ class ResilienceTest(object):
110128
config2_lower = config2.lower()
111129
output_obj = os.path.join(self.tmp_dir,
112130
config1_lower + '_' + config2_lower)
131+
if self.is_apple_platform():
132+
rpath_origin = '@executable_path'
133+
else:
134+
rpath_origin = '$ORIGIN'
135+
113136
compiler_flags = [
114-
os.path.join(self.config_dir_map[config1],
115-
self.lib_obj_name),
137+
'-L', self.config_dir_map[config2],
138+
'-l' + self.lib_name,
116139
os.path.join(self.config_dir_map[config2],
117140
'main.o'),
141+
'-Xlinker', '-rpath', '-Xlinker',
142+
os.path.join(rpath_origin,
143+
os.path.relpath(self.config_dir_map[config1],
144+
self.tmp_dir)),
118145
'-o', output_obj
119146
]
147+
120148
command = self.target_build_swift + compiler_flags
121149
verbose_print_command(command)
122150
returncode = subprocess.call(command)
@@ -128,7 +156,7 @@ class ResilienceTest(object):
128156
config2_lower = config2.lower()
129157
output_obj = os.path.join(self.tmp_dir,
130158
config1_lower + '_' + config2_lower)
131-
command = self.target_run + [output_obj]
159+
command = self.target_run + [output_obj, self.tmp_dir]
132160
verbose_print_command(command)
133161
returncode = subprocess.call(command)
134162
assert returncode == 0, str(command)
@@ -138,17 +166,22 @@ def main():
138166
parser = argparse.ArgumentParser(description='Resilience test helper')
139167
parser.add_argument('--target-build-swift', required=True)
140168
parser.add_argument('--target-run', required=True)
169+
parser.add_argument('--target-codesign', default='echo')
141170
parser.add_argument('--t', required=True)
142171
parser.add_argument('--S', required=True)
143172
parser.add_argument('--s', required=True)
173+
parser.add_argument('--lib-prefix', required=True)
174+
parser.add_argument('--lib-suffix', required=True)
144175
parser.add_argument('--additional-compile-flags-library', default='')
145176
parser.add_argument('--no-backward-deployment', default=False,
146177
action='store_true')
147178

148179
args = parser.parse_args()
149180

150181
resilience_test = ResilienceTest(args.target_build_swift, args.target_run,
151-
args.t, args.S, args.s,
182+
args.target_codesign,
183+
args.t, args.S, args.s, args.lib_prefix,
184+
args.lib_suffix,
152185
args.additional_compile_flags_library,
153186
args.no_backward_deployment)
154187

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#if BEFORE
2+
public var libIsAfter = false
3+
#else
4+
public var libIsAfter = true
5+
#endif
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %target-resilience-test
2+
// REQUIRES: executable_test
3+
4+
// Check for the 'rth' tool itself, to make sure it's doing what we expect.
5+
6+
import rth
7+
8+
#if BEFORE
9+
let clientIsAfter = false
10+
#else
11+
let clientIsAfter = true
12+
#endif
13+
14+
let execPath = CommandLine.arguments.first!
15+
// FIXME: Don't hardcode "/" here.
16+
let execName = execPath.split(separator: "/").last!
17+
switch execName {
18+
case "after_after":
19+
precondition(clientIsAfter)
20+
precondition(libIsAfter)
21+
22+
case "before_after":
23+
precondition(clientIsAfter)
24+
precondition(!libIsAfter)
25+
26+
case "before_before":
27+
precondition(!clientIsAfter)
28+
precondition(!libIsAfter)
29+
30+
case "after_before":
31+
precondition(!clientIsAfter)
32+
precondition(libIsAfter)
33+
34+
default:
35+
fatalError("unknown exec name \(execName)")
36+
}

0 commit comments

Comments
 (0)