ifndef BOOSTDIR
BOOSTDIR = ../../boost_1_34_1
endif

CXXFLAGS = -g -O2 -Wall -mthreads -I../include -I$(BOOSTDIR)
LDFLAGS = -g -O2 -mthreads
LIBS = -lws2_32 -lmswsock
DEFINES = -D_WIN32_WINNT=0x0501

PERFORMANCE_TEST_EXES = \
	tests/performance/client.exe \
	tests/performance/server.exe

UNIT_TEST_EXES = \
	tests/unit/basic_datagram_socket.exe \
	tests/unit/basic_deadline_timer.exe \
	tests/unit/basic_raw_socket.exe \
	tests/unit/basic_seq_packet_socket.exe \
	tests/unit/basic_serial_port.exe \
	tests/unit/basic_signal_set.exe \
	tests/unit/basic_socket_acceptor.exe \
	tests/unit/basic_stream_socket.exe \
	tests/unit/basic_streambuf.exe \
	tests/unit/basic_waitable_timer.exe \
	tests/unit/buffered_read_stream.exe \
	tests/unit/buffered_stream.exe \
	tests/unit/buffered_write_stream.exe \
	tests/unit/buffer.exe \
	tests/unit/buffers_iterator.exe \
	tests/unit/completion_condition.exe \
	tests/unit/connect.exe \
	tests/unit/coroutine.exe \
	tests/unit/datagram_socket_service.exe \
	tests/unit/deadline_timer_service.exe \
	tests/unit/deadline_timer.exe \
	tests/unit/error.exe \
	tests/unit/generic/basic_endpoint.exe \
	tests/unit/generic/datagram_protocol.exe \
	tests/unit/generic/raw_protocol.exe \
	tests/unit/generic/seq_packet_protocol.exe \
	tests/unit/generic/stream_protocol.exe \
	tests/unit/high_resolution_timer.exe \
	tests/unit/io_context.exe \
	tests/unit/ip/address.exe \
	tests/unit/ip/address_v4.exe \
	tests/unit/ip/address_v6.exe \
	tests/unit/ip/basic_endpoint.exe \
	tests/unit/ip/basic_resolver.exe \
	tests/unit/ip/basic_resolver_entry.exe \
	tests/unit/ip/basic_resolver_iterator.exe \
	tests/unit/ip/basic_resolver_query.exe \
	tests/unit/ip/host_name.exe \
	tests/unit/ip/icmp.exe \
	tests/unit/ip/multicast.exe \
	tests/unit/ip/resolver_query_base.exe \
	tests/unit/ip/resolver_service.exe \
	tests/unit/ip/tcp.exe \
	tests/unit/ip/udp.exe \
	tests/unit/ip/unicast.exe \
	tests/unit/ip/v6_only.exe \
	tests/unit/is_read_buffered.exe \
	tests/unit/is_write_buffered.exe \
	tests/unit/placeholders.exe \
	tests/unit/raw_socket_service.exe \
	tests/unit/read.exe \
	tests/unit/read_at.exe \
	tests/unit/read_until.exe \
	tests/unit/seq_packet_socket_service.exe \
	tests/unit/serial_port.exe \
	tests/unit/serial_port_base.exe \
	tests/unit/serial_port_service.exe \
	tests/unit/signal_set.exe \
	tests/unit/signal_set_service.exe \
	tests/unit/socket_acceptor_service.exe \
	tests/unit/socket_base.exe \
	tests/unit/steady_timer.exe \
	tests/unit/strand.exe \
	tests/unit/stream_socket_service.exe \
	tests/unit/streambuf.exe \
	tests/unit/system_executor.exe \
	tests/unit/system_context.exe \
	tests/unit/system_timer.exe \
	tests/unit/thread.exe \
	tests/unit/time_traits.exe \
	tests/unit/ts/buffer.exe \
	tests/unit/ts/executor.exe \
	tests/unit/ts/internet.exe \
	tests/unit/ts/io_context.exe \
	tests/unit/ts/net.exe \
	tests/unit/ts/netfwd.exe \
	tests/unit/ts/socket.exe \
	tests/unit/ts/timer.exe \
	tests/unit/waitable_timer_service.exe \
	tests/unit/windows/basic_handle.exe \
	tests/unit/windows/basic_object_handle.exe \
	tests/unit/windows/basic_random_access_handle.exe \
	tests/unit/windows/basic_stream_handle.exe \
	tests/unit/windows/object_handle.exe \
	tests/unit/windows/object_handle_service.exe \
	tests/unit/windows/overlapped_ptr.exe \
	tests/unit/windows/random_access_handle.exe \
	tests/unit/windows/random_access_handle_service.exe \
	tests/unit/windows/stream_handle.exe \
	tests/unit/windows/stream_handle_service.exe \
	tests/unit/write.exe \
	tests/unit/write_at.exe

EXAMPLE_EXES = \
	examples/cpp03/allocation/server.exe \
	examples/cpp03/buffers/reference_counted.exe \
	examples/cpp03/chat/chat_client.exe \
	examples/cpp03/chat/chat_server.exe \
	examples/cpp03/echo/async_tcp_echo_server.exe \
	examples/cpp03/echo/async_udp_echo_server.exe \
	examples/cpp03/echo/blocking_tcp_echo_client.exe \
	examples/cpp03/echo/blocking_tcp_echo_server.exe \
	examples/cpp03/echo/blocking_udp_echo_client.exe \
	examples/cpp03/echo/blocking_udp_echo_server.exe \
	examples/cpp03/http/client/async_client.exe \
	examples/cpp03/http/client/sync_client.exe \
	examples/cpp03/icmp/ping.exe \
	examples/cpp03/invocation/prioritised_handlers.exe \
	examples/cpp03/iostreams/daytime_client.exe \
	examples/cpp03/iostreams/daytime_server.exe \
	examples/cpp03/iostreams/http_client.exe \
	examples/cpp03/multicast/receiver.exe \
	examples/cpp03/multicast/sender.exe \
	examples/cpp03/socks4/sync_client.exe \
	examples/cpp03/timeouts/async_tcp_client.exe \
	examples/cpp03/timeouts/blocking_tcp_client.exe \
	examples/cpp03/timeouts/blocking_token_tcp_client.exe \
	examples/cpp03/timeouts/blocking_udp_client.exe \
	examples/cpp03/timeouts/server.exe \
	examples/cpp03/timers/time_t_timer.exe \
	examples/cpp03/tutorial/timer1/timer.exe \
	examples/cpp03/tutorial/timer2/timer.exe \
	examples/cpp03/tutorial/timer3/timer.exe \
	examples/cpp03/tutorial/timer4/timer.exe \
	examples/cpp03/tutorial/timer5/timer.exe \
	examples/cpp03/tutorial/daytime1/client.exe \
	examples/cpp03/tutorial/daytime2/server.exe \
	examples/cpp03/tutorial/daytime3/server.exe \
	examples/cpp03/tutorial/daytime4/client.exe \
	examples/cpp03/tutorial/daytime5/server.exe \
	examples/cpp03/tutorial/daytime6/server.exe \
	examples/cpp03/tutorial/daytime7/server.exe \
	examples/cpp03/windows/transmit_file.exe

OTHER_EXAMPLE_EXES = \
	examples/cpp03/http/server/http_server.exe \
	examples/cpp03/http/server2/http_server.exe \
	examples/cpp03/http/server3/http_server.exe \
	examples/cpp03/services/daytime_client.exe

all: \
	$(PERFORMANCE_TEST_EXES) \
	$(UNIT_TEST_EXES) \
	$(EXAMPLE_EXES) \
	$(OTHER_EXAMPLE_EXES)

check: $(UNIT_TEST_EXES) $(addprefix run.,$(UNIT_TEST_EXES))

$(addprefix run.,$(UNIT_TEST_EXES))::
	@echo === Running $(@:run.%=%) ===
	@$(@:run.%=%)
	@echo.

clean:
	-rm -f $(PERFORMANCE_TEST_EXES)
	-rm -f $(PERFORMANCE_TEST_EXES:.exe=.o)
	-rm -f $(UNIT_TEST_OBJ)
	-rm -f $(UNIT_TEST_EXES)
	-rm -f $(UNIT_TEST_EXES:.exe=.o)
	-rm -f $(EXAMPLE_EXES)
	-rm -f $(EXAMPLE_EXES:.exe=.o)
	-rm -f $(OTHER_EXAMPLE_EXES)
	-rm -f $(OTHER_EXAMPLE_EXES:.exe=.o)

$(UNIT_TEST_EXES): %.exe: %.o
	g++ -o$@ $(LDFLAGS) $< $(LIBS)

$(PERFORMANCE_TEST_EXES) $(EXAMPLE_EXES): %.exe: %.o
	g++ -o$@ $(LDFLAGS) $< $(LIBS)

examples/cpp03/http/server/http_server.exe: \
		examples/cpp03/http/server/connection.o \
		examples/cpp03/http/server/connection_manager.o \
		examples/cpp03/http/server/main.o \
		examples/cpp03/http/server/mime_types.o \
		examples/cpp03/http/server/reply.o \
		examples/cpp03/http/server/request_handler.o \
		examples/cpp03/http/server/request_parser.o \
		examples/cpp03/http/server/server.o
	g++ -o$@ $(LDFLAGS) $^ $(LIBS)

examples/cpp03/http/server2/http_server.exe: \
		examples/cpp03/http/server2/connection.o \
		examples/cpp03/http/server2/io_context_pool.o \
		examples/cpp03/http/server2/main.o \
		examples/cpp03/http/server2/mime_types.o \
		examples/cpp03/http/server2/reply.o \
		examples/cpp03/http/server2/request_handler.o \
		examples/cpp03/http/server2/request_parser.o \
		examples/cpp03/http/server2/server.o
	g++ -o$@ $(LDFLAGS) $^ $(LIBS)

examples/cpp03/http/server3/http_server.exe: \
		examples/cpp03/http/server3/connection.o \
		examples/cpp03/http/server3/main.o \
		examples/cpp03/http/server3/mime_types.o \
		examples/cpp03/http/server3/reply.o \
		examples/cpp03/http/server3/request_handler.o \
		examples/cpp03/http/server3/request_parser.o \
		examples/cpp03/http/server3/server.o
	g++ -o$@ $(LDFLAGS) $^ $(LIBS)

examples/cpp03/services/daytime_client.exe: \
		examples/cpp03/services/daytime_client.o \
		examples/cpp03/services/logger_service.o
	g++ -o$@ $(LDFLAGS) $^ $(LIBS)

.cpp.o:
	g++ -o$@ -c $(CXXFLAGS) $(DEFINES) $<
