본문으로 건너뛰기

Debian package Makefile

Basic

.PHONY: all
all:

.PHONY: install
install:

.PHONY: clean
clean:

.PHONY: distclean
distclean: clean

.PHONY: uninstall
uninstall:

Dynamic library

Ref: https://github.com/hhk7734/lot-SBC/blob/master/Makefile

prefix = /usr

VERSION = $(shell head -n1 debian/changelog | sed 's/.* .\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/')
MAJOR = $(shell echo $(VERSION) | sed 's/\([0-9]*\)\..*/\1/')

DYNAMIC_LIB = lib<package>.so

BUILD_DIR = build

CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
LD = $(CROSS_COMPILE)ld

DEBUG = -g
COMMON_FLAGS = -Wall -fPIC $(DEBUG)
COMMON_FLAGS += -O1
COMMON_FLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

CFLAGS = $(COMMON_FLAGS)

CXXFLAGS = $(COMMON_FLAGS)
CXXFLAGS += -std=c++11

LIBS =

rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))

CSRCS =

CXXSRCS =

INCS_DIR = -I.

DEV_HEADERS = $(shell ls <path>/*.h)
DEV_HEADERS += $(shell ls <path>/*.hpp)

.PHONY: all
all: $(DYNAMIC_LIB).$(VERSION)

$(BUILD_DIR):
mkdir $@

vpath %.c $(sort $(dir $(CSRCS)))
$(BUILD_DIR)/%.o: %.c | $(BUILD_DIR)
$(CC) $(CFLAGS) $(INCS_DIR) -Wa,-a,-ad,-alms=$(@:.o=.lst) -c -o $@ $<

vpath %.cpp $(sort $(dir $(CXXSRCS)))
$(BUILD_DIR)/%.o: %.cpp | $(BUILD_DIR)
$(CXX) $(CXXFLAGS) $(INCS_DIR) -Wa,-a,-ad,-alms=$(@:.o=.lst) -c -o $@ $<

OBJS = $(addprefix $(BUILD_DIR)/,$(notdir $(patsubst %.c,%.o,$(CSRCS))))
OBJS += $(addprefix $(BUILD_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(CXXSRCS))))
OBJS += $(call rwildcard,obj,*.o)

$(DYNAMIC_LIB).$(VERSION): $(OBJS)
$(LD) -shared $(LIBS) $^ -o $(BUILD_DIR)/$@

-include $(wildcard $(BUILD_DIR)/*.d)

.PHONY: install
install:
install -m 0755 -d $(DESTDIR)$(prefix)/include/
install -m 0644 $(DEV_HEADERS) $(DESTDIR)$(prefix)/include/
install -m 0755 -d $(DESTDIR)$(prefix)/lib
install -m 0755 $(BUILD_DIR)/$(DYNAMIC_LIB).$(VERSION) $(DESTDIR)$(prefix)/lib
ln -sf $(DYNAMIC_LIB).$(VERSION) $(DESTDIR)$(prefix)/lib/$(DYNAMIC_LIB).$(MAJOR)
ln -sf $(DYNAMIC_LIB).$(MAJOR) $(DESTDIR)$(prefix)/lib/$(DYNAMIC_LIB)

.PHONY: clean
clean:
rm -rf $(BUILD_DIR)

.PHONY: distclean
distclean: clean

.PHONY: uninstall
uninstall:
rm -f $(addprefix $(DESTDIR)$(prefix)/include/,$(notdir $(DEV_HEADERS)))
rm -f $(DESTDIR)$(prefix)/lib/lib<package>.*

.PHONY: clang-format
clang: $(call rwildcard,,*.c) $(call rwildcard,,*.cpp) $(call rwildcard,,*.h) $(call rwildcard,,*.hpp)
clang-format -style=file -i -verbose $^