From fc160d427161d9545656f0850e3f01cfb18cd6e7 Mon Sep 17 00:00:00 2001
From: Amin Ben Ramdhane <amin.benramdhane@pivasoftware.com>
Date: Tue, 30 Jun 2020 14:56:14 +0100
Subject: [PATCH] swmod: compile swmod_lxc only if lxc library is defined

---
 Makefile     | 13 +++++++++----
 swmod.c      | 18 +++++++++++++++++-
 swmod_host.c |  4 +++-
 swmod_lxc.c  |  2 +-
 swmod_lxc.h  |  2 ++
 swmod_opkg.c |  7 +++++--
 tools.c      |  8 +++++++-
 7 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile
index 6fc8167..38d9806 100644
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,14 @@
 PROG = swmodd
-OBJS =swmod.o swmod_host.o swmod_lxc.o swmod_opkg.o swmod_uci.o tools.o
+OBJS = swmod.o swmod_host.o swmod_opkg.o swmod_uci.o tools.o
 
-PROG_CFLAGS = $(CFLAGS) -fstrict-aliasing
-PROG_LDFLAGS = $(LDFLAGS)
-PROG_LDFLAGS += -luci -lubus -lubox -ljson-c -lblobmsg_json -luuid -lopkg -llxc
+PROG_CFLAGS = $(CFLAGS) -fstrict-aliasing -Wall
+PROG_LDFLAGS = $(LDFLAGS) -luci -lubus -lubox -ljson-c -lblobmsg_json -luuid -lopkg
+
+ifeq ($(SWMOD_LXC),yes)
+OBJS += swmod_lxc.o
+PROG_CFLAGS += -DSWMOD_LXC
+PROG_LDFLAGS += -llxc
+endif
 
 %.o: %.c
 	$(CC) $(PROG_CFLAGS) $(FPIC) -c -o $@ $<
diff --git a/swmod.c b/swmod.c
index 7fbc7ed..184bd89 100644
--- a/swmod.c
+++ b/swmod.c
@@ -29,10 +29,12 @@
 
 #include "swmod_uci.h"
 #include "swmod_opkg.h"
-#include "swmod_lxc.h"
 #include "tools.h"
 #include "swmod_host.h"
 #include "swmod.h"
+#ifdef SWMOD_LXC
+#include "swmod_lxc.h"
+#endif
 
 ExecEnv environments[MAX_ENV] = {0};
 ExecUnit exec_units[MAX_ENV] = {0};
@@ -113,7 +115,9 @@ populate_environments(void)
 	populate_host_system_environment();
 
 	/* Linux containers */
+#ifdef SWMOD_LXC
 	populate_lxc_environment();
+#endif
 }
 
 static int
@@ -221,6 +225,7 @@ update_map_du_file(struct blob_buf *bb, char *pkg_name,
 {
 	char lxc_map_du_path[128] = {0};
 
+#ifdef SWMOD_LXC
 	if (!env || strcmp(env, HOST_SYSTEM) == 0)
 		swmod_strncpy(lxc_map_du_path, HOST_SYSTEM, 14);
 	else {
@@ -230,6 +235,9 @@ update_map_du_file(struct blob_buf *bb, char *pkg_name,
 				env,
 				SWMOD_PATH);
 	}
+#else
+	swmod_strncpy(lxc_map_du_path, HOST_SYSTEM, 14);
+#endif
 
 	swmod_uci_init(lxc_map_du_path);
 
@@ -328,6 +336,7 @@ update_corresponding_section_in_map_du_file(char *pkg_version, char *url,
 {
 	char lxc_map_du_path[128] = {0};
 
+#ifdef SWMOD_LXC
 	if (!env || strcmp(env, HOST_SYSTEM) == 0)
 		swmod_strncpy(lxc_map_du_path, SWMOD_PATH, 12);
 	else {
@@ -337,6 +346,9 @@ update_corresponding_section_in_map_du_file(char *pkg_version, char *url,
 				env,
 				SWMOD_PATH);
 	}
+#else
+	swmod_strncpy(lxc_map_du_path, SWMOD_PATH, 12);
+#endif
 
 	swmod_uci_init(lxc_map_du_path);
 
@@ -478,6 +490,7 @@ swmod_du_list(struct ubus_context *ctx, struct ubus_object *obj,
 	swmod_uci_fini(SWMOD_MAP_DU);
 
 	/* Show all containers  */
+#ifdef SWMOD_LXC
 	const char *lxcpath = NULL;
 
 	if (lxc_is_supported(&lxcpath)) {
@@ -522,6 +535,7 @@ swmod_du_list(struct ubus_context *ctx, struct ubus_object *obj,
 		}
 		if (dir) closedir(dir);
 	}
+#endif
 
 	blobmsg_close_array(&bb, a);
 
@@ -539,7 +553,9 @@ populate_execution_unit(void)
 	populate_host_system_execution_unit();
 
 	/* Linux containers */
+#ifdef SWMOD_LXC
 	populate_lxc_deployment_execution_units(SWMOD_EXECUTION_UNIT);
+#endif
 }
 
 static int
diff --git a/swmod_host.c b/swmod_host.c
index 0ac1ff0..c6fe1c7 100644
--- a/swmod_host.c
+++ b/swmod_host.c
@@ -166,7 +166,9 @@ void populate_host_system_execution_unit(void)
 
 					if (strstr(p->cmd, spch)) {
 
-						char line[1024], path[512], *spch = NULL, *config = NULL;
+						char line[1024], path[512], *spch = NULL;
+
+
 						int disk_space;
 
 						exec_units[0].eu_exists[nbr] = true;
diff --git a/swmod_lxc.c b/swmod_lxc.c
index 9653c4c..4bfb0a5 100644
--- a/swmod_lxc.c
+++ b/swmod_lxc.c
@@ -316,7 +316,7 @@ static int lxc_attach_run_execution_unit_func(void *args)
 					if (strstr(p->cmd, spch)) {
 
 						char line[1024], description[1024], path[512], version[32];
-						char *spch = NULL, *config = NULL;
+						char *spch = NULL;
 						int disk_space;
 
 						char *pkg_version = pkg_version_str_alloc(pkg);
diff --git a/swmod_lxc.h b/swmod_lxc.h
index ee171d2..e7f21ab 100644
--- a/swmod_lxc.h
+++ b/swmod_lxc.h
@@ -23,6 +23,8 @@
 #ifndef LXC_H
 #define LXC_H
 
+#include <stdbool.h>
+
 bool lxc_is_supported(const char **lxcpath);
 const char *get_lxc_path_from_config(void);
 void populate_lxc_environment(void);
diff --git a/swmod_opkg.c b/swmod_opkg.c
index 66ccbbb..82ddc40 100644
--- a/swmod_opkg.c
+++ b/swmod_opkg.c
@@ -25,8 +25,10 @@
 #include "tools.h"
 #include "swmod.h"
 #include "swmod_uci.h"
-#include "swmod_lxc.h"
 #include "swmod_opkg.h"
+#ifdef SWMOD_LXC
+#include "swmod_lxc.h"
+#endif
 
 char package_name[64] = "";
 char package_version[64] = "";
@@ -75,9 +77,10 @@ static int swmod_host_system_install_remove_package(const char *package_path, in
 
 int swmod_install_remove_package(const char *package_path, const char *environment, int action)
 {
+#ifdef SWMOD_LXC
 	if (environment && *environment != '\0' && (strcmp(environment, HOST_SYSTEM) != 0))
 		return swmod_lxc_install_remove_package(package_path, environment, action);
-
+#endif
 	return swmod_host_system_install_remove_package(package_path, action);
 }
 
diff --git a/tools.c b/tools.c
index 176d124..6d91602 100644
--- a/tools.c
+++ b/tools.c
@@ -29,8 +29,10 @@
 #include "swmod_uci.h"
 #include "swmod_opkg.h"
 #include "swmod_host.h"
-#include "swmod_lxc.h"
 #include "tools.h"
+#ifdef SWMOD_LXC
+#include "swmod_lxc.h"
+#endif
 
 #define PROC_PATH "/proc"
 
@@ -120,7 +122,9 @@ int synchronize_deployment_units_with_map_du_file(void)
 	populate_host_system_deployment_unit();
 
 	/* Linux Container */
+#ifdef SWMOD_LXC
 	populate_lxc_deployment_execution_units(SWMOD_DEPLOYMENT_UNIT);
+#endif
 
 	return 0;
 }
@@ -140,6 +144,7 @@ char *get_environment_from_map_du_file(const char *uuid)
 	}
 	swmod_uci_fini(SWMOD_MAP_DU);
 
+#ifdef SWMOD_LXC
 	if (env)
 		return env;
 
@@ -177,6 +182,7 @@ char *get_environment_from_map_du_file(const char *uuid)
 		}
 		if (dir) closedir(dir);
 	}
+#endif
 
 	return env;
 }
-- 
GitLab