Merge pull request #11112 from alalek:cmake_src_include_fix
[platform/upstream/opencv.git] / cmake / cl2cpp.cmake
1 if (NOT EXISTS "${CL_DIR}")
2   message(FATAL_ERROR "Specified wrong OpenCL kernels directory: ${CL_DIR}")
3 endif()
4
5 file(GLOB cl_list "${CL_DIR}/*.cl" )
6 list(SORT cl_list)
7
8 if (NOT cl_list)
9   message(FATAL_ERROR "Can't find OpenCL kernels in directory: ${CL_DIR}")
10 endif()
11
12 string(REGEX REPLACE "\\.cpp$" ".hpp" OUTPUT_HPP "${OUTPUT}")
13 get_filename_component(OUTPUT_HPP_NAME "${OUTPUT_HPP}" NAME)
14
15 set(nested_namespace_start "namespace ${MODULE_NAME}\n{")
16 set(nested_namespace_end "}")
17
18 set(STR_CPP "// This file is auto-generated. Do not edit!
19
20 #include \"opencv2/core.hpp\"
21 #include \"cvconfig.h\"
22 #include \"${OUTPUT_HPP_NAME}\"
23
24 #ifdef HAVE_OPENCL
25
26 namespace cv
27 {
28 namespace ocl
29 {
30 ${nested_namespace_start}
31
32 static const char* const moduleName = \"${MODULE_NAME}\";
33
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 #ifdef HAVE_OPENCL
43
44 namespace cv
45 {
46 namespace ocl
47 {
48 ${nested_namespace_start}
49
50 ")
51
52 foreach(cl ${cl_list})
53   get_filename_component(cl_filename "${cl}" NAME_WE)
54   #message("${cl_filename}")
55
56   file(READ "${cl}" lines)
57
58   string(REPLACE "\r" "" lines "${lines}\n")
59   string(REPLACE "\t" "  " lines "${lines}")
60
61   string(REGEX REPLACE "/\\*([^*]/|\\*[^/]|[^*/])*\\*/" ""   lines "${lines}") # multiline comments
62   string(REGEX REPLACE "/\\*([^\n])*\\*/"               ""   lines "${lines}") # single-line comments
63   string(REGEX REPLACE "[ ]*//[^\n]*\n"                 "\n" lines "${lines}") # single-line comments
64   string(REGEX REPLACE "\n[ ]*(\n[ ]*)*"                "\n" lines "${lines}") # empty lines & leading whitespace
65   string(REGEX REPLACE "^\n"                            ""   lines "${lines}") # leading new line
66
67   string(REPLACE "\\" "\\\\" lines "${lines}")
68   string(REPLACE "\"" "\\\"" lines "${lines}")
69   string(REPLACE "\n" "\\n\"\n\"" lines "${lines}")
70
71   string(REGEX REPLACE "\"$" "" lines "${lines}") # unneeded " at the eof
72
73   string(MD5 hash "${lines}")
74
75   set(STR_CPP_DECL "struct cv::ocl::internal::ProgramEntry ${cl_filename}_oclsrc={moduleName, \"${cl_filename}\",\n\"${lines}, \"${hash}\", NULL};\n")
76   set(STR_HPP_DECL "extern struct cv::ocl::internal::ProgramEntry ${cl_filename}_oclsrc;\n")
77
78   set(STR_CPP "${STR_CPP}${STR_CPP_DECL}")
79   set(STR_HPP "${STR_HPP}${STR_HPP_DECL}")
80 endforeach()
81
82 set(STR_CPP "${STR_CPP}\n${nested_namespace_end}}}\n#endif\n")
83 set(STR_HPP "${STR_HPP}\n${nested_namespace_end}}}\n#endif\n")
84
85 file(WRITE "${OUTPUT}" "${STR_CPP}")
86
87 if(EXISTS "${OUTPUT_HPP}")
88   file(READ "${OUTPUT_HPP}" hpp_lines)
89 endif()
90 if("${hpp_lines}" STREQUAL "${STR_HPP}")
91   message(STATUS "${OUTPUT_HPP} contains the same content")
92 else()
93   file(WRITE "${OUTPUT_HPP}" "${STR_HPP}")
94 endif()