Makefile 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. TESTS ?= $(wildcard tests/*.toml)
  31. TEST_SRC ?= $(SRC) \
  32. $(filter-out $(wildcard bd/*.*.c),$(wildcard bd/*.c)) \
  33. runners/test_runner.c
  34. TEST_TSRC := $(TESTS:%.toml=$(BUILDDIR)%.t.c) $(TEST_SRC:%.c=$(BUILDDIR)%.t.c)
  35. TEST_TASRC := $(TEST_TSRC:%.t.c=%.t.a.c)
  36. TEST_TAOBJ := $(TEST_TASRC:%.t.a.c=%.t.a.o)
  37. TEST_TADEP := $(TEST_TASRC:%.t.a.c=%.t.a.d)
  38. ifdef DEBUG
  39. override CFLAGS += -O0
  40. else
  41. override CFLAGS += -Os
  42. endif
  43. ifdef TRACE
  44. override CFLAGS += -DLFS_YES_TRACE
  45. endif
  46. override CFLAGS += -g3
  47. override CFLAGS += -I.
  48. override CFLAGS += -std=c99 -Wall -pedantic
  49. override CFLAGS += -Wextra -Wshadow -Wjump-misses-init -Wundef
  50. override CFLAGS += -ftrack-macro-expansion=0
  51. override TESTFLAGS += -b
  52. # forward -j flag
  53. override TESTFLAGS += $(filter -j%,$(MAKEFLAGS))
  54. ifdef VERBOSE
  55. override TESTFLAGS += -v
  56. override CODEFLAGS += -v
  57. override DATAFLAGS += -v
  58. override STACKFLAGS += -v
  59. override STRUCTSFLAGS += -v
  60. override COVERAGEFLAGS += -v
  61. override TESTFLAGS += -v
  62. override TESTCFLAGS += -v
  63. endif
  64. ifdef EXEC
  65. override TESTFLAGS += --exec="$(EXEC)"
  66. endif
  67. ifdef COVERAGE
  68. override TESTFLAGS += --coverage
  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: $(BUILDDIR)runners/test_runner
  98. .PHONY: test
  99. test: test_runner
  100. ./scripts/test.py --runner=$(BUILDDIR)runners/test_runner $(TESTFLAGS)
  101. .PHONY: test_list
  102. test_list: test_runner
  103. ./scripts/test.py --runner=$(BUILDDIR)runners/test_runner $(TESTFLAGS) -l
  104. .PHONY: code
  105. code: $(OBJ)
  106. ./scripts/code.py $^ -S $(CODEFLAGS)
  107. .PHONY: data
  108. data: $(OBJ)
  109. ./scripts/data.py $^ -S $(DATAFLAGS)
  110. .PHONY: stack
  111. stack: $(CGI)
  112. ./scripts/stack.py $^ -S $(STACKFLAGS)
  113. .PHONY: structs
  114. structs: $(OBJ)
  115. ./scripts/structs.py $^ -S $(STRUCTSFLAGS)
  116. .PHONY: coverage
  117. coverage:
  118. ./scripts/coverage.py $(BUILDDIR)tests/*.toml.info -s $(COVERAGEFLAGS)
  119. .PHONY: summary
  120. summary: $(BUILDDIR)lfs.csv
  121. ./scripts/summary.py -Y $^ $(SUMMARYFLAGS)
  122. # rules
  123. -include $(DEP)
  124. -include $(TEST_TADEP)
  125. .SUFFIXES:
  126. .SECONDARY:
  127. $(BUILDDIR)lfs: $(OBJ)
  128. $(CC) $(CFLAGS) $^ $(LFLAGS) -o $@
  129. $(BUILDDIR)lfs.a: $(OBJ)
  130. $(AR) rcs $@ $^
  131. $(BUILDDIR)lfs.csv: $(OBJ) $(CGI)
  132. ./scripts/code.py $(OBJ) -q $(CODEFLAGS) -o $@
  133. ./scripts/data.py $(OBJ) -q -m $@ $(DATAFLAGS) -o $@
  134. ./scripts/stack.py $(CGI) -q -m $@ $(STACKFLAGS) -o $@
  135. ./scripts/structs.py $(OBJ) -q -m $@ $(STRUCTSFLAGS) -o $@
  136. $(if $(COVERAGE),\
  137. ./scripts/coverage.py $(BUILDDIR)tests/*.toml.info \
  138. -q -m $@ $(COVERAGEFLAGS) -o $@)
  139. $(BUILDDIR)runners/test_runner: $(TEST_TAOBJ)
  140. $(CC) $(CFLAGS) $^ $(LFLAGS) -o $@
  141. # our main build rule generates .o, .d, and .ci files, the latter
  142. # used for stack analysis
  143. $(BUILDDIR)%.o $(BUILDDIR)%.ci: %.c
  144. $(CC) -c -MMD -fcallgraph-info=su $(CFLAGS) $< -o $(BUILDDIR)$*.o
  145. $(BUILDDIR)%.s: %.c
  146. $(CC) -S $(CFLAGS) $< -o $@
  147. $(BUILDDIR)%.a.c: %.c
  148. ./scripts/explode_asserts.py $< -o $@
  149. $(BUILDDIR)%.a.c: $(BUILDDIR)%.c
  150. ./scripts/explode_asserts.py $< -o $@
  151. $(BUILDDIR)%.t.c: %.toml
  152. ./scripts/test.py -c $< $(TESTCFLAGS) -o $@
  153. $(BUILDDIR)%.t.c: %.c $(TESTS)
  154. ./scripts/test.py -c $(TESTS) -s $< $(TESTCFLAGS) -o $@
  155. # clean everything
  156. .PHONY: clean
  157. clean:
  158. rm -f $(BUILDDIR)lfs
  159. rm -f $(BUILDDIR)lfs.a
  160. rm -f $(BUILDDIR)lfs.csv
  161. rm -f $(BUILDDIR)runners/test_runner
  162. rm -f $(OBJ)
  163. rm -f $(CGI)
  164. rm -f $(DEP)
  165. rm -f $(ASM)
  166. rm -f $(TEST_TSRC)
  167. rm -f $(TEST_TASRC)
  168. rm -f $(TEST_TAOBJ)
  169. rm -f $(TEST_TADEP)