[CRIU] [PATCH 4/5] compel: piegen -- Introduce actions

Cyrill Gorcunov gorcunov at openvz.org
Tue Apr 5 08:41:14 PDT 2016


Here we introduce actions:

 - "piegen" to generate blobs, which is used
    by criu already;

 - "cflags" and "ldflags" to print out options
   needed for compiler and linker when building
   compel compatible objects.

We rather moved old "main" function body into
piegen helper function and implement the rest
of actions since they are one-liners.

Note the usage uses new "compel" brand, but it's
safe because we don't export anything yet.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 criu/pie/Makefile      |  2 +-
 criu/pie/piegen/main.c | 96 ++++++++++++++++++++++++++++++++------------------
 2 files changed, 63 insertions(+), 35 deletions(-)

diff --git a/criu/pie/Makefile b/criu/pie/Makefile
index c125e8c40f90..504562e5dd1d 100644
--- a/criu/pie/Makefile
+++ b/criu/pie/Makefile
@@ -70,7 +70,7 @@ $(obj)/%.built-in.bin.o: $(obj)/%.built-in.o $(obj)/lib.a $(obj)/$(PIELDS)
 
 $(obj)/%-blob.h: $(obj)/%.built-in.bin.o $(obj)/$(PIELDS) pie/piegen
 	$(call msg-gen, $@)
-	$(Q) pie/piegen/piegen -f $< -v $(call target-name,$@)_relocs -p $(call target-name,$@)_blob_offset__ -s $(call target-name,$@)_blob -o $@ $(piegen_stdout)
+	$(Q) pie/piegen/piegen piegen -f $< -v $(call target-name,$@)_relocs -p $(call target-name,$@)_blob_offset__ -s $(call target-name,$@)_blob -o $@ $(piegen_stdout)
 
 else
 
diff --git a/criu/pie/piegen/main.c b/criu/pie/piegen/main.c
index 546430c9e876..23c96f7af9c1 100644
--- a/criu/pie/piegen/main.c
+++ b/criu/pie/piegen/main.c
@@ -70,17 +70,53 @@ static int handle_elf(void *mem, size_t size)
 	return -1;
 }
 
-/*
- * That;s the tool to generate patches object files.
- */
-int main(int argc, char *argv[])
+static int piegen(void)
 {
-	const char *current_cflags = NULL;
 	struct stat st;
-	int opt, idx, i;
 	void *mem;
 	int fd;
 
+	fd = open(opts.input_filename, O_RDONLY);
+	if (fd < 0) {
+		pr_perror("Can't open file %s", opts.input_filename);
+		goto err;
+	}
+
+	if (fstat(fd, &st)) {
+		pr_perror("Can't stat file %s", opts.input_filename);
+		goto err;
+	}
+
+	fout = fopen(opts.output_filename, "w");
+	if (fout == NULL) {
+		pr_perror("Can't open %s", opts.output_filename);
+		goto err;
+	}
+
+	mem = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0);
+	if (mem == MAP_FAILED) {
+		pr_perror("Can't mmap file %s", opts.input_filename);
+		goto err;
+	}
+
+	if (handle_elf(mem, st.st_size)) {
+		fclose(fout);
+		unlink(opts.output_filename);
+		goto err;
+	}
+
+err:
+	fclose(fout);
+	printf("%s generated successfully.\n", opts.output_filename);
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	const char *current_cflags = NULL;
+	int opt, idx, i;
+	char *action;
+
 	typedef struct {
 		const char	*arch;
 		const char	*cflags;
@@ -161,47 +197,39 @@ int main(int argc, char *argv[])
 			opts.nrgotpcrel_name = optarg;
 			break;
 		case 'h':
-		default:
 			goto usage;
+		default:
+			break;
 		}
 	}
 
-	if (!opts.input_filename)
+	if (optind >= argc)
 		goto usage;
 
-	fd = open(opts.input_filename, O_RDONLY);
-	if (fd < 0) {
-		pr_perror("Can't open file %s", opts.input_filename);
-		goto err;
-	}
+	action = argv[optind++];
 
-	if (fstat(fd, &st)) {
-		pr_perror("Can't stat file %s", opts.input_filename);
-		goto err;
+	if (!strcmp(action, "cflags")) {
+		if (!current_cflags)
+			goto usage;
+		printf("%s", current_cflags);
+		return 0;
 	}
 
-	fout = fopen(opts.output_filename, "w");
-	if (fout == NULL) {
-		pr_perror("Can't open %s", opts.output_filename);
-		goto err;
+	if (!strcmp(action, "ldflags")) {
+		printf("%s", compel_ldflags);
+		return 0;
 	}
 
-	mem = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0);
-	if (mem == MAP_FAILED) {
-		pr_perror("Can't mmap file %s", opts.input_filename);
-		goto err;
+	if (!strcmp(action, "piegen")) {
+		if (!opts.input_filename)
+			goto usage;
+		return piegen();
 	}
 
-	if (handle_elf(mem, st.st_size)) {
-		fclose(fout);
-		unlink(opts.output_filename);
-		goto err;
-	}
-	fclose(fout);
-	printf("%s generated successfully.\n", opts.output_filename);
-	return 0;
 usage:
-	fprintf(stderr, "Usage: %s -f filename\n", argv[0]);
-err:
+	printf("Usage:\n");
+	printf("  compel --arch=(x86|ia32|aarch64|arm|ppc64) cflags\n");
+	printf("  compel --arch=(x86|ia32|aarch64|arm|ppc64) ldflags\n");
+	printf("  compel -f filename piegen\n");
 	return 1;
 }
-- 
2.5.5



More information about the CRIU mailing list