Merge commit '60906d917' into merge-2.4
[profile/ivi/opencv.git] / cmake / cl2cpp.cmake
1 file(GLOB cl_list "${CL_DIR}/*.cl" )
2 list(SORT cl_list)
3
4 string(REPLACE ".cpp" ".hpp" OUTPUT_HPP "${OUTPUT}")
5 get_filename_component(OUTPUT_HPP_NAME "${OUTPUT_HPP}" NAME)
6
7 if("${MODULE_NAME}" STREQUAL "ocl")
8     set(nested_namespace_start "")
9     set(nested_namespace_end "")
10 else()
11     set(new_mode ON)
12     set(nested_namespace_start "namespace ${MODULE_NAME}\n{")
13     set(nested_namespace_end "}")
14 endif()
15
16 set(STR_CPP "// This file is auto-generated. Do not edit!
17
18 #include \"precomp.hpp\"
19 #include \"${OUTPUT_HPP_NAME}\"
20
21 namespace cv
22 {
23 namespace ocl
24 {
25 ${nested_namespace_start}
26
27 ")
28
29 set(STR_HPP "// This file is auto-generated. Do not edit!
30
31 #include \"opencv2/core/ocl_genbase.hpp\"
32
33 namespace cv
34 {
35 namespace ocl
36 {
37 ${nested_namespace_start}
38
39 ")
40
41 foreach(cl ${cl_list})
42   get_filename_component(cl_filename "${cl}" NAME_WE)
43   #message("${cl_filename}")
44
45   file(READ "${cl}" lines)
46
47   string(REPLACE "\r" "" lines "${lines}\n")
48   string(REPLACE "\t" "  " lines "${lines}")
49
50   string(REGEX REPLACE "/\\*([^*]/|\\*[^/]|[^*/])*\\*/" ""   lines "${lines}") # multiline comments
51   string(REGEX REPLACE "/\\*([^\n])*\\*/"               ""   lines "${lines}") # single-line comments
52   string(REGEX REPLACE "[ ]*//[^\n]*\n"                 "\n" lines "${lines}") # single-line comments
53   string(REGEX REPLACE "\n[ ]*(\n[ ]*)*"                "\n" lines "${lines}") # empty lines & leading whitespace
54   string(REGEX REPLACE "^\n"                            ""   lines "${lines}") # leading new line
55
56   string(REPLACE "\\" "\\\\" lines "${lines}")
57   string(REPLACE "\"" "\\\"" lines "${lines}")
58   string(REPLACE "\n" "\\n\"\n\"" lines "${lines}")
59
60   string(REGEX REPLACE "\"$" "" lines "${lines}") # unneeded " at the eof
61
62   string(MD5 hash "${lines}")
63
64   set(STR_CPP_DECL "const struct ProgramEntry ${cl_filename}={\"${cl_filename}\",\n\"${lines}, \"${hash}\"};\n")
65   set(STR_HPP_DECL "extern const struct ProgramEntry ${cl_filename};\n")
66   if(new_mode)
67     set(STR_CPP_DECL "${STR_CPP_DECL}ProgramSource2 ${cl_filename}_oclsrc(${cl_filename}.programStr);\n")
68     set(STR_HPP_DECL "${STR_HPP_DECL}extern ProgramSource2 ${cl_filename}_oclsrc;\n")
69   endif()
70
71   set(STR_CPP "${STR_CPP}${STR_CPP_DECL}")
72   set(STR_HPP "${STR_HPP}${STR_HPP_DECL}")
73 endforeach()
74
75 set(STR_CPP "${STR_CPP}}\n${nested_namespace_end}}\n")
76 set(STR_HPP "${STR_HPP}}\n${nested_namespace_end}}\n")
77
78 file(WRITE "${OUTPUT}" "${STR_CPP}")
79
80 if(EXISTS "${OUTPUT_HPP}")
81   file(READ "${OUTPUT_HPP}" hpp_lines)
82 endif()
83 if("${hpp_lines}" STREQUAL "${STR_HPP}")
84   message(STATUS "${OUTPUT_HPP} contains same content")
85 else()
86   file(WRITE "${OUTPUT_HPP}" "${STR_HPP}")
87 endif()