Makefile 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. CXX = clang++
  2. CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion -Wshadow $(EXTRA_CXXFLAGS) -DCPPHTTPLIB_USE_NON_BLOCKING_GETADDRINFO -fsanitize=address # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS
  3. ifneq ($(OS), Windows_NT)
  4. UNAME_S := $(shell uname -s)
  5. ifeq ($(UNAME_S), Darwin)
  6. PREFIX ?= $(shell brew --prefix)
  7. OPENSSL_DIR = $(PREFIX)/opt/openssl@3
  8. OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto
  9. OPENSSL_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework Security
  10. MBEDTLS_DIR ?= $(shell brew --prefix mbedtls@3)
  11. MBEDTLS_SUPPORT = -DCPPHTTPLIB_MBEDTLS_SUPPORT -I$(MBEDTLS_DIR)/include -L$(MBEDTLS_DIR)/lib -lmbedtls -lmbedx509 -lmbedcrypto
  12. MBEDTLS_SUPPORT += -DCPPHTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN -framework Security
  13. else
  14. OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -lssl -lcrypto
  15. MBEDTLS_SUPPORT = -DCPPHTTPLIB_MBEDTLS_SUPPORT -lmbedtls -lmbedx509 -lmbedcrypto
  16. endif
  17. endif
  18. ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
  19. ifneq ($(OS), Windows_NT)
  20. UNAME_S := $(shell uname -s)
  21. ifeq ($(UNAME_S), Darwin)
  22. # macOS: use Homebrew paths for brotli and zstd
  23. BROTLI_DIR = $(PREFIX)/opt/brotli
  24. BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
  25. ZSTD_DIR = $(PREFIX)/opt/zstd
  26. ZSTD_SUPPORT = -DCPPHTTPLIB_ZSTD_SUPPORT -I$(ZSTD_DIR)/include -L$(ZSTD_DIR)/lib -lzstd
  27. LIBS = -lpthread -lcurl -framework CoreFoundation -framework CFNetwork
  28. else
  29. # Linux: use system paths
  30. BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -lbrotlicommon -lbrotlienc -lbrotlidec
  31. ZSTD_SUPPORT = -DCPPHTTPLIB_ZSTD_SUPPORT -lzstd
  32. LIBS = -lpthread -lcurl -lanl
  33. endif
  34. endif
  35. TEST_ARGS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS)
  36. TEST_ARGS_MBEDTLS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(MBEDTLS_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS)
  37. TEST_ARGS_NO_TLS = gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(ZSTD_SUPPORT) $(LIBS)
  38. # By default, use standalone_fuzz_target_runner.
  39. # This runner does no fuzzing, but simply executes the inputs
  40. # provided via parameters.
  41. # Run e.g. "make all LIB_FUZZING_ENGINE=/path/to/libFuzzer.a"
  42. # to link the fuzzer(s) against a real fuzzing engine.
  43. # OSS-Fuzz will define its own value for LIB_FUZZING_ENGINE.
  44. LIB_FUZZING_ENGINE ?= standalone_fuzz_target_runner.o
  45. CLANG_FORMAT = clang-format
  46. REALPATH = $(shell which grealpath 2>/dev/null || which realpath 2>/dev/null)
  47. STYLE_CHECK_FILES = $(filter-out httplib.h httplib.cc, \
  48. $(wildcard example/*.h example/*.cc fuzzing/*.h fuzzing/*.cc *.h *.cc ../httplib.h))
  49. all : test test_split
  50. LSAN_OPTIONS=suppressions=lsan_suppressions.txt ./test
  51. SHARDS ?= 4
  52. define run_parallel
  53. @echo "Running $(1) with $(SHARDS) shards in parallel..."
  54. @fail=0; \
  55. for i in $$(seq 0 $$(($(SHARDS) - 1))); do \
  56. GTEST_TOTAL_SHARDS=$(SHARDS) GTEST_SHARD_INDEX=$$i \
  57. LSAN_OPTIONS=suppressions=lsan_suppressions.txt \
  58. ./$(1) --gtest_color=yes > $(1)_shard_$$i.log 2>&1 & \
  59. done; \
  60. wait; \
  61. for i in $$(seq 0 $$(($(SHARDS) - 1))); do \
  62. if ! grep -q "\[ PASSED \]" $(1)_shard_$$i.log; then \
  63. echo "=== Shard $$i FAILED ==="; \
  64. cat $(1)_shard_$$i.log; \
  65. fail=1; \
  66. else \
  67. passed=$$(grep "\[ PASSED \]" $(1)_shard_$$i.log); \
  68. echo "Shard $$i: $$passed"; \
  69. fi; \
  70. done; \
  71. if [ $$fail -ne 0 ]; then exit 1; fi; \
  72. echo "All shards passed."
  73. endef
  74. .PHONY: test_openssl_parallel test_mbedtls_parallel test_no_tls_parallel
  75. test_openssl_parallel : test
  76. $(call run_parallel,test)
  77. test_mbedtls_parallel : test_mbedtls
  78. $(call run_parallel,test_mbedtls)
  79. test_no_tls_parallel : test_no_tls
  80. $(call run_parallel,test_no_tls)
  81. proxy : test_proxy
  82. @echo "Starting proxy server..."
  83. cd proxy && \
  84. docker compose up -d
  85. @echo "Waiting for proxy to be ready..."
  86. @until nc -z localhost 3128 && nc -z localhost 3129; do sleep 1; done
  87. @echo "Proxy servers are ready, waiting additional 5 seconds for full startup..."
  88. @sleep 5
  89. @echo "Checking proxy server status..."
  90. @cd proxy && docker compose ps
  91. @echo "Checking proxy server logs..."
  92. @cd proxy && docker compose logs --tail=20
  93. @echo "Running proxy tests..."
  94. ./test_proxy; \
  95. exit_code=$$?; \
  96. echo "Stopping proxy server..."; \
  97. cd proxy && docker compose down; \
  98. exit $$exit_code
  99. proxy_mbedtls : test_proxy_mbedtls
  100. @echo "Starting proxy server..."
  101. cd proxy && \
  102. docker compose up -d
  103. @echo "Waiting for proxy to be ready..."
  104. @until nc -z localhost 3128 && nc -z localhost 3129; do sleep 1; done
  105. @echo "Proxy servers are ready, waiting additional 5 seconds for full startup..."
  106. @sleep 5
  107. @echo "Checking proxy server status..."
  108. @cd proxy && docker compose ps
  109. @echo "Checking proxy server logs..."
  110. @cd proxy && docker compose logs --tail=20
  111. @echo "Running proxy tests (Mbed TLS)..."
  112. ./test_proxy_mbedtls; \
  113. exit_code=$$?; \
  114. echo "Stopping proxy server..."; \
  115. cd proxy && docker compose down; \
  116. exit $$exit_code
  117. test : test.cc include_httplib.cc ../httplib.h Makefile cert.pem
  118. $(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS)
  119. @file $@
  120. # Note: The intention of test_split is to verify that it works to compile and
  121. # link the split httplib.h, so there is normally no need to execute it.
  122. test_split : test.cc ../httplib.h httplib.cc Makefile cert.pem
  123. $(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS)
  124. # Mbed TLS backend targets
  125. test_mbedtls : test.cc include_httplib.cc ../httplib.h Makefile cert.pem
  126. $(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS_MBEDTLS)
  127. @file $@
  128. test_split_mbedtls : test.cc ../httplib.h httplib.cc Makefile cert.pem
  129. $(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS_MBEDTLS)
  130. # No TLS
  131. test_no_tls : test.cc include_httplib.cc ../httplib.h Makefile
  132. $(CXX) -o $@ -I.. $(CXXFLAGS) test.cc include_httplib.cc $(TEST_ARGS_NO_TLS)
  133. @file $@
  134. test_split_no_tls : test.cc ../httplib.h httplib.cc Makefile
  135. $(CXX) -o $@ $(CXXFLAGS) test.cc httplib.cc $(TEST_ARGS_NO_TLS)
  136. # ThreadPool unit tests (no TLS, no compression needed)
  137. test_thread_pool : test_thread_pool.cc ../httplib.h Makefile
  138. $(CXX) -o $@ -I.. $(CXXFLAGS) test_thread_pool.cc gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include -lpthread
  139. check_abi:
  140. @./check-shared-library-abi-compatibility.sh
  141. .PHONY: style_check
  142. style_check: $(STYLE_CHECK_FILES)
  143. @for file in $(STYLE_CHECK_FILES); do \
  144. $(CLANG_FORMAT) $$file > $$file.formatted; \
  145. if ! diff -u $$file $$file.formatted; then \
  146. file2=$$($(REALPATH) --relative-to=.. $$file); \
  147. printf "\n%*s\n" 80 | tr ' ' '#'; \
  148. printf "##%*s##\n" 76; \
  149. printf "## %-70s ##\n" "$$file2 not properly formatted. Please run clang-format."; \
  150. printf "##%*s##\n" 76; \
  151. printf "%*s\n\n" 80 | tr ' ' '#'; \
  152. failed=1; \
  153. fi; \
  154. rm -f $$file.formatted; \
  155. done; \
  156. if [ -n "$$failed" ]; then \
  157. echo "Style check failed for one or more files. See above for details."; \
  158. false; \
  159. else \
  160. echo "All files are properly formatted."; \
  161. fi
  162. test_websocket_heartbeat : test_websocket_heartbeat.cc ../httplib.h Makefile
  163. $(CXX) -o $@ -I.. $(CXXFLAGS) test_websocket_heartbeat.cc $(TEST_ARGS)
  164. @file $@
  165. test_proxy : test_proxy.cc ../httplib.h Makefile cert.pem
  166. $(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS)
  167. test_proxy_mbedtls : test_proxy.cc ../httplib.h Makefile cert.pem
  168. $(CXX) -o $@ -I.. $(CXXFLAGS) test_proxy.cc $(TEST_ARGS_MBEDTLS)
  169. # Runs server_fuzzer.cc based on value of $(LIB_FUZZING_ENGINE).
  170. # Usage: make fuzz_test LIB_FUZZING_ENGINE=/path/to/libFuzzer
  171. fuzz_test: server_fuzzer
  172. ./server_fuzzer fuzzing/corpus/*
  173. # Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use.
  174. server_fuzzer : fuzzing/server_fuzzer.cc ../httplib.h standalone_fuzz_target_runner.o
  175. $(CXX) -o $@ -I.. $(CXXFLAGS) $< $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(LIB_FUZZING_ENGINE) $(ZSTD_SUPPORT) $(LIBS)
  176. @file $@
  177. # Standalone fuzz runner, which just reads inputs from fuzzing/corpus/ dir and
  178. # feeds it to server_fuzzer.
  179. standalone_fuzz_target_runner.o : fuzzing/standalone_fuzz_target_runner.cpp
  180. $(CXX) -o $@ -I.. $(CXXFLAGS) -c $<
  181. httplib.cc : ../httplib.h
  182. python3 ../split.py -o .
  183. cert.pem:
  184. ./gen-certs.sh
  185. clean:
  186. rm -rf test test_split test_mbedtls test_split_mbedtls test_no_tls, test_split_no_tls test_proxy test_proxy_mbedtls server_fuzzer *.pem *.0 *.o *.1 *.srl httplib.h httplib.cc _build* *.dSYM *_shard_*.log