Makefile 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. ifdef BUILDDIR
  2. # make sure BUILDDIR ends with a slash
  3. override BUILDDIR := $(BUILDDIR)/
  4. # bit of a hack, but we want to make sure BUILDDIR directory structure
  5. # is correct before any commands
  6. $(if $(findstring n,$(MAKEFLAGS)),, $(shell mkdir -p \
  7. $(BUILDDIR) \
  8. $(BUILDDIR)bd \
  9. $(BUILDDIR)runners \
  10. $(BUILDDIR)tests))
  11. endif
  12. # overridable target/src/tools/flags/etc
  13. ifneq ($(wildcard test.c main.c),)
  14. TARGET ?= $(BUILDDIR)lfs
  15. else
  16. TARGET ?= $(BUILDDIR)lfs.a
  17. endif
  18. CC ?= gcc
  19. AR ?= ar
  20. SIZE ?= size
  21. CTAGS ?= ctags
  22. NM ?= nm
  23. OBJDUMP ?= objdump
  24. LCOV ?= lcov
  25. SRC ?= $(filter-out $(wildcard *.*.c),$(wildcard *.c))
  26. OBJ := $(SRC:%.c=$(BUILDDIR)%.o)
  27. DEP := $(SRC:%.c=$(BUILDDIR)%.d)
  28. ASM := $(SRC:%.c=$(BUILDDIR)%.s)
  29. CGI := $(SRC:%.c=$(BUILDDIR)%.ci)
  30. TAGCDA := $(SRC:%.c=$(BUILDDIR)%.t.a.gcda)
  31. TESTS ?= $(wildcard tests/*.toml)
  32. TEST_SRC ?= $(SRC) \
  33. $(filter-out $(wildcard bd/*.*.c),$(wildcard bd/*.c)) \
  34. runners/test_runner.c
  35. TEST_TSRC := $(TESTS:%.toml=$(BUILDDIR)%.t.c) $(TEST_SRC:%.c=$(BUILDDIR)%.t.c)
  36. TEST_TASRC := $(TEST_TSRC:%.t.c=%.t.a.c)
  37. TEST_TAOBJ := $(TEST_TASRC:%.t.a.c=%.t.a.o)
  38. TEST_TADEP := $(TEST_TASRC:%.t.a.c=%.t.a.d)
  39. TEST_TAGCNO := $(TEST_TASRC:%.t.a.c=%.t.a.gcno)
  40. TEST_TAGCDA := $(TEST_TASRC:%.t.a.c=%.t.a.gcda)
  41. ifdef DEBUG
  42. override CFLAGS += -O0
  43. else
  44. override CFLAGS += -Os
  45. endif
  46. ifdef TRACE
  47. override CFLAGS += -DLFS_YES_TRACE
  48. endif
  49. override CFLAGS += -g3
  50. override CFLAGS += -I.
  51. override CFLAGS += -std=c99 -Wall -pedantic
  52. override CFLAGS += -Wextra -Wshadow -Wjump-misses-init -Wundef
  53. override CFLAGS += -ftrack-macro-expansion=0
  54. override TESTFLAGS += -b
  55. # forward -j flag
  56. override TESTFLAGS += $(filter -j%,$(MAKEFLAGS))
  57. ifdef VERBOSE
  58. override TESTFLAGS += -v
  59. override CODEFLAGS += -v
  60. override DATAFLAGS += -v
  61. override STACKFLAGS += -v
  62. override STRUCTSFLAGS += -v
  63. override COVERAGEFLAGS += -v
  64. override TESTFLAGS += -v
  65. override TESTCFLAGS += -v
  66. endif
  67. ifdef EXEC
  68. override TESTFLAGS += --exec="$(EXEC)"
  69. endif
  70. ifdef BUILDDIR
  71. override TESTFLAGS += --build-dir="$(BUILDDIR:/=)"
  72. override CODEFLAGS += --build-dir="$(BUILDDIR:/=)"
  73. override DATAFLAGS += --build-dir="$(BUILDDIR:/=)"
  74. override STACKFLAGS += --build-dir="$(BUILDDIR:/=)"
  75. override STRUCTSFLAGS += --build-dir="$(BUILDDIR:/=)"
  76. override COVERAGEFLAGS += --build-dir="$(BUILDDIR:/=)"
  77. endif
  78. ifneq ($(NM),nm)
  79. override CODEFLAGS += --nm-tool="$(NM)"
  80. override DATAFLAGS += --nm-tool="$(NM)"
  81. endif
  82. ifneq ($(OBJDUMP),objdump)
  83. override STRUCTSFLAGS += --objdump-tool="$(OBJDUMP)"
  84. endif
  85. # commands
  86. .PHONY: all build
  87. all build: $(TARGET)
  88. .PHONY: asm
  89. asm: $(ASM)
  90. .PHONY: size
  91. size: $(OBJ)
  92. $(SIZE) -t $^
  93. .PHONY: tags
  94. tags:
  95. $(CTAGS) --totals --c-types=+p $(shell find -H -name '*.h') $(SRC)
  96. .PHONY: test-runner
  97. test-runner: override CFLAGS+=--coverage
  98. test-runner: $(BUILDDIR)runners/test_runner
  99. .PHONY: test
  100. test: test-runner
  101. rm -f $(TEST_TAGCDA)
  102. ./scripts/test.py --runner=$(BUILDDIR)runners/test_runner $(TESTFLAGS)
  103. .PHONY: test-list
  104. test-list: test-runner
  105. ./scripts/test.py --runner=$(BUILDDIR)runners/test_runner $(TESTFLAGS) -l
  106. .PHONY: code
  107. code: $(OBJ)
  108. ./scripts/code.py $^ -S $(CODEFLAGS)
  109. .PHONY: data
  110. data: $(OBJ)
  111. ./scripts/data.py $^ -S $(DATAFLAGS)
  112. .PHONY: stack
  113. stack: $(CGI)
  114. ./scripts/stack.py $^ -S $(STACKFLAGS)
  115. .PHONY: structs
  116. structs: $(OBJ)
  117. ./scripts/structs.py $^ -S $(STRUCTSFLAGS)
  118. .PHONY: coverage
  119. coverage: $(TAGCDA)
  120. ./scripts/coverage.py $^ -s $(COVERAGEFLAGS)
  121. .PHONY: summary
  122. summary: $(BUILDDIR)lfs.csv
  123. ./scripts/summary.py -Y $^ $(SUMMARYFLAGS)
  124. # rules
  125. -include $(DEP)
  126. -include $(TEST_TADEP)
  127. .SUFFIXES:
  128. .SECONDARY:
  129. $(BUILDDIR)lfs: $(OBJ)
  130. $(CC) $(CFLAGS) $^ $(LFLAGS) -o $@
  131. $(BUILDDIR)lfs.a: $(OBJ)
  132. $(AR) rcs $@ $^
  133. $(BUILDDIR)lfs.csv: $(OBJ) $(CGI)
  134. ./scripts/code.py $(OBJ) -q $(CODEFLAGS) -o $@
  135. ./scripts/data.py $(OBJ) -q -m $@ $(DATAFLAGS) -o $@
  136. ./scripts/stack.py $(CGI) -q -m $@ $(STACKFLAGS) -o $@
  137. ./scripts/structs.py $(OBJ) -q -m $@ $(STRUCTSFLAGS) -o $@
  138. $(BUILDDIR)runners/test_runner: $(TEST_TAOBJ)
  139. $(CC) $(CFLAGS) $^ $(LFLAGS) -o $@
  140. # our main build rule generates .o, .d, and .ci files, the latter
  141. # used for stack analysis
  142. $(BUILDDIR)%.o $(BUILDDIR)%.ci: %.c
  143. $(CC) -c -MMD -fcallgraph-info=su $(CFLAGS) $< -o $(BUILDDIR)$*.o
  144. $(BUILDDIR)%.s: %.c
  145. $(CC) -S $(CFLAGS) $< -o $@
  146. $(BUILDDIR)%.a.c: %.c
  147. ./scripts/explode_asserts.py $< -o $@
  148. $(BUILDDIR)%.a.c: $(BUILDDIR)%.c
  149. ./scripts/explode_asserts.py $< -o $@
  150. $(BUILDDIR)%.t.c: %.toml
  151. ./scripts/test.py -c $< $(TESTCFLAGS) -o $@
  152. $(BUILDDIR)%.t.c: %.c $(TESTS)
  153. ./scripts/test.py -c $(TESTS) -s $< $(TESTCFLAGS) -o $@
  154. # clean everything
  155. .PHONY: clean
  156. clean:
  157. rm -f $(BUILDDIR)lfs
  158. rm -f $(BUILDDIR)lfs.a
  159. rm -f $(BUILDDIR)lfs.csv
  160. rm -f $(BUILDDIR)runners/test_runner
  161. rm -f $(OBJ)
  162. rm -f $(DEP)
  163. rm -f $(ASM)
  164. rm -f $(CGI)
  165. rm -f $(TEST_TSRC)
  166. rm -f $(TEST_TASRC)
  167. rm -f $(TEST_TAOBJ)
  168. rm -f $(TEST_TADEP)
  169. rm -f $(TEST_TAGCNO)
  170. rm -f $(TEST_TAGCDA)