From 00824fd4db22e931193a0c86e38b22025b79306f Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 13 Dec 2017 00:56:44 +0300 Subject: Add libpkgconf API basic test --- tests/api/buildfile | 7 ++ tests/api/driver.c | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++ tests/api/testscript | 110 ++++++++++++++++++++++++++++++ 3 files changed, 302 insertions(+) create mode 100644 tests/api/buildfile create mode 100644 tests/api/driver.c create mode 100644 tests/api/testscript diff --git a/tests/api/buildfile b/tests/api/buildfile new file mode 100644 index 0000000..001001a --- /dev/null +++ b/tests/api/buildfile @@ -0,0 +1,7 @@ +# file : tests/api/buildfile +# copyright : Copyright (c) 2016-2017 Code Synthesis Ltd +# license : ISC; see accompanying COPYING file + +import libs = libpkgconf%lib{pkgconf} + +exe{driver}: {h c}{*} $libs test{testscript} diff --git a/tests/api/driver.c b/tests/api/driver.c new file mode 100644 index 0000000..2649b8d --- /dev/null +++ b/tests/api/driver.c @@ -0,0 +1,185 @@ +/* file : tests/api/driver.c + * copyright : Copyright (c) 2016-2017 Code Synthesis Ltd + * license : ISC; see accompanying COPYING file + */ + +/* + * Enable assertions. + */ +#ifdef NDEBUG +# undef NDEBUG +#endif + +#include + +#include /* printf(), fprintf(), stderr */ +#include /* size_t, NULL */ +#include +#include /* strcmp(), strlen() */ +#include /* bool, true, false */ + +static bool +error_handler (const char* msg, const pkgconf_client_t* c, const void* d) +{ + (void) c; /* Unused. */ + (void) d; /* Unused. */ + + /* + * Seems it always have a trailing newline char. Probably it still a good + * idea to check if it is. Let's see if it ever be missed. + * + */ + fprintf (stderr, "%s", msg); + return true; +} + +static void +frags_print_and_free (pkgconf_list_t* list) +{ + pkgconf_node_t *node; + PKGCONF_FOREACH_LIST_ENTRY(list->head, node) + { + pkgconf_fragment_t* frag = node->data; + printf("%c %s\n", frag->type, frag->data); + } + + pkgconf_fragment_free (list); +} + +static void +tuples_print (pkgconf_list_t *list) +{ + pkgconf_node_t *node; + PKGCONF_FOREACH_LIST_ENTRY(list->head, node) + { + pkgconf_tuple_t *tuple = node->data; + + // Skip the automatically added variable. + // + if (strcmp (tuple->key, "pcfiledir") != 0) + printf("%s %s\n", tuple->key, tuple->value); + } +} + +/* + * Usage: argv[0] (--cflags|--libs|--vars) + * + * Print package compiler flags, linker flags or variable name/values one per + * line. The specified package file must have .pc extension. + * + * --cflags + * Print compiler flags in the ' ' format. + * + * --libs + * Print linker flags in the ' ' format. + * + * --vars + * Print variables in the ' ' format. + */ +int +main (int argc, const char* argv[]) +{ + enum + { + dump_none, + dump_cflags, + dump_libs, + dump_vars + } mode = dump_none; + + int i = 1; + for (; i < argc; ++i) + { + const char* o = argv[i]; + + if (strcmp (o, "--cflags") == 0) + { + assert (mode == dump_none); + mode = dump_cflags; + } + else if (strcmp (o, "--libs") == 0) + { + assert (mode == dump_none); + mode = dump_libs; + } + else if (strcmp (o, "--vars") == 0) + { + assert (mode == dump_none); + mode = dump_vars; + } + else + break; + } + + assert (mode != dump_none); + + assert (i + 1 == argc); + const char* path = argv[i]; + + // Make sure the file has .pc extension. + // + size_t n = strlen (path); + assert (n > 3 && strcmp (path + n - 3, ".pc") == 0); + + pkgconf_client_t* c = + pkgconf_client_new (error_handler, NULL /* error_handler_data */); + + assert (c != NULL); + + int r = 1; + int max_depth = 2000; + + pkgconf_client_set_flags (c, 0); + pkgconf_pkg_t* p = pkgconf_pkg_find (c, path); + + if (p != NULL) + { + int e = PKGCONF_PKG_ERRF_OK; + + switch (mode) + { + case dump_cflags: + { + pkgconf_client_set_flags (c, PKGCONF_PKG_PKGF_SEARCH_PRIVATE); + + pkgconf_list_t list = PKGCONF_LIST_INITIALIZER; + e = pkgconf_pkg_cflags (c, p, &list, max_depth); + + if (e == PKGCONF_PKG_ERRF_OK) + frags_print_and_free (&list); + + pkgconf_client_set_flags (c, 0); /* Restore. */ + break; + } + case dump_libs: + { + pkgconf_list_t list = PKGCONF_LIST_INITIALIZER; + e = pkgconf_pkg_libs (c, p, &list, max_depth); + + if (e == PKGCONF_PKG_ERRF_OK) + frags_print_and_free (&list); + + break; + } + case dump_vars: + { + tuples_print (&p->vars); + break; + } + default: + { + assert (false); + } + } + + if (e == PKGCONF_PKG_ERRF_OK) + r = 0; + + pkgconf_pkg_unref (c, p); + } + else + fprintf (stderr, "package file '%s' not found or invalid\n", path); + + pkgconf_client_free (c); + return r; +} diff --git a/tests/api/testscript b/tests/api/testscript new file mode 100644 index 0000000..01f883b --- /dev/null +++ b/tests/api/testscript @@ -0,0 +1,110 @@ +# file : tests/api/testscript +# copyright : Copyright (c) 2016-2017 Code Synthesis Ltd +# license : ISC; see accompanying COPYING file + +: double-quoted +: +{ + +cat <=libfoo.pc + prefix="C:\\Program Files\\Foo" + exec_prefix=${prefix} + Name: libfoo + Description: Foo library + Version: 1.0 + Libs: "-LC:\\Program Files\\Foo" "-lC:\\Program Files\\Foo\\foo" + Cflags: "-IC:\\Program Files\\Foo" + EOI + + f = $~/libfoo.pc + + : cflags + : + $* --cflags $f >>EOO + I C:\Program Files\Foo + EOO + + : libs + : + $* --libs $f >>EOO + L C:\Program Files\Foo + l C:\Program Files\Foo\foo + EOO + + : vars + : + $* --vars $f >>EOO + exec_prefix "C:\\Program Files\\Foo" + prefix "C:\\Program Files\\Foo" + EOO +} + +: single-quoted +{ + +cat <=libfoo.pc + prefix='C:\Program Files\Foo' + exec_prefix=${prefix} + Name: libfoo + Description: Foo library + Version: 1.0 + Libs: '-LC:\Program Files\Foo' '-lC:\Program Files\Foo\foo' + Cflags: '-IC:\Program Files\Foo' + EOI + + f = $~/libfoo.pc + + : cflags + : + $* --cflags $f >>EOO + I C:Program FilesFoo + EOO + + : libs + : + $* --libs $f >>EOO + L C:Program FilesFoo + l C:Program FilesFoofoo + EOO + + : vars + : + $* --vars $f >>EOO + exec_prefix 'C:\Program Files\Foo' + prefix 'C:\Program Files\Foo' + EOO +} + +: unquoted +: +{ + +cat <=libfoo.pc + prefix=C:\\Program\ \ \ Files\\Foo + exec_prefix=${prefix} + Name: libfoo + Description: Foo library + Version: 1.0 + Libs: -LC:\\Program\ \ \ Files\\Foo -lC:\\Program\ \ \ Files\\Foo\\foo + Cflags: -IC:\\Program\ \ \ Files\\Foo + EOI + + f = $~/libfoo.pc + + : cflags + : + $* --cflags $f >>EOO + I C:\\Program Files\\Foo + EOO + + : libs + : + $* --libs $f >>EOO + L C:\\Program Files\\Foo + l C:\\Program Files\\Foo\\foo + EOO + + : vars + : + $* --vars $f >>EOO + exec_prefix C:\\Program\ \ \ Files\\Foo + prefix C:\\Program\ \ \ Files\\Foo + EOO +} -- cgit v1.1