makefile 13 KB

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