From 79e23d781f8474a0422a7956b16b4812a6bfbd80 Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Tue, 8 Sep 2015 16:36:34 -0400 Subject: [PATCH] Move openslide-tools-common and test-common to new static library --- .gitignore | 1 + Makefile.am | 64 ++++++++++++---------- .../openslide-common-cmdline.c | 20 +++---- test/test-common.c => common/openslide-common-fd.c | 4 +- test/test-common.h => common/openslide-common.h | 25 ++++++++- test/extended.c | 4 +- test/try_open.c | 4 +- tools/openslide-quickhash1sum.c | 8 +-- tools/openslide-show-properties.c | 8 +-- tools/openslide-tools-common.h | 41 -------------- tools/openslide-write-png.c | 8 +-- 11 files changed, 86 insertions(+), 101 deletions(-) rename tools/openslide-tools-common.c => common/openslide-common-cmdline.c (84%) rename test/test-common.c => common/openslide-common-fd.c (97%) rename test/test-common.h => common/openslide-common.h (61%) delete mode 100644 tools/openslide-tools-common.h diff --git a/.gitignore b/.gitignore index fe9e6c3c..f1e6d9b3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *~ +*.a *.la *.lo *.loT diff --git a/Makefile.am b/Makefile.am index c8412f3d..ee1cec16 100644 --- a/Makefile.am +++ b/Makefile.am @@ -74,6 +74,7 @@ pkginclude_HEADERS = \ src/openslide-features.h noinst_HEADERS = \ + common/openslide-common.h \ src/openslide-cairo.h \ src/openslide-decode-gdkpixbuf.h \ src/openslide-decode-jp2k.h \ @@ -85,11 +86,23 @@ noinst_HEADERS = \ src/openslide-decode-xml.h \ src/openslide-error.h \ src/openslide-hash.h \ - src/openslide-private.h \ - test/test-common.h \ - tools/openslide-tools-common.h + src/openslide-private.h +# common program code + +noinst_LIBRARIES = common/libopenslide-common.a + +common_libopenslide_common_a_CPPFLAGS = $(COMMON_CPPFLAGS) +common_libopenslide_common_a_SOURCES = \ + common/openslide-common-cmdline.c \ + common/openslide-common-fd.c + +COMMON_CPPFLAGS = $(GLIB2_CFLAGS) -I$(top_srcdir)/src -I$(top_srcdir)/common +COMMON_LDADD = src/libopenslide.la common/libopenslide-common.a $(GLIB2_LIBS) + + +# test noinst_PROGRAMS = test/test test/try_open test/parallel test/query \ test/extended test/mosaic test/profile @@ -97,29 +110,27 @@ noinst_SCRIPTS = test/driver CLEANFILES += test/driver EXTRA_DIST += test/driver.in -test_test_CPPFLAGS = $(GLIB2_CFLAGS) $(CAIRO_CFLAGS) $(VALGRIND_CFLAGS) -I$(top_srcdir)/src +test_test_CPPFLAGS = $(COMMON_CPPFLAGS) $(CAIRO_CFLAGS) $(VALGRIND_CFLAGS) # VALGRIND_LIBS not needed -test_test_LDADD = src/libopenslide.la $(GLIB2_LIBS) $(CAIRO_LIBS) +test_test_LDADD = $(COMMON_LDADD) $(CAIRO_LIBS) -test_try_open_SOURCES = test/test-common.c test/try_open.c -test_try_open_CPPFLAGS = $(GLIB2_CFLAGS) -I$(top_srcdir)/src -test_try_open_LDADD = src/libopenslide.la $(GLIB2_LIBS) +test_try_open_CPPFLAGS = $(COMMON_CPPFLAGS) +test_try_open_LDADD = $(COMMON_LDADD) -test_parallel_CPPFLAGS = $(GLIB2_CFLAGS) -I$(top_srcdir)/src -test_parallel_LDADD = src/libopenslide.la $(GLIB2_LIBS) +test_parallel_CPPFLAGS = $(COMMON_CPPFLAGS) +test_parallel_LDADD = $(COMMON_LDADD) -test_query_CPPFLAGS = $(GLIB2_CFLAGS) -I$(top_srcdir)/src -test_query_LDADD = src/libopenslide.la $(GLIB2_LIBS) +test_query_CPPFLAGS = $(COMMON_CPPFLAGS) +test_query_LDADD = $(COMMON_LDADD) -test_extended_SOURCES = test/test-common.c test/extended.c -test_extended_CPPFLAGS = $(GLIB2_CFLAGS) -I$(top_srcdir)/src -test_extended_LDADD = src/libopenslide.la $(GLIB2_LIBS) +test_extended_CPPFLAGS = $(COMMON_CPPFLAGS) +test_extended_LDADD = $(COMMON_LDADD) -test_mosaic_CPPFLAGS = $(GLIB2_CFLAGS) $(CAIRO_CFLAGS) -I$(top_srcdir)/src -test_mosaic_LDADD = src/libopenslide.la $(GLIB2_LIBS) $(CAIRO_LIBS) +test_mosaic_CPPFLAGS = $(COMMON_CPPFLAGS) $(CAIRO_CFLAGS) +test_mosaic_LDADD = $(COMMON_LDADD) $(CAIRO_LIBS) -test_profile_CPPFLAGS = $(GLIB2_CFLAGS) $(VALGRIND_CFLAGS) -I$(top_srcdir)/src -test_profile_LDADD = src/libopenslide.la $(GLIB2_LIBS) +test_profile_CPPFLAGS = $(COMMON_CPPFLAGS) $(VALGRIND_CFLAGS) +test_profile_LDADD = $(COMMON_LDADD) if CYGWIN_CROSS_TEST noinst_PROGRAMS += test/symlink @@ -142,23 +153,20 @@ test/driver: test/driver.in Makefile # show-properties bin_PROGRAMS = tools/openslide-show-properties man_MANS = tools/openslide-show-properties.1 -tools_openslide_show_properties_SOURCES = tools/openslide-tools-common.c tools/openslide-show-properties.c -tools_openslide_show_properties_CPPFLAGS = -I$(top_srcdir)/src $(GLIB2_CFLAGS) -tools_openslide_show_properties_LDADD = src/libopenslide.la $(GLIB2_LIBS) +tools_openslide_show_properties_CPPFLAGS = $(COMMON_CPPFLAGS) +tools_openslide_show_properties_LDADD = $(COMMON_LDADD) # quickhash1sum bin_PROGRAMS += tools/openslide-quickhash1sum man_MANS += tools/openslide-quickhash1sum.1 -tools_openslide_quickhash1sum_SOURCES = tools/openslide-tools-common.c tools/openslide-quickhash1sum.c -tools_openslide_quickhash1sum_CPPFLAGS = -I$(top_srcdir)/src $(GLIB2_CFLAGS) -tools_openslide_quickhash1sum_LDADD = src/libopenslide.la $(GLIB2_LIBS) +tools_openslide_quickhash1sum_CPPFLAGS = $(COMMON_CPPFLAGS) +tools_openslide_quickhash1sum_LDADD = $(COMMON_LDADD) # write-png bin_PROGRAMS += tools/openslide-write-png man_MANS += tools/openslide-write-png.1 -tools_openslide_write_png_SOURCES = tools/openslide-tools-common.c tools/openslide-write-png.c -tools_openslide_write_png_CPPFLAGS = -I$(top_srcdir)/src $(LIBPNG_CFLAGS) $(GLIB2_CFLAGS) -tools_openslide_write_png_LDADD = src/libopenslide.la $(LIBPNG_LIBS) $(GLIB2_LIBS) +tools_openslide_write_png_CPPFLAGS = $(COMMON_CPPFLAGS) $(LIBPNG_CFLAGS) +tools_openslide_write_png_LDADD = $(COMMON_LDADD) $(LIBPNG_LIBS) # man pages EXTRA_DIST += $(man_MANS:=.in) diff --git a/tools/openslide-tools-common.c b/common/openslide-common-cmdline.c similarity index 84% rename from tools/openslide-tools-common.c rename to common/openslide-common-cmdline.c index 88dbf892..820ce79b 100644 --- a/tools/openslide-tools-common.c +++ b/common/openslide-common-cmdline.c @@ -30,7 +30,7 @@ #include #include #include "openslide.h" -#include "openslide-tools-common.h" +#include "openslide-common.h" #include "config.h" static const char *version_format = "%s " SUFFIXED_VERSION ", " @@ -53,7 +53,7 @@ static const GOptionEntry options[] = { }; -static GOptionContext *make_option_context(const struct openslide_tools_usage_info *info) { +static GOptionContext *make_option_context(const struct common_usage_info *info) { GOptionContext *octx = g_option_context_new(info->parameter_string); g_option_context_set_summary(octx, info->summary); g_option_context_add_main_entries(octx, options, NULL); @@ -67,7 +67,7 @@ static void free_argv(void) { g_strfreev(fixed_argv); } -bool _openslide_tools_fix_argv(int *argc, char ***argv) { +bool common_fix_argv(int *argc, char ***argv) { if (fixed_argv == NULL) { #ifdef G_OS_WIN32 fixed_argv = g_win32_get_command_line(); @@ -81,21 +81,19 @@ bool _openslide_tools_fix_argv(int *argc, char ***argv) { return true; } #else -bool _openslide_tools_fix_argv(int *argc G_GNUC_UNUSED, - char ***argv G_GNUC_UNUSED) { +bool common_fix_argv(int *argc G_GNUC_UNUSED, char ***argv G_GNUC_UNUSED) { return false; } #endif -void _openslide_tools_parse_commandline(const struct openslide_tools_usage_info *info, - int *argc, - char ***argv) { +void common_parse_commandline(const struct common_usage_info *info, + int *argc, char ***argv) { GError *err = NULL; GOptionContext *octx = make_option_context(info); // use modern parsing functions if possible, so we can properly handle // Unicode arguments on Windows - bool free_args = _openslide_tools_fix_argv(argc, argv); + bool free_args = common_fix_argv(argc, argv); if (free_args) { g_option_context_parse_strv(octx, argv, &err); *argc = g_strv_length(*argv); @@ -107,7 +105,7 @@ void _openslide_tools_parse_commandline(const struct openslide_tools_usage_info if (err) { fprintf(stderr, "%s: %s\n\n", g_get_prgname(), err->message); g_error_free(err); - _openslide_tools_usage(info); + common_usage(info); } else if (show_version) { fprintf(stderr, version_format, g_get_prgname(), openslide_get_version()); @@ -129,7 +127,7 @@ void _openslide_tools_parse_commandline(const struct openslide_tools_usage_info } } -void _openslide_tools_usage(const struct openslide_tools_usage_info *info) { +void common_usage(const struct common_usage_info *info) { GOptionContext *octx = make_option_context(info); gchar *help = g_option_context_get_help(octx, TRUE, NULL); diff --git a/test/test-common.c b/common/openslide-common-fd.c similarity index 97% rename from test/test-common.c rename to common/openslide-common-fd.c index 18a19334..8a1b3867 100644 --- a/test/test-common.c +++ b/common/openslide-common-fd.c @@ -38,9 +38,9 @@ #include #endif -#include "test-common.h" +#include "openslide-common.h" -char *get_fd_path(int fd) { +char *common_get_fd_path(int fd) { struct stat st; if (fstat(fd, &st)) { return NULL; diff --git a/test/test-common.h b/common/openslide-common.h similarity index 61% rename from test/test-common.h rename to common/openslide-common.h index dc78f74b..901b6693 100644 --- a/test/test-common.h +++ b/common/openslide-common.h @@ -19,9 +19,28 @@ * */ -#ifndef OPENSLIDE_TEST_TEST_COMMON_H -#define OPENSLIDE_TEST_TEST_COMMON_H +#ifndef OPENSLIDE_COMMON_H +#define OPENSLIDE_COMMON_H -char *get_fd_path(int fd); +#include +#include + +// cmdline + +struct common_usage_info { + const char *parameter_string; + const char *summary; +}; + +void common_parse_commandline(const struct common_usage_info *info, + int *argc, char ***argv); + +void common_usage(const struct common_usage_info *info) G_GNUC_NORETURN; + +bool common_fix_argv(int *argc, char ***argv); + +// fd + +char *common_get_fd_path(int fd); #endif diff --git a/test/extended.c b/test/extended.c index 97ff9dba..7e2ce143 100644 --- a/test/extended.c +++ b/test/extended.c @@ -33,7 +33,7 @@ #include #include -#include "test-common.h" +#include "openslide-common.h" #include "config.h" #define MAX_LEAK_FD 128 @@ -100,7 +100,7 @@ static gpointer cloexec_thread(const gpointer prog) { static void child_check_open_fds(void) { for (int i = 3; i < MAX_LEAK_FD; i++) { - gchar *path = get_fd_path(i); + gchar *path = common_get_fd_path(i); if (path != NULL) { printf("%s\n", path); g_free(path); diff --git a/test/try_open.c b/test/try_open.c index dd3a6a2f..729d8e9d 100644 --- a/test/try_open.c +++ b/test/try_open.c @@ -27,7 +27,7 @@ #include #include #include "openslide.h" -#include "test-common.h" +#include "openslide-common.h" #define MAX_FDS 128 #define TIME_ITERATIONS 5 @@ -255,7 +255,7 @@ int main(int argc, char **argv) { // Check for file descriptor leaks for (int i = 0; i < MAX_FDS; i++) { if (!g_hash_table_lookup(fds, GINT_TO_POINTER(i))) { - char *path = get_fd_path(i); + char *path = common_get_fd_path(i); if (path != NULL) { // leaked fprintf(stderr, "Leaked file descriptor to %s\n", path); diff --git a/tools/openslide-quickhash1sum.c b/tools/openslide-quickhash1sum.c index a18bac0b..80e00965 100644 --- a/tools/openslide-quickhash1sum.c +++ b/tools/openslide-quickhash1sum.c @@ -22,7 +22,7 @@ #include #include #include "openslide.h" -#include "openslide-tools-common.h" +#include "openslide-common.h" static gboolean process(const char *file) { openslide_t *osr = openslide_open(file); @@ -58,15 +58,15 @@ static gboolean process(const char *file) { } -static const struct openslide_tools_usage_info usage_info = { +static const struct common_usage_info usage_info = { "FILE...", "Print OpenSlide quickhash-1 (256-bit) checksums.", }; int main (int argc, char **argv) { - _openslide_tools_parse_commandline(&usage_info, &argc, &argv); + common_parse_commandline(&usage_info, &argc, &argv); if (argc < 2) { - _openslide_tools_usage(&usage_info); + common_usage(&usage_info); } int ret = 0; diff --git a/tools/openslide-show-properties.c b/tools/openslide-show-properties.c index 7bd9da00..656e73d9 100644 --- a/tools/openslide-show-properties.c +++ b/tools/openslide-show-properties.c @@ -22,7 +22,7 @@ #include #include #include "openslide.h" -#include "openslide-tools-common.h" +#include "openslide-common.h" static gboolean process(const char *file, int successes, int total) { openslide_t *osr = openslide_open(file); @@ -65,15 +65,15 @@ static gboolean process(const char *file, int successes, int total) { } -static const struct openslide_tools_usage_info usage_info = { +static const struct common_usage_info usage_info = { "FILE...", "Print OpenSlide properties for a slide.", }; int main (int argc, char **argv) { - _openslide_tools_parse_commandline(&usage_info, &argc, &argv); + common_parse_commandline(&usage_info, &argc, &argv); if (argc < 2) { - _openslide_tools_usage(&usage_info); + common_usage(&usage_info); } int successes = 0; diff --git a/tools/openslide-tools-common.h b/tools/openslide-tools-common.h deleted file mode 100644 index 723e7aaf..00000000 --- a/tools/openslide-tools-common.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * OpenSlide, a library for reading whole slide image files - * - * Copyright (c) 2007-2012 Carnegie Mellon University - * All rights reserved. - * - * OpenSlide is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, version 2.1. - * - * OpenSlide is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with OpenSlide. If not, see - * . - * - */ - -#ifndef _OPENSLIDE_TOOLS_COMMON_H -#define _OPENSLIDE_TOOLS_COMMON_H - -#include - -struct openslide_tools_usage_info { - const char *parameter_string; - const char *summary; -}; - -void _openslide_tools_parse_commandline(const struct openslide_tools_usage_info *info, - int *argc, - char ***argv); - -void _openslide_tools_usage(const struct openslide_tools_usage_info *info) - G_GNUC_NORETURN; - -bool _openslide_tools_fix_argv(int *argc, char ***argv); - -#endif diff --git a/tools/openslide-write-png.c b/tools/openslide-write-png.c index 8ff5d83b..8991bcd8 100644 --- a/tools/openslide-write-png.c +++ b/tools/openslide-write-png.c @@ -20,7 +20,7 @@ */ #include "openslide.h" -#include "openslide-tools-common.h" +#include "openslide-common.h" #include #include @@ -172,15 +172,15 @@ static void write_png(openslide_t *osr, FILE *f, } -static const struct openslide_tools_usage_info usage_info = { +static const struct common_usage_info usage_info = { "slide x y level width height output.png", "Write a region of a virtual slide to a PNG.", }; int main (int argc, char **argv) { - _openslide_tools_parse_commandline(&usage_info, &argc, &argv); + common_parse_commandline(&usage_info, &argc, &argv); if (argc != 8) { - _openslide_tools_usage(&usage_info); + common_usage(&usage_info); } // get args