From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 20BB7431E2;
	Mon, 23 Oct 2023 15:17:01 +0200 (CEST)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 1BBE242DDD;
	Mon, 23 Oct 2023 15:16:18 +0200 (CEST)
Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93])
 by mails.dpdk.org (Postfix) with ESMTP id 685BF42DC3
 for <dev@dpdk.org>; Mon, 23 Oct 2023 15:16:11 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple;
 d=intel.com; i=@intel.com; q=dns/txt; s=Intel;
 t=1698066971; x=1729602971;
 h=from:to:cc:subject:date:message-id:in-reply-to:
 references:mime-version:content-transfer-encoding;
 bh=eHJRZHmN4kFqKeivEFmViSHUGQL6TI2gSbwpJ0KbhFQ=;
 b=P0Nl+G5y+eA+F4KwoX3+8k6T6Q0oJuAUTUzOTSXtMSIChvfNu5mqFHtB
 iUQCNTwkATY/W9lVQs5U8AXVbO2S8zQJm+TybjQIJaA+EspxG40X6Ms10
 HUIW0Va1+gs+0CBxuLuSsm/wBa0x47lggo1uFBBkMWr4FczwUG1DCq/11
 PmlMRcqBNmbfCxzysPUYVrPvf8L32BJz2DmHwZOjMZ3Qn1LasQCIE9JSr
 +WSI8hD1yGDG0wTJt4I9iup6CTWMkqfCwZA4YFhs5V6YYPuHBDzb1XlTZ
 oBNMSKJZK5Acwu0Zh17NB7JqPw2z3mK2yTujm9xPVaSQhcND3B6vp7jJL A==;
X-IronPort-AV: E=McAfee;i="6600,9927,10872"; a="384033704"
X-IronPort-AV: E=Sophos;i="6.03,244,1694761200"; d="scan'208";a="384033704"
Received: from orsmga005.jf.intel.com ([10.7.209.41])
 by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384;
 23 Oct 2023 06:16:10 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=McAfee;i="6600,9927,10872"; a="931680672"
X-IronPort-AV: E=Sophos;i="6.03,244,1694761200"; d="scan'208";a="931680672"
Received: from silpixa00401385.ir.intel.com ([10.237.214.154])
 by orsmga005.jf.intel.com with ESMTP; 23 Oct 2023 06:16:09 -0700
From: Bruce Richardson <bruce.richardson@intel.com>
To: dev@dpdk.org
Cc: david.marchand@redhat.com,
	Bruce Richardson <bruce.richardson@intel.com>
Subject: [PATCH v6 7/9] examples/vdpa: auto-generate cmdline boilerplate
Date: Mon, 23 Oct 2023 14:15:50 +0100
Message-Id: <20231023131552.1024375-8-bruce.richardson@intel.com>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20231023131552.1024375-1-bruce.richardson@intel.com>
References: <20230802170052.955323-1-bruce.richardson@intel.com>
 <20231023131552.1024375-1-bruce.richardson@intel.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

Use the dpdk-cmdline-gen script to autogenerate all the boilerplate
structs and defines for the commandline part of the app.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 examples/vdpa/Makefile      |  12 ++--
 examples/vdpa/commands.list |   8 +++
 examples/vdpa/main.c        | 131 ++----------------------------------
 examples/vdpa/meson.build   |   7 ++
 4 files changed, 30 insertions(+), 128 deletions(-)
 create mode 100644 examples/vdpa/commands.list

diff --git a/examples/vdpa/Makefile b/examples/vdpa/Makefile
index d974db4f40..aa60a000cf 100644
--- a/examples/vdpa/Makefile
+++ b/examples/vdpa/Makefile
@@ -6,6 +6,7 @@ APP = vdpa
 
 # all source are stored in SRCS-y
 SRCS-y := main.c
+SRC-DEPS := build/commands.h
 CFLAGS += -DALLOW_EXPERIMENTAL_API
 
 PKGCONF ?= pkg-config
@@ -23,10 +24,13 @@ static: build/$(APP)-static
 	ln -sf $(APP)-static build/$(APP)
 
 PC_FILE := $(shell $(PKGCONF) --path libdpdk 2>/dev/null)
-CFLAGS += -O3 $(shell $(PKGCONF) --cflags libdpdk)
+CFLAGS += -O3 $(shell $(PKGCONF) --cflags libdpdk) -I build/
 LDFLAGS_SHARED = $(shell $(PKGCONF) --libs libdpdk)
 LDFLAGS_STATIC = $(shell $(PKGCONF) --static --libs libdpdk)
 
+build/commands.h: commands.list Makefile
+	dpdk-cmdline-gen.py -o $@ --context-name=main_ctx $<
+
 ifeq ($(MAKECMDGOALS),static)
 # check for broken pkg-config
 ifeq ($(shell echo $(LDFLAGS_STATIC) | grep 'whole-archive.*l:lib.*no-whole-archive'),)
@@ -35,10 +39,10 @@ $(error "Cannot generate statically-linked binaries with this version of pkg-con
 endif
 endif
 
-build/$(APP)-shared: $(SRCS-y) Makefile $(PC_FILE) | build
+build/$(APP)-shared: $(SRCS-y) Makefile $(PC_FILE) | build $(SRC-DEPS)
 	$(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS) $(LDFLAGS_SHARED)
 
-build/$(APP)-static: $(SRCS-y) Makefile $(PC_FILE) | build
+build/$(APP)-static: $(SRCS-y) Makefile $(PC_FILE) | build $(SRC-DEPS)
 	$(CC) $(CFLAGS) $(SRCS-y) -o $@ $(LDFLAGS) $(LDFLAGS_STATIC)
 
 build:
@@ -46,5 +50,5 @@ build:
 
 .PHONY: clean
 clean:
-	rm -f build/$(APP) build/$(APP)-static build/$(APP)-shared
+	rm -f build/$(APP) build/$(APP)-static build/$(APP)-shared build/*.h
 	test -d build && rmdir -p build || true
diff --git a/examples/vdpa/commands.list b/examples/vdpa/commands.list
new file mode 100644
index 0000000000..1eb8486c45
--- /dev/null
+++ b/examples/vdpa/commands.list
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2023 Intel Corporation
+#
+help                                    # show help
+list                                    # list all available vdpa devices
+create <STRING>socket_path <STRING>bdf  # create a new vdpa port
+stats <STRING>bdf <UINT32>qid           # show device statistics
+quit                                    # exit application
diff --git a/examples/vdpa/main.c b/examples/vdpa/main.c
index 4d3203f3a7..289db26498 100644
--- a/examples/vdpa/main.c
+++ b/examples/vdpa/main.c
@@ -16,11 +16,8 @@
 #include <rte_pci.h>
 #include <rte_string_fns.h>
 
-#include <cmdline_parse.h>
 #include <cmdline_socket.h>
-#include <cmdline_parse_string.h>
-#include <cmdline_parse_num.h>
-#include <cmdline.h>
+#include "commands.h"  /* auto-generated file from commands.list */
 #include "vdpa_blk_compact.h"
 
 #define MAX_PATH_LEN 128
@@ -301,14 +298,9 @@ signal_handler(int signum)
 	}
 }
 
-/* interactive cmds */
+/* interactive cmd functions */
 
-/* *** Help command with introduction. *** */
-struct cmd_help_result {
-	cmdline_fixed_string_t help;
-};
-
-static void cmd_help_parsed(__rte_unused void *parsed_result,
+void cmd_help_parsed(__rte_unused void *parsed_result,
 		struct cmdline *cl,
 		__rte_unused void *data)
 {
@@ -325,25 +317,7 @@ static void cmd_help_parsed(__rte_unused void *parsed_result,
 	);
 }
 
-cmdline_parse_token_string_t cmd_help_help =
-	TOKEN_STRING_INITIALIZER(struct cmd_help_result, help, "help");
-
-cmdline_parse_inst_t cmd_help = {
-	.f = cmd_help_parsed,
-	.data = NULL,
-	.help_str = "show help",
-	.tokens = {
-		(void *)&cmd_help_help,
-		NULL,
-	},
-};
-
-/* *** List all available vdpa devices *** */
-struct cmd_list_result {
-	cmdline_fixed_string_t action;
-};
-
-static void cmd_list_vdpa_devices_parsed(
+void cmd_list_parsed(
 		__rte_unused void *parsed_result,
 		struct cmdline *cl,
 		__rte_unused void *data)
@@ -376,27 +350,7 @@ static void cmd_list_vdpa_devices_parsed(
 	}
 }
 
-cmdline_parse_token_string_t cmd_action_list =
-	TOKEN_STRING_INITIALIZER(struct cmd_list_result, action, "list");
-
-cmdline_parse_inst_t cmd_list_vdpa_devices = {
-	.f = cmd_list_vdpa_devices_parsed,
-	.data = NULL,
-	.help_str = "list all available vdpa devices",
-	.tokens = {
-		(void *)&cmd_action_list,
-		NULL,
-	},
-};
-
-/* *** Create new vdpa port *** */
-struct cmd_create_result {
-	cmdline_fixed_string_t action;
-	cmdline_fixed_string_t socket_path;
-	cmdline_fixed_string_t bdf;
-};
-
-static void cmd_create_vdpa_port_parsed(void *parsed_result,
+void cmd_create_parsed(void *parsed_result,
 		struct cmdline *cl,
 		__rte_unused void *data)
 {
@@ -417,33 +371,7 @@ static void cmd_create_vdpa_port_parsed(void *parsed_result,
 		devcnt++;
 }
 
-cmdline_parse_token_string_t cmd_action_create =
-	TOKEN_STRING_INITIALIZER(struct cmd_create_result, action, "create");
-cmdline_parse_token_string_t cmd_socket_path =
-	TOKEN_STRING_INITIALIZER(struct cmd_create_result, socket_path, NULL);
-cmdline_parse_token_string_t cmd_bdf =
-	TOKEN_STRING_INITIALIZER(struct cmd_create_result, bdf, NULL);
-
-cmdline_parse_inst_t cmd_create_vdpa_port = {
-	.f = cmd_create_vdpa_port_parsed,
-	.data = NULL,
-	.help_str = "create a new vdpa port",
-	.tokens = {
-		(void *)&cmd_action_create,
-		(void *)&cmd_socket_path,
-		(void *)&cmd_bdf,
-		NULL,
-	},
-};
-
-/* *** STATS *** */
-struct cmd_stats_result {
-	cmdline_fixed_string_t stats;
-	cmdline_fixed_string_t bdf;
-	uint16_t qid;
-};
-
-static void cmd_device_stats_parsed(void *parsed_result, struct cmdline *cl,
+void cmd_stats_parsed(void *parsed_result, struct cmdline *cl,
 				    __rte_unused void *data)
 {
 	struct cmd_stats_result *res = parsed_result;
@@ -525,31 +453,7 @@ static void cmd_device_stats_parsed(void *parsed_result, struct cmdline *cl,
 	}
 }
 
-cmdline_parse_token_string_t cmd_device_stats_ =
-	TOKEN_STRING_INITIALIZER(struct cmd_stats_result, stats, "stats");
-cmdline_parse_token_string_t cmd_device_bdf =
-	TOKEN_STRING_INITIALIZER(struct cmd_stats_result, bdf, NULL);
-cmdline_parse_token_num_t cmd_queue_id =
-	TOKEN_NUM_INITIALIZER(struct cmd_stats_result, qid, RTE_UINT32);
-
-cmdline_parse_inst_t cmd_device_stats = {
-	.f = cmd_device_stats_parsed,
-	.data = NULL,
-	.help_str = "stats: show device statistics",
-	.tokens = {
-		(void *)&cmd_device_stats_,
-		(void *)&cmd_device_bdf,
-		(void *)&cmd_queue_id,
-		NULL,
-	},
-};
-
-/* *** QUIT *** */
-struct cmd_quit_result {
-	cmdline_fixed_string_t quit;
-};
-
-static void cmd_quit_parsed(__rte_unused void *parsed_result,
+void cmd_quit_parsed(__rte_unused void *parsed_result,
 		struct cmdline *cl,
 		__rte_unused void *data)
 {
@@ -557,27 +461,6 @@ static void cmd_quit_parsed(__rte_unused void *parsed_result,
 	cmdline_quit(cl);
 }
 
-cmdline_parse_token_string_t cmd_quit_quit =
-	TOKEN_STRING_INITIALIZER(struct cmd_quit_result, quit, "quit");
-
-cmdline_parse_inst_t cmd_quit = {
-	.f = cmd_quit_parsed,
-	.data = NULL,
-	.help_str = "quit: exit application",
-	.tokens = {
-		(void *)&cmd_quit_quit,
-		NULL,
-	},
-};
-cmdline_parse_ctx_t main_ctx[] = {
-	(cmdline_parse_inst_t *)&cmd_help,
-	(cmdline_parse_inst_t *)&cmd_list_vdpa_devices,
-	(cmdline_parse_inst_t *)&cmd_create_vdpa_port,
-	(cmdline_parse_inst_t *)&cmd_device_stats,
-	(cmdline_parse_inst_t *)&cmd_quit,
-	NULL,
-};
-
 int
 main(int argc, char *argv[])
 {
diff --git a/examples/vdpa/meson.build b/examples/vdpa/meson.build
index bd086050dc..a48028da4d 100644
--- a/examples/vdpa/meson.build
+++ b/examples/vdpa/meson.build
@@ -16,3 +16,10 @@ allow_experimental_apis = true
 sources = files(
         'main.c',
 )
+cmd_h = custom_target('commands_hdr',
+	output: 'commands.h',
+	input: files('commands.list'),
+	capture: true,
+	command: [cmdline_gen_cmd, '--context-name=main_ctx', '@INPUT@']
+)
+sources += cmd_h
-- 
2.39.2