[lxc-devel] Static bulding of LXC tools (was: Re: Duplicate definitions of `lxc_log_define`)

Ciprian Dorin, Craciun ciprian.craciun at gmail.com
Sun Jan 10 12:45:45 UTC 2010


On Sun, Jan 10, 2010 at 5:29 AM, Daniel Lezcano <dlezcano at fr.ibm.com> wrote:
> It would be simple to define lxc_log_define(start, lxc) for start.c and
> lxc_log_define(lxc_start, lxc) for lxc_start.c and so on for the rest of the
> lxc_log_define eg. lxc_log_define(lxc_namespace, lxc) =>
> lxc_log_define(namespace, lxc).

    Done.

>>    P.S.: Could you also consider adding the option to statically
>> build the tools? (My need for them is for an minimal Linux
>> distribution that would only bootstrap the containers, and I would
>> prefer to have the binaries self-contained and not to bother about
>> missing `.so`.)
>
> Yep, looks feasible.

    Done.

    I put below the patches that add the needed options to
`./confiure`. (See the commit comment.)

    I've also created a repository on gitorious.org that shall be the
mirror of the official LXC repository:
        http://gitorious.org/lxc/mainline
    Does anyone want to be the "owner" of this repository?

    I've also put my patches in a clone of that repository (It would
be easier for patching if you like my updates):
        http://gitorious.org/~ciprian.craciun/lxc/ciprian-craciun-patches
        git://gitorious.org/~ciprian.craciun/lxc/ciprian-craciun-patches.git

    Below are the patches (with `git format-patch master`):




>From 29cfb1458e0ce5f58076079b9bd4611f756f7435 Mon Sep 17 00:00:00 2001
From: Ciprian Dorin, Craciun <ciprian at volution.ro>
Date: Sun, 10 Jan 2010 13:52:23 +0200
Subject: [PATCH 1/2] Updated `lxc_log_define(...)` usage, so that
there are no duplicate (exportable) definitions when (statically)
building.

> These updates were based on the reply from Daniel Lezcano:
> It would be simple to define lxc_log_define(start, lxc) for start.c
> and lxc_log_define(lxc_start, lxc) for lxc_start.c and so on for
> the rest of the lxc_log_define
> eg. lxc_log_define(lxc_namespace, lxc) => lxc_log_define(namespace, lxc).
---
 src/lxc/af_unix.c     |    2 +-
 src/lxc/cgroup.c      |    2 +-
 src/lxc/checkpoint.c  |    2 +-
 src/lxc/commands.c    |    2 +-
 src/lxc/conf.c        |    2 +-
 src/lxc/confile.c     |    2 +-
 src/lxc/console.c     |    2 +-
 src/lxc/error.c       |    2 +-
 src/lxc/freezer.c     |    2 +-
 src/lxc/log.c         |    2 +-
 src/lxc/lxc_monitor.c |    2 +-
 src/lxc/monitor.c     |    2 +-
 src/lxc/namespace.c   |    2 +-
 src/lxc/parse.c       |    2 +-
 src/lxc/restart.c     |    2 +-
 src/lxc/start.c       |    2 +-
 src/lxc/state.c       |    2 +-
 src/lxc/stop.c        |    2 +-
 src/lxc/utils.c       |    2 +-
 19 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/src/lxc/af_unix.c b/src/lxc/af_unix.c
index fdbf113..c2d4518 100644
--- a/src/lxc/af_unix.c
+++ b/src/lxc/af_unix.c
@@ -31,7 +31,7 @@

 #include "log.h"

-lxc_log_define(lxc_af_unix, lxc);
+lxc_log_define(af_unix, lxc);

 int lxc_af_unix_open(const char *path, int type, int flags)
 {
diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c
index 55aa8b2..af1fca5 100644
--- a/src/lxc/cgroup.c
+++ b/src/lxc/cgroup.c
@@ -43,7 +43,7 @@
 #include <lxc/cgroup.h>
 #include <lxc/start.h>

-lxc_log_define(lxc_cgroup, lxc);
+lxc_log_define(cgroup, lxc);

 #define MTAB "/etc/mtab"

diff --git a/src/lxc/checkpoint.c b/src/lxc/checkpoint.c
index 7e8a93e..014e01c 100644
--- a/src/lxc/checkpoint.c
+++ b/src/lxc/checkpoint.c
@@ -23,7 +23,7 @@
 #include <lxc/lxc.h>
 #include <lxc/log.h>

-lxc_log_define(lxc_checkpoint, lxc);
+lxc_log_define(checkpoint, lxc);

 int lxc_checkpoint(const char *name, const char *statefile, int flags)
 {
diff --git a/src/lxc/commands.c b/src/lxc/commands.c
index 4c48571..e6ca235 100644
--- a/src/lxc/commands.c
+++ b/src/lxc/commands.c
@@ -53,7 +53,7 @@
  *
  */

-lxc_log_define(lxc_commands, lxc);
+lxc_log_define(commands, lxc);

 #define abstractname LXCPATH "/%s/command"

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index df60688..a199264 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -54,7 +54,7 @@
 #include <lxc/log.h>
 #include <lxc/lxc.h>	/* for lxc_cgroup_set() */

-lxc_log_define(lxc_conf, lxc);
+lxc_log_define(conf, lxc);

 #define MAXHWLEN    18
 #define MAXINDEXLEN 20
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 6e15d09..686e9d9 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -38,7 +38,7 @@
 #include <lxc/log.h>
 #include <lxc/conf.h>

-lxc_log_define(lxc_confile, lxc);
+lxc_log_define(confile, lxc);

 static int config_pts(const char *, char *, struct lxc_conf *);
 static int config_tty(const char *, char *, struct lxc_conf *);
diff --git a/src/lxc/console.c b/src/lxc/console.c
index 96a6edd..4ebf02a 100644
--- a/src/lxc/console.c
+++ b/src/lxc/console.c
@@ -34,7 +34,7 @@
 #include "commands.h"
 #include "af_unix.h"

-lxc_log_define(lxc_console, lxc);
+lxc_log_define(console, lxc);

 extern int lxc_console(const char *name, int ttynum, int *fd)
 {
diff --git a/src/lxc/error.c b/src/lxc/error.c
index 5ecfbac..ebb6899 100644
--- a/src/lxc/error.c
+++ b/src/lxc/error.c
@@ -27,7 +27,7 @@
 #include "error.h"
 #include "log.h"

-lxc_log_define(lxc_error, lxc);
+lxc_log_define(error, lxc);

 /*---------------------------------------------------------------------------*/
 /* lxc_error_set_and_log
diff --git a/src/lxc/freezer.c b/src/lxc/freezer.c
index cff954e..f2503dd 100644
--- a/src/lxc/freezer.c
+++ b/src/lxc/freezer.c
@@ -36,7 +36,7 @@
 #include <lxc/log.h>
 #include <lxc/cgroup.h>

-lxc_log_define(lxc_freezer, lxc);
+lxc_log_define(freezer, lxc);

 static int freeze_unfreeze(const char *name, int freeze)
 {
diff --git a/src/lxc/log.c b/src/lxc/log.c
index 596ed99..7f35cb6 100644
--- a/src/lxc/log.c
+++ b/src/lxc/log.c
@@ -41,7 +41,7 @@
 int lxc_log_fd = -1;
 static char log_prefix[LXC_LOG_PREFIX_SIZE] = "lxc";

-lxc_log_define(lxc_log, lxc);
+lxc_log_define(log, lxc);

 /*---------------------------------------------------------------------------*/
 static int log_append_stderr(const struct lxc_log_appender *appender,
diff --git a/src/lxc/lxc_monitor.c b/src/lxc/lxc_monitor.c
index 7d14188..f7b66e8 100644
--- a/src/lxc/lxc_monitor.c
+++ b/src/lxc/lxc_monitor.c
@@ -33,7 +33,7 @@
 #include <lxc/monitor.h>
 #include "arguments.h"

-lxc_log_define(monitor, lxc);
+lxc_log_define(lxc_monitor, lxc);

 static const struct option my_longopts[] = {
 	LXC_COMMON_OPTIONS
diff --git a/src/lxc/monitor.c b/src/lxc/monitor.c
index 7ec1701..1e50217 100644
--- a/src/lxc/monitor.c
+++ b/src/lxc/monitor.c
@@ -41,7 +41,7 @@
 #include <lxc/state.h>
 #include <lxc/monitor.h>

-lxc_log_define(lxc_monitor, lxc);
+lxc_log_define(monitor, lxc);

 #ifndef UNIX_PATH_MAX
 #define UNIX_PATH_MAX 108
diff --git a/src/lxc/namespace.c b/src/lxc/namespace.c
index 72e47e1..61c91c4 100644
--- a/src/lxc/namespace.c
+++ b/src/lxc/namespace.c
@@ -29,7 +29,7 @@

 #include <lxc/log.h>

-lxc_log_define(lxc_namespace, lxc);
+lxc_log_define(namespace, lxc);

 struct clone_arg {
 	int (*fn)(void *);
diff --git a/src/lxc/parse.c b/src/lxc/parse.c
index b3d1194..3f01840 100644
--- a/src/lxc/parse.c
+++ b/src/lxc/parse.c
@@ -29,7 +29,7 @@
 #include "parse.h"
 #include <lxc/log.h>

-lxc_log_define(lxc_parse, lxc);
+lxc_log_define(parse, lxc);

 static int dir_filter(const struct dirent *dirent)
 {
diff --git a/src/lxc/restart.c b/src/lxc/restart.c
index 42576c1..8098a54 100644
--- a/src/lxc/restart.c
+++ b/src/lxc/restart.c
@@ -23,7 +23,7 @@
 #include <lxc/lxc.h>
 #include <lxc/log.h>

-lxc_log_define(lxc_restart, lxc);
+lxc_log_define(restart, lxc);

 int lxc_restart(const char *name, const char *statefile, const char *rcfile,
 		int flags)
diff --git a/src/lxc/start.c b/src/lxc/start.c
index 7e9d924..2dd837b 100644
--- a/src/lxc/start.c
+++ b/src/lxc/start.c
@@ -102,7 +102,7 @@ int signalfd(int fd, const sigset_t *mask, int flags)
 #include "commands.h"


-lxc_log_define(lxc_start, lxc);
+lxc_log_define(start, lxc);

 LXC_TTY_HANDLER(SIGINT);
 LXC_TTY_HANDLER(SIGQUIT);
diff --git a/src/lxc/state.c b/src/lxc/state.c
index ba63805..9521ee2 100644
--- a/src/lxc/state.c
+++ b/src/lxc/state.c
@@ -37,7 +37,7 @@
 #include "commands.h"
 #include "config.h"

-lxc_log_define(lxc_state, lxc);
+lxc_log_define(state, lxc);

 static char *strstate[] = {
 	"STOPPED", "STARTING", "RUNNING", "STOPPING",
diff --git a/src/lxc/stop.c b/src/lxc/stop.c
index b751af5..4e70a2c 100644
--- a/src/lxc/stop.c
+++ b/src/lxc/stop.c
@@ -37,7 +37,7 @@

 #include "commands.h"

-lxc_log_define(lxc_stop, lxc);
+lxc_log_define(stop, lxc);

 int lxc_stop(const char *name)
 {
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index 1869c0c..de34e21 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -37,7 +37,7 @@
 #include "list.h"
 #include "log.h"

-lxc_log_define(lxc_utils, lxc);
+lxc_log_define(utils, lxc);

 int lxc_copy_file(const char *srcfile, const char *dstfile)
 {
-- 
1.6.3.1




>From 2bfc3d1be1715d582f7d319580d49723ff913d99 Mon Sep 17 00:00:00 2001
From: Ciprian Dorin, Craciun <ciprian at volution.ro>
Date: Sun, 10 Jan 2010 14:22:47 +0200
Subject: [PATCH 2/2] Added options for building (enabling / disabling:
static / shared libraries and linking).

Added the following options for `./configure` script:
 * `--enable-shared` / `--disable-shared`, that instructs
  if the (dynamic) `liblxc.so` is getting built and installed;
 * `--enable-static` / `--disable-static`, that instructs
  if the (static) `liblxc.a` is getting built and installed;
 * `--enable-static-linking` that instructs if
  the resulting executables are statically linked against `liblxc.a`;

There are also sensible defaults:
 * `static` is by default `no`;
 * `shared` is by default the negation of `static`;
 * `static-linking` is by default equal with `static`;

As a consequence of the defaults there are the following scenarios:
 * none of these options are specified => the build behaves exectly
like before the patch;
 * `--enable-static` => static linking, shared libraries are not built;
 * `--enable-static --enable-shared` => we build both libraries, but
we statically link;
 * `--enable-static-linking` => static linking, but also the shared
library is installed;

(Also minor updates to the `configure.ac` and `Makefile.am` regarding
mixing spaces and tabs:
it seems that the one that edited them had tab equal to 8 spaces, and
in some places he used
8 spaces rather than a tab. I've simply replaced (in only a few
places), the eight spaces with a tab.)
---
 configure.ac        |   35 +++++++++++++++++++++++++++++------
 src/lxc/Makefile.am |   33 +++++++++++++++++++++++++++------
 2 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/configure.ac b/configure.ac
index faa31b3..12496e0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -31,6 +31,29 @@ AC_ARG_ENABLE([examples],

 AM_CONDITIONAL([ENABLE_EXAMPLES], [test "x$enable_examples" = "xyes"])

+AC_ARG_ENABLE([static],
+	[AC_HELP_STRING([--enable-static], [build liblxc.a static library
(default is no)])],
+	[static_libraries=$enableval], [static_libraries=no])
+
+AM_CONDITIONAL([STATIC_LIBRARIES], [test "x$static_libraries" = "xyes"])
+
+AC_ARG_ENABLE([static-linking],
+	[AC_HELP_STRING([--enable-static-linking], [link all binaries
statically (default is taken from static)])],
+	[static_linking=$enableval], [static_linking=$static_libraries])
+
+AC_ARG_ENABLE([static-linking],
+	[AC_HELP_STRING([--enable-static-linking], [link all binaries
statically (default is taken from static)])],
+	[static_linking=$enableval], [static_linking=$static_libraries])
+
+AM_CONDITIONAL([STATIC_LINKING], [test "x$static_linking" = "xyes"])
+
+AC_ARG_ENABLE([shared],
+	[AC_HELP_STRING([--enable-shared], [build liblxc.so shared library
(default is negated from static)])],
+	[shared_libraries=$enableval],
+	[shared_libraries=$( if test "x$static_linking" = "xyes" ; then echo
no ; else echo yes ; fi )])
+
+AM_CONDITIONAL([SHARED_LIBRARIES], [test "x$shared_libraries" = "xyes"])
+
 AS_AC_EXPAND(PREFIX, $prefix)
 AS_AC_EXPAND(LIBDIR, $libdir)
 AS_AC_EXPAND(BINDIR, $bindir)
@@ -57,8 +80,7 @@ AC_DEFINE_UNQUOTED(LXCLIBEXECDIR, "$LIBEXECDIR")
 AC_CHECK_HEADERS([linux/netlink.h linux/genetlink.h],
                  [],
                  AC_MSG_ERROR([netlink headers not found. Please
install the linux kernel headers.]),
-                 [#include <sys/socket.h>
-		 ])
+                 [#include <sys/socket.h>])

 AC_CHECK_HEADERS([sys/capability.h], [], AC_MSG_ERROR([please install
libcap-devel.]),
 [#include <sys/types.h>
@@ -70,18 +92,19 @@ AC_CHECK_DECLS([PR_CAPBSET_DROP], [], [],
[#include <sys/prctl.h>])
 AC_CHECK_HEADERS([sys/signalfd.h])

 AC_PROG_GCC_TRADITIONAL
+AC_PROG_RANLIB

 if test "x$GCC" = "xyes"; then
   CFLAGS="$CFLAGS -Wall"
 fi

 AC_CONFIG_FILES([
-        Makefile
+	Makefile
 	lxc.pc
 	lxc.spec
-        config/Makefile
+	config/Makefile

-        doc/Makefile
+	doc/Makefile
 	doc/lxc-create.sgml
 	doc/lxc-destroy.sgml
 	doc/lxc-execute.sgml
@@ -115,7 +138,7 @@ AC_CONFIG_FILES([
 	scripts/lxc-fedora
 	scripts/lxc-sshd

-        src/Makefile
+	src/Makefile
 	src/lxc/Makefile
 	src/lxc/lxc-ps
 	src/lxc/lxc-ls
diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index be64b2a..9a601f8 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -11,11 +11,17 @@ pkginclude_HEADERS = \
 		log.h \
 		state.h

+if SHARED_LIBRARIES
 sodir=$(libdir)
 # use PROGRAMS to avoid complains from automake
 so_PROGRAMS = liblxc.so
+endif

-liblxc_so_SOURCES = \
+if STATIC_LIBRARIES
+lib_LIBRARIES = liblxc.a
+endif
+
+liblxc_SOURCES = \
 	arguments.c arguments.h \
 	commands.c commands.h \
 	start.c \
@@ -38,22 +44,28 @@ liblxc_so_SOURCES = \
 	log.c log.h \
 	\
 	network.c network.h \
-        nl.c nl.h \
-        rtnl.c rtnl.h \
-        genl.c genl.h \
+	nl.c nl.h \
+	rtnl.c rtnl.h \
+	genl.c genl.h \
 	\
 	mainloop.c mainloop.h \
 	af_unix.c af_unix.h

 AM_CFLAGS=-I$(top_srcdir)/src

+liblxc_so_SOURCES = $(liblxc_SOURCES)
+
 liblxc_so_CFLAGS = -fPIC -DPIC $(AM_CFLAGS)

 liblxc_so_LDFLAGS = \
 	-shared \
 	-Wl,-soname,liblxc.so.$(firstword $(subst ., ,$(VERSION)))

-liblxc_so_LDADD = -lutil
+liblxc_so_LDADD =
+
+liblxc_a_SOURCES = $(liblxc_SOURCES)
+
+liblxc_a_CFLAGS = -fPIC -DPIC $(AM_CFLAGS)

 bin_SCRIPTS = \
 	lxc-ps \
@@ -83,8 +95,13 @@ bin_PROGRAMS = \
 libexec_PROGRAMS = \
 	lxc-init

+if STATIC_LINKING
+AM_LDFLAGS=-static
+LDADD=liblxc.a -lutil
+else
 AM_LDFLAGS=-Wl,-E -Wl,-rpath -Wl,$(libdir)
-LDADD=liblxc.so
+LDADD=liblxc.so -lutil
+endif

 lxc_cgroup_SOURCES = lxc_cgroup.c
 lxc_checkpoint_SOURCES = lxc_checkpoint.c
@@ -101,11 +118,15 @@ lxc_unfreeze_SOURCES = lxc_unfreeze.c
 lxc_unshare_SOURCES = lxc_unshare.c
 lxc_wait_SOURCES = lxc_wait.c

+if !STATIC_LINKING
 install-exec-local: install-soPROGRAMS
 	mv $(DESTDIR)$(libdir)/liblxc.so $(DESTDIR)$(libdir)/liblxc.so.$(VERSION)
 	/sbin/ldconfig -l $(DESTDIR)$(libdir)/liblxc.so.$(VERSION)
 	cd $(DESTDIR)$(libdir); \
 	ln -sf liblxc.so.$(firstword $(subst ., ,$(VERSION))) liblxc.so
+endif

+if !STATIC_LINKING
 uninstall-local:
 	$(RM) $(DESTDIR)$(libdir)/liblxc.so*
+endif
-- 
1.6.3.1




More information about the lxc-devel mailing list