node-editor: Redo saving location
authorMatthias Clasen <mclasen@redhat.com>
Tue, 6 Dec 2022 11:13:59 +0000 (11:13 +0000)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 9 Dec 2022 14:35:32 +0000 (09:35 -0500)
Determine the location to save testcases in dynamically,
trying first a GTK_SOURCE_DIR environment variable
and then the current directory as the GTK source dir,
ultimatively falling back to just saving in the current
directory.

This avoids leaking details of the build environment
into the produced artifacts and should make GTK builds
more reproducible.

Fixes: #5403
demos/node-editor/meson.build
demos/node-editor/node-editor-window.c

index 03489f3f75a2bc8e99bc9b265950d64fe934f3ef..72d48cd12ce64dca9f3ac08cde0a35aee2b9193a 100644 (file)
@@ -14,9 +14,7 @@ executable('gtk4-node-editor',
   sources: [node_editor_sources, node_editor_resources],
   dependencies: [ libgtk_dep, demo_conf_h ],
   include_directories: confinc,
-  c_args: [
-    '-DNODE_EDITOR_SOURCE_DIR="@0@/../../testsuite/gsk/compare/"'.format(meson.current_source_dir())
-  ] + common_cflags,
+  c_args: common_cflags,
   win_subsystem: 'windows',
   link_args: extra_demo_ldflags,
   install: true,
index 3a4800ff7362675ce693ce6b4b24c9b38c2c8189..4833a8224845b3e2577528edd0409b414ee8ea96 100644 (file)
 #include "gsk/vulkan/gskvulkanrenderer.h"
 #endif
 
-#ifndef NODE_EDITOR_SOURCE_DIR
-#define NODE_EDITOR_SOURCE_DIR "." /* Fallback */
-#endif
-
 typedef struct
 {
   gsize  start_chars;
@@ -788,12 +784,54 @@ testcase_name_entry_changed_cb (GtkWidget        *button,
     gtk_widget_set_sensitive (self->testcase_save_button, FALSE);
 }
 
+/* Returns the location where gsk test cases are stored in
+ * the GTK testsuite, if we can determine it.
+ *
+ * When running node editor outside of a GTK build, you can
+ * set GTK_SOURCE_DIR to point it at the checkout.
+ */
+static char *
+get_source_dir (void)
+{
+  const char *subdir = "testsuite/gsk/compare";
+  const char *source_dir;
+  char *current_dir;
+  char *dir;
+
+  source_dir = g_getenv ("GTK_SOURCE_DIR");
+  current_dir = g_get_current_dir ();
+
+  if (source_dir)
+    {
+      char *abs_source_dir = g_canonicalize_filename (source_dir, NULL);
+      dir = g_canonicalize_filename (subdir, abs_source_dir);
+      g_free (abs_source_dir);
+    }
+  else
+    {
+      dir = g_canonicalize_filename (subdir, current_dir);
+    }
+
+  if (g_file_test (dir, G_FILE_TEST_EXISTS))
+    {
+      g_print ("file exists: %s\n", dir);
+      g_free (current_dir);
+
+      return dir;
+    }
+
+  g_print ("file does not exists: %s\n", dir);
+  g_free (dir);
+
+  return current_dir;
+}
+
 static void
 testcase_save_clicked_cb (GtkWidget        *button,
                           NodeEditorWindow *self)
 {
   const char *testcase_name = gtk_editable_get_text (GTK_EDITABLE (self->testcase_name_entry));
-  char *source_dir = g_canonicalize_filename (NODE_EDITOR_SOURCE_DIR, NULL);
+  char *source_dir = get_source_dir ();
   char *node_file_name;
   char *node_file;
   char *png_file_name;
@@ -806,6 +844,8 @@ testcase_save_clicked_cb (GtkWidget        *button,
   node_file = g_build_filename (source_dir, node_file_name, NULL);
   g_free (node_file_name);
 
+  g_debug ("Saving testcase in %s", node_file);
+
   png_file_name = g_strconcat (testcase_name, ".png", NULL);
   png_file = g_build_filename (source_dir, png_file_name, NULL);
   g_free (png_file_name);