From 7ab8e7c7fb5666a55da4f70ea49be872f59d79c2 Mon Sep 17 00:00:00 2001 From: "Jason W. Bacon" Date: Sat, 21 Jun 2025 08:23:00 -0500 Subject: [PATCH] Makefile: Respect build environment, add destdir support These patches have been tested with FreeBSD ports and dreckly package manager on numerous platforms --- Makefile | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 3896861a..23b3e9b1 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,8 @@ BINDIR ?= $(PREFIX)/bin INCLUDE_DIRS ?= LIBRARY_DIRS ?= +STRIP ?= strip + SRC := $(wildcard ${DIR_SRC}/*.cpp) OBJ := $(patsubst %.cpp,${DIR_OBJ}/%.o,$(notdir ${SRC})) @@ -15,15 +17,21 @@ TARGET := fastp BIN_TARGET := ${TARGET} CXX ?= g++ -CXXFLAGS := -std=c++11 -pthread -g -O3 -MD -MP -I${DIR_INC} $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir)) ${CXXFLAGS} +# Optional flags that the user can override by setting CXXFLAGS in the +# env or make argument. -pthread is a link flag, and serves no purpose +# in the compile command. It is handled by -lpthread in LIBS. +CXXFLAGS ?= -g -O3 -MD -MP +# Append required flags to standard CXXFLAGS from env +CXXFLAGS += -std=c++11 -I${DIR_INC} $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir)) LIBS := -lisal -ldeflate -lpthread STATIC_FLAGS := -static -Wl,--no-as-needed -pthread -LD_FLAGS := $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(LIBS) $(LD_FLAGS) +# Append required flags to standard LDFLAGS from env +LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(LIBS) STATIC_LD_FLAGS := $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) $(STATIC_FLAGS) $(LIBS) $(STATIC_LD_FLAGS) ${BIN_TARGET}:${OBJ} - $(CXX) $(OBJ) -o $@ $(LD_FLAGS) + $(CXX) $(OBJ) -o $@ $(LDFLAGS) static:${OBJ} $(CXX) $(OBJ) -o ${BIN_TARGET} $(STATIC_LD_FLAGS) @@ -38,8 +46,16 @@ clean: @rm -rf $(DIR_OBJ) @rm -f $(TARGET) +# Respect DESTDIR for staged installs (used by most package managers). +# DESTDIR is empty by default, so this will install directly to BINDIR +# unless DESTDIR is supplied by the user. install: - install $(TARGET) $(BINDIR)/$(TARGET) + mkdir -p $(DESTDIR)$(BINDIR) + install $(TARGET) $(DESTDIR)$(BINDIR) @echo "Installed." +# Many package managers use install-strip target if debugging is not enabled +install-strip: install + $(STRIP) $(DESTDIR)$(BINDIR)/$(TARGET) + -include $(OBJ:.o=.d)