PREFIX=/usr
-CFLAGS=-O2 -Wall -g
+INSTALL=install
+CFLAGS+=-std=gnu99 -O2 -Wall -g -MMD -D_FILE_OFFSET_BITS=64 -I.
+LDFLAGS+=-static
-all: make-bcache probe-bcache
+PKGCONFIG_LIBS="blkid uuid"
+CFLAGS+=`pkg-config --cflags ${PKGCONFIG_LIBS}`
+LDLIBS+=`pkg-config --libs ${PKGCONFIG_LIBS}` -lscrypt -lsodium -lkeyutils -lm
-install: make-bcache probe-bcache
- install -m0755 make-bcache ${PREFIX}/sbin/
- install -m0755 probe-bcache /sbin/
- install -m0644 61-bcache.rules /lib/udev/rules.d/
- install -m0755 initramfs /usr/share/initramfs-tools/hooks/bcache
- install -m0644 make-bcache.8 ${PREFIX}/share/man/man8
-# install -m0755 bcache-test ${PREFIX}/sbin/
+ifeq ($(PREFIX),/usr)
+ ROOT_SBINDIR=/sbin
+else
+ ROOT_SBINDIR=$(PREFIX)/sbin
+endif
+.PHONY: all
+all: bcache
+
+CCANSRCS=$(wildcard ccan/*/*.c)
+CCANOBJS=$(patsubst %.c,%.o,$(CCANSRCS))
+
+libccan.a: $(CCANOBJS)
+ $(AR) r $@ $(CCANOBJS)
+
+bcache-objs = bcache.o bcache-assemble.o bcache-device.o bcache-format.o\
+ bcache-fs.o bcache-run.o bcache-key.o libbcache.o crypto.o util.o
+
+-include $(bcache-objs:.o=.d)
+
+bcache: $(bcache-objs) libccan.a
+
+.PHONY: install
+install: bcache
+ mkdir -p $(DESTDIR)$(ROOT_SBINDIR)
+ mkdir -p $(DESTDIR)$(PREFIX)/share/man/man8/
+ $(INSTALL) -m0755 bcache $(DESTDIR)$(ROOT_SBINDIR)
+ $(INSTALL) -m0755 mkfs.bcache $(DESTDIR)$(ROOT_SBINDIR)
+ $(INSTALL) -m0644 bcache.8 $(DESTDIR)$(PREFIX)/share/man/man8/
+
+.PHONY: clean
clean:
- rm -f make-bcache probe-bcache bcache-test *.o
+ $(RM) bcache *.o *.d *.a
-bcache-test: LDLIBS += -lm -lssl -lcrypto
-make-bcache: LDLIBS += -luuid
-make-bcache: bcache.o
-probe-bcache: LDLIBS += -luuid
+.PHONY: deb
+deb: all
+ debuild -nc -us -uc -i -I