Skip to content

Commit

Permalink
demo: change strategy for uninstalled detection
Browse files Browse the repository at this point in the history
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:

    mesonbuild/meson#4019
  • Loading branch information
ntd committed Apr 19, 2021
1 parent 41252f3 commit 4739eb8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 79 deletions.
37 changes: 10 additions & 27 deletions demo/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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)
17 changes: 6 additions & 11 deletions demo/adg-demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 3 additions & 13 deletions demo/cpml-demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
89 changes: 61 additions & 28 deletions demo/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,88 @@
#include <string.h>


/* 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 <limits.h>
#include <stdlib.h>

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);
}

0 comments on commit 4739eb8

Please sign in to comment.