makefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #
  2. # Copyright (c) 2019 - 2024 Stefan Strobel <stefan.strobel@shimatta.net>
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. #
  8. OBJ_DIR := output/
  9. OBJ_DIR_EXAMPLE := $(OBJ_DIR)example_obj/
  10. INTEGRATIONTEST_CPP_OBJ_DIR := $(OBJ_DIR)test/integrationtest/
  11. INTEGRATIONTEST_AUTH_CPP_OBJ_DIR := $(OBJ_DIR)test/integrationtest_auth/
  12. INTEGRATIONTEST_C_OBJ_DIR := $(INTEGRATIONTEST_CPP_OBJ_DIR)
  13. INTEGRATIONTEST_CPP_OBJ_DIR_TRANSPORT := $(OBJ_DIR)test/integrationtest_transport/
  14. INTEGRATIONTEST_C_OBJ_DIR_TRANSPORT := $(INTEGRATIONTEST_CPP_OBJ_DIR_TRANSPORT)
  15. INTEGRATIONTEST_AUTH_C_OBJ_DIR := $(INTEGRATIONTEST_AUTH_CPP_OBJ_DIR)
  16. UNITTEST_OBJ_DIR := $(OBJ_DIR)test/unittest/
  17. # helper define to run tests from a list
  18. define \n
  19. endef
  20. CC := gcc
  21. CPP := g++
  22. LN := ln
  23. SOURCES := src/shellmatta.c \
  24. src/shellmatta_autocomplete.c \
  25. src/shellmatta_history.c \
  26. src/shellmatta_utils.c \
  27. src/shellmatta_escape.c \
  28. src/shellmatta_opt.c \
  29. src/shellmatta_ymodem.c \
  30. src/shellmatta_crc.c
  31. SOURCES_TRANPORT_LAYER := $(SOURCES) \
  32. src/shellmatta_transport.c \
  33. src/shellmatta_crc.c
  34. AUTH_SOURCES := $(SOURCES) src/shellmatta_auth.c
  35. EXAMPLE_SOURCES := example/main.c
  36. EXAMPLE_SOURCES += $(filter-out $(EXAMPLE_SOURCES),$(AUTH_SOURCES))
  37. EXAMPLE_SOURCES += $(filter-out $(EXAMPLE_SOURCES),$(SOURCES_TRANPORT_LAYER))
  38. EXAMPLE_COBJ := $(patsubst %.c,$(OBJ_DIR_EXAMPLE)%.o,$(EXAMPLE_SOURCES))
  39. CPPCHECK_SOURCES := $(AUTH_SOURCES) \
  40. src/shellmatta_transport.c \
  41. src/shellmatta_crc.c
  42. CPPCHECK_OPTIONS := --enable=all \
  43. --check-level=exhaustive \
  44. --template=gcc \
  45. --cppcheck-build-dir=output/cppcheck \
  46. --suppress-xml=cfg/cppcheck/cppcheck_suppressions.xml \
  47. -DSHELLMATTA_AUTHENTICATION \
  48. -Iapi \
  49. $(CPPCHECK_SOURCES)
  50. INCLUDES := api .
  51. UNITTEST_SOURCES := test/unittest/test_main.cpp \
  52. test/unittest/shellmatta_opt/test_opt_findNextHunk.cpp \
  53. test/unittest/shellmatta_opt/test_opt_peekNextHunk.cpp \
  54. test/unittest/shellmatta_utils/test_utils_writeEcho.cpp \
  55. test/unittest/shellmatta_utils/test_utils_shellItoa.cpp \
  56. test/unittest/shellmatta_utils/test_utils_shellAsciiToUInt32.cpp \
  57. test/unittest/shellmatta_utils/test_utils_saveCursorPos.cpp \
  58. test/unittest/shellmatta_utils/test_utils_restoreCursorPos.cpp \
  59. test/unittest/shellmatta_utils/test_utils_eraseLine.cpp \
  60. test/unittest/shellmatta_utils/test_utils_rewindCursor.cpp \
  61. test/unittest/shellmatta_utils/test_utils_forwardCursor.cpp \
  62. test/unittest/shellmatta_utils/test_utils_clearInput.cpp \
  63. test/unittest/shellmatta_utils/test_utils_insertChars.cpp \
  64. test/unittest/shellmatta_utils/test_utils_terminateInput.cpp \
  65. test/unittest/shellmatta_utils/test_utils_removeChars.cpp \
  66. test/unittest/shellmatta_autocomplete/test_autocomplete_run.cpp \
  67. test/unittest/shellmatta_escape/test_escape_processArrowKeys.cpp \
  68. test/unittest/shellmatta_history/test_appendHistoryByte.cpp \
  69. test/unittest/shellmatta/test_shellmatta_doInit.cpp \
  70. test/unittest/shellmatta_crc/test_crc32Slow.cpp \
  71. test/unittest/shellmatta_crc/test_crc32Fast.cpp \
  72. test/unittest/shellmatta_crc/test_crc16Slow.cpp \
  73. test/unittest/shellmatta_crc/test_crc16Fast.cpp \
  74. test/unittest/shellmatta_ymodem/test_ymodem.cpp
  75. INTEGRATIONTEST_SOURCES := test/integrationtest/test_main.cpp \
  76. test/integrationtest/test_integration.cpp \
  77. test/integrationtest/test_integration_opt.cpp \
  78. test/integrationtest/test_integration_optLong.cpp \
  79. test/integrationtest/test_integration_continue.cpp \
  80. test/integrationtest/test_integration_busy.cpp \
  81. test/integrationtest/test_integration_history.cpp \
  82. test/integrationtest/test_integration_help.cpp
  83. INTEGRATIONTEST_TRANSPORT_SOURCES := $(INTEGRATIONTEST_SOURCES) \
  84. test/integrationtest_transport/test_integration_transport.cpp
  85. INTEGRATIONTEST_AUTH_SOURCES := test/integrationtest_auth/test_main.cpp \
  86. test/integrationtest_auth/test_integration_auth.cpp
  87. UNITTEST_CPPOBJ := $(patsubst %.cpp,$(UNITTEST_OBJ_DIR)%.o,$(UNITTEST_SOURCES))
  88. INTEGRATIONTEST_CPPOBJ := $(patsubst %.cpp,$(INTEGRATIONTEST_CPP_OBJ_DIR)%.o,$(INTEGRATIONTEST_SOURCES))
  89. INTEGRATIONTEST_COBJ := $(patsubst %.c,$(INTEGRATIONTEST_CPP_OBJ_DIR)%.o,$(SOURCES))
  90. INTEGRATIONTEST_TRANSPORT_CPPOBJ := $(patsubst %.cpp,$(INTEGRATIONTEST_CPP_OBJ_DIR_TRANSPORT)%.o,$(INTEGRATIONTEST_TRANSPORT_SOURCES))
  91. INTEGRATIONTEST_TRANSPORT_COBJ := $(patsubst %.c,$(INTEGRATIONTEST_CPP_OBJ_DIR_TRANSPORT)%.o,$(SOURCES_TRANPORT_LAYER))
  92. INTEGRATIONTEST_AUTH_CPPOBJ := $(patsubst %.cpp,$(INTEGRATIONTEST_AUTH_CPP_OBJ_DIR)%.o,$(INTEGRATIONTEST_AUTH_SOURCES))
  93. INTEGRATIONTEST_AUTH_COBJ := $(patsubst %.c,$(INTEGRATIONTEST_AUTH_CPP_OBJ_DIR)%.o,$(AUTH_SOURCES))
  94. CFLAGS := $(INCLUDES:%=-I%) -g -Wall -Werror -Wextra -pedantic
  95. CFLAGS += -DSHELLMATTA_HELP_ALIAS=\(char*\)\"?\"
  96. CFLAGS_EXAMPLE := $(CFLAGS) -DSHELLMATTA_AUTHENTICATION -DSHELLMATTA_TRANSPORT
  97. TESTFLAGS := $(CFLAGS) -fprofile-arcs -ftest-coverage
  98. TESTFLAGS_AUTH := $(CFLAGS) -DSHELLMATTA_AUTHENTICATION -fprofile-arcs -ftest-coverage
  99. TESTLFLAGS := -fprofile-arcs -Wl,--allow-multiple-definition
  100. TESTFLAGS_TRANSPORT := $(TESTFLAGS) -DSHELLMATTA_TRANSPORT
  101. TESTLFLAGS_TRANSPORT := $(TESTLFLAGS)
  102. DEPEND = -MT $@ -MF "$(@:%.o=%.d)" -MG -MM
  103. COBJ := $(patsubst %.c,$(OBJ_DIR)%.o,$(SOURCES))
  104. EXAMPLE_TARGET := $(OBJ_DIR)example/example
  105. UNITTEST_TARGET := $(OBJ_DIR)test/unittest/unittest
  106. INTEGRATIONTEST_TARGET := $(OBJ_DIR)test/integrationtest/integrationtest
  107. INTEGRATIONTEST_TARGET_AUTH := $(OBJ_DIR)test/integrationtest_auth/integrationtest_auth
  108. INTEGRATIONTEST_TARGET_TRANSPORT := $(OBJ_DIR)test/integrationtest_transport/integrationtest_transport
  109. TEST_RUN_TARGETS := unittest integrationtest integrationtest_auth integrationtest_transport
  110. # ensure the tests are running one after another when general test target is called
  111. ifneq ($(filter test,$(MAKECMDGOALS)),)
  112. TEST_RUN_TARGETS_HELPER := $(TEST_RUN_TARGETS)
  113. endif
  114. TEST_TARGETS := $(UNITTEST_TARGET) $(INTEGRATIONTEST_TARGET) $(INTEGRATIONTEST_TARGET_AUTH) $(INTEGRATIONTEST_TARGET_TRANSPORT)
  115. OBJ := $(COBJ) \
  116. $(EXAMPLE_COBJ) \
  117. $(UNITTEST_CPPOBJ) \
  118. $(INTEGRATIONTEST_CPPOBJ) \
  119. $(INTEGRATIONTEST_COBJ) \
  120. $(INTEGRATIONTEST_AUTH_CPPOBJ) \
  121. $(INTEGRATIONTEST_AUTH_COBJ) \
  122. $(INTEGRATIONTEST_TRANSPORT_CPPOBJ) \
  123. $(INTEGRATIONTEST_TRANSPORT_COBJ)
  124. DEPS := $(OBJ:%.o=%.d)
  125. export
  126. .PHONY: help cppcheck doc clean
  127. help:
  128. @echo Shellmatta help
  129. @echo -----------------------------------------------
  130. @echo test - run all tests
  131. @echo cppcheck - run static code analysis \(cppcheck\)
  132. @echo example - build example
  133. @echo -----------------------------------------------
  134. test: $(TEST_RUN_TARGETS)
  135. - @mkdir -p output/test/report
  136. # run the test alltogether to get coverage from all combined tests
  137. # remove coverage from former run
  138. @-find . -name "*.gcda" -type f -delete
  139. $(foreach TEST_RUN_TARGET,$(TEST_RUN_TARGETS),./$(OBJ_DIR)test/$(TEST_RUN_TARGET)/$(TEST_RUN_TARGET)${\n})
  140. # remove report from former run
  141. -rm -rf $(OBJ_DIR)test/report/*
  142. gcovr --html-details --output $(OBJ_DIR)test/report/report.html -f src -f api -d
  143. cppcheck:
  144. - @mkdir -p output/cppcheck/html
  145. cppcheck $(CPPCHECK_OPTIONS)
  146. cppcheck $(CPPCHECK_OPTIONS) --xml 2>output/cppcheck/cppcheck.xml
  147. cppcheck-htmlreport --file=output/cppcheck/cppcheck.xml --title="Shellmatta" --report-dir=output/cppcheck/html
  148. $(TEST_RUN_TARGETS): $(TEST_TARGETS) $(filter-out $@,$(TEST_RUN_TARGETS_HELPER))
  149. - @mkdir -p output/test/$@/report
  150. @echo running test $@:
  151. # remove coverage from former run
  152. @-find . -name "*.gcda" -type f -delete
  153. -$(OBJ_DIR)test/$@/$@
  154. # remove report from former run
  155. -rm -rf $(OBJ_DIR)test/$@/report/*
  156. gcovr --html-details --output $(OBJ_DIR)test/$@/report/report.html output/test/$@ -f src -f api -d
  157. example: $(EXAMPLE_TARGET)
  158. @echo building example
  159. doc:
  160. - @mkdir -p output/doc/html
  161. - @mkdir -p output/doc/latex
  162. doxygen cfg/doxygen/doxyfile
  163. clean:
  164. - rm -rf $(OBJ_DIR)
  165. $(EXAMPLE_TARGET): $(EXAMPLE_COBJ)
  166. - @mkdir -p $(@D)
  167. $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS)
  168. $(UNITTEST_TARGET): $(UNITTEST_CPPOBJ)
  169. - @mkdir -p $(@D)
  170. $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS)
  171. $(INTEGRATIONTEST_TARGET): $(INTEGRATIONTEST_CPPOBJ) $(INTEGRATIONTEST_COBJ)
  172. - @mkdir -p $(@D)
  173. $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS)
  174. $(INTEGRATIONTEST_TARGET_TRANSPORT): $(INTEGRATIONTEST_TRANSPORT_CPPOBJ) $(INTEGRATIONTEST_TRANSPORT_COBJ)
  175. - @mkdir -p $(@D)
  176. $(CPP) $(TESTLFLAGS_TRANSPORT) $(LIB_PATH) -o $@ $^ $(LIBS)
  177. $(INTEGRATIONTEST_TARGET_AUTH): $(INTEGRATIONTEST_AUTH_CPPOBJ) $(INTEGRATIONTEST_AUTH_COBJ)
  178. - @mkdir -p $(@D)
  179. $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS)
  180. $(TARGET): $(OBJ)
  181. - @mkdir -p $(@D)
  182. $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS)
  183. $(COBJ):
  184. - @mkdir -p $(@D)
  185. @$(CC) -c $(CFLAGS) $(DEPEND) -o $(@:%.o=%.d) $(subst $(OBJ_DIR), ,$(@:%.o=%.c))
  186. $(CC) -c $(CFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c))
  187. $(EXAMPLE_COBJ):
  188. - @mkdir -p $(@D)
  189. @$(CC) -c $(CFLAGS_EXAMPLE) $(DEPEND) -o $(@:%.o=%.d) $(subst $(OBJ_DIR_EXAMPLE), ,$(@:%.o=%.c))
  190. $(CC) -c $(CFLAGS_EXAMPLE) -o $@ $(subst $(OBJ_DIR_EXAMPLE), ,$(@:%.o=%.c))
  191. $(UNITTEST_CPPOBJ):
  192. - @mkdir -p $(@D)
  193. @$(CPP) -c $(TESTFLAGS) $(DEPEND) -o $(@:%.o=%.d) $(subst $(UNITTEST_OBJ_DIR), ,$(@:%.o=%.cpp))
  194. $(CPP) -c $(TESTFLAGS) -o $@ $(subst $(UNITTEST_OBJ_DIR), ,$(@:%.o=%.cpp))
  195. $(INTEGRATIONTEST_CPPOBJ):
  196. - @mkdir -p $(@D)
  197. @$(CPP) -c $(TESTFLAGS) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_CPP_OBJ_DIR), ,$(@:%.o=%.cpp))
  198. $(CPP) -c $(TESTFLAGS) -o $@ $(subst $(INTEGRATIONTEST_CPP_OBJ_DIR), ,$(@:%.o=%.cpp))
  199. $(INTEGRATIONTEST_COBJ):
  200. - @mkdir -p $(@D)
  201. @$(CC) -c $(TESTFLAGS) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_C_OBJ_DIR), ,$(@:%.o=%.c))
  202. $(CC) -c $(TESTFLAGS) -o $@ $(subst $(INTEGRATIONTEST_C_OBJ_DIR), ,$(@:%.o=%.c))
  203. $(INTEGRATIONTEST_TRANSPORT_CPPOBJ):
  204. - @mkdir -p $(@D)
  205. @$(CPP) -c $(TESTFLAGS_TRANSPORT) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_CPP_OBJ_DIR_TRANSPORT), ,$(@:%.o=%.cpp))
  206. $(CPP) -c $(TESTFLAGS_TRANSPORT) -o $@ $(subst $(INTEGRATIONTEST_CPP_OBJ_DIR_TRANSPORT), ,$(@:%.o=%.cpp))
  207. $(INTEGRATIONTEST_TRANSPORT_COBJ):
  208. - @mkdir -p $(@D)
  209. @$(CC) -c $(TESTFLAGS_TRANSPORT) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_C_OBJ_DIR_TRANSPORT), ,$(@:%.o=%.c))
  210. $(CC) -c $(TESTFLAGS_TRANSPORT) -o $@ $(subst $(INTEGRATIONTEST_C_OBJ_DIR_TRANSPORT), ,$(@:%.o=%.c))
  211. $(INTEGRATIONTEST_AUTH_CPPOBJ):
  212. - @mkdir -p $(@D)
  213. @$(CPP) -c $(TESTFLAGS_AUTH) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_AUTH_CPP_OBJ_DIR), ,$(@:%.o=%.cpp))
  214. $(CPP) -c $(TESTFLAGS_AUTH) -o $@ $(subst $(INTEGRATIONTEST_AUTH_CPP_OBJ_DIR), ,$(@:%.o=%.cpp))
  215. $(INTEGRATIONTEST_AUTH_COBJ):
  216. - @mkdir -p $(@D)
  217. $(CC) -c $(TESTFLAGS_AUTH) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_AUTH_C_OBJ_DIR), ,$(@:%.o=%.c))
  218. $(CC) -c $(TESTFLAGS_AUTH) -o $@ $(subst $(INTEGRATIONTEST_AUTH_C_OBJ_DIR), ,$(@:%.o=%.c))
  219. %.o: %.cpp
  220. - @mkdir -p $(@D)
  221. @$(CPP) -c $(CFLAGS) $(DEPEND) -o $(@:%.o=%.d) $(subst $(OBJ_DIR), ,$(@:%.o=%.c))
  222. $(CPP) -c $(CFLAGS) -o $@ $<
  223. -include $(DEPS)