makefile 13 KB

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