Skip to content

Commit

Permalink
Add cosmopolitan cosmo_dl*() support
Browse files Browse the repository at this point in the history
It seems to mostly work to load SDL2 at runtime. There is still some
weirdness with anything higher than -O0, though.
  • Loading branch information
vkoskiv committed Nov 30, 2023
1 parent b3468a2 commit b8ea678
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
28 changes: 20 additions & 8 deletions src/utils/platform/dyn.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
#ifdef WINDOWS
#include <windows.h>
#include <libloaderapi.h>
#if defined(WINDOWS)
#include <windows.h>
#include <libloaderapi.h>
#elif defined(__COSMOPOLITAN__)
#define _COSMO_SOURCE
#include "libc/dlopen/dlfcn.h"
#else
#include <dlfcn.h>
#include <dlfcn.h>
#endif

#include "dyn.h"

void *dyn_load(const char *filename) {
#ifdef WINDOWS
#if defined(WINDOWS)
return (void *)LoadLibraryA(filename);
#elif defined(__COSMOPOLITAN__)
return cosmo_dlopen(filename, RTLD_LAZY);
#else
return dlopen(filename, RTLD_LAZY);
#endif
}

void *dyn_sym(void *handle, const char *name) {
#ifdef WINDOWS
#if defined(WINDOWS)
return (void *)GetProcAddress((HMODULE)handle, name);
#elif defined(__COSMOPOLITAN__)
return cosmo_dlsym(handle, name);
#else
return dlsym(handle, name);
#endif
Expand All @@ -25,15 +33,19 @@ void *dyn_sym(void *handle, const char *name) {
char *dyn_error(void) {
#if defined(WINDOWS)
return NULL;
#elif defined(__COSMOPOLITAN__)
return cosmo_dlerror();
#else
return dlerror();
#endif
}

int dyn_close(void *handle) {
#ifdef WINDOWS
#if defined(WINDOWS)
return (int)FreeLibrary((HMODULE)handle);
#elif defined(__COSMOPOLITAN__)
return cosmo_dlclose(handle);
#else
return dlclose(handle);
#endif
}
}
2 changes: 1 addition & 1 deletion src/utils/platform/dyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ void *dyn_sym(void *handle, const char *name);
char *dyn_error(void);
int dyn_close(void *handle);

#endif
#endif

0 comments on commit b8ea678

Please sign in to comment.