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