Skip to content

Commit

Permalink
made library, examples and Makefile MINGW64 friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
gpakosz committed Jan 28, 2016
1 parent cc8a01b commit 783a033
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
26 changes: 19 additions & 7 deletions _gnu-make/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ ifeq ($(platform),)
override platform := mac
override architecture := $(__uname_m)
endif
ifeq ($(findstring mingw,$(__uname_s)),mingw)
override platform := windows
override architecture := $(if $(findstring MINGW32,$(MSYSTEM)),i686,$(if $(findstring MINGW64,$(MSYSTEM)),x86_64,))
ifeq ($(CC),cc)
override CC := gcc
endif
endif
endif
ifeq ($(architecture),)
override architecture := unknown-architecture
Expand All @@ -35,30 +42,35 @@ CXXFLAGS := -O2 -g -Wall -pedantic -Werror

ifeq ($(platform),linux)
override LDFLAGS := $(LDFLAGS) -ldl
CFLAGS +=-D_XOPEN_SOURCE=500
CFLAGS += -D_XOPEN_SOURCE=500 -fpic
CXXFLAGS += -fpic
endif
ifeq ($(platform),mac)
CFLAGS +=-D_DARWIN_C_SOURCE
CFLAGS += -D_DARWIN_C_SOURCE
endif

ifeq ($(platform),mac)
libsuffix := .dylib
else
endif
ifeq ($(platform),linux)
libsuffix := .so
endif
ifeq ($(platform),windows)
libsuffix := .dll
endif

.PHONY: build-executable
build: build-executable
build-executable: $(bindir)/executable $(bindir)/executable-cpp

$(bindir)/executable: $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/executable.c
mkdir -p $(@D)
$(CC) -o $@ -I $(srcdir) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -fpic $(filter-out %.h,$^)
$(CC) -o $@ -I $(srcdir) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(filter-out %.h,$^)
$(if $(postbuild),$(postbuild) $@)

$(bindir)/executable-cpp: $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/executable.c
mkdir -p $(@D)
$(CXX) -x c++ -o $@ -I $(srcdir) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -fpic $(filter-out %.h,$^)
$(CXX) -x c++ -o $@ -I $(srcdir) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) $(filter-out %.h,$^)
$(if $(postbuild),$(postbuild) $@)

.PHONY: build-library
Expand All @@ -67,12 +79,12 @@ build-library: $(bindir)/library$(libsuffix) $(bindir)/library-cpp$(libsuffix)

$(bindir)/library$(libsuffix): $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/library.c
mkdir -p $(@D)
$(CC) -shared -o $@ -I $(srcdir) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -fpic $(filter-out %.h,$^)
$(CC) -shared -o $@ -I $(srcdir) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(filter-out %.h,$^)
$(if $(postbuild),$(postbuild) $@)

$(bindir)/library-cpp$(libsuffix): $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/library.c
mkdir -p $(@D)
$(CXX) -x c++ -shared -o $@ -I $(srcdir) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -fpic $(filter-out %.h,$^)
$(CXX) -x c++ -shared -o $@ -I $(srcdir) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) $(filter-out %.h,$^)
$(if $(postbuild),$(postbuild) $@)

clean:
Expand Down
2 changes: 1 addition & 1 deletion example/library.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ static void unload()
#define WIN32_LEAN_AND_MEAN
#if defined(_MSC_VER)
#pragma warning(push, 3)
#include <windows.h>
#endif
#include <windows.h>

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
Expand Down
50 changes: 22 additions & 28 deletions src/whereami.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ extern "C" {
#define WAI_REALLOC(p, size) realloc(p, size)
#endif

#ifndef WAI_NOINLINE
#if defined(_MSC_VER)
#define WAI_NOINLINE __declspec(noinline)
#elif defined(__GNUC__)
#define WAI_NOINLINE __attribute__((noinline))
#else
#error unsupported compiler
#endif
#endif

#if defined(_MSC_VER)
#define WAI_RETURN_ADDRESS() _ReturnAddress()
#elif defined(__GNUC__)
#define WAI_RETURN_ADDRESS() __builtin_extract_return_addr(__builtin_return_address(0))
#else
#error unsupported compiler
#endif

#if defined(_WIN32)

#define WIN32_LEAN_AND_MEAN
Expand All @@ -38,12 +56,6 @@ extern "C" {
#pragma warning(pop)
#endif

#ifndef WAI_NOINLINE
#ifdef _MSC_VER
#define WAI_NOINLINE __declspec(noinline)
#endif
#endif

static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, int* dirname_length)
{
wchar_t buffer1[MAX_PATH];
Expand Down Expand Up @@ -124,7 +136,7 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
#pragma warning(push)
#pragma warning(disable: 4054)
#endif
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)_ReturnAddress(), &module))
if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)WAI_RETURN_ADDRESS(), &module))
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
Expand All @@ -150,12 +162,6 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
#define WAI_PROC_SELF_EXE "/proc/self/exe"
#endif

#ifndef WAI_NOINLINE
#ifdef __GNUC__
#define WAI_NOINLINE __attribute__((noinline))
#endif
#endif

WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
{
Expand Down Expand Up @@ -237,7 +243,7 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)

if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %x:%x %u %s\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8)
{
uint64_t addr = (uint64_t)(uintptr_t) __builtin_extract_return_addr(__builtin_return_address(0));
uint64_t addr = (uint64_t)(uintptr_t)WAI_RETURN_ADDRESS();
if (low <= addr && addr <= high)
{
char* resolved;
Expand Down Expand Up @@ -326,12 +332,6 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
#include <string.h>
#include <dlfcn.h>

#ifndef WAI_NOINLINE
#ifdef __GNUC__
#define WAI_NOINLINE __attribute__((noinline))
#endif
#endif

WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
{
Expand Down Expand Up @@ -396,7 +396,7 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
{
Dl_info info;

if (dladdr(__builtin_extract_return_addr(__builtin_return_address(0)), &info))
if (dladdr(WAI_RETURN_ADDRESS(), &info))
{
resolved = realpath(info.dli_fname, buffer);
if (!resolved)
Expand Down Expand Up @@ -437,12 +437,6 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
#include <string.h>
#include <dlfcn.h>

#ifndef WAI_NOINLINE
#ifdef __GNUC__
#define WAI_NOINLINE __attribute__((noinline))
#endif
#endif

#if !defined(WAI_PROC_SELF_EXE)
#define WAI_PROC_SELF_EXE "/proc/self/exefile"
#endif
Expand Down Expand Up @@ -508,7 +502,7 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
{
Dl_info info;

if (dladdr(__builtin_extract_return_addr(__builtin_return_address(0)), &info))
if (dladdr(WAI_RETURN_ADDRESS(), &info))
{
resolved = realpath(info.dli_fname, buffer);
if (!resolved)
Expand Down

0 comments on commit 783a033

Please sign in to comment.