]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/env-DYLD_IMAGE_SUFFIX.dtest/main.c
dyld-732.8.tar.gz
[apple/dyld.git] / testing / test-cases / env-DYLD_IMAGE_SUFFIX.dtest / main.c
1
2 // BUILD: mkdir -p $BUILD_DIR/Bar.framework
3 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/libfoo.dylib -install_name $RUN_DIR/libfoo.dylib -DVALUE=1
4 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/libfoo_other.dylib -install_name $RUN_DIR/libfoo.dylib -DVALUE=42
5 // BUILD: $CC bar.c -dynamiclib -o $BUILD_DIR/Bar.framework/Bar -install_name $RUN_DIR/Bar.framework/Bar -DVALUE=1
6 // BUILD: $CC bar.c -dynamiclib -o $BUILD_DIR/Bar.framework/Bar_alt -install_name $RUN_DIR/Bar.framework/Bar -DVALUE=42
7 // BUILD: $CC main.c -o $BUILD_DIR/env-DYLD_IMAGE_SUFFIX.exe $BUILD_DIR/libfoo.dylib $BUILD_DIR/Bar.framework/Bar
8 // BUILD: $DYLD_ENV_VARS_ENABLE $BUILD_DIR/env-DYLD_IMAGE_SUFFIX.exe
9 // BUILD: $CC main.c -o $BUILD_DIR/env-DYLD_IMAGE_SUFFIX-dynamic.exe -DRUN_DIR="$RUN_DIR"
10 // BUILD: $DYLD_ENV_VARS_ENABLE $BUILD_DIR/env-DYLD_IMAGE_SUFFIX-dynamic.exe
11
12 // RUN: ./env-DYLD_IMAGE_SUFFIX.exe
13 // RUN: DYLD_IMAGE_SUFFIX=_other ./env-DYLD_IMAGE_SUFFIX.exe
14 // RUN: DYLD_IMAGE_SUFFIX=_alt ./env-DYLD_IMAGE_SUFFIX.exe
15 // RUN: DYLD_IMAGE_SUFFIX=_alt:_other ./env-DYLD_IMAGE_SUFFIX.exe
16 // RUN: ./env-DYLD_IMAGE_SUFFIX-dynamic.exe
17 // RUN: DYLD_IMAGE_SUFFIX=_other ./env-DYLD_IMAGE_SUFFIX-dynamic.exe
18 // RUN: DYLD_IMAGE_SUFFIX=_alt ./env-DYLD_IMAGE_SUFFIX-dynamic.exe
19 // RUN: DYLD_IMAGE_SUFFIX=_alt:_other ./env-DYLD_IMAGE_SUFFIX-dynamic.exe
20
21
22
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <dlfcn.h>
28
29 extern int foo();
30 extern int bar();
31
32 typedef int (*IntProc)();
33
34 int main()
35 {
36 const char* suffix = getenv("DYLD_IMAGE_SUFFIX");
37 if ( suffix == NULL )
38 suffix = "";
39 printf("[BEGIN] env-DYLD_IMAGE_SUFFIX-%s\n", suffix);
40
41 const int expectedFoo = (strstr(suffix, "_other") != NULL) ? 42 : 1;
42 const int expectedBar = (strstr(suffix, "_alt") != NULL) ? 42 : 1;;
43
44 #ifdef RUN_DIR
45 void* fooHandle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_LAZY);
46 if ( fooHandle == NULL ) {
47 printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, libfoo.dylib could not be loaded, %s\n", suffix, dlerror());
48 return 0;
49 }
50 void* barHandle = dlopen(RUN_DIR "/Bar.framework/Bar", RTLD_LAZY);
51 if ( barHandle == NULL ) {
52 printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, Bar.framework/Bar could not be loaded, %s\n", suffix, dlerror());
53 return 0;
54 }
55 IntProc fooProc = (IntProc)dlsym(fooHandle, "foo");
56 if ( fooProc == NULL ) {
57 printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, symbol 'foo' not found %s\n", suffix, dlerror());
58 return 0;
59 }
60 IntProc barProc = (IntProc)dlsym(barHandle, "bar");
61 if ( barProc == NULL ) {
62 printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, symbol 'bar' not found %s\n", suffix, dlerror());
63 return 0;
64 }
65 int fooValue = (*fooProc)();
66 int barValue = (*barProc)();
67 #else
68 int fooValue = foo();
69 int barValue = bar();
70 #endif
71 if ( fooValue != expectedFoo )
72 printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, foo()=%d expected=%d\n", suffix, fooValue, expectedFoo);
73 else if ( barValue != expectedBar )
74 printf("[FAIL] env-DYLD_IMAGE_SUFFIX-%s, bar()=%d expected=%d\n", suffix, barValue, expectedBar);
75 else
76 printf("[PASS] env-DYLD_IMAGE_SUFFIX-%s\n", suffix);
77
78 return 0;
79 }
80