media/webrtc/trunk/tools/gyp/test/module/src/program.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 #include <stdio.h>
michael@0 2 #include <stdlib.h>
michael@0 3
michael@0 4 #if defined(PLATFORM_WIN)
michael@0 5 #include <windows.h>
michael@0 6 #elif defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
michael@0 7 #include <dlfcn.h>
michael@0 8 #include <libgen.h>
michael@0 9 #include <string.h>
michael@0 10 #include <sys/param.h>
michael@0 11 #define MAX_PATH PATH_MAX
michael@0 12 #endif
michael@0 13
michael@0 14 #if defined(PLATFORM_WIN)
michael@0 15 #define MODULE_SUFFIX ".dll"
michael@0 16 #elif defined(PLATFORM_MAC)
michael@0 17 #define MODULE_SUFFIX ".so"
michael@0 18 #elif defined(PLATFORM_LINUX)
michael@0 19 #define MODULE_SUFFIX ".so"
michael@0 20 #endif
michael@0 21
michael@0 22 typedef void (*module_symbol)(void);
michael@0 23 char bin_path[MAX_PATH + 1];
michael@0 24
michael@0 25
michael@0 26 void CallModule(const char* module) {
michael@0 27 char module_path[MAX_PATH + 1];
michael@0 28 const char* module_function = "module_main";
michael@0 29 module_symbol funcptr;
michael@0 30 #if defined(PLATFORM_WIN)
michael@0 31 HMODULE dl;
michael@0 32 char drive[_MAX_DRIVE];
michael@0 33 char dir[_MAX_DIR];
michael@0 34
michael@0 35 if (_splitpath_s(bin_path, drive, _MAX_DRIVE, dir, _MAX_DIR,
michael@0 36 NULL, 0, NULL, 0)) {
michael@0 37 fprintf(stderr, "Failed to split executable path.\n");
michael@0 38 return;
michael@0 39 }
michael@0 40 if (_makepath_s(module_path, MAX_PATH, drive, dir, module, MODULE_SUFFIX)) {
michael@0 41 fprintf(stderr, "Failed to calculate module path.\n");
michael@0 42 return;
michael@0 43 }
michael@0 44
michael@0 45 dl = LoadLibrary(module_path);
michael@0 46 if (!dl) {
michael@0 47 fprintf(stderr, "Failed to open module: %s\n", module_path);
michael@0 48 return;
michael@0 49 }
michael@0 50
michael@0 51 funcptr = (module_symbol) GetProcAddress(dl, module_function);
michael@0 52 if (!funcptr) {
michael@0 53 fprintf(stderr, "Failed to find symbol: %s\n", module_function);
michael@0 54 return;
michael@0 55 }
michael@0 56 funcptr();
michael@0 57
michael@0 58 FreeLibrary(dl);
michael@0 59 #elif defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
michael@0 60 void* dl;
michael@0 61 char* path_copy = strdup(bin_path);
michael@0 62 char* bin_dir = dirname(path_copy);
michael@0 63 int path_size = snprintf(module_path, MAX_PATH, "%s/%s%s", bin_dir, module,
michael@0 64 MODULE_SUFFIX);
michael@0 65 free(path_copy);
michael@0 66 if (path_size < 0 || path_size > MAX_PATH) {
michael@0 67 fprintf(stderr, "Failed to calculate module path.\n");
michael@0 68 return;
michael@0 69 }
michael@0 70 module_path[path_size] = 0;
michael@0 71
michael@0 72 dl = dlopen(module_path, RTLD_LAZY);
michael@0 73 if (!dl) {
michael@0 74 fprintf(stderr, "Failed to open module: %s\n", module_path);
michael@0 75 return;
michael@0 76 }
michael@0 77
michael@0 78 funcptr = dlsym(dl, module_function);
michael@0 79 if (!funcptr) {
michael@0 80 fprintf(stderr, "Failed to find symbol: %s\n", module_function);
michael@0 81 return;
michael@0 82 }
michael@0 83 funcptr();
michael@0 84
michael@0 85 dlclose(dl);
michael@0 86 #endif
michael@0 87 }
michael@0 88
michael@0 89 int main(int argc, char *argv[])
michael@0 90 {
michael@0 91 fprintf(stdout, "Hello from program.c\n");
michael@0 92 fflush(stdout);
michael@0 93
michael@0 94 #if defined(PLATFORM_WIN)
michael@0 95 if (!GetModuleFileName(NULL, bin_path, MAX_PATH)) {
michael@0 96 fprintf(stderr, "Failed to determine executable path.\n");
michael@0 97 return;
michael@0 98 }
michael@0 99 #elif defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
michael@0 100 // Using argv[0] should be OK here since we control how the tests run, and
michael@0 101 // can avoid exec and such issues that make it unreliable.
michael@0 102 if (!realpath(argv[0], bin_path)) {
michael@0 103 fprintf(stderr, "Failed to determine executable path (%s).\n", argv[0]);
michael@0 104 return;
michael@0 105 }
michael@0 106 #endif
michael@0 107
michael@0 108 CallModule("lib1");
michael@0 109 CallModule("lib2");
michael@0 110 return 0;
michael@0 111 }

mercurial