cl2cpp.cmake 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. if (NOT EXISTS "${CL_DIR}")
  2. message(FATAL_ERROR "Specified wrong OpenCL kernels directory: ${CL_DIR}")
  3. endif()
  4. file(GLOB cl_list "${CL_DIR}/*.cl" )
  5. list(SORT cl_list)
  6. if (NOT cl_list)
  7. message(FATAL_ERROR "Can't find OpenCL kernels in directory: ${CL_DIR}")
  8. endif()
  9. string(REGEX REPLACE "\\.cpp$" ".hpp" OUTPUT_HPP "${OUTPUT}")
  10. get_filename_component(OUTPUT_HPP_NAME "${OUTPUT_HPP}" NAME)
  11. set(nested_namespace_start "namespace ${MODULE_NAME}\n{")
  12. set(nested_namespace_end "}")
  13. set(STR_CPP "// This file is auto-generated. Do not edit!
  14. #include \"opencv2/core.hpp\"
  15. #include \"cvconfig.h\"
  16. #include \"${OUTPUT_HPP_NAME}\"
  17. #ifdef HAVE_OPENCL
  18. namespace cv
  19. {
  20. namespace ocl
  21. {
  22. ${nested_namespace_start}
  23. static const char* const moduleName = \"${MODULE_NAME}\";
  24. ")
  25. set(STR_HPP "// This file is auto-generated. Do not edit!
  26. #include \"opencv2/core/ocl.hpp\"
  27. #include \"opencv2/core/ocl_genbase.hpp\"
  28. #include \"opencv2/core/opencl/ocl_defs.hpp\"
  29. #ifdef HAVE_OPENCL
  30. namespace cv
  31. {
  32. namespace ocl
  33. {
  34. ${nested_namespace_start}
  35. ")
  36. foreach(cl ${cl_list})
  37. get_filename_component(cl_filename "${cl}" NAME_WE)
  38. #message("${cl_filename}")
  39. file(READ "${cl}" lines)
  40. string(REPLACE "\r" "" lines "${lines}\n")
  41. string(REPLACE "\t" " " lines "${lines}")
  42. string(REGEX REPLACE "/\\*([^*]/|\\*[^/]|[^*/])*\\*/" "" lines "${lines}") # multiline comments
  43. string(REGEX REPLACE "/\\*([^\n])*\\*/" "" lines "${lines}") # single-line comments
  44. string(REGEX REPLACE "[ ]*//[^\n]*\n" "\n" lines "${lines}") # single-line comments
  45. string(REGEX REPLACE "\n[ ]*(\n[ ]*)*" "\n" lines "${lines}") # empty lines & leading whitespace
  46. string(REGEX REPLACE "^\n" "" lines "${lines}") # leading new line
  47. string(REPLACE "\\" "\\\\" lines "${lines}")
  48. string(REPLACE "\"" "\\\"" lines "${lines}")
  49. string(REPLACE "\n" "\\n\"\n\"" lines "${lines}")
  50. string(REGEX REPLACE "\"$" "" lines "${lines}") # unneeded " at the eof
  51. string(MD5 hash "${lines}")
  52. set(STR_CPP_DECL "struct cv::ocl::internal::ProgramEntry ${cl_filename}_oclsrc={moduleName, \"${cl_filename}\",\n\"${lines}, \"${hash}\", NULL};\n")
  53. set(STR_HPP_DECL "extern struct cv::ocl::internal::ProgramEntry ${cl_filename}_oclsrc;\n")
  54. set(STR_CPP "${STR_CPP}${STR_CPP_DECL}")
  55. set(STR_HPP "${STR_HPP}${STR_HPP_DECL}")
  56. endforeach()
  57. set(STR_CPP "${STR_CPP}\n${nested_namespace_end}}}\n#endif\n")
  58. set(STR_HPP "${STR_HPP}\n${nested_namespace_end}}}\n#endif\n")
  59. file(WRITE "${OUTPUT}" "${STR_CPP}")
  60. if(EXISTS "${OUTPUT_HPP}")
  61. file(READ "${OUTPUT_HPP}" hpp_lines)
  62. endif()
  63. if("${hpp_lines}" STREQUAL "${STR_HPP}")
  64. message(STATUS "${OUTPUT_HPP} contains the same content")
  65. else()
  66. file(WRITE "${OUTPUT_HPP}" "${STR_HPP}")
  67. endif()