blob: 746d470a3c2dcaad8d38bc0b1671d75daddc0e62 [file] [log] [blame]
djsollen@google.comdcdd57f2013-04-29 12:09:31 +00001/*
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.come69a54b2013-06-03 14:02:15 +000011void usage() {
12 printf("[USAGE] skia_launcher program_name [options]\n");
djsollen@google.comdcdd57f2013-04-29 12:09:31 +000013 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
17bool 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
26int launch_app(int (*app_main)(int, const char**), int argc,
27 const char** argv) {
28 return (*app_main)(argc, argv);
29}
30
djsollen@google.come69a54b2013-06-03 14:02:15 +000031void* load_library(const char* appLocation, const char* libraryName)
djsollen@google.com52f02972013-06-03 12:10:19 +000032{
33 // attempt to lookup the location of the shared libraries
34 char libraryLocation[100];
djsollen@google.comcc95b1a2013-08-12 12:30:04 +000035 sprintf(libraryLocation, "%s/lib%s.so", appLocation, libraryName);
djsollen@google.com52f02972013-06-03 12:10:19 +000036 if (!file_exists(libraryLocation)) {
djsollen@google.comecde97e2013-06-03 14:51:14 +000037 printf("ERROR: Unable to find the '%s' library in the Skia App.\n", libraryName);
djsollen@google.com52f02972013-06-03 12:10:19 +000038 printf("ERROR: Did you provide the correct program_name?\n");
djsollen@google.come69a54b2013-06-03 14:02:15 +000039 usage();
djsollen@google.com52f02972013-06-03 12:10:19 +000040 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.comdcdd57f2013-04-29 12:09:31 +000054int 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.come69a54b2013-06-03 14:02:15 +000059 usage();
djsollen@google.comdcdd57f2013-04-29 12:09:31 +000060 return -1;
61 }
62
63 // attempt to lookup the location of the skia app
djsollen@google.comcc95b1a2013-08-12 12:30:04 +000064 const char* appLocation = "/data/local/tmp";
djsollen@google.comdcdd57f2013-04-29 12:09:31 +000065 if (!file_exists(appLocation)) {
djsollen@google.comcc95b1a2013-08-12 12:30:04 +000066 printf("ERROR: Unable to find /data/local/tmp on the device.\n");
djsollen@google.comdcdd57f2013-04-29 12:09:31 +000067 return -1;
68 }
69
rmistry@google.comd6bab022013-12-02 13:50:38 +000070 void* skiaLibrary;
71
72#if defined(SKIA_DLL)
djsollen@google.com52f02972013-06-03 12:10:19 +000073 // load the local skia shared library
rmistry@google.comd6bab022013-12-02 13:50:38 +000074 skiaLibrary = load_library(appLocation, "skia_android");
djsollen@google.com52f02972013-06-03 12:10:19 +000075 if (NULL == skiaLibrary)
76 {
djsollen@google.comdcdd57f2013-04-29 12:09:31 +000077 return -1;
78 }
rmistry@google.comd6bab022013-12-02 13:50:38 +000079#endif
djsollen@google.comdcdd57f2013-04-29 12:09:31 +000080
81 // load the appropriate library
djsollen@google.come69a54b2013-06-03 14:02:15 +000082 void* appLibrary = load_library(appLocation, argv[1]);
djsollen@google.com52f02972013-06-03 12:10:19 +000083 if (NULL == appLibrary) {
djsollen@google.comdcdd57f2013-04-29 12:09:31 +000084 return -1;
85 }
86
rmistry@google.comd6bab022013-12-02 13:50:38 +000087#if !defined(SKIA_DLL)
88 skiaLibrary = appLibrary;
89#endif
90
djsollen@google.comdcdd57f2013-04-29 12:09:31 +000091 // 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.comecde97e2013-06-03 14:51:14 +0000103 *(void **) (&app_SkDebugToStdOut) = dlsym(skiaLibrary, "AndroidSkDebugToStdOut");
djsollen@google.comdcdd57f2013-04-29 12:09:31 +0000104
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}