cl2cpp.cmake 2.71 KB
Newer Older
1 2 3 4
if (NOT EXISTS "${CL_DIR}")
  message(FATAL_ERROR "Specified wrong OpenCL kernels directory: ${CL_DIR}")
endif()

5
file(GLOB cl_list "${CL_DIR}/*.cl" )
6
list(SORT cl_list)
7

8 9 10 11
if (NOT cl_list)
  message(FATAL_ERROR "Can't find OpenCL kernels in directory: ${CL_DIR}")
endif()

12
string(REGEX REPLACE "\\.cpp$" ".hpp" OUTPUT_HPP "${OUTPUT}")
13 14
get_filename_component(OUTPUT_HPP_NAME "${OUTPUT_HPP}" NAME)

15 16
set(nested_namespace_start "namespace ${MODULE_NAME}\n{")
set(nested_namespace_end "}")
17

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

20
#include \"opencv2/core.hpp\"
21
#include \"cvconfig.h\"
22 23
#include \"${OUTPUT_HPP_NAME}\"

24 25
#ifdef HAVE_OPENCL

26 27 28 29
namespace cv
{
namespace ocl
{
30 31
${nested_namespace_start}

32 33
static const char* const moduleName = \"${MODULE_NAME}\";

34 35 36
")

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

38
#include \"opencv2/core/ocl.hpp\"
39
#include \"opencv2/core/ocl_genbase.hpp\"
40
#include \"opencv2/core/opencl/ocl_defs.hpp\"
41

42 43
#ifdef HAVE_OPENCL

44 45 46 47
namespace cv
{
namespace ocl
{
48
${nested_namespace_start}
49

50 51 52 53 54 55 56 57 58 59 60 61
")

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
62
  string(REGEX REPLACE "/\\*([^\n])*\\*/"               ""   lines "${lines}") # single-line comments
63 64 65 66 67 68 69 70 71 72
  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

73 74
  string(MD5 hash "${lines}")

75 76
  set(STR_CPP_DECL "struct cv::ocl::internal::ProgramEntry ${cl_filename}_oclsrc={moduleName, \"${cl_filename}\",\n\"${lines}, \"${hash}\", NULL};\n")
  set(STR_HPP_DECL "extern struct cv::ocl::internal::ProgramEntry ${cl_filename}_oclsrc;\n")
77 78 79

  set(STR_CPP "${STR_CPP}${STR_CPP_DECL}")
  set(STR_HPP "${STR_HPP}${STR_HPP_DECL}")
80 81
endforeach()

82 83
set(STR_CPP "${STR_CPP}\n${nested_namespace_end}}}\n#endif\n")
set(STR_HPP "${STR_HPP}\n${nested_namespace_end}}}\n#endif\n")
84

85 86 87 88 89 90
file(WRITE "${OUTPUT}" "${STR_CPP}")

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