4
#if defined(PLATFORM_WIN)
6
#elif defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
10
#include <sys/param.h>
11
#define MAX_PATH PATH_MAX
14
#if defined(PLATFORM_WIN)
15
#define MODULE_SUFFIX ".dll"
16
#elif defined(PLATFORM_MAC)
17
#define MODULE_SUFFIX ".dylib"
18
#elif defined(PLATFORM_LINUX)
19
#define MODULE_SUFFIX ".so"
22
typedef void (*module_symbol)(void);
23
char bin_path[MAX_PATH + 1];
26
void CallModule(const char* module) {
27
char module_path[MAX_PATH + 1];
28
const char* module_function = "module_main";
29
module_symbol funcptr;
30
#if defined(PLATFORM_WIN)
32
char drive[_MAX_DRIVE];
35
if (_splitpath_s(bin_path, drive, _MAX_DRIVE, dir, _MAX_DIR,
37
fprintf(stderr, "Failed to split executable path.\n");
40
if (_makepath_s(module_path, MAX_PATH, drive, dir, module, MODULE_SUFFIX)) {
41
fprintf(stderr, "Failed to calculate module path.\n");
45
dl = LoadLibrary(module_path);
47
fprintf(stderr, "Failed to open module: %s\n", module_path);
51
funcptr = (module_symbol) GetProcAddress(dl, module_function);
53
fprintf(stderr, "Failed to find symbol: %s\n", module_function);
59
#elif defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
61
char* path_copy = strdup(bin_path);
62
char* bin_dir = dirname(path_copy);
63
int path_size = snprintf(module_path, MAX_PATH, "%s/%s%s", bin_dir, module,
66
if (path_size < 0 || path_size > MAX_PATH) {
67
fprintf(stderr, "Failed to calculate module path.\n");
70
module_path[path_size] = 0;
72
dl = dlopen(module_path, RTLD_LAZY);
74
fprintf(stderr, "Failed to open module: %s\n", module_path);
78
funcptr = dlsym(dl, module_function);
80
fprintf(stderr, "Failed to find symbol: %s\n", module_function);
89
int main(int argc, char *argv[])
91
fprintf(stdout, "Hello from program.c\n");
94
#if defined(PLATFORM_WIN)
95
if (!GetModuleFileName(NULL, bin_path, MAX_PATH)) {
96
fprintf(stderr, "Failed to determine executable path.\n");
99
#elif defined(PLATFORM_MAC) || defined(PLATFORM_LINUX)
100
// Using argv[0] should be OK here since we control how the tests run, and
101
// can avoid exec and such issues that make it unreliable.
102
if (!realpath(argv[0], bin_path)) {
103
fprintf(stderr, "Failed to determine executable path (%s).\n", argv[0]);