2
0

xmake.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package("opencv")
  2. set_homepage("https://opencv.org/")
  3. set_description("A open source computer vision library.")
  4. set_license("Apache-2.0")
  5. add_urls("https://github.com/opencv/opencv/archive/$(version).tar.gz",
  6. "https://github.com/opencv/opencv.git")
  7. add_versions("4.9.0", "ddf76f9dffd322c7c3cb1f721d0887f62d747b82059342213138dc190f28bc6c")
  8. add_versions("4.8.0", "cbf47ecc336d2bff36b0dcd7d6c179a9bb59e805136af6b9670ca944aef889bd")
  9. add_versions("4.6.0", "1ec1cba65f9f20fe5a41fda1586e01c70ea0c9a6d7b67c9e13edf0cfe2239277")
  10. add_versions("4.5.5", "a1cfdcf6619387ca9e232687504da996aaa9f7b5689986b8331ec02cb61d28ad")
  11. add_versions("4.5.4", "c20bb83dd790fc69df9f105477e24267706715a9d3c705ca1e7f613c7b3bad3d")
  12. add_versions("4.5.3", "77f616ae4bea416674d8c373984b20c8bd55e7db887fd38c6df73463a0647bab")
  13. add_versions("4.5.2", "ae258ed50aa039279c3d36afdea5c6ecf762515836b27871a8957c610d0424f8")
  14. add_versions("4.5.1", "e27fe5b168918ab60d58d7ace2bd82dd14a4d0bd1d3ae182952c2113f5637513")
  15. add_versions("4.2.0", "9ccb2192d7e8c03c58fee07051364d94ed7599363f3b0dce1c5e6cc11c1bb0ec")
  16. add_versions("3.4.9", "b7ea364de7273cfb3b771a0d9c111b8b8dfb42ff2bcd2d84681902fb8f49892a")
  17. add_resources("4.9.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.9.0.tar.gz", "8952c45a73b75676c522dd574229f563e43c271ae1d5bbbd26f8e2b6bc1a4dae")
  18. add_resources("4.8.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.8.0.tar.gz", "b4aef0f25a22edcd7305df830fa926ca304ea9db65de6ccd02f6cfa5f3357dbb")
  19. add_resources("4.6.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.6.0.tar.gz", "1777d5fd2b59029cf537e5fd6f8aa68d707075822f90bde683fcde086f85f7a7")
  20. add_resources("4.5.5", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.5.5.tar.gz", "a97c2eaecf7a23c6dbd119a609c6d7fae903e5f9ff5f1fe678933e01c67a6c11")
  21. add_resources("4.5.4", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.5.4.tar.gz", "ad74b440b4539619dc9b587995a16b691246023d45e34097c73e259f72de9f81")
  22. add_resources("4.5.3", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.5.3.tar.gz", "73da052fd10e73aaba2560eaff10cc5177e2dcc58b27f8aedf7c649e24c233bc")
  23. add_resources("4.5.2", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.5.2.tar.gz", "9f52fd3114ac464cb4c9a2a6a485c729a223afb57b9c24848484e55cef0b5c2a")
  24. add_resources("4.5.1", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.5.1.tar.gz", "12c3b1ddd0b8c1a7da5b743590a288df0934e5cef243e036ca290c2e45e425f5")
  25. add_resources("4.2.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.2.0.tar.gz", "8a6b5661611d89baa59a26eb7ccf4abb3e55d73f99bb52d8f7c32265c8a43020")
  26. add_resources("3.4.9", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/3.4.9.tar.gz", "dc7d95be6aaccd72490243efcec31e2c7d3f21125f88286186862cf9edb14a57")
  27. add_configs("bundled", {description = "Build 3rd-party libraries with OpenCV.", default = true, type = "boolean"})
  28. add_configs("tesseract", {description = "Enable tesseract on text module", default = false, type = "boolean"})
  29. local features = {"1394", "vtk", "eigen", "ffmpeg", "gstreamer", "gtk", "ipp", "halide", "vulkan", "jasper", "openjpeg", "jpeg", "webp", "openexr", "opengl", "png", "tbb", "openmp", "tiff", "itt", "protobuf", "quirc", "obsensor"}
  30. local default_features = {"1394", "eigen", "ffmpeg", "jpeg", "opengl", "png", "protobuf", "quirc", "webp", "tiff"}
  31. for _, feature in ipairs(features) do
  32. add_configs(feature, {description = "Include " .. feature .. " support.", default = table.contains(default_features, feature), type = "boolean"})
  33. end
  34. add_configs("blas", {description = "Set BLAS vendor.", values = {"mkl", "openblas"}})
  35. add_configs("cuda", {description = "Enable CUDA support.", default = false, type = "boolean"})
  36. add_configs("dynamic_parallel", {description = "Dynamically load parallel runtime (TBB etc.).", default = false, type = "boolean"})
  37. if is_plat("macosx") then
  38. add_frameworks("Foundation", "CoreFoundation", "CoreGraphics", "AppKit", "OpenCL", "Accelerate")
  39. add_extsources("brew::opencv")
  40. elseif is_plat("linux") then
  41. add_extsources("pacman::opencv", "apt::libopencv-dev")
  42. add_syslinks("pthread", "dl")
  43. elseif is_plat("windows", "mingw") then
  44. add_syslinks("gdi32", "user32", "glu32", "opengl32", "advapi32", "comdlg32", "ws2_32")
  45. end
  46. on_fetch("macosx", function (package, opt)
  47. if opt.system then
  48. local result = package:find_package("brew::opencv", opt)
  49. if result then
  50. local includedirs = table.wrap(result.sysincludedirs or result.includedirs)
  51. for _, includedir in ipairs(includedirs) do
  52. local dir = path.join(includedir, "opencv4")
  53. if os.isdir(dir) then
  54. table.insert(includedirs, dir)
  55. end
  56. end
  57. if result.sysincludedirs then
  58. result.sysincludedirs = includedirs
  59. end
  60. if result.includedirs then
  61. result.includedirs = includedirs
  62. end
  63. end
  64. return result
  65. end
  66. end)
  67. on_load("linux", "macosx", "windows", "mingw@windows,msys", function (package)
  68. if package:is_plat("windows") then
  69. local arch = "x64"
  70. if package:is_arch("x86") then arch = "x86"
  71. elseif package:is_arch("arm64") then arch = "ARM64"
  72. end
  73. local linkdir = (package:config("shared") and "lib" or "staticlib")
  74. local vs = import("core.tool.toolchain").load("msvc"):config("vs")
  75. local vc_ver = "vc13"
  76. if vs == "2015" then vc_ver = "vc14"
  77. elseif vs == "2017" then vc_ver = "vc15"
  78. elseif vs == "2019" then vc_ver = "vc16"
  79. elseif vs == "2022" then vc_ver = "vc17"
  80. end
  81. package:add("linkdirs", path.join(arch, vc_ver, linkdir))
  82. elseif package:is_plat("mingw") then
  83. local arch = (package:is_arch("x86_64") and "x64" or "x86")
  84. local linkdir = (package:config("shared") and "lib" or "staticlib")
  85. package:add("linkdirs", path.join(arch, "mingw", linkdir))
  86. elseif package:version():ge("4.0") then
  87. package:add("includedirs", "include/opencv4")
  88. package:add("linkdirs", "lib", "lib/opencv4/3rdparty")
  89. end
  90. if package:config("blas") then
  91. package:add("deps", package:config("blas"))
  92. end
  93. if package:config("cuda") then
  94. package:add("deps", "cuda", {system = true, configs = {utils = {"cudnn", "cufft", "cublas"}}})
  95. end
  96. if package:is_plat("linux") then
  97. if package:config("gtk") then
  98. package:add("deps", "gtk+3", {optional = true})
  99. end
  100. end
  101. if not package.is_built or package:is_built() then
  102. package:add("deps", "cmake", "python 3.x", {kind = "binary"})
  103. end
  104. if package:config("tesseract") then
  105. package:add("deps", "tesseract 4.1.3") -- Opencv need tesseract from the v4 series
  106. end
  107. end)
  108. on_install("linux", "macosx", "windows", "mingw@windows,msys", function (package)
  109. io.replace("cmake/OpenCVUtils.cmake", "if(PKG_CONFIG_FOUND OR PkgConfig_FOUND)", "if(NOT WIN32 AND (PKG_CONFIG_FOUND OR PkgConfig_FOUND))", {plain = true})
  110. local configs = {"-DCMAKE_OSX_DEPLOYMENT_TARGET=",
  111. "-DBUILD_PERF_TESTS=OFF",
  112. "-DBUILD_TESTS=OFF",
  113. "-DBUILD_opencv_hdf=OFF",
  114. "-DBUILD_opencv_java=OFF",
  115. "-DBUILD_opencv_text=ON",
  116. "-DOPENCV_ENABLE_NONFREE=ON",
  117. "-DOPENCV_GENERATE_PKGCONFIG=ON",
  118. "-DBUILD_opencv_python2=OFF",
  119. "-DBUILD_opencv_python3=OFF",
  120. "-DBUILD_JAVA=OFF"}
  121. if package:config("tesseract") then
  122. table.insert(configs, "-DWITH_TESSERACT=ON")
  123. end
  124. if package:config("bundled") then
  125. table.insert(configs, "-DOPENCV_FORCE_3RDPARTY_BUILD=ON")
  126. end
  127. for _, feature in ipairs(features) do
  128. table.insert(configs, "-DWITH_" .. feature:upper() .. "=" .. (package:config(feature) and "ON" or "OFF"))
  129. end
  130. if package:config("cuda") then
  131. table.insert(configs, "-DWITH_CUDA=ON")
  132. end
  133. table.insert(configs, "-DPARALLEL_ENABLE_PLUGINS=" .. (package:config("dynamic_parallel") and "ON" or "OFF"))
  134. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  135. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  136. if package:is_plat("windows") then
  137. table.insert(configs, "-DBUILD_WITH_STATIC_CRT=" .. (package:config("vs_runtime"):startswith("MT") and "ON" or "OFF"))
  138. if package:is_arch("arm64") then
  139. local vs = import("core.tool.toolchain").load("msvc"):config("vs")
  140. assert(tonumber(vs) >= 2022, "opencv requires Visual Studio 2022 and later for arm targets")
  141. table.insert(configs, "-DCMAKE_SYSTEM_NAME=Windows")
  142. table.insert(configs, "-DCMAKE_SYSTEM_PROCESSOR=ARM64")
  143. end
  144. elseif package:is_plat("mingw") then
  145. table.insert(configs, "-DCMAKE_SYSTEM_PROCESSOR=" .. (package:is_arch("x86_64") and "AMD64" or "i686"))
  146. elseif package:is_plat("macosx") then
  147. table.insert(configs, "-DCMAKE_SYSTEM_PROCESSOR=" .. (package:is_arch("x86_64") and "AMD64" or "ARM64"))
  148. end
  149. local resourcedir = package:resourcedir("opencv_contrib")
  150. if resourcedir then
  151. import("lib.detect.find_path")
  152. local modulesdir = assert(find_path("modules", path.join(resourcedir, "*")), "modules not found!")
  153. table.insert(configs, "-DOPENCV_EXTRA_MODULES_PATH=" .. path.absolute(path.join(modulesdir, "modules")))
  154. end
  155. import("package.tools.cmake").install(package, configs, {buildir = "bd"})
  156. for _, link in ipairs({"opencv_phase_unwrapping", "opencv_surface_matching", "opencv_saliency", "opencv_wechat_qrcode", "opencv_mcc", "opencv_face", "opencv_img_hash", "opencv_videostab", "opencv_structured_light", "opencv_intensity_transform", "opencv_ccalib", "opencv_line_descriptor", "opencv_stereo", "opencv_dnn_objdetect", "opencv_dnn_superres", "opencv_fuzzy", "opencv_hfs", "opencv_rapid", "opencv_bgsegm", "opencv_bioinspired", "opencv_rgbd", "opencv_dpm", "opencv_aruco", "opencv_reg", "opencv_tracking", "opencv_datasets", "opencv_xfeatures2d", "opencv_shape", "opencv_barcode", "opencv_superres", "opencv_viz", "opencv_plot", "opencv_quality", "opencv_text", "opencv_cudaoptflow", "opencv_optflow", "opencv_ximgproc", "opencv_xobjdetect", "opencv_xphoto", "opencv_stitching", "opencv_ml", "opencv_photo", "opencv_cudaobjdetect", "opencv_cudalegacy", "opencv_cudabgsegm", "opencv_cudafeatures2d", "opencv_cudastereo", "opencv_cudaimgproc", "opencv_cudafilters", "opencv_cudaarithm", "opencv_cudawarping", "opencv_cudacodec", "opencv_cudev", "opencv_gapi", "opencv_objdetect", "opencv_highgui", "opencv_videoio", "opencv_video", "opencv_calib3d", "opencv_dnn", "opencv_features2d", "opencv_flann", "opencv_imgcodecs", "opencv_imgproc", "opencv_core"}) do
  157. local reallink = link
  158. if package:is_plat("windows", "mingw") then
  159. reallink = reallink .. package:version():gsub("%.", "")
  160. reallink = reallink .. (package:debug() and "d" or "")
  161. end
  162. package:add("links", reallink)
  163. end
  164. if package:is_plat("windows") then
  165. local arch = "x64"
  166. if package:is_arch("x86") then arch = "x86"
  167. elseif package:is_arch("arm64") then arch = "ARM64"
  168. end
  169. local linkdir = (package:config("shared") and "lib" or "staticlib")
  170. local vs = import("core.tool.toolchain").load("msvc"):config("vs")
  171. local vc_ver = "vc13"
  172. if vs == "2015" then vc_ver = "vc14"
  173. elseif vs == "2017" then vc_ver = "vc15"
  174. elseif vs == "2019" then vc_ver = "vc16"
  175. elseif vs == "2022" then vc_ver = "vc17"
  176. end
  177. local installdir = package:installdir(arch, vc_ver)
  178. for _, f in ipairs(os.files(path.join(installdir, linkdir, "*.lib"))) do
  179. if not f:match("opencv_.+") then
  180. package:add("links", path.basename(f))
  181. end
  182. end
  183. package:addenv("PATH", path.join(arch, vc_ver, "bin"))
  184. elseif package:is_plat("mingw") then
  185. local arch = package:is_arch("x86_64") and "x64" or "x86"
  186. local linkdir = (package:config("shared") and "lib" or "staticlib")
  187. for _, f in ipairs(os.files(path.join(package:installdir(), arch, "mingw", linkdir, "lib*.a"))) do
  188. if not f:match("libopencv_.+") then
  189. package:add("links", path.basename(f):match("lib(.+)"))
  190. end
  191. end
  192. package:addenv("PATH", path.join(arch, "mingw", "bin"))
  193. else
  194. if package:version():ge("4.0") then
  195. for _, suffix in ipairs({"*.a", "*.so", "*.dylib"}) do
  196. for _, f in ipairs(os.files(path.join(package:installdir("lib/opencv4/3rdparty"), suffix))) do
  197. package:add("links", path.basename(f):match("lib(.+)"))
  198. end
  199. end
  200. end
  201. package:addenv("PATH", "bin")
  202. end
  203. end)
  204. on_test(function (package)
  205. if not package:is_cross() then
  206. if package:debug() and package:is_plat("windows", "mingw") then
  207. os.vrun("opencv_versiond")
  208. else
  209. os.vrun("opencv_version")
  210. end
  211. end
  212. assert(package:check_cxxsnippets({test = [[
  213. #include <iostream>
  214. void test(int argc, char** argv) {
  215. cv::CommandLineParser parser(argc, argv, "{help h||show help message}");
  216. if (parser.has("help")) {
  217. parser.printMessage();
  218. }
  219. cv::Mat image(3, 3, CV_8UC1);
  220. std::cout << CV_VERSION << std::endl;
  221. }
  222. ]]}, {configs = {languages = "c++11"},
  223. includes = package:version():ge("4.0") and "opencv2/opencv.hpp" or "opencv/cv.h"}))
  224. end)