Btrfs-progs: add static compile target
authorIan Kumlien <pomac@demius.net>
Fri, 25 Jan 2013 23:12:28 +0000 (00:12 +0100)
committerDavid Sterba <dsterba@suse.cz>
Tue, 12 Feb 2013 14:22:55 +0000 (15:22 +0100)
Sometimes, when you least expect it, a static binary is what you need to
rescue your data... Or just get a good enough handle on things to make
it work again ;)

"make static" is a gift to you, dear user with filesystem problems!

Anyway, on a more serious note, changed the cflags and ldflags so that
we create a smaller binary, 1.1MB stripped on my 64 bit system
(2.7MB with debug data)

Signed-off-by: Ian Kumlien <pomac@demius.net>
.gitignore
Makefile

index 0e560d5..230dfbd 100644 (file)
@@ -1,8 +1,10 @@
 *.o
+*.static.o
 .*.o.d
 version.h
 man/*.gz
 btrfs
+btrfs.static
 btrfs-debug-tree
 btrfs-map-logical
 btrfs-show
index 596bf93..d9dd07a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -39,6 +39,16 @@ progs = btrfsctl mkfs.btrfs btrfs-debug-tree btrfs-show btrfs-vol btrfsck \
        btrfs btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \
        btrfs-find-root btrfs-restore btrfstune btrfs-show-super
 
+# Create all the static targets
+static_objects = $(patsubst %.o, %.static.o, $(objects))
+static_cmds_objects = $(patsubst %.o, %.static.o, $(cmds_objects))
+static_progs = $(patsubst %.o, %.static.o, $(progs))
+
+# Define static compilation flags
+STATIC_CFLAGS = $(CFLAGS) -ffunction-sections -fdata-sections
+STATIC_LDFLAGS = -static -Wl,--gc-sections
+STATIC_LIBS = $(LIBS) -lpthread
+
 # make C=1 to enable sparse
 ifdef C
        check = sparse $(CHECKFLAGS)
@@ -51,9 +61,18 @@ endif
        @echo "    [CC]     $@"
        $(Q)$(CC) $(DEPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c $<
 
+%.static.o: %.c
+       @echo "    [CC]     $@"
+       $(Q)$(CC) $(DEPFLAGS) $(AM_CFLAGS) $(STATIC_CFLAGS) -c $< -o $@
 
 all: version.h $(progs) manpages
 
+#
+# NOTE: For static compiles, you need to have all the required libs
+#      static equivalent available
+#
+static: version.h btrfs.static
+
 version.h:
        $(Q)bash version.sh
 
@@ -62,6 +81,11 @@ btrfs: $(objects) btrfs.o help.o $(cmds_objects)
        $(Q)$(CC) $(CFLAGS) -o btrfs btrfs.o help.o $(cmds_objects) \
                $(objects) $(LDFLAGS) $(LIBS) -lpthread
 
+btrfs.static: $(static_objects) btrfs.static.o help.static.o $(static_cmds_objects)
+       @echo "    [LD]     $@"
+       $(Q)$(CC) $(STATIC_CFLAGS) -o btrfs.static btrfs.static.o help.static.o $(static_cmds_objects) \
+               $(static_objects) $(STATIC_LDFLAGS) $(STATIC_LIBS)
+
 calc-size: $(objects) calc-size.o
        @echo "    [LD]     $@"
        $(Q)$(CC) $(CFLAGS) -o calc-size calc-size.o $(objects) $(LDFLAGS) $(LIBS)
@@ -151,7 +175,8 @@ install-man:
 clean :
        @echo "Cleaning"
        $(Q)rm -f $(progs) cscope.out *.o .*.d btrfs-convert btrfs-image btrfs-select-super \
-             btrfs-zero-log btrfstune dir-test ioctl-test quick-test version.h
+             btrfs-zero-log btrfstune dir-test ioctl-test quick-test btrfs.static \
+             version.h
        $(Q)$(MAKE) $(MAKEOPTS) -C man $@
 
 install: $(progs) install-man