LIBRARY_PATH:=lib
LDLIBS:=-l$(BINARY_NAME)
BUILD_TYPE:=debug
-#BUILD_TYPE=release
+#BUILD_TYPE:=release
# ====================================
# DO NOT CHANGE STUFF BEYOND THIS LINE
WARN_FLAGS = -Wall -Wextra
STD_FLAG = -std=c11
-UNAME := $(shell uname -o)
+UNAME := $(shell uname -s)
ifeq ($(BUILD_TYPE),debug)
BUILDDIR := .build/debug
BINARY_SUFFIX :=
DLIB_PREFIX :=
SDLIB_PREFIX :=
+SHLIBLDFLAG := -shared
DLIB_SUFFIX := .so
SLIB_SUFFIX := .a
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td
POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d
-ifeq ($(UNAME),Cygwin)
+ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT)
CYGWIN_DLIBRARY_NAME := $(subst lib,cyg,$(LIBRARY_NAME))
GOLD_SUPPORT = no
+# Cygwin ncurses do not contain tinfo
+LDLIBS := $(filter-out -ltinfo,$(LDLIBS))
BINARY_SUFFIX := .exe
DLIB_SUFFIX := .dll
-CYGWIN_LDLIBS_NOARCHIVE :=
+# do not archive external libraries
+CYGWIN_LDLIBS_NOARCHIVE := $(filter-out -l$(BINARY_NAME),$(LDLIBS))
CYGWIN_LIBLDFLAGS := -Wl,--out-implib=$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a \
-Wl,--export-all-symbols \
-Wl,--enable-auto-import
endif
+ifeq ($(UNAME),Darwin)
+SHLIBLDFLAG=-dynamiclib
+LDLIBS := $(filter-out -ltinfo,$(LDLIBS))
+DLIB_SUFFIX=.dylib
+LTO_SUPPORT=no
+GOLD_SUPPORT=no
+endif
+
ifeq ($(LTO_SUPPORT),yes)
CFLAGS_LTO = -flto -ffat-lto-objects
LDFLAGS_LTO = -fuse-linker-plugin -flto
CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES_PATH)
LIBCFLAGS := -fPIC $(CFLAGS)
LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG)
-LIBLDFLAGS := -shared $(LDFLAGS)
+LIBLDFLAGS := $(SHLIBLDFLAG) $(LDFLAGS)
STATICLIBLDFLAGS := -static $(LDFLAGS)
LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH)
+# Mac OS X do not support static linking
+ifeq ($(UNAME),Darwin)
+all: $(BINARY_NAME)$(BINARY_SUFFIX) $(BINARY_NAME).dynamic$(BINARY_SUFFIX)
+else
all: $(BINARY_NAME)$(BINARY_SUFFIX) $(BINARY_NAME).dynamic$(BINARY_SUFFIX) $(BINARY_NAME).static$(BINARY_SUFFIX)
+endif
$(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX): $(LIBOBJS)
@echo "[AR StO] $@"
@$(AR) rcs $@ $^
-ifeq ($(UNAME),Cygwin)
+ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT)
$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a: $(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX)
$(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS)
@echo "[LD DLL] $@"
else
$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS)
@echo "[LD ShO] $@"
- @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@
+ @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ $(LDLIBS) -o $@
endif
#$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBOBJS)
@echo "[LD ] $@"
@$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@
-ifeq ($(UNAME),Cygwin)
+ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT)
$(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a
else
$(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)
@echo "[CLN]"
-@rm -r $(BUILDDIR)
-@rm $(BINARY_NAME)$(BINARY_SUFFIX)
+ifneq ($(UNAME),Darwin)
-@rm $(BINARY_NAME).static$(BINARY_SUFFIX)
+endif
-@rm $(BINARY_NAME).dynamic$(BINARY_SUFFIX)
-@rm $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX)
-ifeq ($(UNAME),Cygwin)
+ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT)
-@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX)
-@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a
else