[gegl] opencl: new source generator, cltostring.py
- From: Daniel Sabo <daniels src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gegl] opencl: new source generator, cltostring.py
- Date: Tue, 22 Oct 2013 08:48:25 +0000 (UTC)
commit 84dd4510b9185e031d7e105fd468d8026f7fe9f5
Author: Daniel Sabo <DanielSabo gmail com>
Date: Tue Oct 22 01:14:14 2013 -0700
opencl: new source generator, cltostring.py
Which support escaping strings and a rather hacky include system.
opencl/Makefile.am | 9 ++++++-
opencl/cltostring.pl | 28 ----------------------
opencl/cltostring.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
opencl/generate-all.pl | 6 ----
4 files changed, 68 insertions(+), 35 deletions(-)
---
diff --git a/opencl/Makefile.am b/opencl/Makefile.am
index e77da81..e56502d 100644
--- a/opencl/Makefile.am
+++ b/opencl/Makefile.am
@@ -1 +1,8 @@
-EXTRA_DIST = $(wildcard $(srcdir)/*.cl) $(wildcard $(srcdir)/*.cl.h) $(wildcard $(srcdir)/*.pl)
+EXTRA_DIST = $(wildcard $(srcdir)/*.h) $(wildcard $(srcdir)/*.py)
+
+SOURCES = $(wildcard $(srcdir)/*.cl)
+
+BUILT_SOURCES = $(subst .cl,.cl.h,$(wildcard $(srcdir)/*.cl))
+
+%.cl.h: %.cl $(srcdir)/cltostring.py
+ $(PYTHON) $(srcdir)/cltostring.py $<
\ No newline at end of file
diff --git a/opencl/cltostring.py b/opencl/cltostring.py
new file mode 100755
index 0000000..21350d8
--- /dev/null
+++ b/opencl/cltostring.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+from __future__ import print_function
+
+import os
+import sys
+
+if len(sys.argv) != 2:
+ print("Usage: %s file.cl" % sys.argv[0])
+ sys.exit(1)
+
+# Search for lines that look like #include "blah.h" and replace them
+# with the contents of blah.h.
+def do_includes (source):
+ result = list()
+ for line in source.split("\n"):
+ if line.lstrip().startswith("#include"):
+ splitstr = line.split('"')
+ if len(splitstr) != 3:
+ raise RuntimeError("Invalid include: %s" % line)
+ include_path = splitstr[1]
+ if not os.path.isfile(include_path):
+ raise RuntimeError("Could not find include: %s" % line)
+ with open(include_path, "r") as inc_file:
+ result += do_includes(inc_file.read()).split("\n")
+ else:
+ result.append(line)
+ return "\n".join(result)
+
+# From http://stackoverflow.com/questions/14945095/how-to-escape-string-for-generated-c
+def escape_string(s, encoding='ascii'):
+ if isinstance(s, unicode):
+ s = s.encode(encoding)
+ result = ''
+ for c in s:
+ if not (32 <= ord(c) < 127) or c in ('\\', '"'):
+ result += '\\%03o' % ord(c)
+ else:
+ result += c
+ return result
+
+
+infile = open(sys.argv[1], "r")
+outfile = open(sys.argv[1] + ".h", "w")
+
+cl_source = infile.read()
+cl_source = do_includes(cl_source)
+infile.close()
+
+string_var_name = sys.argv[1].replace("-", "_").replace(":", "_")
+if string_var_name.endswith(".cl"):
+ string_var_name = string_var_name[:-3]
+
+outfile.write("static const char* %s_cl_source =\n" % string_var_name)
+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()
\ No newline at end of file
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]