makefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_saveCursorPos.cpp \
  57. test/unittest/shellmatta_utils/test_utils_restoreCursorPos.cpp \
  58. test/unittest/shellmatta_utils/test_utils_eraseLine.cpp \
  59. test/unittest/shellmatta_utils/test_utils_rewindCursor.cpp \
  60. test/unittest/shellmatta_utils/test_utils_forwardCursor.cpp \
  61. test/unittest/shellmatta_utils/test_utils_clearInput.cpp \
  62. test/unittest/shellmatta_utils/test_utils_insertChars.cpp \
  63. test/unittest/shellmatta_utils/test_utils_terminateInput.cpp \
  64. test/unittest/shellmatta_utils/test_utils_removeChars.cpp \
  65. test/unittest/shellmatta_autocomplete/test_autocomplete_run.cpp \
  66. test/unittest/shellmatta_escape/test_escape_processArrowKeys.cpp \
  67. test/unittest/shellmatta_history/test_appendHistoryByte.cpp \
  68. test/unittest/shellmatta/test_shellmatta_doInit.cpp \
  69. test/unittest/shellmatta_crc/test_crc32Slow.cpp \
  70. test/unittest/shellmatta_crc/test_crc32Fast.cpp \
  71. test/unittest/shellmatta_ymodem/test_ymodem.cpp
  72. INTEGRATIONTEST_SOURCES := test/integrationtest/test_main.cpp \
  73. test/integrationtest/test_integration.cpp \
  74. test/integrationtest/test_integration_opt.cpp \
  75. test/integrationtest/test_integration_optLong.cpp \
  76. test/integrationtest/test_integration_continue.cpp \
  77. test/integrationtest/test_integration_busy.cpp \
  78. test/integrationtest/test_integration_history.cpp \
  79. test/integrationtest/test_integration_help.cpp
  80. INTEGRATIONTEST_TRANSPORT_SOURCES := $(INTEGRATIONTEST_SOURCES) \
  81. test/integrationtest_transport/test_integration_transport.cpp
  82. INTEGRATIONTEST_AUTH_SOURCES := test/integrationtest_auth/test_main.cpp \
  83. test/integrationtest_auth/test_integration_auth.cpp
  84. UNITTEST_CPPOBJ := $(patsubst %.cpp,$(UNITTEST_OBJ_DIR)%.o,$(UNITTEST_SOURCES))
  85. INTEGRATIONTEST_CPPOBJ := $(patsubst %.cpp,$(INTEGRATIONTEST_CPP_OBJ_DIR)%.o,$(INTEGRATIONTEST_SOURCES))
  86. INTEGRATIONTEST_COBJ := $(patsubst %.c,$(INTEGRATIONTEST_CPP_OBJ_DIR)%.o,$(SOURCES))
  87. INTEGRATIONTEST_TRANSPORT_CPPOBJ := $(patsubst %.cpp,$(INTEGRATIONTEST_CPP_OBJ_DIR_TRANSPORT)%.o,$(INTEGRATIONTEST_TRANSPORT_SOURCES))
  88. INTEGRATIONTEST_TRANSPORT_COBJ := $(patsubst %.c,$(INTEGRATIONTEST_CPP_OBJ_DIR_TRANSPORT)%.o,$(SOURCES_TRANPORT_LAYER))
  89. INTEGRATIONTEST_AUTH_CPPOBJ := $(patsubst %.cpp,$(INTEGRATIONTEST_AUTH_CPP_OBJ_DIR)%.o,$(INTEGRATIONTEST_AUTH_SOURCES))
  90. INTEGRATIONTEST_AUTH_COBJ := $(patsubst %.c,$(INTEGRATIONTEST_AUTH_CPP_OBJ_DIR)%.o,$(AUTH_SOURCES))
  91. CFLAGS := $(INCLUDES:%=-I%) -g -Wall -Werror -Wextra -pedantic
  92. CFLAGS += -DSHELLMATTA_HELP_ALIAS=\(char*\)\"?\"
  93. CFLAGS_EXAMPLE := $(CFLAGS) -DSHELLMATTA_AUTHENTICATION -DSHELLMATTA_TRANSPORT
  94. TESTFLAGS := $(CFLAGS) -fprofile-arcs -ftest-coverage
  95. TESTFLAGS_AUTH := $(CFLAGS) -DSHELLMATTA_AUTHENTICATION -fprofile-arcs -ftest-coverage
  96. TESTLFLAGS := -fprofile-arcs -Wl,--allow-multiple-definition
  97. TESTFLAGS_TRANSPORT := $(TESTFLAGS) -DSHELLMATTA_TRANSPORT
  98. TESTLFLAGS_TRANSPORT := $(TESTLFLAGS)
  99. DEPEND = -MT $@ -MF "$(@:%.o=%.d)" -MG -MM
  100. COBJ := $(patsubst %.c,$(OBJ_DIR)%.o,$(SOURCES))
  101. EXAMPLE_TARGET := $(OBJ_DIR)example/example
  102. UNITTEST_TARGET := $(OBJ_DIR)test/unittest/unittest
  103. INTEGRATIONTEST_TARGET := $(OBJ_DIR)test/integrationtest/integrationtest
  104. INTEGRATIONTEST_TARGET_AUTH := $(OBJ_DIR)test/integrationtest_auth/integrationtest_auth
  105. INTEGRATIONTEST_TARGET_TRANSPORT := $(OBJ_DIR)test/integrationtest_transport/integrationtest_transport
  106. TEST_RUN_TARGETS := unittest integrationtest integrationtest_auth integrationtest_transport
  107. # ensure the tests are running one after another when general test target is called
  108. ifneq ($(filter test,$(MAKECMDGOALS)),)
  109. TEST_RUN_TARGETS_HELPER := $(TEST_RUN_TARGETS)
  110. endif
  111. TEST_TARGETS := $(UNITTEST_TARGET) $(INTEGRATIONTEST_TARGET) $(INTEGRATIONTEST_TARGET_AUTH) $(INTEGRATIONTEST_TARGET_TRANSPORT)
  112. OBJ := $(COBJ) \
  113. $(EXAMPLE_COBJ) \
  114. $(UNITTEST_CPPOBJ) \
  115. $(INTEGRATIONTEST_CPPOBJ) \
  116. $(INTEGRATIONTEST_COBJ) \
  117. $(INTEGRATIONTEST_AUTH_CPPOBJ) \
  118. $(INTEGRATIONTEST_AUTH_COBJ) \
  119. $(INTEGRATIONTEST_TRANSPORT_CPPOBJ) \
  120. $(INTEGRATIONTEST_TRANSPORT_COBJ)
  121. DEPS := $(OBJ:%.o=%.d)
  122. export
  123. .PHONY: help cppcheck doc clean
  124. help:
  125. @echo Shellmatta help
  126. @echo -----------------------------------------------
  127. @echo test - run all tests
  128. @echo cppcheck - run static code analysis \(cppcheck\)
  129. @echo example - build example
  130. @echo -----------------------------------------------
  131. test: $(TEST_RUN_TARGETS)
  132. - @mkdir -p output/test/report
  133. # run the test alltogether to get coverage from all combined tests
  134. # remove coverage from former run
  135. @-find . -name "*.gcda" -type f -delete
  136. $(foreach TEST_RUN_TARGET,$(TEST_RUN_TARGETS),./$(OBJ_DIR)test/$(TEST_RUN_TARGET)/$(TEST_RUN_TARGET)${\n})
  137. # remove report from former run
  138. -rm -rf $(OBJ_DIR)test/report/*
  139. gcovr --html-details --output $(OBJ_DIR)test/report/report.html -f src -f api -d
  140. cppcheck:
  141. - @mkdir -p output/cppcheck/html
  142. cppcheck $(CPPCHECK_OPTIONS)
  143. cppcheck $(CPPCHECK_OPTIONS) --xml 2>output/cppcheck/cppcheck.xml
  144. cppcheck-htmlreport --file=output/cppcheck/cppcheck.xml --title="Shellmatta" --report-dir=output/cppcheck/html
  145. $(TEST_RUN_TARGETS): $(TEST_TARGETS) $(filter-out $@,$(TEST_RUN_TARGETS_HELPER))
  146. - @mkdir -p output/test/$@/report
  147. @echo running test $@:
  148. # remove coverage from former run
  149. @-find . -name "*.gcda" -type f -delete
  150. -$(OBJ_DIR)test/$@/$@
  151. # remove report from former run
  152. -rm -rf $(OBJ_DIR)test/$@/report/*
  153. gcovr --html-details --output $(OBJ_DIR)test/$@/report/report.html output/test/$@ -f src -f api -d
  154. example: $(EXAMPLE_TARGET)
  155. @echo building example
  156. doc:
  157. - @mkdir -p output/doc/html
  158. - @mkdir -p output/doc/latex
  159. doxygen cfg/doxygen/doxyfile
  160. clean:
  161. - rm -rf $(OBJ_DIR)
  162. $(EXAMPLE_TARGET): $(EXAMPLE_COBJ)
  163. - @mkdir -p $(@D)
  164. $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS)
  165. $(UNITTEST_TARGET): $(UNITTEST_CPPOBJ)
  166. - @mkdir -p $(@D)
  167. $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS)
  168. $(INTEGRATIONTEST_TARGET): $(INTEGRATIONTEST_CPPOBJ) $(INTEGRATIONTEST_COBJ)
  169. - @mkdir -p $(@D)
  170. $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS)
  171. $(INTEGRATIONTEST_TARGET_TRANSPORT): $(INTEGRATIONTEST_TRANSPORT_CPPOBJ) $(INTEGRATIONTEST_TRANSPORT_COBJ)
  172. - @mkdir -p $(@D)
  173. $(CPP) $(TESTLFLAGS_TRANSPORT) $(LIB_PATH) -o $@ $^ $(LIBS)
  174. $(INTEGRATIONTEST_TARGET_AUTH): $(INTEGRATIONTEST_AUTH_CPPOBJ) $(INTEGRATIONTEST_AUTH_COBJ)
  175. - @mkdir -p $(@D)
  176. $(CPP) $(TESTLFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS)
  177. $(TARGET): $(OBJ)
  178. - @mkdir -p $(@D)
  179. $(CC) $(LFLAGS) $(LIB_PATH) -o $@ $^ $(LIBS)
  180. $(COBJ):
  181. - @mkdir -p $(@D)
  182. @$(CC) -c $(CFLAGS) $(DEPEND) -o $(@:%.o=%.d) $(subst $(OBJ_DIR), ,$(@:%.o=%.c))
  183. $(CC) -c $(CFLAGS) -o $@ $(subst $(OBJ_DIR), ,$(@:%.o=%.c))
  184. $(EXAMPLE_COBJ):
  185. - @mkdir -p $(@D)
  186. @$(CC) -c $(CFLAGS_EXAMPLE) $(DEPEND) -o $(@:%.o=%.d) $(subst $(OBJ_DIR_EXAMPLE), ,$(@:%.o=%.c))
  187. $(CC) -c $(CFLAGS_EXAMPLE) -o $@ $(subst $(OBJ_DIR_EXAMPLE), ,$(@:%.o=%.c))
  188. $(UNITTEST_CPPOBJ):
  189. - @mkdir -p $(@D)
  190. @$(CPP) -c $(TESTFLAGS) $(DEPEND) -o $(@:%.o=%.d) $(subst $(UNITTEST_OBJ_DIR), ,$(@:%.o=%.cpp))
  191. $(CPP) -c $(TESTFLAGS) -o $@ $(subst $(UNITTEST_OBJ_DIR), ,$(@:%.o=%.cpp))
  192. $(INTEGRATIONTEST_CPPOBJ):
  193. - @mkdir -p $(@D)
  194. @$(CPP) -c $(TESTFLAGS) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_CPP_OBJ_DIR), ,$(@:%.o=%.cpp))
  195. $(CPP) -c $(TESTFLAGS) -o $@ $(subst $(INTEGRATIONTEST_CPP_OBJ_DIR), ,$(@:%.o=%.cpp))
  196. $(INTEGRATIONTEST_COBJ):
  197. - @mkdir -p $(@D)
  198. @$(CC) -c $(TESTFLAGS) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_C_OBJ_DIR), ,$(@:%.o=%.c))
  199. $(CC) -c $(TESTFLAGS) -o $@ $(subst $(INTEGRATIONTEST_C_OBJ_DIR), ,$(@:%.o=%.c))
  200. $(INTEGRATIONTEST_TRANSPORT_CPPOBJ):
  201. - @mkdir -p $(@D)
  202. @$(CPP) -c $(TESTFLAGS_TRANSPORT) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_CPP_OBJ_DIR_TRANSPORT), ,$(@:%.o=%.cpp))
  203. $(CPP) -c $(TESTFLAGS_TRANSPORT) -o $@ $(subst $(INTEGRATIONTEST_CPP_OBJ_DIR_TRANSPORT), ,$(@:%.o=%.cpp))
  204. $(INTEGRATIONTEST_TRANSPORT_COBJ):
  205. - @mkdir -p $(@D)
  206. @$(CC) -c $(TESTFLAGS_TRANSPORT) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_C_OBJ_DIR_TRANSPORT), ,$(@:%.o=%.c))
  207. $(CC) -c $(TESTFLAGS_TRANSPORT) -o $@ $(subst $(INTEGRATIONTEST_C_OBJ_DIR_TRANSPORT), ,$(@:%.o=%.c))
  208. $(INTEGRATIONTEST_AUTH_CPPOBJ):
  209. - @mkdir -p $(@D)
  210. @$(CPP) -c $(TESTFLAGS_AUTH) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_AUTH_CPP_OBJ_DIR), ,$(@:%.o=%.cpp))
  211. $(CPP) -c $(TESTFLAGS_AUTH) -o $@ $(subst $(INTEGRATIONTEST_AUTH_CPP_OBJ_DIR), ,$(@:%.o=%.cpp))
  212. $(INTEGRATIONTEST_AUTH_COBJ):
  213. - @mkdir -p $(@D)
  214. $(CC) -c $(TESTFLAGS_AUTH) $(DEPEND) -o $(@:%.o=%.d) $(subst $(INTEGRATIONTEST_AUTH_C_OBJ_DIR), ,$(@:%.o=%.c))
  215. $(CC) -c $(TESTFLAGS_AUTH) -o $@ $(subst $(INTEGRATIONTEST_AUTH_C_OBJ_DIR), ,$(@:%.o=%.c))
  216. %.o: %.cpp
  217. - @mkdir -p $(@D)
  218. @$(CPP) -c $(CFLAGS) $(DEPEND) -o $(@:%.o=%.d) $(subst $(OBJ_DIR), ,$(@:%.o=%.c))
  219. $(CPP) -c $(CFLAGS) -o $@ $<
  220. -include $(DEPS)