BuildSystem: Cygwin support for building proper DLL.
authorJerome Benoit <jerome.benoit@sap.com>
Mon, 20 Mar 2017 15:28:29 +0000 (16:28 +0100)
committerJerome Benoit <jerome.benoit@sap.com>
Mon, 20 Mar 2017 15:28:29 +0000 (16:28 +0100)
Signed-off-by: Jerome Benoit <jerome.benoit@sap.com>
TP_13/exo2/Makefile

index 2fa2084721105e90944063a06a376fcb9fe8ca9e..1a75ccfd150056e9d835ffd364d94ee12c2d7186 100644 (file)
@@ -29,8 +29,6 @@ BUILD_TYPE=debug
 # DO NOT CHANGE STUFF BEYOND THIS LINE
 # ====================================
 
-all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).static
-
 CC=gcc
 LD=gcc
 AR=ar
@@ -55,8 +53,35 @@ LTO_SUPPORT = yes
 GOLD_SUPPORT = yes
 endif
 
+BINARY_SUFFIX :=
+DLIB_PREFIX :=
+SDLIB_PREFIX :=
+DLIB_SUFFIX := .so
+SLIB_SUFFIX := .a
+
+OBJDIR := $(BUILDDIR)/objs
+$(shell mkdir -p $(OBJDIR))
+
+SRCS=$(wildcard $(SRC_PATH)/*.c)
+LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c)
+OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS)))
+LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS)))
+
+DEPDIR := $(BUILDDIR)/deps
+$(shell mkdir -p $(DEPDIR))
+DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td
+POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d
+
 ifeq ($(UNAME),Cygwin)
 GOLD_SUPPORT = no
+# Cygwin ncurses do not contain tinfo
+LDLIBS := $(filter-out -ltinfo,$(LDLIBS))
+BINARY_SUFFIX := .exe
+DLIB_SUFFIX := .dll
+CYGWIN_LDLIBS_NOARCHIVE = -lncurses
+CYGWIN_LIBLDFLAGS := -Wl,--out-implib=$(LIBRARY_PATH)/$(LIBRARY_NAME)$(DLIB_SUFFIX).a \
+    -Wl,--export-all-symbols \
+    -Wl,--enable-auto-import
 endif
 
 ifeq ($(LTO_SUPPORT),yes)
@@ -77,40 +102,40 @@ LIBLDFLAGS := -shared $(LDFLAGS)
 STATICLIBLDFLAGS := -static $(LDFLAGS)
 LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME)
 
-OBJDIR := $(BUILDDIR)/objs
-$(shell mkdir -p $(OBJDIR))
+all: $(BINARY_NAME)$(BINARY_SUFFIX) $(BINARY_NAME).dynamic$(BINARY_SUFFIX) $(BINARY_NAME).static$(BINARY_SUFFIX)
 
-SRCS=$(wildcard $(SRC_PATH)/*.c)
-LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c)
-OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS)))
-LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS)))
-
-DEPDIR := $(BUILDDIR)/deps
-$(shell mkdir -p $(DEPDIR))
-DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td
-POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d
-
-$(LIBRARY_PATH)/$(LIBRARY_NAME).a: $(LIBOBJS)
+$(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX): $(LIBOBJS)
        @echo "[AR StO] $@"
        @$(AR) rcs $@ $^
 
-$(LIBRARY_PATH)/$(LIBRARY_NAME).so: $(LIBOBJS)
+ifeq ($(UNAME),Cygwin)
+$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a: $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)
+$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS)
+       @echo "[LD DLL] $@"
+       @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $(CYGWIN_LIBLDFLAGS) -Wl,--whole-archive $^ -Wl,--no-whole-archive $(CYGWIN_LDLIBS_NOARCHIVE) -o $@
+else
+$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS)
        @echo "[LD ShO] $@"
        @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@
+endif
 
-#$(BINARY_NAME): $(OBJS) $(LIBOBJS)
+#$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBOBJS)
 #      @echo "[LD ] $@"
 #      @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
 
-$(BINARY_NAME): $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
+$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX)
        @echo "[LD ] $@"
        @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
 
-$(BINARY_NAME).static: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
+$(BINARY_NAME).static$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX)
        @echo "[LD ] $@"
        @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@
 
-$(BINARY_NAME).dynamic: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).so
+ifeq ($(UNAME),Cygwin)
+$(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)
+endif
        @echo "[LD ] $@"
        @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
 
@@ -137,14 +162,17 @@ $(DEPDIR)/%.d: ;
 clean:
        @echo "[CLN]"
        -@rm -r $(BUILDDIR)
-       -@rm $(BINARY_NAME)
-       -@rm $(BINARY_NAME).static
-       -@rm $(BINARY_NAME).dynamic
-       -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).a
-       -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).so
+       -@rm $(BINARY_NAME)$(BINARY_SUFFIX)
+       -@rm $(BINARY_NAME).static$(BINARY_SUFFIX)
+       -@rm $(BINARY_NAME).dynamic$(BINARY_SUFFIX)
+       -@rm $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX)
+       -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)
+ifeq ($(UNAME),Cygwin)
+       -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a
+endif
 
-disassemble: $(BINARY_NAME)
+disassemble: $(BINARY_NAME)$(BINARY_SUFFIX)
        objdump -d $< | less
 
-symbols: $(BINARY_NAME)
+symbols: $(BINARY_NAME)$(BINARY_SUFFIX)
        objdump -t $< | sort | less