From df311203dcdaff246c0a0d0a71ae986b6dcc245f Mon Sep 17 00:00:00 2001
From: Alexander Alashkin <alexander.alashkin@cesanta.com>
Date: Tue, 8 Mar 2016 13:27:34 +0200
Subject: [PATCH] Fix mongoose/examples compilation with mingw

    PUBLISHED_FROM=46800f43ba03082cc589d70e6118d754f4cdc9d0
---
 examples/Makefile                             |  5 +++-
 examples/api_server/Makefile                  |  7 ++++--
 examples/captive_dns_server/Makefile          |  6 ++---
 examples/coap_client/Makefile                 | 15 ++---------
 examples/coap_server/Makefile                 | 15 ++---------
 examples/examples.mk                          | 18 +++++++++++++
 examples/http_client/Makefile                 | 15 ++---------
 examples/json_rpc_server/Makefile             | 14 +----------
 examples/mqtt_broker/Makefile                 | 16 +++---------
 examples/mqtt_client/Makefile                 | 16 +++---------
 .../multithreaded_restful_server/Makefile     | 20 +++------------
 examples/netcat/Makefile                      | 16 +++---------
 examples/publish_subscribe/Makefile           | 15 ++---------
 examples/restful_client/Makefile              | 14 +----------
 examples/restful_server/Makefile              | 25 +------------------
 examples/restful_server_s3/Makefile           | 20 ++-------------
 examples/rules.mk                             | 13 ----------
 examples/settings_panel_for_a_device/Makefile | 12 ++-------
 examples/simplest_web_server/Makefile         | 16 ++----------
 examples/tcp_echo_server/Makefile             | 14 +----------
 examples/websocket_chat/Makefile              | 12 +--------
 21 files changed, 61 insertions(+), 243 deletions(-)
 delete mode 100644 examples/rules.mk

diff --git a/examples/Makefile b/examples/Makefile
index 8b1313a6b..7759464be 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -2,7 +2,10 @@
 # All rights reserved
 
 SUBDIRS = $(sort $(dir $(wildcard */)))
-X = $(SUBDIRS)
+
+ifeq ($(OS), Windows_NT)
+  SUBDIRS:=$(filter-out load_balancer/ netcat/ raspberry_pi_mjpeg_led/, $(SUBDIRS))
+endif
 
 .PHONY: $(SUBDIRS)
 
diff --git a/examples/api_server/Makefile b/examples/api_server/Makefile
index 9face7f6c..4886a5936 100644
--- a/examples/api_server/Makefile
+++ b/examples/api_server/Makefile
@@ -1,12 +1,15 @@
 PROG = api_server
 SOURCES = $(PROG).c sqlite3.c db_plugin_sqlite.c ../../mongoose.c
-CFLAGS = -W -Wall -pthread $(CFLAGS_EXTRA)
+CFLAGS = -W -Wall $(CFLAGS_EXTRA)
+
 
 ifeq ($(OS), Windows_NT)
+  CFLAGS += -lws2_32 -D_MG_PROVIDE_STRNLEN
+  CC = mingw32-gcc
 else
   UNAME_S := $(shell uname -s)
   ifeq ($(UNAME_S), Linux)
-    CFLAGS += -ldl -lm
+    CFLAGS += -ldl -lm -pthread
   endif
 endif
 
diff --git a/examples/captive_dns_server/Makefile b/examples/captive_dns_server/Makefile
index 86640fd39..ff6b2053f 100644
--- a/examples/captive_dns_server/Makefile
+++ b/examples/captive_dns_server/Makefile
@@ -1,4 +1,4 @@
 PROG = captive_dns_server
-MODULE_CFLAGS=-DMG_ENABLE_DNS_SERVER
-
-include ../rules.mk
+MODULE_CFLAGS=-DMG_ENABLE_DNS_SERVER -DMG_ENABLE_IPV6
+SSL_LIB=openssl
+include ../examples.mk
diff --git a/examples/coap_client/Makefile b/examples/coap_client/Makefile
index 1726c41c1..c169d2ee6 100644
--- a/examples/coap_client/Makefile
+++ b/examples/coap_client/Makefile
@@ -1,14 +1,3 @@
 PROG = coap_client
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA) -DMG_ENABLE_COAP
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+MODULE_CFLAGS = -DMG_ENABLE_COAP
+include ../examples.mk
diff --git a/examples/coap_server/Makefile b/examples/coap_server/Makefile
index c2920f95d..c192f51b0 100644
--- a/examples/coap_server/Makefile
+++ b/examples/coap_server/Makefile
@@ -1,14 +1,3 @@
 PROG = coap_server
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA) -DMG_ENABLE_COAP
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+MODULE_CFLAGS = -DMG_ENABLE_COAP
+include ../examples.mk
diff --git a/examples/examples.mk b/examples/examples.mk
index 4d5815d74..7edd607d1 100644
--- a/examples/examples.mk
+++ b/examples/examples.mk
@@ -3,6 +3,24 @@ CFLAGS = -g -W -Wall -I../.. -Wno-unused-function $(CFLAGS_EXTRA) $(MODULE_CFLAG
 
 all: $(PROG)
 
+ifeq ($(OS), Windows_NT)
+# TODO(alashkin): enable SSL in Windows
+CFLAGS += -lws2_32 -D_MG_PROVIDE_STRNLEN
+CC = mingw32-gcc
+else
+ifeq ($(SSL_LIB),openssl)
+CFLAGS += -DMG_ENABLE_SSL -lssl -lcrypto
+else ifeq ($(SSL_LIB), krypton)
+CFLAGS += -DMG_ENABLE_SSL ../../../krypton/krypton.c
+endif
+CFLAGS += -lpthread
+endif
+
+ifeq ($(JS), yes)
+	V7_PATH = ../../deps/v7
+	CFLAGS_EXTRA += -DMG_ENABLE_JAVASCRIPT -I $(V7_PATH) $(V7_PATH)/v7.c
+endif
+
 $(PROG): $(SOURCES)
 	$(CC) $(SOURCES) -o $@ $(CFLAGS)
 
diff --git a/examples/http_client/Makefile b/examples/http_client/Makefile
index 93932ad01..c801491ab 100644
--- a/examples/http_client/Makefile
+++ b/examples/http_client/Makefile
@@ -1,14 +1,3 @@
 PROG = http_client
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. -DMG_ENABLE_SSL -lssl -lcrypto -pthread $(CFLAGS_EXTRA)
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+SSL_LIB=open_ssl
+include ../examples.mk
diff --git a/examples/json_rpc_server/Makefile b/examples/json_rpc_server/Makefile
index 84ea60494..5ba409719 100644
--- a/examples/json_rpc_server/Makefile
+++ b/examples/json_rpc_server/Makefile
@@ -1,14 +1,2 @@
 PROG = json_rpc_server
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. -pthread $(CFLAGS_EXTRA)
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+include ../examples.mk
diff --git a/examples/mqtt_broker/Makefile b/examples/mqtt_broker/Makefile
index f97d40000..b289e3380 100644
--- a/examples/mqtt_broker/Makefile
+++ b/examples/mqtt_broker/Makefile
@@ -1,14 +1,4 @@
 PROG = mqtt_broker
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. -pthread -DMG_ENABLE_SSL -DMG_ENABLE_MQTT_BROKER -lssl -lcrypto $(CFLAGS_EXTRA)
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /DMG_ENABLE_SSL /DMG_ENABLE_MQTT_BROKER /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+MODULE_CFLAGS = -DMG_ENABLE_MQTT_BROKER
+SSL_LIB=open_ssl
+include ../examples.mk
diff --git a/examples/mqtt_client/Makefile b/examples/mqtt_client/Makefile
index 7ad990dd1..d6c4ed939 100644
--- a/examples/mqtt_client/Makefile
+++ b/examples/mqtt_client/Makefile
@@ -1,14 +1,4 @@
 PROG = mqtt_client
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. -pthread -DMG_ENABLE_SSL -lssl -lcrypto $(CFLAGS_EXTRA)
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /DMG_ENABLE_SSL /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+MODULE_CFLAGS = -DMG_ENABLE_MQTT_BROKER
+SSL_LIB=open_ssl
+include ../examples.mk
diff --git a/examples/multithreaded_restful_server/Makefile b/examples/multithreaded_restful_server/Makefile
index 7e7db3b14..54fddd794 100644
--- a/examples/multithreaded_restful_server/Makefile
+++ b/examples/multithreaded_restful_server/Makefile
@@ -1,18 +1,4 @@
 PROG = multithreaded_restful_server
-SOURCES = $(PROG).c ../../mongoose.c
-APP_FLAGS = -DMG_ENABLE_THREADS $(CFLAGS_EXTRA)
-
-ifeq ($(OS), Windows_NT)
-APP_FLAGS += advapi32.lib
-endif
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ -W -Wall -I../.. -pthread $(APP_FLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /Fe$@ $(APP_FLAGS)
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+MODULE_CFLAGS = -DMG_ENABLE_THREADS
+SSL_LIB=open_ssl
+include ../examples.mk
diff --git a/examples/netcat/Makefile b/examples/netcat/Makefile
index 71b75b920..c71d7fe8c 100644
--- a/examples/netcat/Makefile
+++ b/examples/netcat/Makefile
@@ -1,14 +1,4 @@
 PROG = nc
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. -pthread -DMG_ENABLE_SSL -DMG_ENABLE_THREADS -lssl -lcrypto $(CFLAGS_EXTRA)
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /DMG_ENABLE_SSL /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+MODULE_CFLAGS=-DMG_ENABLE_THREADS
+SSL_LIB=open_ssl
+include ../examples.mk
diff --git a/examples/publish_subscribe/Makefile b/examples/publish_subscribe/Makefile
index 98f2900ae..3a4f9886b 100644
--- a/examples/publish_subscribe/Makefile
+++ b/examples/publish_subscribe/Makefile
@@ -1,14 +1,3 @@
 PROG = publish_subscribe
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. -DMG_ENABLE_THREADS -pthread $(CFLAGS_EXTRA)
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+MODULE_CFLAGS=-DMG_ENABLE_THREADS
+include ../examples.mk
diff --git a/examples/restful_client/Makefile b/examples/restful_client/Makefile
index 9a05d7990..a7ab34d09 100644
--- a/examples/restful_client/Makefile
+++ b/examples/restful_client/Makefile
@@ -1,14 +1,2 @@
 PROG = restful_client
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. -pthread $(CFLAGS_EXTRA)
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+include ../examples.mk
diff --git a/examples/restful_server/Makefile b/examples/restful_server/Makefile
index 4e48d637a..3ab02afd8 100644
--- a/examples/restful_server/Makefile
+++ b/examples/restful_server/Makefile
@@ -1,25 +1,2 @@
 PROG = restful_server
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA)
-
-ifeq ($(SSL), openssl)
-	CFLAGS += -DMG_ENABLE_SSL -lssl -lcrypto -lcrypto
-else ifeq ($(SSL), krypton)
-	CFLAGS += -DMG_ENABLE_SSL ../../../krypton/krypton.c
-endif
-
-ifeq ($(JS), yes)
-	V7_PATH = ../../deps/v7
-	CFLAGS_EXTRA += -DMG_ENABLE_JAVASCRIPT -I $(V7_PATH) $(V7_PATH)/v7.c
-endif
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /DMG_ENABLE_THREADS /Fe$@ advapi32.lib
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+include ../examples.mk
diff --git a/examples/restful_server_s3/Makefile b/examples/restful_server_s3/Makefile
index 224555f49..2e7e35289 100644
--- a/examples/restful_server_s3/Makefile
+++ b/examples/restful_server_s3/Makefile
@@ -1,20 +1,4 @@
 PROG = restful_server_s3
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA)
+SSL_LIB=open_ssl
+include ../examples.mk
 
-ifeq ($(SSL), openssl)
-	CFLAGS += -DMG_ENABLE_SSL -lssl -lcrypto -lcrypto
-else ifeq ($(SSL), krypton)
-	CFLAGS += -DMG_ENABLE_SSL ../../../krypton/krypton.c
-endif
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /DMG_ENABLE_THREADS /Fe$@ advapi32.lib
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
diff --git a/examples/rules.mk b/examples/rules.mk
deleted file mode 100644
index 775c1d088..000000000
--- a/examples/rules.mk
+++ /dev/null
@@ -1,13 +0,0 @@
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. -pthread -DMG_ENABLE_SSL -DMG_ENABLE_IPV6 -DMG_ENABLE_THREADS -lssl -lcrypto $(CFLAGS_EXTRA) $(MODULE_CFLAGS)
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -g -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /DMG_ENABLE_SSL /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
diff --git a/examples/settings_panel_for_a_device/Makefile b/examples/settings_panel_for_a_device/Makefile
index ff57b6f45..374049eeb 100644
--- a/examples/settings_panel_for_a_device/Makefile
+++ b/examples/settings_panel_for_a_device/Makefile
@@ -1,11 +1,3 @@
 PROG = settings_panel
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. -DMG_ENABLE_SSL -lssl -lcrypto $(CFLAGS_EXTRA)
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+SSL_LIB=openssl
+include ../examples.mk
diff --git a/examples/simplest_web_server/Makefile b/examples/simplest_web_server/Makefile
index f792e4c79..d26ead16f 100644
--- a/examples/simplest_web_server/Makefile
+++ b/examples/simplest_web_server/Makefile
@@ -1,15 +1,3 @@
 PROG = simplest_web_server
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA) -DMG_DISABLE_DAV_AUTH \
-				 -DMG_ENABLE_FAKE_DAVLOCK
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+MODULE_CFLAGS=-DMG_DISABLE_DAV_AUTH -DMG_ENABLE_FAKE_DAVLOCK
+include ../examples.mk
diff --git a/examples/tcp_echo_server/Makefile b/examples/tcp_echo_server/Makefile
index 22fee1b6d..87a88cdfc 100644
--- a/examples/tcp_echo_server/Makefile
+++ b/examples/tcp_echo_server/Makefile
@@ -1,14 +1,2 @@
 PROG = echo_server
-SOURCES = $(PROG).c ../../mongoose.c
-CFLAGS = -W -Wall -I../.. -pthread $(CFLAGS_EXTRA)
-
-all: $(PROG)
-
-$(PROG): $(SOURCES)
-	$(CC) $(SOURCES) -o $@ $(CFLAGS)
-
-$(PROG).exe: $(SOURCES)
-	cl $(SOURCES) /I../.. /MD /Fe$@
-
-clean:
-	rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
+include ../examples.mk
diff --git a/examples/websocket_chat/Makefile b/examples/websocket_chat/Makefile
index 780276ef4..ae1e7cc6b 100644
--- a/examples/websocket_chat/Makefile
+++ b/examples/websocket_chat/Makefile
@@ -1,12 +1,2 @@
-# Copyright (c) 2014 Cesanta Software
-# All rights reserved
-
 PROG = websocket_chat
-CFLAGS = -W -Wall -I../.. -pthread -g -O0 $(CFLAGS_EXTRA)
-SOURCES = $(PROG).c ../../mongoose.c
-
-$(PROG): $(SOURCES)
-	$(CC) -o $(PROG) $(SOURCES) $(CFLAGS)
-
-clean:
-	rm -rf $(PROG) *.exe *.dSYM *.obj *.exp .*o *.lib
+include ../examples.mk
-- 
GitLab