From 4739eb85c6e54c82ca6f9cedcde5de8919be15dc Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Mon, 19 Apr 2021 21:59:20 +0200 Subject: [PATCH] demo: change strategy for uninstalled detection Instead of looking for the `-uninstalled` suffix in the program name (that requires renaming the binaries after the installation), check if the program is called from BUILDDIR. This allows to switch from autotools to meson, where the latter does not handle renaming installed file properly: https://github.com/mesonbuild/meson/issues/4019 --- demo/Makefile.am | 37 ++++++-------------- demo/adg-demo.c | 17 ++++----- demo/cpml-demo.c | 16 ++------- demo/demo.c | 89 +++++++++++++++++++++++++++++++++--------------- 4 files changed, 80 insertions(+), 79 deletions(-) diff --git a/demo/Makefile.am b/demo/Makefile.am index ff48f6b1..2c1d6df8 100644 --- a/demo/Makefile.am +++ b/demo/Makefile.am @@ -13,27 +13,27 @@ AM_CFLAGS= -mwindows endif -bin_PROGRAMS= adg-demo-uninstalled \ - cpml-demo-uninstalled +bin_PROGRAMS= adg-demo \ + cpml-demo -adg_demo_uninstalled_SOURCES= adg-demo.c \ +adg_demo_SOURCES= adg-demo.c \ demo.c \ demo.h -adg_demo_uninstalled_CFLAGS= $(CPML_CFLAGS) \ +adg_demo_CFLAGS= $(CPML_CFLAGS) \ $(ADG_CFLAGS) -adg_demo_uninstalled_LDADD= $(CPML_LIBS) \ +adg_demo_LDADD= $(CPML_LIBS) \ $(top_builddir)/src/cpml/libcpml-1.la \ $(ADG_LIBS) \ $(top_builddir)/src/adg/libadg-1.la # The CPML demo program uses the ADG library only marginally for # looking up the ui file (adg_find_file()) and for i18n. -cpml_demo_uninstalled_SOURCES= cpml-demo.c \ +cpml_demo_SOURCES= cpml-demo.c \ demo.c \ demo.h -cpml_demo_uninstalled_CFLAGS= $(CPML_CFLAGS) \ +cpml_demo_CFLAGS= $(CPML_CFLAGS) \ $(ADG_CFLAGS) -cpml_demo_uninstalled_LDADD= $(CPML_LIBS) \ +cpml_demo_LDADD= $(CPML_LIBS) \ $(top_builddir)/src/cpml/libcpml-1.la \ $(ADG_LIBS) \ $(top_builddir)/src/adg/libadg-1.la @@ -47,8 +47,8 @@ if OS_WINDOWS --define PACKAGE_VERSIONS=`echo $(PACKAGE_VERSION) | tr . ,` \ $(srcdir)/adg.rc $@ -adg_demo_uninstalled_LDADD+= adg-demo.rc.o -cpml_demo_uninstalled_LDADD+= cpml-demo.rc.o +adg_demo_LDADD+= adg-demo.rc.o +cpml_demo_LDADD+= cpml-demo.rc.o endif @@ -79,20 +79,3 @@ EXTRA_DIST= cpml-demo.ui.in \ # Possibly remove files created by 'make coverage' mostlyclean-local: -rm -f *.gcno - - -# adg-demo-uninstalled and cpml-demo-uninstalled are renamed after installation: -# the uninstalled version should not be checked for --help and --version. -AM_INSTALLCHECK_STD_OPTIONS_EXEMPT= \ - adg-demo-uninstalled$(EXEEXT) \ - cpml-demo-uninstalled$(EXEEXT) - -install-exec-hook: - $(AM_V_at)cd $(DESTDIR)$(bindir) ; \ - mv -f adg-demo-uninstalled$(EXEEXT) adg-demo$(EXEEXT) ; \ - mv -f cpml-demo-uninstalled$(EXEEXT) cpml-demo$(EXEEXT) - -uninstall-hook: - $(AM_V_at)cd $(DESTDIR)$(bindir) ; \ - rm -f adg-demo$(EXEEXT) ; \ - rm -f cpml-demo$(EXEEXT) diff --git a/demo/adg-demo.c b/demo/adg-demo.c index 81aa7366..2f970758 100644 --- a/demo/adg-demo.c +++ b/demo/adg-demo.c @@ -1095,23 +1095,18 @@ _adg_save_as_window(GtkBuilder *builder, AdgCanvas *canvas) static GtkWidget * _adg_main_window(GtkBuilder *builder) { + gchar *icon, *path; GtkWidget *window; DemoPart *part; AdgCanvas *canvas; GtkWidget *button_edit, *button_save_as, *button_print; GtkWidget *button_help, *button_about, *button_quit; - if (is_installed) { -#ifdef G_OS_WIN32 - gchar *icondir = g_build_filename(basedir, PKGDATADIR, NULL); - adg_gtk_use_default_icons(icondir); - g_free(icondir); -#else - adg_gtk_use_default_icons(PKGDATADIR); -#endif - } else { - adg_gtk_use_default_icons(SRCDIR); - } + icon = _demo_file("adg-64.png"); + path = g_path_get_dirname(icon); + free(icon); + adg_gtk_use_default_icons(path); + g_free(path); window = (GtkWidget *) gtk_builder_get_object(builder, "wndMain"); part = _adg_part_new(builder); diff --git a/demo/cpml-demo.c b/demo/cpml-demo.c index a6a2ffd6..479b4c13 100644 --- a/demo/cpml-demo.c +++ b/demo/cpml-demo.c @@ -178,24 +178,14 @@ main(gint argc, gchar **argv) GtkBuilder *builder; GError *error; GtkWidget *window; - gchar *icons_dir; _demo_init(argc, argv); parse_args(&argc, &argv); /* Prepend the package icons path */ - if (is_installed) { -#ifdef G_OS_WIN32 - icons_dir = g_build_filename(basedir, PKGDATADIR, "icons", NULL); -#else - icons_dir = g_strdup(PKGDATADIR "/icons"); -#endif - } else { - icons_dir = g_strdup(SRCDIR "/icons"); - } - - gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(), icons_dir); - g_free(icons_dir); + path = _demo_file("icons"); + gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(), path); + g_free(path); path = _demo_file("cpml-demo.ui"); if (path == NULL) { diff --git a/demo/demo.c b/demo/demo.c index 6fa7a082..352c7ee5 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -21,55 +21,88 @@ #include -/* Whether the program is running installed or uninstalled */ -gboolean is_installed = TRUE; +#if GLIB_CHECK_VERSION(2, 58, 0) -/* The base directory where all files must be referred, - * usually left unset _adg_init() on POSIX systems. */ -gchar *basedir = NULL; +static gchar * +_demo_absolutepath(const gchar *path) +{ + return g_canonicalize_filename(path, NULL); +} + +#else + +#include +#include + +static gchar * +_demo_absolutepath(const gchar *path) +{ + char abspath[PATH_MAX]; + return g_strdup(realpath(path, abspath)); +} + +#endif + + +/* Base folder all installed files should be referred to. */ +static gchar *pkg_data_dir = NULL; + + +static gchar * +_demo_basedir(const gchar *program) +{ + gchar *dir, *absdir; + dir = g_path_get_dirname(program); + if (dir == NULL) { + return NULL; + } + + absdir = _demo_absolutepath(dir); + g_free(dir); + return absdir; +} void _demo_init(gint argc, gchar *argv[]) { - gchar *name; + gchar *basedir; if (argc < 1 || argv == NULL || argv[0] == NULL) { g_error(_("Invalid arguments: arg[0] not set")); g_assert_not_reached(); } - name = g_path_get_basename(argv[0]); - if (name != NULL) { - is_installed = strstr(name, "uninstalled") == NULL; - g_free(name); + basedir = _demo_basedir(argv[0]); + if (basedir == NULL) { + /* This should never happen but... just in case */ + pkg_data_dir = "."; + return; } - basedir = g_path_get_dirname(argv[0]); - if (basedir == NULL) - basedir = g_strdup(""); + if (strcmp(basedir, BUILDDIR) != 0) { + /* Installed program: set pkg_data_dir */ +#ifdef G_OS_WIN32 + gchar *parent = g_path_get_dirname(dirname); + /* XXX: `pkg_data_dir` will be leaked... who cares? */ + pkg_data_dir = g_build_filename(parent, PKGDATADIR, NULL); + g_free(parent); +#else + pkg_data_dir = PKGDATADIR; +#endif + } + g_free(basedir); } gchar * _demo_file(const gchar *file_name) { - static gchar *pkg_data_dir = NULL; - - if (! is_installed) { - /* Running uninstalled: look up the file in BUILDDIR before and - * in SRCDIR after, returning the first match */ - return adg_find_file(file_name, BUILDDIR, SRCDIR, NULL); - } - - /* Otherwise, look up the file only in PKGDATADIR */ if (pkg_data_dir == NULL) { -#ifdef G_OS_WIN32 - /* On windows, PKGDATADIR is relative to basedir */ - pkg_data_dir = g_build_filename(basedir, PKGDATADIR, NULL); -#else - pkg_data_dir = g_strdup(PKGDATADIR); -#endif + /* Running uninstalled: look up the file in BUILDDIR first and + * SRCDIR later, returning the first match */ + return adg_find_file(file_name, BUILDDIR, SRCDIR, NULL); } + /* Running installed: look up the file only in `pkg_data_dir` */ return adg_find_file(file_name, pkg_data_dir, NULL); }