Merge pull request #2801 from ilya-lavrenov:tapi_reduction
[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 #include \"opencv2/core/opencl/ocl_defs.hpp\"
33
34 namespace cv
35 {
36 namespace ocl
37 {
38 ${nested_namespace_start}
39
40 ")
41
42 foreach(cl ${cl_list})
43   get_filename_component(cl_filename "${cl}" NAME_WE)
44   #message("${cl_filename}")
45
46   file(READ "${cl}" lines)
47
48   string(REPLACE "\r" "" lines "${lines}\n")
49   string(REPLACE "\t" "  " lines "${lines}")
50
51   string(REGEX REPLACE "/\\*([^*]/|\\*[^/]|[^*/])*\\*/" ""   lines "${lines}") # multiline comments
52   string(REGEX REPLACE "/\\*([^\n])*\\*/"               ""   lines "${lines}") # single-line comments
53   string(REGEX REPLACE "[ ]*//[^\n]*\n"                 "\n" lines "${lines}") # single-line comments
54   string(REGEX REPLACE "\n[ ]*(\n[ ]*)*"                "\n" lines "${lines}") # empty lines & leading whitespace
55   string(REGEX REPLACE "^\n"                            ""   lines "${lines}") # leading new line
56
57   string(REPLACE "\\" "\\\\" lines "${lines}")
58   string(REPLACE "\"" "\\\"" lines "${lines}")
59   string(REPLACE "\n" "\\n\"\n\"" lines "${lines}")
60
61   string(REGEX REPLACE "\"$" "" lines "${lines}") # unneeded " at the eof
62
63   string(MD5 hash "${lines}")
64
65   set(STR_CPP_DECL "const struct ProgramEntry ${cl_filename}={\"${cl_filename}\",\n\"${lines}, \"${hash}\"};\n")
66   set(STR_HPP_DECL "extern const struct ProgramEntry ${cl_filename};\n")
67   if(new_mode)
68     set(STR_CPP_DECL "${STR_CPP_DECL}ProgramSource ${cl_filename}_oclsrc(${cl_filename}.programStr);\n")
69     set(STR_HPP_DECL "${STR_HPP_DECL}extern ProgramSource ${cl_filename}_oclsrc;\n")
70   endif()
71
72   set(STR_CPP "${STR_CPP}${STR_CPP_DECL}")
73   set(STR_HPP "${STR_HPP}${STR_HPP_DECL}")
74 endforeach()
75
76 set(STR_CPP "${STR_CPP}}\n${nested_namespace_end}}\n")
77 set(STR_HPP "${STR_HPP}}\n${nested_namespace_end}}\n")
78
79 file(WRITE "${OUTPUT}" "${STR_CPP}")
80
81 if(EXISTS "${OUTPUT_HPP}")
82   file(READ "${OUTPUT_HPP}" hpp_lines)
83 endif()
84 if("${hpp_lines}" STREQUAL "${STR_HPP}")
85   message(STATUS "${OUTPUT_HPP} contains same content")
86 else()
87   file(WRITE "${OUTPUT_HPP}" "${STR_HPP}")
88 endif()