[CRIU] [PATCH 06/24] compel cli: kill --arch option, add --compat

Kir Kolyshkin kir at openvz.org
Sat Dec 17 03:21:59 PST 2016


There is no need to support all possible architectures
for "compel cflags" action. In fact, "compel hgen" can
only support the one it was compiled for (with the only
exception of 32-bit mode for x86).

It looks like if we can use a few #ifdefs, there is
no need to specify --arch anymore, let's drop it!

Still, for the x86 32-bit mode we need to introduce
--compat option. Note that "compel hgen" autodetects
32-bit mode for x86 by looking into ELF header, but
in case of "compel clfags" there are no files to look
into, so we need this --compat specified explicitly.

While at it,
 - Makefile: define CONFIG_AARCH64 if building for ARM64
 - fail to compile on unsupported/unspecified ARCH
 - make "compel --help" output a bit more compact

Signed-off-by: Kir Kolyshkin <kir at openvz.org>
---
 Makefile                    |  4 +++
 compel/src/main.c           | 86 +++++++++++++++------------------------------
 compel/test/infect/Makefile |  2 +-
 criu/pie/Makefile           |  2 +-
 4 files changed, 35 insertions(+), 59 deletions(-)

diff --git a/Makefile b/Makefile
index bfa328d..9b7b474 100644
--- a/Makefile
+++ b/Makefile
@@ -72,6 +72,10 @@ ifeq ($(ARCH),arm)
         PROTOUFIX	:= y
 endif
 
+ifeq ($(ARCH),aarch64)
+	DEFINES		:= -DCONFIG_AARCH64
+endif
+
 ifeq ($(ARCH),x86)
         DEFINES		:= -DCONFIG_X86_64
 endif
diff --git a/compel/src/main.c b/compel/src/main.c
index 4202b12..9be3c8b 100644
--- a/compel/src/main.c
+++ b/compel/src/main.c
@@ -28,27 +28,23 @@
 #define COMPEL_LDFLAGS_DEFAULT "-r -z noexecstack"
 
 typedef struct {
-	const char	*arch;
 	const char	*cflags;
-} compel_cflags_t;
-
-static const compel_cflags_t compel_cflags[] = {
-	{
-		.arch	= "x86",
-		.cflags	= COMPEL_CFLAGS_PIE,
-	}, {
-		.arch	= "ia32",
-		.cflags	= COMPEL_CFLAGS_NOPIC,
-	}, {
-		.arch	= "aarch64",
-		.cflags	= COMPEL_CFLAGS_PIE,
-	}, {
-		.arch	= "arm",
-		.cflags	= COMPEL_CFLAGS_PIE,
-	}, {
-		.arch	= "ppc64",
-		.cflags	= COMPEL_CFLAGS_PIE,
-	},
+	const char	*cflags_compat;
+} flags_t;
+
+static const flags_t flags = {
+#if defined CONFIG_X86_64
+	.cflags		= COMPEL_CFLAGS_PIE,
+	.cflags_compat	= COMPEL_CFLAGS_NOPIC,
+#elif defined CONFIG_AARCH64
+	.cflags		= COMPEL_CFLAGS_PIE,
+#elif defined(CONFIG_ARMV6) || defined(CONFIG_ARMV7)
+	.cflags		= COMPEL_CFLAGS_PIE,
+#elif defined CONFIG_PPC64
+	.cflags		= COMPEL_CFLAGS_PIE,
+#else
+#error "CONFIG_<ARCH> not defined, or unsupported ARCH"
+#endif
 };
 
 piegen_opt_t opts = {};
@@ -114,24 +110,9 @@ static void cli_log(unsigned int lvl, const char *fmt, va_list parms)
 }
 
 static int usage(int rc) {
-	int i = 0;
 	printf(
 "Usage:\n"
-"  compel --arch=ARCH cflags\n"
-"  compel --arch=ARCH ldflags\n"
-"    ARCH := { "
-);
-
-	/* Print list of known arches */
-	while (1) {
-		printf("%s", compel_cflags[i++].arch);
-		if (i == ARRAY_SIZE(compel_cflags))
-			break;
-		printf(" | ");
-	}
-
-	printf(
-" }\n"
+"  compel [--compat] cflags | ldflags\n"
 "  compel -f FILE -o FILE -p NAME [-l N] hgen\n"
 "    -f, --file FILE		input (parasite object) file name\n"
 "    -o, --output FILE		output (header) file name\n"
@@ -145,16 +126,21 @@ static int usage(int rc) {
 	return rc;
 }
 
+static void print_cflags(bool compat)
+{
+	printf("%s\n", compat ? flags.cflags_compat : flags.cflags);
+}
+
 int main(int argc, char *argv[])
 {
-	const char *current_cflags = NULL;
 	int log_level = DEFAULT_LOGLEVEL;
-	int opt, idx, i;
+	bool compat = false;
+	int opt, idx;
 	char *action;
 
-	static const char short_opts[] = "a:f:o:p:hVl:";
+	static const char short_opts[] = "cf:o:p:hVl:";
 	static struct option long_opts[] = {
-		{ "arch",	required_argument,	0, 'a' },
+		{ "compat",	no_argument,		0, 'c' },
 		{ "file",	required_argument,	0, 'f' },
 		{ "output",	required_argument,	0, 'o' },
 		{ "prefix",	required_argument,	0, 'p' },
@@ -170,18 +156,8 @@ int main(int argc, char *argv[])
 		if (opt == -1)
 			break;
 		switch (opt) {
-		case 'a':
-			for (i = 0; i < ARRAY_SIZE(compel_cflags); i++) {
-				if (!strcmp(optarg, compel_cflags[i].arch)) {
-					current_cflags = compel_cflags[i].cflags;
-					break;
-				}
-			}
-			if (!current_cflags) {
-				fprintf(stderr, "Error: unknown arch '%s'\n",
-						optarg);
-				return usage(1);
-			}
+		case 'c':
+			compat = true;
 			break;
 		case 'f':
 			opts.input_filename = optarg;
@@ -218,11 +194,7 @@ int main(int argc, char *argv[])
 	action = argv[optind++];
 
 	if (!strcmp(action, "cflags")) {
-		if (!current_cflags) {
-			fprintf(stderr, "Error: option --arch required\n");
-			return usage(1);
-		}
-		printf("%s", current_cflags);
+		print_cflags(compat);
 		return 0;
 	}
 
diff --git a/compel/test/infect/Makefile b/compel/test/infect/Makefile
index f244755..ede9a47 100644
--- a/compel/test/infect/Makefile
+++ b/compel/test/infect/Makefile
@@ -32,4 +32,4 @@ parasite.po: parasite.o $(COMPEL_PLUGINS)/std.built-in.o
 	ld -r -T $(COMPEL_PACK_LDS) -o $@ $^
 
 parasite.o: parasite.c
-	$(CC) $(CFLAGS) -c $(shell $(COMPEL) --arch=$(ARCH) cflags) -I$(COMPEL_IDIR) -o $@ $^
+	$(CC) $(CFLAGS) -c $(shell $(COMPEL) cflags) -I$(COMPEL_IDIR) -o $@ $^
diff --git a/criu/pie/Makefile b/criu/pie/Makefile
index 2e9b1d3..a45936c 100644
--- a/criu/pie/Makefile
+++ b/criu/pie/Makefile
@@ -34,7 +34,7 @@ ccflags-y	+= -DCR_NOGLIBC
 ccflags-y	+= -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0
 
 ifneq ($(filter-out clean mrproper,$(MAKECMDGOALS)),)
-        CFLAGS	+= $(shell $(SRC_DIR)/compel/compel-host --arch=$(ARCH) cflags)
+        CFLAGS	+= $(shell $(SRC_DIR)/compel/compel-host cflags)
 endif
 
 ifeq ($(SRCARCH),arm)
-- 
2.7.4



More information about the CRIU mailing list