Skip to content

Commit

Permalink
Simplify makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
leonmavr committed Sep 27, 2024
1 parent bc75d76 commit 36a9cbb
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,30 @@ TEST_DIR = test
CFLAGS = -g -I$(INC_DIR) -Wall
LDFLAGS = -lm
TEST_SRC = $(wildcard $(TEST_DIR)/*.c)
TARGET_SRC = $(wildcard $(TARGET_DIR)/*.c)

# If `test` is passed as a cmd argument, extend flags to handle unit tests
ifeq ($(MAKECMDGOALS), test)
SRC = $(wildcard $(SRC_DIR)/*.c)
TARGETS = $(patsubst $(TEST_DIR)/%.c,%,$(TEST_SRC))
TARGET_DIR = test
OBJECTS = $(SRC:%.c=%.o) $(TEST_SRC)
# If `test` is passed as a cmd argument, extend flags to handle unit tests
TARGET_SRC = $(wildcard $(TEST_DIR)/*.c)
TARGETS = $(patsubst $(TEST_DIR)/%.c,%,$(TEST_SRC))
TARGET_DIR = test
else
# Strip file path so each demo source gets a target,
# e.g. examples/01_demo.c -> 01_demo
TARGETS = $(patsubst $(TARGET_DIR)/%.c, %, $(TARGET_SRC))
SRC = $(wildcard $(SRC_DIR)/*.c)
OBJECTS = $(SRC:%.c=%.o)
# Strip file path so each demo source gets a target,
# e.g. examples/01_demo.c -> 01_demo
TARGET_SRC = $(wildcard $(TARGET_DIR)/*.c)
TARGETS = $(patsubst $(TARGET_DIR)/%.c, %, $(TARGET_SRC))
TARGET_DIR = examples
endif


# What to do by default (no arguments)
all: $(TARGETS)
test: all

$(TARGETS): %: $(TARGET_DIR)/%.c $(wildcard $(SRC_DIR)/*.c)
$(CC) $(CFLAGS) $(SRC_DIR)/*.c $< -o $@ $(LDFLAGS)

%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@

test: all
.PHONY: clean

RM = rm -rf
clean:
$(RM) $(TARGETS) $(SRC_DIR)/*.o test/*.o tests
$(RM) $(TARGETS) $(SRC_DIR)/*.o $(TARGET_DIR)/*.o tests

0 comments on commit 36a9cbb

Please sign in to comment.