made library, examples and Makefile MINGW64 friendly

This commit is contained in:
Gregory Pakosz 2016-01-25 22:23:45 +01:00
parent cc8a01b8a7
commit 783a033c9e
3 changed files with 42 additions and 36 deletions

View File

@ -17,6 +17,13 @@ ifeq ($(platform),)
override platform := mac override platform := mac
override architecture := $(__uname_m) override architecture := $(__uname_m)
endif 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 endif
ifeq ($(architecture),) ifeq ($(architecture),)
override architecture := unknown-architecture override architecture := unknown-architecture
@ -35,17 +42,22 @@ CXXFLAGS := -O2 -g -Wall -pedantic -Werror
ifeq ($(platform),linux) ifeq ($(platform),linux)
override LDFLAGS := $(LDFLAGS) -ldl override LDFLAGS := $(LDFLAGS) -ldl
CFLAGS +=-D_XOPEN_SOURCE=500 CFLAGS += -D_XOPEN_SOURCE=500 -fpic
CXXFLAGS += -fpic
endif endif
ifeq ($(platform),mac) ifeq ($(platform),mac)
CFLAGS +=-D_DARWIN_C_SOURCE CFLAGS += -D_DARWIN_C_SOURCE
endif endif
ifeq ($(platform),mac) ifeq ($(platform),mac)
libsuffix := .dylib libsuffix := .dylib
else endif
ifeq ($(platform),linux)
libsuffix := .so libsuffix := .so
endif endif
ifeq ($(platform),windows)
libsuffix := .dll
endif
.PHONY: build-executable .PHONY: build-executable
build: 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 $(bindir)/executable: $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/executable.c
mkdir -p $(@D) 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) $@) $(if $(postbuild),$(postbuild) $@)
$(bindir)/executable-cpp: $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/executable.c $(bindir)/executable-cpp: $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/executable.c
mkdir -p $(@D) 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) $@) $(if $(postbuild),$(postbuild) $@)
.PHONY: build-library .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 $(bindir)/library$(libsuffix): $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/library.c
mkdir -p $(@D) 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) $@) $(if $(postbuild),$(postbuild) $@)
$(bindir)/library-cpp$(libsuffix): $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/library.c $(bindir)/library-cpp$(libsuffix): $(srcdir)/whereami.c $(srcdir)/whereami.h $(exampledir)/library.c
mkdir -p $(@D) 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) $@) $(if $(postbuild),$(postbuild) $@)
clean: clean:

View File

@ -55,8 +55,8 @@ static void unload()
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#if defined(_MSC_VER) #if defined(_MSC_VER)
#pragma warning(push, 3) #pragma warning(push, 3)
#include <windows.h>
#endif #endif
#include <windows.h>
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{ {

View File

@ -26,6 +26,24 @@ extern "C" {
#define WAI_REALLOC(p, size) realloc(p, size) #define WAI_REALLOC(p, size) realloc(p, size)
#endif #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) #if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
@ -38,12 +56,6 @@ extern "C" {
#pragma warning(pop) #pragma warning(pop)
#endif #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) static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, int* dirname_length)
{ {
wchar_t buffer1[MAX_PATH]; 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(push)
#pragma warning(disable: 4054) #pragma warning(disable: 4054)
#endif #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) #if defined(_MSC_VER)
#pragma warning(pop) #pragma warning(pop)
#endif #endif
@ -150,12 +162,6 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
#define WAI_PROC_SELF_EXE "/proc/self/exe" #define WAI_PROC_SELF_EXE "/proc/self/exe"
#endif #endif
#ifndef WAI_NOINLINE
#ifdef __GNUC__
#define WAI_NOINLINE __attribute__((noinline))
#endif
#endif
WAI_FUNCSPEC WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length) 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) 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) if (low <= addr && addr <= high)
{ {
char* resolved; char* resolved;
@ -326,12 +332,6 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
#include <string.h> #include <string.h>
#include <dlfcn.h> #include <dlfcn.h>
#ifndef WAI_NOINLINE
#ifdef __GNUC__
#define WAI_NOINLINE __attribute__((noinline))
#endif
#endif
WAI_FUNCSPEC WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length) 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; 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); resolved = realpath(info.dli_fname, buffer);
if (!resolved) if (!resolved)
@ -437,12 +437,6 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
#include <string.h> #include <string.h>
#include <dlfcn.h> #include <dlfcn.h>
#ifndef WAI_NOINLINE
#ifdef __GNUC__
#define WAI_NOINLINE __attribute__((noinline))
#endif
#endif
#if !defined(WAI_PROC_SELF_EXE) #if !defined(WAI_PROC_SELF_EXE)
#define WAI_PROC_SELF_EXE "/proc/self/exefile" #define WAI_PROC_SELF_EXE "/proc/self/exefile"
#endif #endif
@ -508,7 +502,7 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
{ {
Dl_info info; 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); resolved = realpath(info.dli_fname, buffer);
if (!resolved) if (!resolved)