OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include <dlfcn.h> | 8 #include <dlfcn.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 | 10 |
11 void usage(const char* argv0) { | 11 void usage() { |
12 printf("[USAGE] %s program_name [options]\n", argv0); | 12 printf("[USAGE] skia_launcher program_name [options]\n"); |
13 printf(" program_name: the skia program you want to launch (e.g. tests, ben
ch)\n"); | 13 printf(" program_name: the skia program you want to launch (e.g. tests, ben
ch)\n"); |
14 printf(" options: options specific to the program you are launching\n"); | 14 printf(" options: options specific to the program you are launching\n"); |
15 } | 15 } |
16 | 16 |
17 bool file_exists(const char* fileName) { | 17 bool file_exists(const char* fileName) { |
18 FILE* file = fopen(fileName, "r"); | 18 FILE* file = fopen(fileName, "r"); |
19 if (file) { | 19 if (file) { |
20 fclose(file); | 20 fclose(file); |
21 return true; | 21 return true; |
22 } | 22 } |
23 return false; | 23 return false; |
24 } | 24 } |
25 | 25 |
26 int launch_app(int (*app_main)(int, const char**), int argc, | 26 int launch_app(int (*app_main)(int, const char**), int argc, |
27 const char** argv) { | 27 const char** argv) { |
28 return (*app_main)(argc, argv); | 28 return (*app_main)(argc, argv); |
29 } | 29 } |
30 | 30 |
31 void* load_library(const char** argv, const char* libraryName) | 31 void* load_library(const char* appLocation, const char* libraryName) |
32 { | 32 { |
33 // attempt to lookup the location of the shared libraries | 33 // attempt to lookup the location of the shared libraries |
34 char libraryLocation[100]; | 34 char libraryLocation[100]; |
35 sprintf(libraryLocation, "%s/lib/lib%s.so", argv[0], libraryName); | 35 sprintf(libraryLocation, "%s/lib/lib%s.so", appLocation, libraryName); |
36 if (!file_exists(libraryLocation)) { | 36 if (!file_exists(libraryLocation)) { |
37 printf("ERROR: Unable to find the appropriate library in the Skia App.\n
"); | 37 printf("ERROR: Unable to find the appropriate library in the Skia App.\n
"); |
38 printf("ERROR: Did you provide the correct program_name?\n"); | 38 printf("ERROR: Did you provide the correct program_name?\n"); |
39 usage(argv[0]); | 39 usage(); |
40 return NULL; | 40 return NULL; |
41 } | 41 } |
42 | 42 |
43 // load the appropriate library | 43 // load the appropriate library |
44 void* appLibrary = dlopen(libraryLocation, RTLD_LOCAL | RTLD_LAZY); | 44 void* appLibrary = dlopen(libraryLocation, RTLD_LOCAL | RTLD_LAZY); |
45 if (!appLibrary) { | 45 if (!appLibrary) { |
46 printf("ERROR: Unable to open the shared library.\n"); | 46 printf("ERROR: Unable to open the shared library.\n"); |
47 printf("ERROR: %s", dlerror()); | 47 printf("ERROR: %s", dlerror()); |
48 return NULL; | 48 return NULL; |
49 } | 49 } |
50 | 50 |
51 return appLibrary; | 51 return appLibrary; |
52 } | 52 } |
53 | 53 |
54 int main(int argc, const char** argv) { | 54 int main(int argc, const char** argv) { |
55 | 55 |
56 // check that the program name was specified | 56 // check that the program name was specified |
57 if (argc < 2) { | 57 if (argc < 2) { |
58 printf("ERROR: No program_name was specified\n"); | 58 printf("ERROR: No program_name was specified\n"); |
59 usage(argv[0]); | 59 usage(); |
60 return -1; | 60 return -1; |
61 } | 61 } |
62 | 62 |
63 // attempt to lookup the location of the skia app | 63 // attempt to lookup the location of the skia app |
64 const char* appLocation = "/data/data/com.skia"; | 64 const char* appLocation = "/data/data/com.skia"; |
65 if (!file_exists(appLocation)) { | 65 if (!file_exists(appLocation)) { |
66 printf("ERROR: Unable to find the com.skia app on the device.\n"); | 66 printf("ERROR: Unable to find the com.skia app on the device.\n"); |
67 return -1; | 67 return -1; |
68 } | 68 } |
69 | 69 |
70 // load the local skia shared library | 70 // load the local skia shared library |
71 void* skiaLibrary = load_library(argv, "libskia_android.so"); | 71 void* skiaLibrary = load_library(appLocation, "libskia_android.so"); |
72 if (NULL == skiaLibrary) | 72 if (NULL == skiaLibrary) |
73 { | 73 { |
74 return -1; | 74 return -1; |
75 } | 75 } |
76 | 76 |
77 // load the appropriate library | 77 // load the appropriate library |
78 void* appLibrary = load_library(argv, argv[1]); | 78 void* appLibrary = load_library(appLocation, argv[1]); |
79 if (NULL == appLibrary) { | 79 if (NULL == appLibrary) { |
80 return -1; | 80 return -1; |
81 } | 81 } |
82 | 82 |
83 // find the address of the main function | 83 // find the address of the main function |
84 int (*app_main)(int, const char**); | 84 int (*app_main)(int, const char**); |
85 *(void **) (&app_main) = dlsym(appLibrary, "main"); | 85 *(void **) (&app_main) = dlsym(appLibrary, "main"); |
86 | 86 |
87 if (!app_main) { | 87 if (!app_main) { |
88 printf("ERROR: Unable to load the main function of the selected program.
\n"); | 88 printf("ERROR: Unable to load the main function of the selected program.
\n"); |
89 printf("ERROR: %s\n", dlerror()); | 89 printf("ERROR: %s\n", dlerror()); |
90 return -1; | 90 return -1; |
91 } | 91 } |
92 | 92 |
93 // find the address of the SkPrintToConsole function | 93 // find the address of the SkPrintToConsole function |
94 void (*app_SkDebugToStdOut)(bool); | 94 void (*app_SkDebugToStdOut)(bool); |
95 *(void **) (&app_SkDebugToStdOut) = dlsym(appLibrary, "AndroidSkDebugToStdOu
t"); | 95 *(void **) (&app_SkDebugToStdOut) = dlsym(appLibrary, "AndroidSkDebugToStdOu
t"); |
96 | 96 |
97 if (app_SkDebugToStdOut) { | 97 if (app_SkDebugToStdOut) { |
98 (*app_SkDebugToStdOut)(true); | 98 (*app_SkDebugToStdOut)(true); |
99 } else { | 99 } else { |
100 printf("WARNING: Unable to redirect output to the console.\n"); | 100 printf("WARNING: Unable to redirect output to the console.\n"); |
101 printf("WARNING: %s\n", dlerror()); | 101 printf("WARNING: %s\n", dlerror()); |
102 } | 102 } |
103 | 103 |
104 // pass all additional arguments to the main function | 104 // pass all additional arguments to the main function |
105 return launch_app(app_main, argc - 1, ++argv); | 105 return launch_app(app_main, argc - 1, ++argv); |
106 } | 106 } |
OLD | NEW |