Makefile 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. SRCS = mongoose.c test/unit_test.c test/packed_fs.c
  2. HDRS = $(wildcard src/*.h) $(wildcard mip/*.h)
  3. DEFS ?= -DMG_MAX_HTTP_HEADERS=7 -DMG_ENABLE_LINES -DMG_ENABLE_PACKED_FS=1 -DMG_ENABLE_SSI=1
  4. C_WARN ?= -Wmissing-prototypes -Wstrict-prototypes
  5. WARN ?= -pedantic -W -Wall -Werror -Wshadow -Wdouble-promotion -fno-common -Wconversion -Wundef $(C_WARN)
  6. OPTS ?= -O3 -g3
  7. VALGRIND_OPTS ?= -O0 -g3
  8. INCS ?= -Isrc -I.
  9. SSL ?= MBEDTLS
  10. CWD ?= $(realpath $(CURDIR))
  11. ENV ?= -e Tmp=. -e WINEDEBUG=-all
  12. DOCKER ?= docker run --platform linux/amd64 --rm $(ENV) -v $(CWD):$(CWD) -w $(CWD)
  13. VCFLAGS = /nologo /W3 /O2 /MD /I. $(DEFS) $(TFLAGS)
  14. IPV6 ?= 1
  15. ASAN ?= -fsanitize=address,undefined -fno-sanitize-recover=all
  16. ASAN_OPTIONS ?= detect_leaks=1
  17. EXAMPLES := $(dir $(wildcard examples/*/Makefile)) examples/stm32/nucleo-f746zg-baremetal
  18. PREFIX ?= /usr/local
  19. VERSION ?= $(shell cut -d'"' -f2 src/version.h)
  20. COMMON_CFLAGS ?= $(WARN) $(INCS) $(DEFS) -DMG_ENABLE_IPV6=$(IPV6) $(TFLAGS)
  21. CFLAGS ?= $(OPTS) $(ASAN) $(COMMON_CFLAGS)
  22. VALGRIND_CFLAGS ?= $(VALGRIND_OPTS) $(COMMON_CFLAGS)
  23. VALGRIND_RUN ?= valgrind --tool=memcheck --gen-suppressions=all --leak-check=full --show-leak-kinds=all --leak-resolution=high --track-origins=yes --error-exitcode=1 --exit-on-first-error=yes
  24. .PHONY: examples test valgrind
  25. ifeq "$(SSL)" "MBEDTLS"
  26. MBEDTLS ?= /usr/local
  27. CFLAGS += -DMG_ENABLE_MBEDTLS=1 -I$(MBEDTLS)/include -I/usr/include
  28. LDFLAGS ?= -L$(MBEDTLS)/lib -lmbedtls -lmbedcrypto -lmbedx509
  29. endif
  30. ifeq "$(SSL)" "OPENSSL"
  31. OPENSSL ?= /usr/local
  32. CFLAGS += -DMG_ENABLE_OPENSSL=1 -I$(OPENSSL)/include
  33. LDFLAGS ?= -L$(OPENSSL)/lib -lssl -lcrypto
  34. endif
  35. all: mg_prefix unamalgamated unpacked test test++ arm examples vc98 vc17 vc22 mingw mingw++ linux linux++ fuzz
  36. examples:
  37. @for X in $(EXAMPLES); do test -f $$X/Makefile || continue; $(MAKE) -C $$X example || exit 1; done
  38. test/packed_fs.c: Makefile src/ssi.h test/fuzz.c test/data/a.txt
  39. $(CC) $(CFLAGS) test/pack.c -o pack
  40. $(RUN) ./pack Makefile src/ssi.h test/fuzz.c test/data/a.txt test/data/range.txt > $@
  41. DIR ?= test/data/
  42. OUT ?= fs_packed.c
  43. mkfs:
  44. $(CC) $(CFLAGS) test/pack.c -o pack
  45. $(RUN) ./pack -s $(DIR) `find $(DIR) -type f` > $(OUT)
  46. # find $(DIR) -type f | sed -e s,^$(DIR),,g -e s,^/,,g
  47. # Check that all external (exported) symbols have "mg_" prefix
  48. mg_prefix: mongoose.c mongoose.h
  49. $(CC) mongoose.c $(CFLAGS) -c -o /tmp/x.o && nm /tmp/x.o | grep ' T' | grep -v 'mg_' ; test $$? = 1
  50. # C++ build
  51. test++: test
  52. test++: CC = g++
  53. test++: C_WARN = -std=c++2a -Wno-vla -Wno-shadow -Wno-missing-field-initializers -Wno-deprecated
  54. musl: test
  55. musl: ASAN =
  56. musl: WARN += -Wno-sign-conversion
  57. musl: CC = $(DOCKER) mdashnet/cc1 gcc
  58. musl: RUN = $(DOCKER) mdashnet/cc1
  59. # Make sure we can build from an unamalgamated sources
  60. unamalgamated: $(HDRS) Makefile test/packed_fs.c
  61. $(CC) src/*.c test/packed_fs.c test/unit_test.c $(CFLAGS) $(LDFLAGS) -g -o unit_test
  62. unpacked:
  63. $(CC) -I. mongoose.c test/unit_test.c -o $@
  64. fuzzer: mongoose.c mongoose.h Makefile test/fuzz.c
  65. clang++ mongoose.c test/fuzz.c $(WARN) $(INCS) $(TFLAGS) -DMG_ENABLE_LINES -fsanitize=fuzzer,signed-integer-overflow,address -Wno-deprecated -Wno-vla-extension -o $@
  66. fuzz: fuzzer
  67. $(RUN) ./fuzzer
  68. unit_test: Makefile mongoose.h $(SRCS)
  69. $(CC) $(SRCS) $(CFLAGS) $(LDFLAGS) -g -o unit_test
  70. # make CC=/usr/local/opt/llvm\@8/bin/clang ASAN_OPTIONS=detect_leaks=1
  71. test: unit_test
  72. ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./unit_test
  73. coverage: CFLAGS += -coverage
  74. coverage: test
  75. gcov -l -n *.gcno | sed '/^$$/d' | sed 'N;s/\n/ /'
  76. gcov -t mongoose.c > mongoose.gcov
  77. upload-coverage: coverage
  78. curl -s https://codecov.io/bash | /bin/bash
  79. valgrind_unit_test: Makefile mongoose.h $(SRCS)
  80. $(CC) $(SRCS) $(VALGRIND_CFLAGS) $(LDFLAGS) -g -o valgrind_unit_test
  81. valgrind: valgrind_unit_test
  82. $(VALGRIND_RUN) ./valgrind_unit_test
  83. infer:
  84. infer run -- cc test/unit_test.c -c -W -Wall -Werror -Isrc -I. -O2 -DMG_ENABLE_MBEDTLS=1 -DMG_ENABLE_LINES -I/usr/local/Cellar/mbedtls/2.23.0/include -DMG_ENABLE_IPV6=1 -g -o /dev/null
  85. arm: DEFS += -DMG_ENABLE_FILE=0 -DMG_ENABLE_MIP=1 -DMG_ARCH=MG_ARCH_NEWLIB
  86. arm: mongoose.h $(SRCS)
  87. $(DOCKER) mdashnet/armgcc arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test -nostartfiles --specs nosys.specs -e 0
  88. riscv: DEFS += -DMG_ENABLE_FILE=0 -DMG_ENABLE_MIP=1 -DMG_ARCH=MG_ARCH_NEWLIB
  89. riscv: mongoose.h $(SRCS)
  90. $(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test
  91. vc98: Makefile mongoose.h $(SRCS)
  92. $(DOCKER) mdashnet/vc98 wine cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe
  93. $(DOCKER) mdashnet/vc98 wine $@.exe
  94. # vc17: DEFS += -DMG_ENABLE_IPV6=1
  95. vc17: Makefile mongoose.h $(SRCS)
  96. $(DOCKER) mdashnet/vc17 wine64 cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe
  97. $(DOCKER) mdashnet/vc17 wine64 $@.exe
  98. # vc22: DEFS += -DMG_ENABLE_IPV6=$(IPV6)
  99. vc22: Makefile mongoose.h $(SRCS)
  100. $(DOCKER) mdashnet/vc22 wine64 cl $(SRCS) $(VCFLAGS) ws2_32.lib /Fe$@.exe
  101. $(DOCKER) mdashnet/vc22 wine64 $@.exe
  102. mingw: Makefile mongoose.h $(SRCS)
  103. $(DOCKER) mdashnet/mingw x86_64-w64-mingw32-gcc $(SRCS) -W -Wall -Werror -I. $(DEFS) -lwsock32 -o $@.exe
  104. $(DOCKER) mdashnet/mingw wine64 $@.exe
  105. mingw++: Makefile mongoose.h $(SRCS)
  106. $(DOCKER) mdashnet/mingw x86_64-w64-mingw32-g++ $(SRCS) -W -Wall -Werror -I. $(DEFS) -lwsock32 -o $@.exe
  107. linux: IPV6=0
  108. linux: Makefile mongoose.h $(SRCS)
  109. $(DOCKER) mdashnet/cc2 gcc $(SRCS) $(CFLAGS) $(LDFLAGS) -o unit_test_gcc
  110. $(DOCKER) mdashnet/cc2 ./unit_test_gcc
  111. linux++: CC = g++
  112. linux++: WARN += -Wno-missing-field-initializers
  113. linux++: linux
  114. linux-libs: CFLAGS += -fPIC
  115. linux-libs: mongoose.o
  116. $(CC) mongoose.o $(LDFLAGS) -shared -o libmongoose.so.$(VERSION)
  117. $(AR) rcs libmongoose.a mongoose.o
  118. install: linux-libs
  119. install -Dm644 libmongoose.a libmongoose.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib
  120. ln -s libmongoose.so.$(VERSION) $(DESTDIR)$(PREFIX)/lib/libmongoose.so
  121. install -Dm644 mongoose.h $(DESTDIR)$(PREFIX)/include/mongoose.h
  122. uninstall:
  123. rm -rf $(DESTDIR)$(PREFIX)/lib/libmongoose.a $(DESTDIR)$(PREFIX)/lib/libmongoose.so.$(VERSION) $(DESTDIR)$(PREFIX)/include/mongoose.h $(DESTDIR)$(PREFIX)/lib/libmongoose.so
  124. mongoose.c: Makefile $(wildcard src/*) $(wildcard mip/*.c)
  125. (cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/*.c mip/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
  126. mongoose.h: $(HDRS) Makefile
  127. (cat src/license.h; echo; echo '#ifndef MONGOOSE_H'; echo '#define MONGOOSE_H'; echo; cat src/version.h ; echo; echo '#ifdef __cplusplus'; echo 'extern "C" {'; echo '#endif'; cat src/arch.h src/arch_*.h src/config.h src/str.h src/fmt.h src/log.h src/timer.h src/fs.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/tls_mbed.h src/tls_openssl.h src/ws.h src/sntp.h src/mqtt.h src/dns.h src/json.h src/rpc.h mip/mip.h | sed -e '/keep/! s,#include ".*,,' -e 's,^#pragma once,,'; echo; echo '#ifdef __cplusplus'; echo '}'; echo '#endif'; echo '#endif // MONGOOSE_H')> $@
  128. clean:
  129. rm -rf $(PROG) *.exe *.o *.dSYM unit_test* valgrind_unit_test* ut fuzzer *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb slow-unit* _CL_* infer-out data.txt crash-* test/packed_fs.c pack unpacked
  130. @for X in $(EXAMPLES); do $(MAKE) -C $$X clean; done