aboutsummaryrefslogtreecommitdiff
path: root/tests/basic
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-09-06 17:40:06 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-09-11 13:58:25 +0300
commit4beec8f055fd3b0cc4ef618cce8b52c58dd0ee08 (patch)
tree08dc183e96625ef8b2068432154851e014047d89 /tests/basic
parent507d73eaa2accb7ad880e8582fd4ffab45b7effd (diff)
Add implementation
Diffstat (limited to 'tests/basic')
-rw-r--r--tests/basic/buildfile7
-rw-r--r--tests/basic/driver.c166
-rw-r--r--tests/basic/testscript74
3 files changed, 247 insertions, 0 deletions
diff --git a/tests/basic/buildfile b/tests/basic/buildfile
new file mode 100644
index 0000000..8abc795
--- /dev/null
+++ b/tests/basic/buildfile
@@ -0,0 +1,7 @@
+# file : tests/basic/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/basic/driver.c b/tests/basic/driver.c
new file mode 100644
index 0000000..1f861de
--- /dev/null
+++ b/tests/basic/driver.c
@@ -0,0 +1,166 @@
+/* file : tests/basic/driver.c
+ * copyright : Copyright (c) 2016-2017 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, /* escape */ true);
+ 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, /* error_handler_data */ NULL);
+
+ 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_pkg_dir_list_build (c);
+
+ 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/tests/basic/testscript b/tests/basic/testscript
new file mode 100644
index 0000000..d7fe796
--- /dev/null
+++ b/tests/basic/testscript
@@ -0,0 +1,74 @@
+# file : tests/basic/testscript
+# copyright : Copyright (c) 2016-2017 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