All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Btrfs-progs: add static compile target
@ 2013-02-08 23:19 Ian Kumlien
  2013-02-09 17:57 ` Sergei Trofimovich
  2013-02-12 13:55 ` David Sterba
  0 siblings, 2 replies; 6+ messages in thread
From: Ian Kumlien @ 2013-02-08 23:19 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Ian Kumlien

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>
---

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 $@
 
-- 
1.8.1.2


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] Btrfs-progs: add static compile target
  2013-02-08 23:19 [PATCH] Btrfs-progs: add static compile target Ian Kumlien
@ 2013-02-09 17:57 ` Sergei Trofimovich
  2013-02-09 18:51   ` Ian Kumlien
  2013-02-12 13:55 ` David Sterba
  1 sibling, 1 reply; 6+ messages in thread
From: Sergei Trofimovich @ 2013-02-09 17:57 UTC (permalink / raw)
  To: Ian Kumlien; +Cc: linux-btrfs

[-- Attachment #1: Type: text/plain, Size: 570 bytes --]

On Sat,  9 Feb 2013 00:19:29 +0100
Ian Kumlien <pomac@demius.net> wrote:

> 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!

What's wrong with conventional LDFLAGS=-static make?

> 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)
> 

-- 

  Sergei

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Btrfs-progs: add static compile target
  2013-02-09 17:57 ` Sergei Trofimovich
@ 2013-02-09 18:51   ` Ian Kumlien
  2013-02-09 19:48     ` Sergei Trofimovich
  0 siblings, 1 reply; 6+ messages in thread
From: Ian Kumlien @ 2013-02-09 18:51 UTC (permalink / raw)
  To: Sergei Trofimovich; +Cc: Ian Kumlien, linux-btrfs

On Sat, Feb 09, 2013 at 08:57:42PM +0300, Sergei Trofimovich wrote:
> On Sat,  9 Feb 2013 00:19:29 +0100
> Ian Kumlien <pomac@demius.net> wrote:
> 
> > 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!
> 
> What's wrong with conventional LDFLAGS=-static make?

That doesn't work for me, does it actually work for you?

I'd say it wouldn't without adding LDFLAGS so that the compiler gets it
for all object compilations.

> > 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)
> > 
> 
> -- 
> 
>   Sergei



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Btrfs-progs: add static compile target
  2013-02-09 18:51   ` Ian Kumlien
@ 2013-02-09 19:48     ` Sergei Trofimovich
  2013-02-09 22:21       ` Ian Kumlien
  0 siblings, 1 reply; 6+ messages in thread
From: Sergei Trofimovich @ 2013-02-09 19:48 UTC (permalink / raw)
  To: Ian Kumlien; +Cc: linux-btrfs

[-- Attachment #1: Type: text/plain, Size: 1156 bytes --]

On Sat, 9 Feb 2013 19:51:44 +0100
Ian Kumlien <pomac@vapor.com> wrote:

> On Sat, Feb 09, 2013 at 08:57:42PM +0300, Sergei Trofimovich wrote:
> > On Sat,  9 Feb 2013 00:19:29 +0100
> > Ian Kumlien <pomac@demius.net> wrote:
> > 
> > > 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!
> > 
> > What's wrong with conventional LDFLAGS=-static make?
> 
> That doesn't work for me, does it actually work for you?
> 
> I'd say it wouldn't without adding LDFLAGS so that the compiler gets it
> for all object compilations.

-static is link-only flag.
Mostly yes, it works.
    LDFLAGS="-static -pthread" make

make all binaries static.

It needs
 sys-apps/util-linux
 sys-libs/e2fsprogs-libs
 sys-fs/e2fsprogs
 sys-libs/zlib
to be built with USE=static-libs .

    $ ldd btrfs
        not a dynamic executable
    $ ldd mkfs.btrfs 
        not a dynamic executable

None of built binaries is dynamic.

-- 

  Sergei

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Btrfs-progs: add static compile target
  2013-02-09 19:48     ` Sergei Trofimovich
@ 2013-02-09 22:21       ` Ian Kumlien
  0 siblings, 0 replies; 6+ messages in thread
From: Ian Kumlien @ 2013-02-09 22:21 UTC (permalink / raw)
  To: Sergei Trofimovich; +Cc: linux-btrfs

On Sat, Feb 09, 2013 at 10:48:51PM +0300, Sergei Trofimovich wrote:
> On Sat, 9 Feb 2013 19:51:44 +0100
> Ian Kumlien <pomac@vapor.com> wrote:
> 
> > On Sat, Feb 09, 2013 at 08:57:42PM +0300, Sergei Trofimovich wrote:
> > > On Sat,  9 Feb 2013 00:19:29 +0100
> > > Ian Kumlien <pomac@demius.net> wrote:
> > > 
> > > > 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!
> > > 
> > > What's wrong with conventional LDFLAGS=-static make?
> > 
> > That doesn't work for me, does it actually work for you?
> > 
> > I'd say it wouldn't without adding LDFLAGS so that the compiler gets it
> > for all object compilations.
> 
> -static is link-only flag.
> Mostly yes, it works.
>     LDFLAGS="-static -pthread" make
> 
> make all binaries static.
> 
> It needs
>  sys-apps/util-linux
>  sys-libs/e2fsprogs-libs
>  sys-fs/e2fsprogs
>  sys-libs/zlib
> to be built with USE=static-libs .
> 
>     $ ldd btrfs
>         not a dynamic executable
>     $ ldd mkfs.btrfs 
>         not a dynamic executable
> 
> None of built binaries is dynamic.

Yes, my point is it's not simply "LDFLAGS=-static", do you see any real
harm in having a static tagrget?

Something that tries to make sure that it's actually built correctly?

(Your approach relies on not likning any old objects since make doesn't
honor CFLAGS/LDFLAGS as dependencies.)
> -- 
> 
>   Sergei



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Btrfs-progs: add static compile target
  2013-02-08 23:19 [PATCH] Btrfs-progs: add static compile target Ian Kumlien
  2013-02-09 17:57 ` Sergei Trofimovich
@ 2013-02-12 13:55 ` David Sterba
  1 sibling, 0 replies; 6+ messages in thread
From: David Sterba @ 2013-02-12 13:55 UTC (permalink / raw)
  To: Ian Kumlien; +Cc: linux-btrfs

On Sat, Feb 09, 2013 at 12:19:29AM +0100, Ian Kumlien wrote:
> +# Define static compilation flags
> +STATIC_CFLAGS = $(CFLAGS) -static -ffunction-sections -fdata-sections
> +STATIC_LDFLAGS = -Wl,--gc-sections
> +STATIC_LIBS = $(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)

-static belongs into LDFLAGS, it's propagated to the link phase through
CFLAGS which is not IMO clean. As the point is to make the static
version as small as possible I think it's ok to separate the built
objects and name the binary differently from the normal one. Also, from
distro side, I can build both targets from within one package without
nonstandard build steps (make, copy, clean, make, copy/rename).

I'll fix the -static flag locally and add this patch to integration.

david

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-02-12 13:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-08 23:19 [PATCH] Btrfs-progs: add static compile target Ian Kumlien
2013-02-09 17:57 ` Sergei Trofimovich
2013-02-09 18:51   ` Ian Kumlien
2013-02-09 19:48     ` Sergei Trofimovich
2013-02-09 22:21       ` Ian Kumlien
2013-02-12 13:55 ` David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.