cl2cpp.py 1.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import os, os.path, sys, glob

indir = sys.argv[1]
outname = sys.argv[2]
#indir = "/Users/vp/work/ocv/opencv/modules/ocl/src/kernels"
#outname = "/Users/vp/work/ocv.build/xcode/modules/ocl/kernels.cpp"

try:
    os.mkdir(os.path.dirname(outname))
except OSError:
    pass

cl_list = glob.glob(os.path.join(indir, "*.cl"))
kfile = open(outname, "wt")

kfile.write("""// This file is auto-generated. Do not edit!
yao's avatar
yao committed
17

18 19 20 21 22 23 24 25 26 27 28 29
namespace cv
{
namespace ocl
{
""")

for cl in cl_list:
    cl_file = open(cl, "rt")
    cl_filename = os.path.basename(cl)
    cl_filename = cl_filename[:cl_filename.rfind(".")]
    kfile.write("const char* %s=" % cl_filename)
    state = 0
30

31 32 33 34 35 36 37 38 39
    for cl_line in cl_file.readlines():
        l = cl_line.strip()
        # skip the leading comments
        if l.startswith("//") and l.find("*/") < 0:
            if state == 0:
                state = 1
        else:
            if state == 1 or l.find("*/") >= 0:
                state = 2
40

41 42
        if state == 1:
            continue
43

44 45 46 47 48 49 50 51 52 53 54 55
        l = l.replace("\\", "\\\\")
        l = l.replace("\r", "")
        l = l.replace("\"", "\\\"")
        l = l.replace("\t", "  ")
        kfile.write("\"%s\\n\"\n" % l)
    kfile.write(";\n")
    cl_file.close()

kfile.write("""}
}
""")
kfile.close()