SHELL=cmd

ifndef BOOSTDIR
BOOSTDIR = ../../boost_1_73_0
endif

ifdef OPTIMISED
OPTIM_CXXFLAGS = -O2
OPTIM_LDFLAGS = -O2
else
OPTIM_CXXFLAGS = -fno-inline
OPTIM_LDFLAGS =
endif

ifndef STANDALONE
BOOST_CXXFLAGS = -I$(BOOSTDIR) -DASIO_ENABLE_BOOST
endif

ifdef SEPARATE_COMPILATION
SEPARATE_COMPILATION_LIB = libasio.a
SEPARATE_COMPILATION_CXXFLAGS = \
	-DASIO_SEPARATE_COMPILATION \
	-DASIO_ENABLE_CANCELIO
endif

CXXFLAGS = -g -Wall -Wextra -mthreads -I../include \
	$(OPTIM_CXXFLAGS) \
	$(BOOST_CXXFLAGS) \
	$(SEPARATE_COMPILATION_CXXFLAGS)
LDFLAGS = -g -mthreads $(OPTIM_LDFLAGS)
LIBS = $(SEPARATE_COMPILATION_LIB) -lws2_32 -lmswsock
DEFINES = -D_WIN32_WINNT=0x0501

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

UNIT_TEST_EXES = \
	tests/unit/any_completion_executor.exe \
	tests/unit/any_completion_handler.exe \
	tests/unit/any_io_executor.exe \
	tests/unit/associated_allocator.exe \
	tests/unit/associated_executor.exe \
	tests/unit/async_result.exe \
	tests/unit/awaitable.exe \
	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.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/bind_cancellation_slot.exe \
	tests/unit/bind_executor.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/co_spawn.exe \
	tests/unit/completion_condition.exe \
	tests/unit/compose.exe \
	tests/unit/connect.exe \
	tests/unit/coroutine.exe \
	tests/unit/deadline_timer.exe \
	tests/unit/defer.exe \
	tests/unit/detached.exe \
	tests/unit/dispatch.exe \
	tests/unit/error.exe \
	tests/unit/execution_context.exe \
	tests/unit/execution/any_executor.exe \
	tests/unit/execution/blocking.exe \
	tests/unit/execution/blocking_adaptation.exe \
	tests/unit/execution/context_as.exe \
	tests/unit/execution/executor.exe \
	tests/unit/execution/invocable_archetype.exe \
	tests/unit/execution/mapping.exe \
	tests/unit/execution/outstanding_work.exe \
	tests/unit/execution/prefer_only.exe \
	tests/unit/execution/relationship.exe \
	tests/unit/executor.exe \
	tests/unit/executor_work_guard.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/io_context_strand.exe \
	tests/unit/ip/address.exe \
	tests/unit/ip/address_v4.exe \
	tests/unit/ip/address_v4_iterator.exe \
	tests/unit/ip/address_v4_range.exe \
	tests/unit/ip/address_v6.exe \
	tests/unit/ip/address_v6_iterator.exe \
	tests/unit/ip/address_v6_range.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/network_v4.exe \
	tests/unit/ip/network_v6.exe \
	tests/unit/ip/resolver_query_base.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/packaged_task.exe \
	tests/unit/placeholders.exe \
	tests/unit/post.exe \
	tests/unit/read.exe \
	tests/unit/read_at.exe \
	tests/unit/read_until.exe \
	tests/unit/redirect_error.exe \
	tests/unit/serial_port.exe \
	tests/unit/serial_port_base.exe \
	tests/unit/signal_set.exe \
	tests/unit/socket_base.exe \
	tests/unit/static_thread_pool.exe \
	tests/unit/steady_timer.exe \
	tests/unit/strand.exe \
	tests/unit/streambuf.exe \
	tests/unit/system_context.exe \
	tests/unit/system_executor.exe \
	tests/unit/system_timer.exe \
	tests/unit/this_coro.exe \
	tests/unit/thread.exe \
	tests/unit/thread_pool.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/use_awaitable.exe \
	tests/unit/use_future.exe \
	tests/unit/uses_executor.exe \
	tests/unit/wait_traits.exe \
	tests/unit/windows/basic_object_handle.exe \
	tests/unit/windows/basic_overlapped_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/overlapped_handle.exe \
	tests/unit/windows/overlapped_ptr.exe \
	tests/unit/windows/random_access_handle.exe \
	tests/unit/windows/stream_handle.exe \
	tests/unit/write.exe \
	tests/unit/write_at.exe

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

ifdef STANDALONE
all: \
	$(SEPARATE_COMPILATION_LIB) \
	$(EXAMPLE_EXES) \
	$(UNIT_TEST_EXES)
else
all: \
	$(SEPARATE_COMPILATION_LIB) \
	$(PERFORMANCE_TEST_EXES) \
	$(EXAMPLE_EXES) \
	$(OTHER_EXAMPLE_EXES) \
	$(UNIT_TEST_EXES)
endif

ifdef SEPARATE_COMPILATION
libasio.a: asio.o
	ar ru libasio.a asio.o
endif

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

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

clean:
	-del libasio.a asio.o
	-del /q /s tests\*.exe
	-del /q /s tests\*.o
	-del /q /s examples\*.exe
	-del /q /s examples\*.o

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

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

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

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

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

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

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