[libblockdev 1/2] Make Makefile mostly automatic.

mulhern amulhern at redhat.com
Tue Jul 1 15:36:13 UTC 2014


There is still room for sorting out variables for flags, etc, and of course
for autotooling.

Other changes are:
- Make test files include the files they exercise, instead of vice-versa.
- reversing the order of includes in src/lib/blockdev.c to
enable compilation w/out the include in src/lib/blockdev.h.
- A few small fixes indicated by warning/error messages.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 Makefile                 | 192 +++++++++++++++++++++++++----------------------
 src/lib/blockdev.c       |  13 +---
 src/lib/blockdev.h       |   1 -
 src/lib/plugin_apis/dm.h |   2 +-
 src/lib/test_blockdev.c  |   2 +
 src/plugins/loop.c       |   4 -
 src/plugins/lvm.c        |   5 --
 src/plugins/swap.c       |   5 --
 src/plugins/test_loop.c  |   4 +-
 src/plugins/test_lvm.c   |   5 +-
 src/plugins/test_swap.c  |   4 +-
 src/utils/sizes.c        |   4 -
 src/utils/test_sizes.c   |   4 +-
 13 files changed, 122 insertions(+), 123 deletions(-)

diff --git a/Makefile b/Makefile
index fe4392f..1e8f039 100644
--- a/Makefile
+++ b/Makefile
@@ -1,105 +1,119 @@
-UTILS_FILES = src/utils/sizes.c src/utils/sizes.h src/utils/exec.c src/utils/exec.h src/utils/utils.h
-LVM_PLUGIN_FILES = src/plugins/lvm.h src/plugins/lvm.c
-SWAP_PLUGIN_FILES = src/plugins/swap.h src/plugins/swap.c
-LOOP_PLUGIN_FILES = src/plugins/loop.h src/plugins/loop.c
-CRYPTO_PLUGIN_FILES = src/plugins/crypto.h src/plugins/crypto.c
-MPATH_PLUGIN_FILES = src/plugins/mpath.h src/plugins/mpath.c
-DM_PLUGIN_FILES = src/plugins/dm.h src/plugins/dm.c
-LIBRARY_FILES = src/lib/blockdev.c src/lib/blockdev.h src/lib/plugins.h src/lib/plugin_apis/lvm.h
-
-build-plugins: ${LVM_PLUGIN_FILES} ${SWAP_PLUGIN_FILES} ${LOOP_PLUGIN_FILES} ${MPATH_PLUGIN_FILES}
-	gcc -c -Wall -Wextra -Werror -fPIC -I src/utils/ -I src/plugins/ \
-		`pkg-config --cflags glib-2.0 gobject-2.0` src/plugins/lvm.c
-	gcc -shared -o src/plugins/libbd_lvm.so lvm.o
-
-	gcc -c -Wall -Wextra -Werror -fPIC -I src/plugins/ -I src/utils/ \
-		`pkg-config --cflags glib-2.0` src/plugins/swap.c
-	gcc -shared -o src/plugins/libbd_swap.so swap.o
-
-	gcc -c -Wall -Wextra -Werror -fPIC -I src/plugins/ -I src/utils/ \
-		`pkg-config --cflags glib-2.0` src/plugins/loop.c
-	gcc -shared -o src/plugins/libbd_loop.so loop.o
-
-	gcc -c -Wall -Wextra -Werror -fPIC -I src/plugins/ -lm `pkg-config --libs --cflags glib-2.0 libcryptsetup`\
-		src/plugins/crypto.c
-	gcc -shared -o src/plugins/libbd_crypto.so crypto.o
-
-	gcc -c -Wall -Wextra -Werror -fPIC -I src/plugins/ -I src/utils/ \
-		`pkg-config --cflags glib-2.0` src/plugins/mpath.c
-	gcc -shared -o src/plugins/libbd_mpath.so mpath.o
-
-	gcc -c -Wall -Wextra -Werror -fPIC -I src/plugins/ -I src/utils/ \
-		`pkg-config --cflags glib-2.0` src/plugins/dm.c
-	gcc -shared -o src/plugins/libbd_dm.so dm.o
-
-generate-boilerplate-code: src/lib/plugin_apis/lvm.h src/lib/plugin_apis/swap.h
-	./boilerplate_generator.py src/lib/plugin_apis/lvm.h > src/lib/plugin_apis/lvm.c
-	./boilerplate_generator.py src/lib/plugin_apis/swap.h > src/lib/plugin_apis/swap.c
-	./boilerplate_generator.py src/lib/plugin_apis/loop.h > src/lib/plugin_apis/loop.c
-	./boilerplate_generator.py src/lib/plugin_apis/crypto.h > src/lib/plugin_apis/crypto.c
-	./boilerplate_generator.py src/lib/plugin_apis/mpath.h > src/lib/plugin_apis/mpath.c
-	./boilerplate_generator.py src/lib/plugin_apis/dm.h > src/lib/plugin_apis/dm.c
-
-build-utils: ${UTILS_FILES}
-	gcc -c -Wall -Wextra -Werror -fPIC `pkg-config --cflags glib-2.0` -I src/utils/ \
-        src/utils/sizes.c
-	gcc -c -Wall -Wextra -Werror -fPIC `pkg-config --cflags glib-2.0` -I src/utils/ \
-        src/utils/exec.c
-	gcc -shared -o src/utils/libbd_utils.so sizes.o exec.o
-
-build-library: generate-boilerplate-code ${LIBRARY_FILES}
-	gcc -fPIC -c `pkg-config --libs --cflags glib-2.0` -ldl src/lib/blockdev.c
-	gcc -shared -o src/lib/libblockdev.so blockdev.o
-
-build-introspection-data: build-utils build-library ${LIBRARY_FILES}
-	LD_LIBRARY_PATH=src/lib/:src/utils/ g-ir-scanner `pkg-config --cflags --libs glib-2.0 gobject-2.0 libcryptsetup` --library=blockdev -I src/lib/ -L src/utils -lbd_utils -L src/lib/ --identifier-prefix=BD --symbol-prefix=bd --namespace BlockDev --nsversion=1.0 -o BlockDev-1.0.gir --warn-all src/lib/blockdev.h src/lib/blockdev.c src/lib/plugins.h src/lib/plugin_apis/lvm.h src/lib/plugin_apis/swap.h src/lib/plugin_apis/loop.h src/lib/plugin_apis/crypto.h src/lib/plugin_apis/mpath.h src/lib/plugin_apis/dm.h
-	g-ir-compiler -o BlockDev-1.0.typelib BlockDev-1.0.gir
-
-test-sizes: ${SIZES_FILES}
-	gcc -Wall -DTESTING_SIZES -o test_sizes -I src/utils/ -lm `pkg-config --libs --cflags glib-2.0`\
-		src/utils/sizes.c
-	@echo "***Running tests***"
-	./test_sizes
-	@rm test_sizes
+# includes
+GLIB_INCLUDES := `pkg-config --cflags glib-2.0`
+GOBJECT_INCLUDES := `pkg-config --cflags gobject-2.0`
+LIBCRYPTSETUP_INCLUDES := `pkg-config --cflags libcryptsetup`
 
-test-lvm-plugin: ${LVM_PLUGIN_FILES} build-utils
-	gcc -DTESTING_LVM -o test_lvm_plugin -I src/utils/ -I src/plugins/ -I src/utils/ \
-		-L src/utils/ -lbd_utils -lm `pkg-config --libs --cflags glib-2.0 gobject-2.0`\
-		src/plugins/lvm.c
-	@echo "***Running tests***"
-	LD_LIBRARY_PATH=src/utils/	./test_lvm_plugin
-	@rm test_lvm_plugin
+# libraries
+GLIB := `pkg-config --libs glib-2.0`
+GOBJECT := `pkg-config --libs gobject-2.0`
+LIBCRYPTSETUP := `pkg-config --libs libcryptsetup`
+
+# library for internal use
+UTILS_SOURCES := src/utils/exec.c src/utils/sizes.c
+UTILS_OBJS := $(patsubst %.c,%.o,${UTILS_SOURCES})
+
+# stub functions for plugins
+PLUGIN_HEADER_NAMES = crypto.h dm.h loop.h lvm.h mpath.h swap.h
+PLUGIN_HEADER_FILES := $(addprefix src/lib/plugin_apis/, ${PLUGIN_HEADER_NAMES})
+PLUGIN_SOURCE_FILES := $(patsubst %.h,%.c,${PLUGIN_HEADER_FILES})
+
+# implemented plugins
+PLUGIN_SOURCES := src/plugins/crypto.c src/plugins/dm.c src/plugins/loop.c src/plugins/lvm.c src/plugins/mpath.c src/plugins/swap.c
+PLUGIN_OBJS := $(patsubst %.c,%.o,${PLUGIN_SOURCES})
+PLUGIN_LIBS := $(addprefix src/plugins/,$(patsubst %.c,libbd_%.so,$(notdir ${PLUGIN_SOURCES})))
+
+# plugin tests
+PLUGIN_TEST_SOURCES := $(wildcard src/plugins/test_*.c)
+PLUGIN_TEST_EXECUTABLES := $(addprefix src/plugins/,$(patsubst test_%.c,test_%,$(notdir ${PLUGIN_TEST_SOURCES})))
+PLUGIN_TESTS := $(patsubst test_%.c,test-%,$(notdir ${PLUGIN_TEST_SOURCES}))
 
-test-swap-plugin: ${SWAP_PLUGIN_FILES} build-utils
-	gcc -DTESTING_SWAP -o test_swap_plugin -I src/plugins/ -I src/utils/ -L src/utils/ -lbd_utils \
-		`pkg-config --libs --cflags glib-2.0` src/plugins/swap.c
+# plugin management
+LIBRARY_FILES := src/lib/blockdev.c src/lib/blockdev.h src/lib/plugins.h
+
+all: BlockDev-1.0.typelib
+
+# object files
+%.o: %.c %.h
+	gcc -c -Wall -Wextra -Werror -fPIC -o $@ -I src/utils/ ${GLIB_INCLUDES} $<
+
+# test object files include the source of the programs they test
+test_%.o: test_%.c %.c %.h
+	gcc -c -Wall -Wextra -Werror -o $@ -I src/utils/ ${GLIB_INCLUDES} $<
+
+test_%: test_%.o ${UTILS_OBJS}
+	gcc -o $@ -lm ${GLIB} $^
+
+test-%: src/plugins/test_%
 	@echo "***Running tests***"
-	LD_LIBRARY_PATH=src/utils/ ./test_swap_plugin
-	@rm test_swap_plugin
+	./$<
+
+# test_sizes executable must avoid two copies of sizes.o
+src/utils/test_sizes: src/utils/test_sizes.o
+	gcc -o $@ -lm ${GLIB} $<
 
-test-loop-plugin: ${LOOP_PLUGIN_FILES} build-utils
-	gcc -DTESTING_LOOP -o test_loop_plugin -I src/plugins/ -I src/utils/ -L src/utils/ -lbd_utils \
-		`pkg-config --libs --cflags glib-2.0` src/plugins/loop.c
+test-sizes: src/utils/test_sizes
 	@echo "***Running tests***"
-	LD_LIBRARY_PATH=src/utils/ ./test_loop_plugin
-	@rm test_loop_plugin
+	./$<
 
-test-library: generate-boilerplate-code build-plugins
-	gcc -DTESTING_LIB -o test_library `pkg-config --libs --cflags glib-2.0 gobject-2.0` -ldl src/lib/blockdev.c
+# compilation does not signal all warnings, as it includes stub sources
+src/lib/test_blockdev.o: src/lib/test_blockdev.c src/lib/blockdev.c src/lib/blockdev.h ${PLUGIN_SOURCE_FILES}
+	gcc -c -Wextra -Werror -o $@ -I src/utils/ -I src/plugins ${GLIB_INCLUDES} $<
+
+src/lib/test_library: src/lib/test_blockdev.o ${PLUGIN_LIBS}
+	gcc -o $@ -ldl ${GLIB} ${GOBJECT} $<
+
+test-library: src/lib/test_library
 	@echo "***Running tests***"
-	LD_LIBRARY_PATH=src/plugins/ ./test_library
-	@rm test_library
+	LD_LIBRARY_PATH=src/plugins/ ./$<
+
+test-plugins: ${PLUGIN_TESTS}
+
+# individual plugin libraries
+libbd_%.so: %.o
+	gcc -shared -fPIC -o $@ $<
+
+# utils library
+src/utils/libbd_utils.so: ${UTILS_OBJS}
+	gcc -shared -fPIC -o $@ $^
+
+# automatic generation of plugin stub functions
+src/lib/plugin_apis/%.c: src/lib/plugin_apis/%.h
+	./boilerplate_generator.py $< > $@
 
-test-from-python: build-library build-plugins build-introspection-data
-	GI_TYPELIB_PATH=. LD_LIBRARY_PATH=src/plugins/:src/lib/ python -c 'from gi.repository import BlockDev; BlockDev.init(None); print BlockDev.lvm_get_max_lv_size()'
+src/lib/blockdev.o: ${LIBRARY_FILES} ${PLUGIN_SOURCE_FILES}
+	gcc -fPIC -c ${GLIB_INCLUDES} $< -o $@
 
-run-ipython: build-library build-plugins build-introspection-data
+src/lib/libblockdev.so: src/lib/blockdev.o
+	gcc -shared -fPIC -o $@ $<
+
+BlockDev-1.0.gir: src/utils/libbd_utils.so src/lib/libblockdev.so ${LIBRARY_FILES}
+	LD_LIBRARY_PATH=src/lib/:src/utils/ g-ir-scanner `pkg-config --cflags --libs glib-2.0 gobject-2.0 libcryptsetup` --library=blockdev -I src/lib/ -L src/utils -lbd_utils -L src/lib/ --identifier-prefix=BD --symbol-prefix=bd --namespace BlockDev --nsversion=1.0 -o $@ --warn-all ${LIBRARY_FILES} ${PLUGIN_HEADER_FILES}
+
+BlockDev-1.0.typelib: BlockDev-1.0.gir
+	g-ir-compiler -o $@ $<
+
+test-from-python: src/lib/libblockdev.so ${PLUGIN_LIBS} BlockDev-1.0.typelib
+	GI_TYPELIB_PATH=. LD_LIBRARY_PATH=src/plugins/:src/lib/:src/utils python -c 'from gi.repository import BlockDev; BlockDev.init(None); print BlockDev.lvm_get_max_lv_size()'
+
+run-ipython: src/lib/libblockdev.so ${PLUGIN_LIBS} BlockDev-1.0.typelib
 	GI_TYPELIB_PATH=. LD_LIBRARY_PATH=src/plugins/:src/lib/:src/utils/ ipython
 
-run-root-ipython: build-library build-plugins build-introspection-data
+run-root-ipython: src/lib/libblockdev.so ${PLUGIN_LIBS} BlockDev-1.0.typelib
 	sudo GI_TYPELIB_PATH=. LD_LIBRARY_PATH=src/plugins/:src/lib/:src/utils/ ipython
 
-test: build-utils build-library build-plugins build-introspection-data
+test: src/utils/libbd_utils.so src/lib/libblockdev.so ${PLUGIN_LIBS} BlockDev-1.0.typelib
 	@echo
 	@sudo GI_TYPELIB_PATH=. LD_LIBRARY_PATH=src/plugins/:src/lib/:src/utils/ PYTHONPATH=.:tests/ \
 		python -m unittest discover -v -s tests/ -p '*_test.py'
+
+clean:
+	-rm BlockDev-1.0.gir
+	-rm BlockDev-1.0.typelib
+	-rm src/lib/plugin_apis/*.c
+	-rm src/lib/test_library
+	-rm src/plugins/test_loop
+	-rm src/plugins/test_lvm
+	-rm src/plugins/test_swap
+	-rm src/utils/test_sizes
+	find . -name '*.o' -exec rm {} \;
+	find . -name '*.so' -exec rm {} \;
diff --git a/src/lib/blockdev.c b/src/lib/blockdev.c
index 9f0fc06..7c21774 100644
--- a/src/lib/blockdev.c
+++ b/src/lib/blockdev.c
@@ -2,20 +2,19 @@
 #include "blockdev.h"
 #include "plugins.h"
 
-#include "plugin_apis/lvm.c"
 #include "plugin_apis/lvm.h"
-#include "plugin_apis/swap.c"
+#include "plugin_apis/lvm.c"
 #include "plugin_apis/swap.h"
-#include "plugin_apis/loop.c"
+#include "plugin_apis/swap.c"
 #include "plugin_apis/loop.h"
-#include "plugin_apis/crypto.c"
+#include "plugin_apis/loop.c"
 #include "plugin_apis/crypto.h"
+#include "plugin_apis/crypto.c"
 #include "plugin_apis/mpath.c"
 #include "plugin_apis/mpath.h"
 #include "plugin_apis/dm.c"
 #include "plugin_apis/dm.h"
 
-
 /**
  * SECTION: libblockdev
  * @short_description: a library for doing low-level operations with block devices
@@ -112,7 +111,3 @@ gboolean bd_is_plugin_available (BDPlugin plugin) {
     else
         return FALSE;
 }
-
-#ifdef TESTING_LIB
-#include "test_blockdev.c"
-#endif
diff --git a/src/lib/blockdev.h b/src/lib/blockdev.h
index d487596..e02016f 100644
--- a/src/lib/blockdev.h
+++ b/src/lib/blockdev.h
@@ -4,7 +4,6 @@
 #define BD_LIB
 
 #include "plugins.h"
-#include "plugin_apis/lvm.h"
 
 gboolean bd_init (BDPluginSpec *force_plugins);
 gboolean bd_reinit (BDPluginSpec *force_plugins, gboolean replace);
diff --git a/src/lib/plugin_apis/dm.h b/src/lib/plugin_apis/dm.h
index 79bdf2d..c2c44ea 100644
--- a/src/lib/plugin_apis/dm.h
+++ b/src/lib/plugin_apis/dm.h
@@ -20,7 +20,7 @@ gboolean bd_dm_create_linear (gchar *map_name, gchar *device, guint64 length, gc
  *
  * Returns: whether the @map_name map was successfully removed or not
  */
-gboolean bd_dm_remove (gchar *map_name);
+gboolean bd_dm_remove (gchar *map_name, gchar **error_message);
 
 /**
  * bd_dm_name_from_dm_node:
diff --git a/src/lib/test_blockdev.c b/src/lib/test_blockdev.c
index 77b6262..48abaf2 100644
--- a/src/lib/test_blockdev.c
+++ b/src/lib/test_blockdev.c
@@ -1,5 +1,7 @@
 #include <glib/gprintf.h>
 
+#include "blockdev.c"
+
 int main (int argc, char* argv[]) {
     bd_init(NULL);
     g_printf ("available plugins: %s\n", g_strjoinv (", ", bd_get_available_plugin_names()));
diff --git a/src/plugins/loop.c b/src/plugins/loop.c
index 0e1941d..2990e37 100644
--- a/src/plugins/loop.c
+++ b/src/plugins/loop.c
@@ -159,7 +159,3 @@ gboolean bd_loop_teardown (gchar *loop, gchar **error_message) {
 
     return success;
 }
-
-#ifdef TESTING_LOOP
-#include "test_loop.c"
-#endif
diff --git a/src/plugins/lvm.c b/src/plugins/lvm.c
index b611a12..c243f6c 100644
--- a/src/plugins/lvm.c
+++ b/src/plugins/lvm.c
@@ -1220,8 +1220,3 @@ gboolean bd_lvm_thsnapshotcreate (gchar *vg_name, gchar *origin_name, gchar *sna
 
     return success;
 }
-
-#ifdef TESTING_LVM
-#include "test_lvm.c"
-#endif
-
diff --git a/src/plugins/swap.c b/src/plugins/swap.c
index f30c243..e6de404 100644
--- a/src/plugins/swap.c
+++ b/src/plugins/swap.c
@@ -217,8 +217,3 @@ gboolean bd_swap_swapstatus (gchar *device, gchar **error_message) {
     g_free (file_content);
     return FALSE;
 }
-
-
-#ifdef TESTING_SWAP
-#include "test_swap.c"
-#endif
diff --git a/src/plugins/test_loop.c b/src/plugins/test_loop.c
index 2acb4a2..51fbbb9 100644
--- a/src/plugins/test_loop.c
+++ b/src/plugins/test_loop.c
@@ -1,6 +1,8 @@
 #include <glib/gprintf.h>
 
-int main (int argc, char **argv) {
+#include "loop.c"
+
+int main (void) {
     gchar *ret = NULL;
     gchar *err_msg = NULL;
     gboolean success = FALSE;
diff --git a/src/plugins/test_lvm.c b/src/plugins/test_lvm.c
index 1ecf76f..8e2d870 100644
--- a/src/plugins/test_lvm.c
+++ b/src/plugins/test_lvm.c
@@ -1,5 +1,7 @@
 #include <glib/gprintf.h>
 
+#include "lvm.c"
+
 void print_hash_table (GHashTable *table) {
     GHashTableIter iter;
     gpointer key, value;
@@ -11,8 +13,7 @@ void print_hash_table (GHashTable *table) {
         g_printf("%s : %s\n", (gchar *) key, (gchar *) value);
 }
 
-int main (int argc, char **argv) {
-    gint exit_status;
+int main (void) {
     gchar const * const *fname = NULL;
     gchar *msg = NULL;
     gchar *stdout = NULL;
diff --git a/src/plugins/test_swap.c b/src/plugins/test_swap.c
index b3f38c5..8fa53bb 100644
--- a/src/plugins/test_swap.c
+++ b/src/plugins/test_swap.c
@@ -1,6 +1,8 @@
 #include <glib/gprintf.h>
 
-int main (int argc, char **argv) {
+#include "swap.c"
+
+int main (void) {
     gboolean succ = FALSE;
     gchar *err_msg = NULL;
 
diff --git a/src/utils/sizes.c b/src/utils/sizes.c
index efb14d7..35c4798 100644
--- a/src/utils/sizes.c
+++ b/src/utils/sizes.c
@@ -36,7 +36,3 @@ gchar* bd_utils_size_human_readable (guint64 size) {
     else
         return g_strdup_printf ("%.2f %sB", value, size_prefixes[i]);
 }
-
-#ifdef TESTING_SIZES
-#include "test_sizes.c"
-#endif
diff --git a/src/utils/test_sizes.c b/src/utils/test_sizes.c
index 2e29887..269b238 100644
--- a/src/utils/test_sizes.c
+++ b/src/utils/test_sizes.c
@@ -1,4 +1,6 @@
-int main (int argc, char **argv) {
+#include "sizes.c"
+
+int main () {
     gchar *human = NULL;
     human = bd_utils_size_human_readable ((16 MiB));
     puts (human);
-- 
1.9.3



More information about the anaconda-patches mailing list