From 783a033c9e53470f0896c4a15507d068026f0909 Mon Sep 17 00:00:00 2001 From: Gregory Pakosz Date: Mon, 25 Jan 2016 22:23:45 +0100 Subject: [PATCH] made library, examples and Makefile MINGW64 friendly --- _gnu-make/Makefile | 26 +++++++++++++++++------- example/library.c | 2 +- src/whereami.c | 50 ++++++++++++++++++++-------------------------- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/_gnu-make/Makefile b/_gnu-make/Makefile index e04fea6..afde4aa 100644 --- a/_gnu-make/Makefile +++ b/_gnu-make/Makefile @@ -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 @@ -35,17 +42,22 @@ 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 @@ -53,12 +65,12 @@ 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 @@ -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: diff --git a/example/library.c b/example/library.c index dec6c12..808a2a4 100644 --- a/example/library.c +++ b/example/library.c @@ -55,8 +55,8 @@ static void unload() #define WIN32_LEAN_AND_MEAN #if defined(_MSC_VER) #pragma warning(push, 3) -#include #endif +#include BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { diff --git a/src/whereami.c b/src/whereami.c index 4797d76..ac12f75 100644 --- a/src/whereami.c +++ b/src/whereami.c @@ -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 @@ -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]; @@ -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 @@ -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) { @@ -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; @@ -326,12 +332,6 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) #include #include -#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) { @@ -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) @@ -437,12 +437,6 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) #include #include -#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 @@ -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)