From patchwork Fri Feb 8 23:19:29 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Kumlien X-Patchwork-Id: 2119001 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 9052FDFE75 for ; Fri, 8 Feb 2013 23:23:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1947294Ab3BHXXV (ORCPT ); Fri, 8 Feb 2013 18:23:21 -0500 Received: from mail.vapor.com ([83.220.149.2]:48397 "EHLO nitrogen.vapor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757638Ab3BHXXU (ORCPT ); Fri, 8 Feb 2013 18:23:20 -0500 Received: from twilight.demius.net (c-297271d5.013-195-6c756e10.cust.bredbandsbolaget.se [213.113.114.41]) by nitrogen.vapor.com (Postfix) with ESMTPSA id C0DD140C023 for ; Sat, 9 Feb 2013 00:23:18 +0100 (CET) Received: from lori.pomac.com (lori.local [10.0.0.13]) by twilight.demius.net (Postfix) with ESMTP id CED0B8E04E5; Sat, 9 Feb 2013 00:23:14 +0100 (CET) From: Ian Kumlien To: linux-btrfs@vger.kernel.org Cc: Ian Kumlien Subject: [PATCH] Btrfs-progs: add static compile target Date: Sat, 9 Feb 2013 00:19:29 +0100 Message-Id: <1360365569-18996-1-git-send-email-pomac@demius.net> X-Mailer: git-send-email 1.8.1.2 X-Virus-Scanned: clamav-milter 0.97.6 at twilight.demius.net X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=5.0 tests=UNPARSEABLE_RELAY autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on twilight.pomac.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org 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 --- This is v2 of the patch, David Sterba raised some questions on IRC and quite correctly pointed out severe problems with the old patch. The old patch depended on you doing make static from a clean state, any objects compiled dynamically would ruin the resulting binary. This version separates all objects and the resulting binary ;) .gitignore | 2 ++ Makefile | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0e560d5..230dfbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,10 @@ *.o +*.static.o .*.o.d version.h man/*.gz btrfs +btrfs.static btrfs-debug-tree btrfs-map-logical btrfs-show diff --git a/Makefile b/Makefile index d9a05f8..d40a9b4 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,16 @@ progs = btrfsctl mkfs.btrfs btrfs-show btrfs-vol btrfs \ btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \ btrfs-find-root btrfstune +# 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) -static -ffunction-sections -fdata-sections +STATIC_LDFLAGS = -Wl,--gc-sections +STATIC_LIBS = $(LIBS) -lpthread + # make C=1 to enable sparse ifdef C check = sparse $(CHECKFLAGS) @@ -52,9 +62,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 @@ -63,6 +82,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) @@ -135,7 +159,7 @@ install-man: clean : @echo "Cleaning" - $(Q)rm -f $(progs) cscope.out *.o .*.d btrfs-convert btrfs-image \ + $(Q)rm -f $(progs) cscope.out *.o .*.d btrfs-convert btrfs-image btrfs.static \ btrfs-zero-log btrfstune dir-test ioctl-test quick-test version.h $(Q)$(MAKE) $(MAKEOPTS) -C man $@