Austin Schuh | f417eaf | 2019-09-16 21:58:36 -0700 | [diff] [blame] | 1 | c_sources := jsont.c |
| 2 | |
| 3 | all: example1 example2 test |
| 4 | |
| 5 | object_dir = .objs |
| 6 | objects = $(patsubst %,$(object_dir)/%,${c_sources:.c=.o}) |
| 7 | object_dirs = $(sort $(foreach fn,$(objects),$(dir $(fn)))) |
| 8 | -include ${objects:.o=.d} |
| 9 | |
| 10 | test_dir = test |
| 11 | test_sources := $(wildcard test/test*.c) |
| 12 | test_object_dir = $(test_dir)/.objs |
| 13 | test_build_dir = $(test_dir)/build |
| 14 | test_objects = $(patsubst test/%,$(test_object_dir)/%,${test_sources:.c=.o}) |
| 15 | test_programs = $(patsubst test/%.c,$(test_build_dir)/%,$(test_sources)) |
| 16 | test_object_dirs = $(sort $(foreach fn,$(test_objects),$(dir $(fn)))) |
| 17 | |
| 18 | CC = clang |
| 19 | LD = clang |
| 20 | |
| 21 | CFLAGS += -Wall -g -MMD -std=c99 -I. |
| 22 | TEST_CFLAGS := $(CFLAGS) -O0 |
| 23 | #LDFLAGS += |
| 24 | ifneq ($(DEBUG),) |
| 25 | CFLAGS += -O0 -DDEBUG=1 |
| 26 | else |
| 27 | CFLAGS += -O3 -DNDEBUG |
| 28 | endif |
| 29 | |
| 30 | clean: |
| 31 | rm -f jsont example1 example2 |
| 32 | rm -rf $(object_dir) |
| 33 | rm -rf $(test_object_dir) |
| 34 | rm -rf $(test_build_dir) |
| 35 | |
| 36 | example1: $(objects) $(object_dir)/example1.o |
| 37 | $(LD) $(LDFLAGS) -o $@ $^ |
| 38 | |
| 39 | example2: $(objects) $(object_dir)/example2.o |
| 40 | $(LD) $(LDFLAGS) -o $@ $^ |
| 41 | |
| 42 | test: $(objects) $(test_programs) |
| 43 | $(test_programs) |
| 44 | |
| 45 | $(test_build_dir)/%: $(objects) $(test_object_dir)/%.o |
| 46 | @mkdir -p `dirname $@` |
| 47 | $(LD) $(LDFLAGS) -o $@ $^ |
| 48 | |
| 49 | $(test_object_dir)/%.o: $(test_dir)/%.c |
| 50 | @mkdir -p `dirname $@` |
| 51 | $(CC) $(TEST_CFLAGS) -c -o $@ $< |
| 52 | |
| 53 | $(object_dir)/%.o: %.c |
| 54 | @mkdir -p `dirname $@` |
| 55 | $(CC) $(CFLAGS) -c -o $@ $< |
| 56 | |
| 57 | .PHONY: clean all test |