diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2019-07-09 22:02:41 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2019-07-11 15:45:23 +0300 |
commit | a0cb8991b2cb61e9c0f3d8075759939cc61d57f0 (patch) | |
tree | 07349d877ea962acc800bdad2695417d10c99330 /libpkgconf/tests | |
parent | 6ef6c9d2da661bc315d4e690120db9079bfe0b9b (diff) |
Add implementation
Diffstat (limited to 'libpkgconf/tests')
-rw-r--r-- | libpkgconf/tests/.gitignore | 3 | ||||
-rw-r--r-- | libpkgconf/tests/api/buildfile | 7 | ||||
-rw-r--r-- | libpkgconf/tests/api/driver.c | 187 | ||||
-rw-r--r-- | libpkgconf/tests/api/testscript | 116 | ||||
-rw-r--r-- | libpkgconf/tests/basic/buildfile | 7 | ||||
-rw-r--r-- | libpkgconf/tests/basic/driver.c | 170 | ||||
-rw-r--r-- | libpkgconf/tests/basic/testscript | 74 | ||||
-rw-r--r-- | libpkgconf/tests/build/.gitignore | 3 | ||||
-rw-r--r-- | libpkgconf/tests/build/bootstrap.build | 9 | ||||
-rw-r--r-- | libpkgconf/tests/build/root.build | 24 | ||||
-rw-r--r-- | libpkgconf/tests/buildfile | 5 |
11 files changed, 605 insertions, 0 deletions
diff --git a/libpkgconf/tests/.gitignore b/libpkgconf/tests/.gitignore new file mode 100644 index 0000000..2e508a9 --- /dev/null +++ b/libpkgconf/tests/.gitignore @@ -0,0 +1,3 @@ +driver +test/ +test-*/ diff --git a/libpkgconf/tests/api/buildfile b/libpkgconf/tests/api/buildfile new file mode 100644 index 0000000..3b343e7 --- /dev/null +++ b/libpkgconf/tests/api/buildfile @@ -0,0 +1,7 @@ +# file : tests/api/buildfile +# copyright : Copyright (c) 2016-2019 Code Synthesis Ltd +# license : ISC; see accompanying COPYING file + +import libs = libpkgconf%lib{pkgconf} + +exe{driver}: {h c}{*} $libs testscript diff --git a/libpkgconf/tests/api/driver.c b/libpkgconf/tests/api/driver.c new file mode 100644 index 0000000..b5e42f3 --- /dev/null +++ b/libpkgconf/tests/api/driver.c @@ -0,0 +1,187 @@ +/* file : tests/api/driver.c + * copyright : Copyright (c) 2016-2019 Code Synthesis Ltd + * license : ISC; see accompanying COPYING file + */ + +/* + * Enable assertions. + */ +#ifdef NDEBUG +# undef NDEBUG +#endif + +#include <libpkgconf/libpkgconf.h> + +#include <stdio.h> /* printf(), fprintf(), stderr */ +#include <stddef.h> /* size_t, NULL */ +#include <assert.h> +#include <string.h> /* strcmp(), strlen() */ +#include <stdbool.h> /* 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) <path> + * + * 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 '<name> <value>' format. + * + * --libs + * Print linker flags in the '<name> <value>' format. + * + * --vars + * Print variables in the '<name> <value>' 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 */, + pkgconf_cross_personality_default ()); + + 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/libpkgconf/tests/api/testscript b/libpkgconf/tests/api/testscript new file mode 100644 index 0000000..88d8d3e --- /dev/null +++ b/libpkgconf/tests/api/testscript @@ -0,0 +1,116 @@ +# file : tests/api/testscript +# copyright : Copyright (c) 2016-2019 Code Synthesis Ltd +# license : ISC; see accompanying COPYING file + +: double-quoted +: +{ + +cat <<EOI >=libfoo.pc + prefix="C:\\Program Files\\Foo" + exec_prefix=${prefix} + var="A\"B" 'C\'D' + 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 + var A"B 'C\'D' + exec_prefix C:\\Program Files\\Foo + prefix C:\\Program Files\\Foo + EOO +} + +: single-quoted +{ + +cat <<EOI >=libfoo.pc + prefix='C:\Program Files\Foo' + exec_prefix=${prefix} + var='A\'B' "C\"D" + 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 + var A'B "C\"D" + exec_prefix C:\Program Files\Foo + prefix C:\Program Files\Foo + EOO +} + +: unquoted +: +{ + +cat <<EOI >=libfoo.pc + prefix=C:\\Program\ \ \ Files\\Foo + exec_prefix=${prefix} + var=X A\'B' "C\"D" + 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 + var X A\'B' "C\"D" + exec_prefix C:\\Program\ \ \ Files\\Foo + prefix C:\\Program\ \ \ Files\\Foo + EOO +} diff --git a/libpkgconf/tests/basic/buildfile b/libpkgconf/tests/basic/buildfile new file mode 100644 index 0000000..5446ccd --- /dev/null +++ b/libpkgconf/tests/basic/buildfile @@ -0,0 +1,7 @@ +# file : tests/basic/buildfile +# copyright : Copyright (c) 2016-2019 Code Synthesis Ltd +# license : ISC; see accompanying COPYING file + +import libs = libpkgconf%lib{pkgconf} + +exe{driver}: {h c}{*} $libs testscript diff --git a/libpkgconf/tests/basic/driver.c b/libpkgconf/tests/basic/driver.c new file mode 100644 index 0000000..b14e0cc --- /dev/null +++ b/libpkgconf/tests/basic/driver.c @@ -0,0 +1,170 @@ +/* file : tests/basic/driver.c + * copyright : Copyright (c) 2016-2019 Code Synthesis Ltd + * license : ISC; see accompanying COPYING file + */ + +/* + * Enable assertions. + */ +#ifdef NDEBUG +# undef NDEBUG +#endif + +#include <libpkgconf/libpkgconf.h> + +#include <stdio.h> /* printf(), fprintf(), stderr */ +#include <stddef.h> /* NULL */ +#include <stdlib.h> /* free() */ +#include <assert.h> +#include <string.h> /* strcmp() */ +#include <stdbool.h> /* 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 +print_and_free (pkgconf_list_t* list) +{ + char* buf = pkgconf_fragment_render (list, + true /* escape */, + NULL /* options */); + printf("%s", buf); + free (buf); + + pkgconf_fragment_free (list); +} + +/* + * Usage: argv[0] [--cflags] [--libs] (--with-path <dir>)* <name> + * + * Print package compiler and linker flags. If the package name has '.pc' + * extension it is interpreted as a file name. Prints all flags, as pkgconf + * utility does when --keep-system-libs and --keep-system-cflags are specified. + * + * --cflags + * Print compiler flags. + * + * --libs + * Print linker flags. + * + * --with-path <dir> + * Search through the directory for pc-files. If at least one --with-path + * is specified then the default directories are not searched through. + */ +int +main (int argc, const char* argv[]) +{ + pkgconf_client_t* c = + pkgconf_client_new (error_handler, + NULL /* error_handler_data */, + pkgconf_cross_personality_default ()); + + assert (c != NULL); + + bool cflags = false; + bool libs = false; + bool default_dirs = true; + int client_flags = 0; + + int i = 1; + for (; i < argc; ++i) + { + const char* o = argv[i]; + + if (strcmp (o, "--cflags") == 0) + cflags = true; + else if (strcmp (o, "--libs") == 0) + libs = true; + else if (strcmp (o, "--static") == 0) + client_flags = PKGCONF_PKG_PKGF_SEARCH_PRIVATE | + PKGCONF_PKG_PKGF_MERGE_PRIVATE_FRAGMENTS; + else if (strcmp (o, "--with-path") == 0) + { + ++i; + assert (i < argc); + + pkgconf_path_add (argv[i], &c->dir_list, /* filter_duplicates */ true); + default_dirs = false; + } + else + break; + } + + assert (i + 1 == argc); + const char* name = argv[i]; + + int r = 1; + int max_depth = 2000; + + pkgconf_client_set_flags (c, client_flags); + + /* + * Bootstrap the package search default paths if not specified explicitly. + */ + if (default_dirs) + pkgconf_client_dir_list_build (c, pkgconf_cross_personality_default ()); + + pkgconf_pkg_t* p = pkgconf_pkg_find (c, name); + + if (p != NULL) + { + int e = PKGCONF_PKG_ERRF_OK; + + /* + * Print C flags. + */ + if (cflags) + { + pkgconf_client_set_flags ( + c, + client_flags | 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) + print_and_free (&list); + + pkgconf_client_set_flags (c, client_flags); /* Restore. */ + } + + /* + * Print libs. + */ + if (libs && e == PKGCONF_PKG_ERRF_OK) + { + pkgconf_list_t list = PKGCONF_LIST_INITIALIZER; + e = pkgconf_pkg_libs (c, p, &list, max_depth); + + if (e == PKGCONF_PKG_ERRF_OK) + print_and_free (&list); + } + + if (e == PKGCONF_PKG_ERRF_OK) + { + r = 0; + + if (cflags || libs) + printf ("\n"); + } + + pkgconf_pkg_unref (c, p); + } + else + fprintf (stderr, "package '%s' not found\n", name); + + pkgconf_client_free (c); + return r; +} diff --git a/libpkgconf/tests/basic/testscript b/libpkgconf/tests/basic/testscript new file mode 100644 index 0000000..081647c --- /dev/null +++ b/libpkgconf/tests/basic/testscript @@ -0,0 +1,74 @@ +# file : tests/basic/testscript +# copyright : Copyright (c) 2016-2019 Code Synthesis Ltd +# license : ISC; see accompanying COPYING file + +test.options = --with-path $~ + ++cat <<EOI >=openssl.pc +prefix=/usr +exec_prefix=${prefix} +libdir=${exec_prefix}/lib64 +includedir=${prefix}/include + +Name: OpenSSL +Description: Secure Sockets Layer and cryptography libraries and tools +Version: 1.0.2g +Requires: libssl libcrypto +EOI + ++cat <<EOI >=libssl.pc +prefix=/usr +exec_prefix=${prefix} +libdir=${exec_prefix}/lib64 +includedir=${prefix}/include + +Name: OpenSSL-libssl +Description: Secure Sockets Layer and cryptography libraries +Version: 1.0.2g +Requires.private: libcrypto +Libs: -L${libdir} -lssl +Libs.private: -ldl -lz -lgssapi_krb5 -lkrb5 -lcom_err -lk5crypto +Cflags: -I${includedir} -I/usr/include +EOI + ++cat <<EOI >=libcrypto.pc +prefix=/usr +exec_prefix=${prefix} +libdir=${exec_prefix}/lib64 +includedir=${prefix}/include + +Name: OpenSSL-libcrypto +Description: OpenSSL cryptography library +Version: 1.0.2g +Requires: +Libs: -L${libdir} -lcrypto +Libs.private: -ldl -lz +Cflags: -I${includedir} +EOI + ++cat <<EOI >=libfaulty.pc +Name: faulty +Description: Faulty library +Version: 1.0 +Requires: non-existent +EOI + +: cflags +: +$* --cflags openssl >'-I/usr/include ' + +: libs +: +$* --libs openssl >'-L/usr/lib64 -lssl -lcrypto ' + +: libs-static +: +$* --libs --static openssl >'-L/usr/lib64 -lssl -ldl -lz -lgssapi_krb5 -lkrb5 -lcom_err -lk5crypto -L/usr/lib64 -ldl -lz -lcrypto -ldl -lz ' + +: non-existent +: +$* non-existent 2>"package 'non-existent' not found" == 1 + +: faulty +: +$* --cflags libfaulty 2>- == 1 diff --git a/libpkgconf/tests/build/.gitignore b/libpkgconf/tests/build/.gitignore new file mode 100644 index 0000000..4a730a3 --- /dev/null +++ b/libpkgconf/tests/build/.gitignore @@ -0,0 +1,3 @@ +config.build +root/ +bootstrap/ diff --git a/libpkgconf/tests/build/bootstrap.build b/libpkgconf/tests/build/bootstrap.build new file mode 100644 index 0000000..a14e33e --- /dev/null +++ b/libpkgconf/tests/build/bootstrap.build @@ -0,0 +1,9 @@ +# file : tests/build/bootstrap.build +# copyright : Copyright (c) 2016-2019 Code Synthesis Ltd +# license : ISC; see accompanying COPYING file + +project = # Unnamed subproject. + +using config +using dist +using test diff --git a/libpkgconf/tests/build/root.build b/libpkgconf/tests/build/root.build new file mode 100644 index 0000000..fa336d1 --- /dev/null +++ b/libpkgconf/tests/build/root.build @@ -0,0 +1,24 @@ +# file : tests/build/root.build +# copyright : Copyright (c) 2016-2019 Code Synthesis Ltd +# license : ISC; see accompanying COPYING file + +c.std = 99 + +using c + +h{*}: extension = h +c{*}: extension = c + +if ($c.class == 'msvc') +{ + c.poptions += -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS + c.coptions += /wd4251 /wd4275 /wd4800 +} + +# Every exe{} in this subproject is by default a test. +# +exe{*}: test = true + +# Specify the test target for cross-testing. +# +test.target = $c.target diff --git a/libpkgconf/tests/buildfile b/libpkgconf/tests/buildfile new file mode 100644 index 0000000..d48342d --- /dev/null +++ b/libpkgconf/tests/buildfile @@ -0,0 +1,5 @@ +# file : tests/buildfile +# copyright : Copyright (c) 2016-2019 Code Synthesis Ltd +# license : ISC; see accompanying COPYING file + +./: {*/ -build/} |