[CMake] Converting a OpenCL program into a C++ header?

David Cole david.cole at kitware.com
Tue Jun 15 23:38:51 EDT 2010


This script will work for your simple input. It will require extra escaping
work if you need to embed double quotes or backslashes. And I do not claim
it is correct for all arbitrary input. But.... it's a starting point, in the
CMake language. Let me know if it gives you any troubles.


set(input_file "square.cl")

set(output_file "square.h")


# Read the whole input file:

#

file(READ "${input_file}" contents)


# Split it into lines:

#    (insert line-ending 'E' chars to allow lines to be blank or end with

#     semi-colons but still be iterate-able via CMake foreach...)

#

string(REGEX REPLACE ";" "\\\\;" contents "${contents}")

string(REGEX REPLACE "\n" "E;" contents "${contents}")


# Open output file:

#

file(WRITE "${output_file}"

  "const char *KernelSource =\n")


# Send each line to output file:

#

foreach(lineE ${contents})

  # Get rid of the trailing 'E':

  #

  string(REGEX REPLACE "^(.*)E$" "\\1" line "${lineE}")

  file(APPEND "${output_file}"

    "  \"${line}\"\n")

endforeach()


# Close output file:

#

file(APPEND "${output_file}"

  ";\n")


Cheers,
David


On Tue, Jun 15, 2010 at 5:24 PM, David Cole <david.cole at kitware.com> wrote:

> On Tue, Jun 15, 2010 at 5:13 PM, Daniel Blezek <Blezek.Daniel at mayo.edu>wrote:
>
>>  Hi,
>>
>>   We would like to convert an OpenCL program written in a separate file to
>> a C++ header (essentially a long string).
>>
>> For example, if my OpenCL program is in the file Square.cl
>>
>> __kernel square(
>>    __global float* input,
>>    __global float* output,
>>    const unsigned int count)
>> {
>>    int i = get_global_id(0);
>>    if(i < count)
>>        output[i] = input[i] * input[i];
>> }
>>
>> I’d like to turn it into something like this in Square.h:
>>
>> const char *KernelSource = "\n" \
>> "__kernel square(
>>                                                       \n" \
>> "   __global float* input,
>>                                              \n" \
>> "   __global float* output,
>>                                             \n" \
>> "   const unsigned int count)
>>                                           \n" \
>> "{
>>                                                                      \n" \
>> "   int i = get_global_id(0);
>>                                           \n" \
>> "   if(i < count)
>>                                                       \n" \
>> "       output[i] = input[i] * input[i];
>>                                \n" \
>> "}
>>                                                                      \n" \
>> "\n";
>>
>> So that my OpenCL code can be directly compiled into my executable.  This
>> is also useful for OpenGL shaders.
>>
>> The question: is this something that CMake could do?  If so, any examples
>> where to begin looking?
>>
>
>
> It is. CMake can definitely do that. I know I've written code like this
> somewhere... I have to dash off at the moment, but when I get back to a
> computer, I'll see if I can look it up and pass along a function that does
> something similar.
>
> Unless somebody else beats me to it. :-)
>
> - David
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20100615/e3e9e16d/attachment.htm>


More information about the CMake mailing list