Makefile 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. CC = gcc
  2. srcdir = src
  3. testdir = test
  4. objdir = build
  5. target = tgnews
  6. target_trainer = tgnews_trainer
  7. target_lib = $(objdir)/libtgnews.a
  8. LDFLAGS := -L$(objdir) \
  9. -l$(subst lib,,$(basename $(notdir $(target_lib))))
  10. CFLAGS := -std=c11 -Wall -Wextra -Wpedantic -O0
  11. includes_lib := -I$(srcdir)
  12. src := $(wildcard $(srcdir)/*/*.c)
  13. src_target_trainer := $(srcdir)/main_trainer.c
  14. src_target := $(srcdir)/main.c
  15. src_test := $(wildcard $(testdir)/*/*.c)
  16. obj_target_trainer := $(src_target_trainer:$(srcdir)/%.c=$(objdir)/%.o)
  17. obj_target := $(src_target:$(srcdir)/%.c=$(objdir)/%.o)
  18. obj := $(src:$(srcdir)/%.c=$(objdir)/%.o)
  19. obj_test := $(src_test:$(testdir)/%.c=$(objdir)/test/%.o)
  20. obj_all := $(obj) $(obj_target) $(obj_target_trainer)
  21. dep := $(obj:%.o=%.d)
  22. dep_test := $(obj_test:%.o=%.d)
  23. all: output_directories $(target_lib) $(target) $(target_trainer)
  24. debug: $(eval CFLAGS += -g) all
  25. test_debug: $(eval CFLAGS += -g) test
  26. output_directories:
  27. @mkdir -p $(objdir) $(dir $(obj))
  28. output_directories_test:
  29. @mkdir -p $(objdir) $(dir $(obj_test))
  30. $(target_lib): $(obj)
  31. ar rcs $@ $^
  32. $(target_trainer): $(obj_target_trainer) $(target_lib)
  33. $(CC) -o $@ $^ $(LDFLAGS)
  34. $(target): $(obj_target) $(target_lib)
  35. $(CC) -o $@ $^ $(LDFLAGS)
  36. target_parser_tests := $(shell basename $(src_test) .c )
  37. $(target_parser_tests): $(obj_test) $(target_lib)
  38. $(CC) -o $@ $^ $(LDFLAGS)
  39. test: output_directories output_directories_test $(target_parser_tests)
  40. -include $(dep)
  41. $(obj_all): $(objdir)/%.o : $(srcdir)/%.c
  42. $(CC) $(CFLAGS) -o $@ -MMD -c $< $(includes_lib)
  43. -include $(dep_test)
  44. $(obj_test): $(objdir)/%.o : %.c
  45. $(CC) $(CFLAGS) -o $@ -MMD -c $< $(includes_lib)
  46. .PHONY: clean output_directories debug
  47. clean:
  48. @rm -fr build
  49. @rm -f $(target) $(target_trainer) $(target_parser_tests)