Skip to content

Commit

Permalink
Update Makefile to track dependencies better
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-akya committed Jan 11, 2025
1 parent e234523 commit 40d52e2
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions c_src/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
MIX_APP_PATH ?= ..

PREFIX = $(MIX_APP_PATH)/priv
VIX = $(PREFIX)/vix.so

VIX_COMPILATION_MODE ?= PRECOMPILED_NIF_AND_LIBVIPS
VIX_COMPILATION_MODE := $(shell echo "$(VIX_COMPILATION_MODE)" | tr '[:lower:]' '[:upper:]')

# System type and C compiler/flags.

UNAME_SYS := $(shell uname -s)

ifeq ($(UNAME_SYS), Darwin)
CFLAGS += -O2 -Wall -Werror -Wextra -Wno-unused-parameter -Wmissing-prototypes -std=c11
LDFLAGS += -flat_namespace -undefined suppress
Expand All @@ -26,18 +22,15 @@ ifeq ($(VIX_COMPILATION_MODE), PLATFORM_PROVIDED_LIBVIPS)
LDLIBS += `pkg-config vips --libs`
else
PRECOMPILED_LIBVIPS_PATH = $(PREFIX)/precompiled_libvips

ifeq ($(VIX_COMPILATION_MODE), PRECOMPILED_LIBVIPS)
# force fetching libvips if previously configured to use PRECOMPILED_NIF_AND_LIBVIPS
# since the precompiled vix does not bundle `include`
PRECOMPILED_LIBVIPS_PREREQUISITE = $(PRECOMPILED_LIBVIPS_PATH)/include
else
PRECOMPILED_LIBVIPS_PREREQUISITE = $(PRECOMPILED_LIBVIPS_PATH)
endif

CFLAGS += -pthread -I $(PRECOMPILED_LIBVIPS_PATH)/include -I $(PRECOMPILED_LIBVIPS_PATH)/lib/glib-2.0/include -I $(PRECOMPILED_LIBVIPS_PATH)/include/glib-2.0
LDLIBS += -L $(PRECOMPILED_LIBVIPS_PATH)/lib

ifeq ($(UNAME_SYS), Darwin)
LDFLAGS += -Wl,-rpath,@loader_path/precompiled_libvips/lib
LDLIBS += -lvips.42
Expand All @@ -48,19 +41,18 @@ else
endif

# Verbosity

c_verbose_0 = @echo " C " $(?F);
c_verbose = $(c_verbose_$(V))

link_verbose_0 = @echo " LD " $(@F);
link_verbose = $(link_verbose_$(V))

# Source files and dependency management
SRC = $(shell find . -type f -name "*.c")

# Object files are put in `c_src` directory.
# Maybe we should put in `${MIX_APP_PATH}/priv`?
# but then we need to compile NIF for each env
OBJ = $(addsuffix .o,$(basename $(SRC)))
DEP = $(OBJ:.o=.d)

# Add dependency generation to CFLAGS
CFLAGS += -MMD -MP

calling_from_make:
cd .. && mix compile
Expand All @@ -70,10 +62,12 @@ all: install

install: $(PREFIX) $(VIX)

$(OBJ): Makefile $(PRECOMPILED_LIBVIPS_PREREQUISITE)
# Include generated dependency files
-include $(DEP)

%.o: %.c
$(c_verbose) $(CC) -c $(CFLAGS) -o $@ $<
# Pattern rule for object files with prerequisite
%.o: %.c $(PRECOMPILED_LIBVIPS_PREREQUISITE)
$(c_verbose) $(CC) $(CFLAGS) -c -o $@ $<

$(VIX): $(OBJ)
$(link_verbose) $(CC) $^ -o $@ $(LDFLAGS) $(LDLIBS)
Expand All @@ -85,7 +79,7 @@ $(PRECOMPILED_LIBVIPS_PREREQUISITE):
@MIX_EXS="../mix.exs" mix run --no-start --no-compile --no-deps-check ../build_scripts/precompiler.exs

clean:
$(RM) $(VIX) $(OBJ)
$(RM) $(VIX) $(OBJ) $(DEP)

clean_precompiled_libvips: clean
$(RM) $(PREFIX)/libvips-*.tar.gz
Expand Down

0 comments on commit 40d52e2

Please sign in to comment.