[igt-dev] [PATCH i-g-t 01/36] lib: Introduce main function macros with custom args

Hiatt, Don don.hiatt at intel.com
Fri May 24 18:26:29 UTC 2019


From: igt-dev [mailto:igt-dev-bounces at lists.freedesktop.org] On Behalf Of Petri Latvala
Sent: Thursday, May 23, 2019 5:27 AM
To: igt-dev at lists.freedesktop.org
Cc: Latvala, Petri <petri.latvala at intel.com>
Subject: [igt-dev] [PATCH i-g-t 01/36] lib: Introduce main function macros with custom args

Instead of using a custom main function and calling igt_subtest_init_parse_opts / igt_simple_init_parse_opts, the _args variants of igt_main and igt_simple_main take the relevant parameters and pass them along to the correct init function.

Open-coding a custom main function is no longer necessary and not recommended.

Signed-off-by: Petri Latvala <petri.latvala at intel.com>
---
 lib/igt_core.h | 64 +++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 48 insertions(+), 16 deletions(-)

diff --git a/lib/igt_core.h b/lib/igt_core.h index 44634e0f..cd89921e 100644
--- a/lib/igt_core.h
+++ b/lib/igt_core.h
@@ -250,23 +250,38 @@ void __igt_subtest_group_restore(int);
 			       __igt_subtest_group_restore(igt_tokencat(__save,__LINE__) ))
 
 /**
- * igt_main:
- *
- * This is a magic control flow block used instead of a main() function for
- * tests with subtests. Open-coding the main() function is only recommended if
- * the test needs to parse additional command line arguments of its own.
- */
-#define igt_main \
+ * igt_main_args:
+ * @extra_short_opts: getopt_long() compliant list with additional 
+short options
+ * @extra_long_opts: getopt_long() compliant list with additional long 
+options
+ * @help_str: help string for the additional options
+ * @extra_opt_handler: handler for the additional options
+ * @handler_data: user data given to @extra_opt_handler when invoked
+ *
+ * This is a magic control flow block used instead of a main()
+ * function for tests with subtests, along with custom command line
+ * arguments. The macro parameters are passed directly to
+ * #igt_subtest_init_parse_opts.
+ */
+#define igt_main_args(short_opts, long_opts, help_str, opt_handler, 
+handler_data) \
 	static void igt_tokencat(__real_main, __LINE__)(void); \
 	int main(int argc, char **argv) { \
-		igt_subtest_init_parse_opts(&argc, argv, NULL, NULL, NULL, \
-					    NULL, NULL); \
+		igt_subtest_init_parse_opts(&argc, argv, \
+					    short_opts, long_opts, help_str, \
+					    opt_handler, handler_data); \
 		igt_tokencat(__real_main, __LINE__)(); \
 		igt_exit(); \
 	} \
 	static void igt_tokencat(__real_main, __LINE__)(void) \
 
 
+/**
+ * igt_main:
+ *
+ * This is a magic control flow block used instead of a main() function 
+for
+ * tests with subtests. Open-coding the main() function is not recommended.
+ */
+#define igt_main igt_main_args(NULL, NULL, NULL, NULL, NULL)
+
 const char *igt_test_name(void);
 void igt_simple_init_parse_opts(int *argc, char **argv,
 				const char *extra_short_opts,
@@ -289,23 +304,40 @@ void igt_simple_init_parse_opts(int *argc, char **argv,  #define igt_simple_init(argc, argv) \
 	igt_simple_init_parse_opts(&argc, argv, NULL, NULL, NULL, NULL, NULL);
 
+
 /**
- * igt_simple_main:
+ * igt_simple_main_args:
+ * @extra_short_opts: getopt_long() compliant list with additional 
+ short options
+ * @extra_long_opts: getopt_long() compliant list with additional long 
+ options
+ * @help_str: help string for the additional options
+ * @extra_opt_handler: handler for the additional options
+ * @handler_data: user data given to @extra_opt_handler when invoked
  *
- * This is a magic control flow block used instead of a main() function for
- * simple tests. Open-coding the main() function is only recommended if
- * the test needs to parse additional command line arguments of its own.
+ * This is a magic control flow block used instead of a main()
+ * function for simple tests with custom command line arguments. The
+ * macro parameters are passed directly to
+ * #igt_simple_init_parse_opts.
  */
-#define igt_simple_main \
+#define igt_simple_main_args(short_opts, long_opts, help_str, 
+opt_handler, handler_data) \
 	static void igt_tokencat(__real_main, __LINE__)(void); \
 	int main(int argc, char **argv) { \
-		igt_simple_init_parse_opts(&argc, argv, NULL, NULL, NULL, \
-					   NULL, NULL); \
+		igt_simple_init_parse_opts(&argc, argv, \
+					   short_opts, long_opts, help_str, \
+					   opt_handler, handler_data);	\
 		igt_tokencat(__real_main, __LINE__)(); \
 		igt_exit(); \
 	} \
 	static void igt_tokencat(__real_main, __LINE__)(void) \
 
+
+/**
+ * igt_simple_main:
+ *
+ * This is a magic control flow block used instead of a main() function 
+for
+ * simple tests. Open-coding the main() function is not recommended.
+ */
+#define igt_simple_main igt_simple_main_args(NULL, NULL, NULL, NULL, 
+NULL)
+
 /**
  * igt_constructor:
  *
--
2.19.1

This is really awesome! I was just looking at how to add some custom arguments to a gem_exec_parallel to aid in my debugging 
and was thinking something like this would be super useful. Thank you!

Reviewed-by: Don Hiatt <don.hiatt at intel.com>
_______________________________________________
igt-dev mailing list
igt-dev at lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev


More information about the igt-dev mailing list