Makefile 10 KB

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