cl2cpp.cmake 1.95 KB
Newer Older
1
file(GLOB cl_list "${CL_DIR}/*.cl" )
2
list(SORT cl_list)
3

4 5 6 7 8
string(REPLACE ".cpp" ".hpp" OUTPUT_HPP "${OUTPUT}")
get_filename_component(OUTPUT_HPP_NAME "${OUTPUT_HPP}" NAME)

set(STR_CPP "// This file is auto-generated. Do not edit!

9
#include \"precomp.hpp\"
10 11 12 13 14 15 16 17 18
#include \"${OUTPUT_HPP_NAME}\"

namespace cv
{
namespace ocl
{
")

set(STR_HPP "// This file is auto-generated. Do not edit!
19

20 21
#include \"opencv2/ocl/private/util.hpp\"

22 23 24 25
namespace cv
{
namespace ocl
{
26

27 28 29 30 31 32 33 34 35 36 37 38
")

foreach(cl ${cl_list})
  get_filename_component(cl_filename "${cl}" NAME_WE)
  #message("${cl_filename}")

  file(READ "${cl}" lines)

  string(REPLACE "\r" "" lines "${lines}\n")
  string(REPLACE "\t" "  " lines "${lines}")

  string(REGEX REPLACE "/\\*([^*]/|\\*[^/]|[^*/])*\\*/" ""   lines "${lines}") # multiline comments
yao's avatar
yao committed
39
  string(REGEX REPLACE "/\\*([^\n])*\\*/"               ""   lines "${lines}") # single-line comments
40 41 42 43 44 45 46 47 48 49
  string(REGEX REPLACE "[ ]*//[^\n]*\n"                 "\n" lines "${lines}") # single-line comments
  string(REGEX REPLACE "\n[ ]*(\n[ ]*)*"                "\n" lines "${lines}") # empty lines & leading whitespace
  string(REGEX REPLACE "^\n"                            ""   lines "${lines}") # leading new line

  string(REPLACE "\\" "\\\\" lines "${lines}")
  string(REPLACE "\"" "\\\"" lines "${lines}")
  string(REPLACE "\n" "\\n\"\n\"" lines "${lines}")

  string(REGEX REPLACE "\"$" "" lines "${lines}") # unneeded " at the eof

50 51 52 53
  string(MD5 hash "${lines}")

  set(STR_CPP "${STR_CPP}const struct ProgramEntry ${cl_filename}={\"${cl_filename}\",\n\"${lines}, \"${hash}\"};\n")
  set(STR_HPP "${STR_HPP}extern const struct ProgramEntry ${cl_filename};\n")
54 55
endforeach()

56 57 58
set(STR_CPP "${STR_CPP}}\n}\n")
set(STR_HPP "${STR_HPP}}\n}\n")

59 60 61 62 63 64 65 66 67 68
file(WRITE "${OUTPUT}" "${STR_CPP}")

if(EXISTS "${OUTPUT_HPP}")
  file(READ "${OUTPUT_HPP}" hpp_lines)
endif()
if("${hpp_lines}" STREQUAL "${STR_HPP}")
  message(STATUS "${OUTPUT_HPP} contains same content")
else()
  file(WRITE "${OUTPUT_HPP}" "${STR_HPP}")
endif()