Merge pull request #5784 from alalek:distrib_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(REPLACE ".cpp" ".hpp" OUTPUT_HPP "${OUTPUT}")
13 get_filename_component(OUTPUT_HPP_NAME "${OUTPUT_HPP}" NAME)
14
15 if("${MODULE_NAME}" STREQUAL "ocl")
16     set(nested_namespace_start "")
17     set(nested_namespace_end "")
18 else()
19     set(new_mode ON)
20     set(nested_namespace_start "namespace ${MODULE_NAME}\n{")
21     set(nested_namespace_end "}")
22 endif()
23
24 set(STR_CPP "// This file is auto-generated. Do not edit!
25
26 #include \"precomp.hpp\"
27 #include \"cvconfig.h\"
28 #include \"${OUTPUT_HPP_NAME}\"
29
30 #ifdef HAVE_OPENCL
31
32 namespace cv
33 {
34 namespace ocl
35 {
36 ${nested_namespace_start}
37
38 ")
39
40 set(STR_HPP "// This file is auto-generated. Do not edit!
41
42 #include \"opencv2/core/ocl.hpp\"
43 #include \"opencv2/core/ocl_genbase.hpp\"
44 #include \"opencv2/core/opencl/ocl_defs.hpp\"
45
46 #ifdef HAVE_OPENCL
47
48 namespace cv
49 {
50 namespace ocl
51 {
52 ${nested_namespace_start}
53
54 ")
55
56 foreach(cl ${cl_list})
57   get_filename_component(cl_filename "${cl}" NAME_WE)
58   #message("${cl_filename}")
59
60   file(READ "${cl}" lines)
61
62   string(REPLACE "\r" "" lines "${lines}\n")
63   string(REPLACE "\t" "  " lines "${lines}")
64
65   string(REGEX REPLACE "/\\*([^*]/|\\*[^/]|[^*/])*\\*/" ""   lines "${lines}") # multiline comments
66   string(REGEX REPLACE "/\\*([^\n])*\\*/"               ""   lines "${lines}") # single-line comments
67   string(REGEX REPLACE "[ ]*//[^\n]*\n"                 "\n" lines "${lines}") # single-line comments
68   string(REGEX REPLACE "\n[ ]*(\n[ ]*)*"                "\n" lines "${lines}") # empty lines & leading whitespace
69   string(REGEX REPLACE "^\n"                            ""   lines "${lines}") # leading new line
70
71   string(REPLACE "\\" "\\\\" lines "${lines}")
72   string(REPLACE "\"" "\\\"" lines "${lines}")
73   string(REPLACE "\n" "\\n\"\n\"" lines "${lines}")
74
75   string(REGEX REPLACE "\"$" "" lines "${lines}") # unneeded " at the eof
76
77   string(MD5 hash "${lines}")
78
79   set(STR_CPP_DECL "const struct ProgramEntry ${cl_filename}={\"${cl_filename}\",\n\"${lines}, \"${hash}\"};\n")
80   set(STR_HPP_DECL "extern const struct ProgramEntry ${cl_filename};\n")
81   if(new_mode)
82     set(STR_CPP_DECL "${STR_CPP_DECL}ProgramSource ${cl_filename}_oclsrc(${cl_filename}.programStr);\n")
83     set(STR_HPP_DECL "${STR_HPP_DECL}extern ProgramSource ${cl_filename}_oclsrc;\n")
84   endif()
85
86   set(STR_CPP "${STR_CPP}${STR_CPP_DECL}")
87   set(STR_HPP "${STR_HPP}${STR_HPP_DECL}")
88 endforeach()
89
90 set(STR_CPP "${STR_CPP}}\n${nested_namespace_end}}\n#endif\n")
91 set(STR_HPP "${STR_HPP}}\n${nested_namespace_end}}\n#endif\n")
92
93 file(WRITE "${OUTPUT}" "${STR_CPP}")
94
95 if(EXISTS "${OUTPUT_HPP}")
96   file(READ "${OUTPUT_HPP}" hpp_lines)
97 endif()
98 if("${hpp_lines}" STREQUAL "${STR_HPP}")
99   message(STATUS "${OUTPUT_HPP} contains same content")
100 else()
101   file(WRITE "${OUTPUT_HPP}" "${STR_HPP}")
102 endif()