Skip to content

Commit d39e069

Browse files
committed
[Tests] Fix RemoteMirror test to work when DYLD_LIBRARY_PATH is set.
Changed the RemoteMirror test to work around DYLD_LIBRARY_PATH when it's given an absolute path; when it's called from CI, we really want it to load the libswiftRemoteMirror.dylib that we just built, *not* the system one, which is what happens for some of the builders. rdar://82124292
1 parent 926a59c commit d39e069

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

test/RemoteMirror/Inputs/interop.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,42 @@
1919
#import <Foundation/Foundation.h>
2020
#import <mach/mach.h>
2121
#import <mach-o/dyld.h>
22+
#import <libgen.h>
23+
#import <stdlib.h>
24+
#import <string.h>
2225

2326
#import "SwiftRemoteMirrorLegacyInterop.h"
2427

2528

2629
void *Load(char *path) {
30+
const char *libpath = getenv("DYLD_LIBRARY_PATH");
31+
char *ourlibpath = NULL;
32+
const char *oldlibpath = NULL;
33+
34+
if (libpath && path[0] == '/') {
35+
// If DYLD_LIBRARY_PATH is set, and the path we've been given is absolute,
36+
// then prepend the directory part of the path we've been given to it.
37+
const char *libdir = dirname(path);
38+
size_t pathlen = strlen(libpath);
39+
size_t dirlen = strlen(libdir);
40+
ourlibpath = (char *)malloc(pathlen + dirlen + 2);
41+
memcpy(ourlibpath, libdir, dirlen);
42+
ourlibpath[dirlen] = ':';
43+
oldlibpath = ourlibpath + dirlen + 1;
44+
memcpy(ourlibpath + dirlen + 1, libpath, pathlen + 1);
45+
46+
setenv("DYLD_LIBRARY_PATH", ourlibpath, 1);
47+
}
48+
49+
// Actually open the dylib
2750
void *Handle = dlopen(path, RTLD_LOCAL);
51+
52+
if (ourlibpath) {
53+
// Reset DYLD_LIBRARY_PATH, if we changed it
54+
setenv("DYLD_LIBRARY_PATH", oldlibpath, 1);
55+
free(ourlibpath);
56+
}
57+
2858
if (Handle == NULL) {
2959
fprintf(stderr, "loading %s: %s\n", path, dlerror());
3060
exit(1);

0 commit comments

Comments
 (0)