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

A
Andrey Kamaev 已提交
5
file(GLOB cl_list "${CL_DIR}/*.cl" )
6
list(SORT cl_list)
A
Andrey Kamaev 已提交
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 17 18 19 20
if("${MODULE_NAME}" STREQUAL "ocl")
    set(nested_namespace_start "")
    set(nested_namespace_end "")
else()
    set(new_mode ON)
    set(nested_namespace_start "namespace ${MODULE_NAME}\n{")
21
    set(nested_namespace_end "}")
22 23
endif()

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

26
#include \"precomp.hpp\"
27
#include \"cvconfig.h\"
28 29
#include \"${OUTPUT_HPP_NAME}\"

30 31
#ifdef HAVE_OPENCL

32 33 34 35
namespace cv
{
namespace ocl
{
36 37
${nested_namespace_start}

38 39 40
")

set(STR_HPP "// This file is auto-generated. Do not edit!
A
Andrey Kamaev 已提交
41

42
#include \"opencv2/core/ocl.hpp\"
43
#include \"opencv2/core/ocl_genbase.hpp\"
44
#include \"opencv2/core/opencl/ocl_defs.hpp\"
45

46 47
#ifdef HAVE_OPENCL

A
Andrey Kamaev 已提交
48 49 50 51
namespace cv
{
namespace ocl
{
52
${nested_namespace_start}
53

A
Andrey Kamaev 已提交
54 55 56 57 58 59 60 61 62 63 64 65
")

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
Y
yao 已提交
66
  string(REGEX REPLACE "/\\*([^\n])*\\*/"               ""   lines "${lines}") # single-line comments
A
Andrey Kamaev 已提交
67 68 69 70 71 72 73 74 75 76
  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

77 78
  string(MD5 hash "${lines}")

79 80 81
  set(STR_CPP_DECL "const struct ProgramEntry ${cl_filename}={\"${cl_filename}\",\n\"${lines}, \"${hash}\"};\n")
  set(STR_HPP_DECL "extern const struct ProgramEntry ${cl_filename};\n")
  if(new_mode)
I
Ilya Lavrenov 已提交
82 83
    set(STR_CPP_DECL "${STR_CPP_DECL}ProgramSource ${cl_filename}_oclsrc(${cl_filename}.programStr);\n")
    set(STR_HPP_DECL "${STR_HPP_DECL}extern ProgramSource ${cl_filename}_oclsrc;\n")
84 85 86 87
  endif()

  set(STR_CPP "${STR_CPP}${STR_CPP_DECL}")
  set(STR_HPP "${STR_HPP}${STR_HPP_DECL}")
A
Andrey Kamaev 已提交
88 89
endforeach()

90 91
set(STR_CPP "${STR_CPP}}\n${nested_namespace_end}}\n#endif\n")
set(STR_HPP "${STR_HPP}}\n${nested_namespace_end}}\n#endif\n")
92

93 94 95 96 97 98 99 100 101 102
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()