Makefile 2.2 KB

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