[gegl] Allow operations to include their source as class metadata



commit 0dcc9acafb0ee5181b296f87cd7ed6544941c5f1
Author: Jon Nordby <jononor gmail com>
Date:   Fri Nov 7 20:42:47 2014 +0100

    Allow operations to include their source as class metadata
    
    Allows GEGL applications to show the code, so users can
    study them or create new operations based on them easily.

 gegl/gegl-op.h                           |   11 ++++++++++
 operations/.gitignore                    |    1 +
 operations/Makefile-common.am            |    3 ++
 operations/common/Makefile.am            |    2 +
 operations/core/Makefile.am              |    1 +
 operations/external/Makefile.am          |    2 +
 operations/workshop/Makefile.am          |    2 +
 operations/workshop/external/Makefile.am |    2 +
 tools/csourcetostring.py                 |   33 ++++++++++++++++++++++++++++++
 9 files changed, 57 insertions(+), 0 deletions(-)
---
diff --git a/gegl/gegl-op.h b/gegl/gegl-op.h
index e1421b9..fe4cd63 100644
--- a/gegl/gegl-op.h
+++ b/gegl/gegl-op.h
@@ -267,6 +267,10 @@ gegl_module_register (GTypeModule *module)
 #undef enum_value
 #undef enum_end
 
+#ifdef GEGL_OP_C_FILE_SOURCE
+#include GEGL_OP_C_FILE_SOURCE
+#endif
+
 #ifdef GETTEXT_PACKAGE
 static const gchar *gegl_op_gettext_package G_GNUC_UNUSED = GETTEXT_PACKAGE;
 #else
@@ -815,6 +819,13 @@ gegl_op_class_intern_init (gpointer klass)
   gboolean G_GNUC_UNUSED ui_digits_set = FALSE;
   GParamFlags flags G_GNUC_UNUSED = (GParamFlags)(G_PARAM_READWRITE | G_PARAM_CONSTRUCT | 
GEGL_PARAM_PAD_INPUT);
 
+#ifdef GEGL_OP_C_FILE_SOURCE
+  GeglOperationClass *operation_class = GEGL_OPERATION_CLASS (klass);
+  gegl_operation_class_set_keys (operation_class,
+    "source", op_c_source,
+    NULL);
+#endif
+
   object_class->set_property = set_property;
   object_class->get_property = get_property;
   object_class->constructor  = gegl_op_constructor;
diff --git a/operations/.gitignore b/operations/.gitignore
index b336cc7..3bab32d 100644
--- a/operations/.gitignore
+++ b/operations/.gitignore
@@ -1,2 +1,3 @@
 /Makefile
 /Makefile.in
+*/*.c.h
diff --git a/operations/Makefile-common.am b/operations/Makefile-common.am
index ea7b0dc..cf14ae0 100644
--- a/operations/Makefile-common.am
+++ b/operations/Makefile-common.am
@@ -31,3 +31,6 @@ AM_CFLAGS = $(DEP_CFLAGS) $(BABL_CFLAGS)
 AM_LDFLAGS = -avoid-version -export-dynamic -module $(no_undefined)
 
 ext_dir = $(libdir)/gegl- GEGL_API_VERSION@
+
+%.c.h: %.c $(top_builddir)/tools/csourcetostring.py
+       $(PYTHON) $(top_builddir)/tools/csourcetostring.py $<
diff --git a/operations/common/Makefile.am b/operations/common/Makefile.am
index 1663c89..1a3a201 100644
--- a/operations/common/Makefile.am
+++ b/operations/common/Makefile.am
@@ -4,6 +4,8 @@ include $(top_srcdir)/operations/Makefile-common.am
 
 EXTRA_DIST = $(wildcard $(srcdir)/*.h)
 
+BUILT_SOURCES = $(subst .c,.c.h,$(wildcard $(srcdir)/*.c))
+
 AM_CPPFLAGS += -I$(srcdir)
 
 LIBS = $(op_libs)
diff --git a/operations/core/Makefile.am b/operations/core/Makefile.am
index accd109..7079038 100644
--- a/operations/core/Makefile.am
+++ b/operations/core/Makefile.am
@@ -1,6 +1,7 @@
 include $(top_srcdir)/operations/Makefile-common.am
 
 EXTRA_DIST = $(wildcard $(srcdir)/*.h)
+BUILT_SOURCES = $(subst .c,.c.h,$(wildcard $(srcdir)/*.c))
 
 AM_CPPFLAGS += -I$(srcdir)
 
diff --git a/operations/external/Makefile.am b/operations/external/Makefile.am
index b929744..76d8d76 100644
--- a/operations/external/Makefile.am
+++ b/operations/external/Makefile.am
@@ -158,5 +158,7 @@ rgbe_save_la_SOURCES = rgbe-save.c
 rgbe_save_la_CFLAGS = $(AM_CFLAGS) -I $(top_srcdir)/libs
 rgbe_save_la_LIBADD = $(op_libs) $(top_builddir)/libs/rgbe/librgbe.la
 
+BUILT_SOURCES = $(subst .c,.c.h,$(wildcard $(srcdir)/*.c))
+
 opdir = $(libdir)/gegl- GEGL_API_VERSION@
 op_LTLIBRARIES = $(ops)
diff --git a/operations/workshop/Makefile.am b/operations/workshop/Makefile.am
index 3e2c3d2..0463347 100644
--- a/operations/workshop/Makefile.am
+++ b/operations/workshop/Makefile.am
@@ -4,6 +4,8 @@ include $(top_srcdir)/operations/Makefile-common.am
 
 EXTRA_DIST = $(wildcard $(srcdir)/*.h)
 
+BUILT_SOURCES = $(subst .c,.c.h,$(wildcard $(srcdir)/*.c))
+
 AM_CPPFLAGS += -I$(srcdir)
 
 LIBS = $(op_libs)
diff --git a/operations/workshop/external/Makefile.am b/operations/workshop/external/Makefile.am
index c8453b0..2437825 100644
--- a/operations/workshop/external/Makefile.am
+++ b/operations/workshop/external/Makefile.am
@@ -39,5 +39,7 @@ v4l2_la_LIBADD = $(op_libs) $(V4L2_LIBS)
 v4l2_la_CFLAGS = $(AM_CFLAGS) $(V4L2_CFLAGS)
 endif
 
+BUILT_SOURCES = $(subst .c,.c.h,$(wildcard $(srcdir)/*.c))
+
 opdir = $(libdir)/gegl- GEGL_API_VERSION@
 op_LTLIBRARIES = $(ops)
diff --git a/tools/csourcetostring.py b/tools/csourcetostring.py
new file mode 100755
index 0000000..62a0b1b
--- /dev/null
+++ b/tools/csourcetostring.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+from __future__ import print_function
+
+import os
+import sys
+
+if len(sys.argv) != 2:
+  print("Usage: %s file.c" % sys.argv[0])
+  sys.exit(1)
+
+# From http://stackoverflow.com/questions/14945095/how-to-escape-string-for-generated-c
+def escape_string(s):
+  return s.replace('\\', r'\\').replace('"', r'\"')
+
+
+infile  = open(sys.argv[1], "r")
+outfile = open(sys.argv[1] + ".h", "w")
+
+cl_source = infile.read()
+infile.close()
+
+string_var_name = os.path.basename(sys.argv[1]).replace("-", "_").replace(":", "_")
+if string_var_name.endswith(".c"):
+  string_var_name = string_var_name.rstrip(".c")
+
+outfile.write("static const char* %s_c_source =\n" % "op")
+for line in cl_source.rstrip().split("\n"):
+  line = line.rstrip()
+  line = escape_string(line)
+  line = '"%-78s\\n"\n' % line
+  outfile.write(line)
+outfile.write(";\n")
+outfile.close()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]