Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# --- Basic Configuration ---
CC ?= gcc
CFLAGS += -std=c99 -Wall -Wextra -pedantic -MMD -ggdb
CFLAGS += -std=c99 -Wall -Wextra -pedantic -ggdb -Iinclude/f3

TARGETS = f3write f3read
EXTRA_TARGETS = f3probe f3brew f3fix
Expand All @@ -9,6 +10,7 @@ INSTALL = install
LN = ln
UNLINK = unlink

# --- OS Detection ---
ifndef OS
OS = $(shell uname -s)
endif
Expand All @@ -23,22 +25,56 @@ ifneq ($(OS), Linux)
LDFLAGS += -L$(ARGP)/lib -largp
endif

all: $(TARGETS)
extra: $(EXTRA_TARGETS)
# --- Output Directory Setup ---
BUILD_DIR := build
BIN_DIR := $(BUILD_DIR)/bin
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new binaries' location breaks the GitHub workflow. Please update the workflow file .github/workflows/test.yml.

OBJ_DIR := $(BUILD_DIR)/obj

BIN_TARGETS := $(addprefix $(BIN_DIR)/,$(TARGETS))
BIN_EXTRAS := $(addprefix $(BIN_DIR)/,$(EXTRA_TARGETS))

# Source directories and automatic search rules
SRC_DIRS = src/f3 src/f3-extra
vpath %.c $(SRC_DIRS)

# Find source files relative to source root and map them to object paths in OBJ_DIR
TARGET_SRCS := $(wildcard $(addsuffix /*.c, $(SRC_DIRS)))
TARGET_OBJS := $(patsubst src/%.c,$(OBJ_DIR)/%.o,$(TARGET_SRCS))

# Reusable object lists
F3_LIB_OBJS := $(patsubst src/%.c,$(OBJ_DIR)/%.o,$(wildcard src/f3/lib/*.c))
F3_EXTRA_LIB_OBJS := $(patsubst src/%.c,$(OBJ_DIR)/%.o,$(wildcard src/f3-extra/lib/*.c))

ALL_OBJS := $(TARGET_OBJS) $(F3_LIB_OBJS) $(F3_EXTRA_LIB_OBJS)

# --- Dependency Inclusion ---
DEPFLAGS = -MMD -MT $@ -MP -MF $(@:.o=.d)
ALL_DEPS := $(ALL_OBJS:.o=.d)
-include $(ALL_DEPS)

# --- Pattern Rule for Compilation ---
$(OBJ_DIR)/%.o: src/%.c | $(OBJ_DIR)
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@

# --- Main Build Targets ---
all: $(BIN_TARGETS)
extra: $(BIN_EXTRAS)

docker:
docker build -f Dockerfile -t f3:latest .

# --- Installation Targets ---
install: all
$(INSTALL) -d $(DESTDIR)$(PREFIX)/bin
$(INSTALL) -m755 $(TARGETS) $(DESTDIR)$(PREFIX)/bin
$(INSTALL) -m755 $(BIN_TARGETS) $(DESTDIR)$(PREFIX)/bin
$(INSTALL) -d $(DESTDIR)$(PREFIX)/share/man/man1
$(INSTALL) -m644 f3read.1 $(DESTDIR)$(PREFIX)/share/man/man1
$(LN) -sf f3read.1 $(DESTDIR)$(PREFIX)/share/man/man1/f3write.1

install-extra: extra
$(INSTALL) -d $(DESTDIR)$(PREFIX)/bin
$(INSTALL) -m755 $(EXTRA_TARGETS) $(DESTDIR)$(PREFIX)/bin
$(INSTALL) -m755 $(BIN_EXTRAS) $(DESTDIR)$(PREFIX)/bin

uninstall: uninstall-extra
cd $(DESTDIR)$(PREFIX)/bin ; rm $(TARGETS)
Expand All @@ -48,27 +84,33 @@ uninstall: uninstall-extra
uninstall-extra:
cd $(DESTDIR)$(PREFIX)/bin ; rm $(EXTRA_TARGETS)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the changes, have you tested the targets uninstall and uninstall-extra?


f3write: utils.o libflow.o f3write.o
# --- Directory Targets ---
$(BUILD_DIR): ; @mkdir -p $@
$(BIN_DIR): | $(BUILD_DIR) ; @mkdir -p $@
$(OBJ_DIR): | $(BUILD_DIR) ; @mkdir -p $@

# --- Binary Linking Rules ---
$(BIN_DIR)/f3write: $(OBJ_DIR)/f3/f3write.o $(F3_LIB_OBJS) | $(BIN_DIR)
$(CC) -o $@ $^ $(LDFLAGS) -lm

f3read: utils.o libflow.o f3read.o
$(BIN_DIR)/f3read: $(OBJ_DIR)/f3/f3read.o $(F3_LIB_OBJS) | $(BIN_DIR)
$(CC) -o $@ $^ $(LDFLAGS) -lm

f3probe: libutils.o libdevs.o libprobe.o f3probe.o
$(BIN_DIR)/f3probe: $(OBJ_DIR)/f3-extra/f3probe.o $(F3_EXTRA_LIB_OBJS) | $(BIN_DIR)
$(CC) -o $@ $^ $(LDFLAGS) -lm -ludev

f3brew: libutils.o libdevs.o f3brew.o
$(BIN_DIR)/f3brew: $(OBJ_DIR)/f3-extra/f3brew.o $(F3_EXTRA_LIB_OBJS) | $(BIN_DIR)
$(CC) -o $@ $^ $(LDFLAGS) -lm -ludev

f3fix: libutils.o f3fix.o
$(BIN_DIR)/f3fix: $(OBJ_DIR)/f3-extra/f3fix.o $(F3_EXTRA_LIB_OBJS) | $(BIN_DIR)
$(CC) -o $@ $^ $(LDFLAGS) -lparted

-include *.d

.PHONY: cscope clean uninstall uninstall-extra
# --- Cleanup and Maintenance Targets ---
.PHONY: all extra docker install install-extra uninstall uninstall-extra cscope clean

cscope:
cscope -b *.c *.h
cscope -b src/**/*.c include/**/*.h

clean:
rm -f *.o *.d cscope.out $(TARGETS) $(EXTRA_TARGETS)
@rm -rf $(BUILD_DIR)
@rm -f cscope.out
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, libflow.c is only used in f3write and f3read, but the goal is to eventually use it in f3brew as well. See my comment here for more context. With this proposed change, how could libflow.c be shared between f3 and f3-extra?

File renamed without changes.
File renamed without changes.
Loading