Makefile 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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)tests))
  10. endif
  11. # overridable target/src/tools/flags/etc
  12. ifneq ($(wildcard test.c main.c),)
  13. TARGET ?= $(BUILDDIR)lfs
  14. else
  15. TARGET ?= $(BUILDDIR)lfs.a
  16. endif
  17. CC ?= gcc
  18. AR ?= ar
  19. SIZE ?= size
  20. CTAGS ?= ctags
  21. NM ?= nm
  22. LCOV ?= lcov
  23. SRC ?= $(wildcard *.c)
  24. OBJ := $(SRC:%.c=$(BUILDDIR)%.o)
  25. DEP := $(SRC:%.c=$(BUILDDIR)%.d)
  26. ASM := $(SRC:%.c=$(BUILDDIR)%.s)
  27. ifdef DEBUG
  28. override CFLAGS += -O0 -g3
  29. else
  30. override CFLAGS += -Os
  31. endif
  32. ifdef TRACE
  33. override CFLAGS += -DLFS_YES_TRACE
  34. endif
  35. override CFLAGS += -I.
  36. override CFLAGS += -std=c99 -Wall -pedantic
  37. override CFLAGS += -Wextra -Wshadow -Wjump-misses-init -Wundef
  38. ifdef VERBOSE
  39. override TESTFLAGS += -v
  40. override CODEFLAGS += -v
  41. override COVERAGEFLAGS += -v
  42. endif
  43. ifdef EXEC
  44. override TESTFLAGS += --exec="$(EXEC)"
  45. endif
  46. ifdef BUILDDIR
  47. override TESTFLAGS += --build-dir="$(BUILDDIR:/=)"
  48. override CODEFLAGS += --build-dir="$(BUILDDIR:/=)"
  49. endif
  50. ifneq ($(NM),nm)
  51. override CODEFLAGS += --nm-tool="$(NM)"
  52. endif
  53. # commands
  54. .PHONY: all build
  55. all build: $(TARGET)
  56. .PHONY: asm
  57. asm: $(ASM)
  58. .PHONY: size
  59. size: $(OBJ)
  60. $(SIZE) -t $^
  61. .PHONY: tags
  62. tags:
  63. $(CTAGS) --totals --c-types=+p $(shell find -H -name '*.h') $(SRC)
  64. .PHONY: code
  65. code: $(OBJ)
  66. ./scripts/code.py $^ $(CODEFLAGS)
  67. .PHONY: test
  68. test:
  69. ./scripts/test.py $(TESTFLAGS)
  70. .SECONDEXPANSION:
  71. test%: tests/test$$(firstword $$(subst \#, ,%)).toml
  72. ./scripts/test.py $@ $(TESTFLAGS)
  73. .PHONY: coverage
  74. coverage:
  75. ./scripts/coverage.py $(BUILDDIR)tests/*.toml.info $(COVERAGEFLAGS)
  76. # rules
  77. -include $(DEP)
  78. .SUFFIXES:
  79. $(BUILDDIR)lfs: $(OBJ)
  80. $(CC) $(CFLAGS) $^ $(LFLAGS) -o $@
  81. $(BUILDDIR)%.a: $(OBJ)
  82. $(AR) rcs $@ $^
  83. $(BUILDDIR)%.o: %.c
  84. $(CC) -c -MMD $(CFLAGS) $< -o $@
  85. $(BUILDDIR)%.s: %.c
  86. $(CC) -S $(CFLAGS) $< -o $@
  87. # clean everything
  88. .PHONY: clean
  89. clean:
  90. rm -f $(TARGET)
  91. rm -f $(OBJ)
  92. rm -f $(DEP)
  93. rm -f $(ASM)
  94. rm -f $(BUILDDIR)tests/*.toml.*