CMakeLists.txt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # ----------------------------------------------------------------------------
  2. # CMake file for js support
  3. # ----------------------------------------------------------------------------
  4. if(OPENCV_INITIAL_PASS)
  5. # generator for Objective-C source code and documentation signatures
  6. add_subdirectory(generator)
  7. endif()
  8. if(NOT BUILD_opencv_js) # should be enabled explicitly (by build_js.py script)
  9. return()
  10. endif()
  11. set(the_description "The JavaScript(JS) bindings")
  12. set(OPENCV_JS "opencv.js")
  13. set(JS_HELPER "${CMAKE_CURRENT_SOURCE_DIR}/src/helpers.js")
  14. find_path(EMSCRIPTEN_INCLUDE_DIR
  15. emscripten/bind.h
  16. PATHS
  17. ENV EMSCRIPTEN_ROOT
  18. PATH_SUFFIXES system/include include
  19. DOC "Location of Emscripten SDK")
  20. if(NOT EMSCRIPTEN_INCLUDE_DIR OR NOT PYTHON_DEFAULT_AVAILABLE)
  21. set(DISABLE_MSG "Module 'js' disabled because the following dependencies are not found:")
  22. if(NOT EMSCRIPTEN_INCLUDE_DIR)
  23. set(DISABLE_MSG "${DISABLE_MSG} Emscripten")
  24. endif()
  25. if(NOT PYTHON_DEFAULT_AVAILABLE)
  26. set(DISABLE_MSG "${DISABLE_MSG} Python")
  27. endif()
  28. message(STATUS ${DISABLE_MSG})
  29. ocv_module_disable(js)
  30. endif()
  31. ocv_add_module(js BINDINGS PRIVATE_REQUIRED opencv_js_bindings_generator)
  32. ocv_module_include_directories(${EMSCRIPTEN_INCLUDE_DIR})
  33. add_definitions("-std=c++11")
  34. set(deps ${OPENCV_MODULE_${the_module}_DEPS})
  35. list(REMOVE_ITEM deps opencv_js_bindings_generator) # don't add dummy module
  36. link_libraries(${deps})
  37. set(bindings_cpp "${OPENCV_JS_BINDINGS_DIR}/gen/bindings.cpp")
  38. set_source_files_properties(${bindings_cpp} PROPERTIES GENERATED TRUE)
  39. OCV_OPTION(BUILD_WASM_INTRIN_TESTS "Build WASM intrin tests" OFF )
  40. if(BUILD_WASM_INTRIN_TESTS)
  41. add_definitions(-DTEST_WASM_INTRIN)
  42. ocv_module_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../ts/include")
  43. ocv_module_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../imgcodecs/include")
  44. ocv_module_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../videoio/include")
  45. ocv_module_include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../highgui/include")
  46. ocv_add_executable(${the_module} ${bindings_cpp} "${CMAKE_CURRENT_SOURCE_DIR}/../ts/src/ts_gtest.cpp")
  47. else()
  48. ocv_add_executable(${the_module} ${bindings_cpp})
  49. endif()
  50. add_dependencies(${the_module} gen_opencv_js_source)
  51. set(COMPILE_FLAGS "")
  52. if(NOT CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
  53. set(COMPILE_FLAGS "${COMPILE_FLAGS} -Wno-missing-prototypes")
  54. endif()
  55. if(COMPILE_FLAGS)
  56. set_target_properties(${the_module} PROPERTIES COMPILE_FLAGS ${COMPILE_FLAGS})
  57. endif()
  58. set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} --memory-init-file 0 -s TOTAL_MEMORY=128MB -s WASM_MEM_MAX=1GB -s ALLOW_MEMORY_GROWTH=1")
  59. set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s MODULARIZE=1 -s SINGLE_FILE=1")
  60. set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s EXPORT_NAME=\"'cv'\" -s DEMANGLE_SUPPORT=1")
  61. set(EMSCRIPTEN_LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS} -s FORCE_FILESYSTEM=1 --use-preload-plugins --bind --post-js ${JS_HELPER} ${COMPILE_FLAGS}")
  62. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "${EMSCRIPTEN_LINK_FLAGS}")
  63. # add UMD wrapper
  64. set(MODULE_JS_PATH "${OpenCV_BINARY_DIR}/bin/${the_module}.js")
  65. set(OCV_JS_PATH "${OpenCV_BINARY_DIR}/bin/${OPENCV_JS}")
  66. add_custom_command(
  67. OUTPUT ${OCV_JS_PATH}
  68. COMMAND ${PYTHON_DEFAULT_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/src/make_umd.py" ${MODULE_JS_PATH} "${OCV_JS_PATH}"
  69. DEPENDS ${the_module}
  70. DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/make_umd.py")
  71. add_custom_target(${OPENCV_JS} ALL
  72. DEPENDS ${OCV_JS_PATH}
  73. DEPENDS ${the_module})
  74. # test
  75. set(opencv_test_js_bin_dir "${EXECUTABLE_OUTPUT_PATH}")
  76. set(test_dir ${CMAKE_CURRENT_SOURCE_DIR}/test)
  77. set(opencv_test_js_file_deps "")
  78. # message(STATUS "${opencv_test_js_bin_dir}")
  79. # make sure the build directory exists
  80. file(MAKE_DIRECTORY "${opencv_test_js_bin_dir}")
  81. # gather and copy specific files for js test
  82. file(GLOB_RECURSE test_files RELATIVE "${test_dir}" "${test_dir}/*")
  83. foreach(f ${test_files})
  84. # message(STATUS "copy ${test_dir}/${f} ${opencv_test_js_bin_dir}/${f}")
  85. add_custom_command(OUTPUT "${opencv_test_js_bin_dir}/${f}"
  86. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${test_dir}/${f}" "${opencv_test_js_bin_dir}/${f}"
  87. DEPENDS "${test_dir}/${f}"
  88. COMMENT "Copying ${f}"
  89. )
  90. list(APPEND opencv_test_js_file_deps "${test_dir}/${f}" "${opencv_test_js_bin_dir}/${f}")
  91. endforeach()
  92. # copy test data
  93. set(test_data "haarcascade_frontalface_default.xml")
  94. set(test_data_path "${PROJECT_SOURCE_DIR}/../../data/haarcascades/${test_data}")
  95. add_custom_command(OUTPUT "${opencv_test_js_bin_dir}/${test_data}"
  96. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${test_data_path}" "${opencv_test_js_bin_dir}/${test_data}"
  97. DEPENDS "${test_data_path}"
  98. COMMENT "Copying ${test_data}"
  99. )
  100. list(APPEND opencv_test_js_file_deps "${test_data_path}" "${opencv_test_js_bin_dir}/${test_data}")
  101. add_custom_target(${PROJECT_NAME}_test
  102. DEPENDS ${OCV_JS_PATH} ${opencv_test_js_file_deps})
  103. # perf
  104. set(opencv_perf_js_bin_dir "${EXECUTABLE_OUTPUT_PATH}/perf")
  105. set(perf_dir ${CMAKE_CURRENT_SOURCE_DIR}/perf)
  106. set(opencv_perf_js_file_deps "")
  107. # make sure the build directory exists
  108. file(MAKE_DIRECTORY "${opencv_perf_js_bin_dir}")
  109. # gather and copy specific files for js perf
  110. file(GLOB_RECURSE perf_files RELATIVE "${perf_dir}" "${perf_dir}/*")
  111. foreach(f ${perf_files})
  112. add_custom_command(OUTPUT "${opencv_perf_js_bin_dir}/${f}"
  113. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${perf_dir}/${f}" "${opencv_perf_js_bin_dir}/${f}"
  114. DEPENDS "${perf_dir}/${f}"
  115. COMMENT "Copying ${f}"
  116. )
  117. list(APPEND opencv_perf_js_file_deps "${perf_dir}/${f}" "${opencv_perf_js_bin_dir}/${f}")
  118. endforeach()
  119. add_custom_target(${PROJECT_NAME}_perf
  120. DEPENDS ${OCV_JS_PATH} ${opencv_perf_js_file_deps})
  121. #loader
  122. set(opencv_loader_js_bin_dir "${EXECUTABLE_OUTPUT_PATH}")
  123. set(loader_dir ${CMAKE_CURRENT_SOURCE_DIR}/src)
  124. set(opencv_loader_js_file_deps "")
  125. # make sure the build directory exists
  126. file(MAKE_DIRECTORY "${opencv_loader_js_bin_dir}")
  127. add_custom_command(
  128. TARGET ${PROJECT_NAME} POST_BUILD
  129. COMMAND ${CMAKE_COMMAND} -E copy
  130. ${loader_dir}/loader.js
  131. ${opencv_loader_js_bin_dir}/loader.js)
  132. list(APPEND opencv_loader_js_file_deps "${loader_dir}/loader.js" "${opencv_loader_js_bin_dir}/loader.js")
  133. add_custom_target(${PROJECT_NAME}_loader ALL
  134. DEPENDS ${OCV_JS_PATH} ${opencv_loader_js_file_deps})
  135. add_custom_target(opencv_test_js ALL DEPENDS opencv_js_test opencv_js_perf opencv_js_loader)