xmake.lua 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.12.0", "44c106d5bb47efec04e531fd93008b3fcd1d27138985c5baf4eafac0e1ec9e9d")
  8. add_versions("4.11.0", "9a7c11f924eff5f8d8070e297b322ee68b9227e003fd600d4b8122198091665f")
  9. add_versions("4.10.0", "b2171af5be6b26f7a06b1229948bbb2bdaa74fcf5cd097e0af6378fce50a6eb9")
  10. add_versions("4.9.0", "ddf76f9dffd322c7c3cb1f721d0887f62d747b82059342213138dc190f28bc6c")
  11. add_versions("4.8.0", "cbf47ecc336d2bff36b0dcd7d6c179a9bb59e805136af6b9670ca944aef889bd")
  12. add_versions("4.6.0", "1ec1cba65f9f20fe5a41fda1586e01c70ea0c9a6d7b67c9e13edf0cfe2239277")
  13. add_versions("4.5.5", "a1cfdcf6619387ca9e232687504da996aaa9f7b5689986b8331ec02cb61d28ad")
  14. add_versions("4.5.4", "c20bb83dd790fc69df9f105477e24267706715a9d3c705ca1e7f613c7b3bad3d")
  15. add_versions("4.5.3", "77f616ae4bea416674d8c373984b20c8bd55e7db887fd38c6df73463a0647bab")
  16. add_versions("4.5.2", "ae258ed50aa039279c3d36afdea5c6ecf762515836b27871a8957c610d0424f8")
  17. add_versions("4.5.1", "e27fe5b168918ab60d58d7ace2bd82dd14a4d0bd1d3ae182952c2113f5637513")
  18. add_versions("4.2.0", "9ccb2192d7e8c03c58fee07051364d94ed7599363f3b0dce1c5e6cc11c1bb0ec")
  19. add_versions("3.4.9", "b7ea364de7273cfb3b771a0d9c111b8b8dfb42ff2bcd2d84681902fb8f49892a")
  20. add_patches("4.11.0", "https://github.com/opencv/opencv/commit/767dd838d3074409fd72a4d76c320b1370e95943.diff", "376dd90500ab7205084fd4298ff26137ce9678b00233ad20ca2189ef9eca3a58")
  21. add_patches("4.12.0", "https://github.com/opencv/opencv/pull/27691/commits/90c444abd387ffa70b2e72a34922903a2f0f4f5a.patch", "4811cf490195a7b2952e075c4d713593326bc54fcfa42a33e19d7ed025bb5b6f")
  22. add_resources("4.12.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.12.0.tar.gz", "4197722b4c5ed42b476d42e29beb29a52b6b25c34ec7b4d589c3ae5145fee98e")
  23. add_resources("4.11.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.11.0.tar.gz", "2dfc5957201de2aa785064711125af6abb2e80a64e2dc246aca4119b19687041")
  24. add_resources("4.10.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.10.0.tar.gz", "65597f8fb8dc2b876c1b45b928bbcc5f772ddbaf97539bf1b737623d0604cba1")
  25. add_resources("4.9.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.9.0.tar.gz", "8952c45a73b75676c522dd574229f563e43c271ae1d5bbbd26f8e2b6bc1a4dae")
  26. add_resources("4.8.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.8.0.tar.gz", "b4aef0f25a22edcd7305df830fa926ca304ea9db65de6ccd02f6cfa5f3357dbb")
  27. add_resources("4.6.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.6.0.tar.gz", "1777d5fd2b59029cf537e5fd6f8aa68d707075822f90bde683fcde086f85f7a7")
  28. add_resources("4.5.5", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.5.5.tar.gz", "a97c2eaecf7a23c6dbd119a609c6d7fae903e5f9ff5f1fe678933e01c67a6c11")
  29. add_resources("4.5.4", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.5.4.tar.gz", "ad74b440b4539619dc9b587995a16b691246023d45e34097c73e259f72de9f81")
  30. add_resources("4.5.3", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.5.3.tar.gz", "73da052fd10e73aaba2560eaff10cc5177e2dcc58b27f8aedf7c649e24c233bc")
  31. add_resources("4.5.2", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.5.2.tar.gz", "9f52fd3114ac464cb4c9a2a6a485c729a223afb57b9c24848484e55cef0b5c2a")
  32. add_resources("4.5.1", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.5.1.tar.gz", "12c3b1ddd0b8c1a7da5b743590a288df0934e5cef243e036ca290c2e45e425f5")
  33. add_resources("4.2.0", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/4.2.0.tar.gz", "8a6b5661611d89baa59a26eb7ccf4abb3e55d73f99bb52d8f7c32265c8a43020")
  34. add_resources("3.4.9", "opencv_contrib", "https://github.com/opencv/opencv_contrib/archive/3.4.9.tar.gz", "dc7d95be6aaccd72490243efcec31e2c7d3f21125f88286186862cf9edb14a57")
  35. add_configs("bundled", {description = "Build 3rd-party libraries with OpenCV.", default = true, type = "boolean"})
  36. add_configs("tesseract", {description = "Enable tesseract on text module", default = false, type = "boolean"})
  37. 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"}
  38. local default_features = {"eigen", "ffmpeg", "jpeg", "opengl", "png", "protobuf", "quirc", "webp", "tiff"}
  39. for _, feature in ipairs(features) do
  40. add_configs(feature, {description = "Include " .. feature .. " support.", default = table.contains(default_features, feature), type = "boolean"})
  41. end
  42. add_configs("blas", {description = "Set BLAS vendor.", values = {"mkl", "openblas"}})
  43. add_configs("cuda", {description = "Enable CUDA support.", default = false, type = "boolean"})
  44. add_configs("dynamic_parallel", {description = "Dynamically load parallel runtime (TBB etc.).", default = false, type = "boolean"})
  45. add_configs("mirror", {description = "Set mirror for download.", values = {"github", "gitcode"}})
  46. if is_plat("macosx") then
  47. add_frameworks("Foundation", "CoreFoundation", "CoreGraphics", "AppKit", "OpenCL", "Accelerate")
  48. add_extsources("brew::opencv")
  49. elseif is_plat("linux") then
  50. add_extsources("pacman::opencv", "apt::libopencv-dev")
  51. add_syslinks("pthread", "dl")
  52. elseif is_plat("windows") then
  53. add_syslinks("gdi32", "user32", "glu32", "opengl32", "advapi32", "comdlg32", "ws2_32")
  54. elseif is_plat("mingw") then
  55. add_syslinks("gdi32", "user32", "glu32", "opengl32", "advapi32", "comdlg32", "ws2_32", "pthread")
  56. end
  57. on_fetch("macosx", function (package, opt)
  58. if opt.system then
  59. local result = package:find_package("brew::opencv", opt)
  60. if result then
  61. local includedirs = table.wrap(result.sysincludedirs or result.includedirs)
  62. for _, includedir in ipairs(includedirs) do
  63. local dir = path.join(includedir, "opencv4")
  64. if os.isdir(dir) then
  65. table.insert(includedirs, dir)
  66. end
  67. end
  68. if result.sysincludedirs then
  69. result.sysincludedirs = includedirs
  70. end
  71. if result.includedirs then
  72. result.includedirs = includedirs
  73. end
  74. end
  75. return result
  76. end
  77. end)
  78. on_load("android", "linux", "macosx", "windows", "mingw@windows,msys", function (package)
  79. if package:is_plat("windows") then
  80. local arch = "x64"
  81. if package:is_arch("x86") then arch = "x86"
  82. elseif package:is_arch("arm64") then arch = "ARM64"
  83. end
  84. local linkdir = (package:config("shared") and "lib" or "staticlib")
  85. local vs = package:toolchain("msvc"):config("vs")
  86. local vc_ver = "vc13"
  87. if vs == "2015" then vc_ver = "vc14"
  88. elseif vs == "2017" then vc_ver = "vc15"
  89. elseif vs == "2019" then vc_ver = "vc16"
  90. elseif vs == "2022" then vc_ver = "vc17"
  91. end
  92. package:add("linkdirs", linkdir) -- fix path for 4.9.0/vs2022
  93. package:add("linkdirs", path.join(arch, vc_ver, linkdir))
  94. elseif package:is_plat("mingw") then
  95. local arch = (package:is_arch("x86_64") and "x64" or "x86")
  96. local linkdir = (package:config("shared") and "lib" or "staticlib")
  97. package:add("linkdirs", path.join(arch, "mingw", linkdir))
  98. elseif package:is_plat("android") then
  99. local linkdir = (package:config("shared") and "libs" or "staticlibs")
  100. package:add("linkdirs", path.join("sdk/native", linkdir, package:targetarch()))
  101. package:add("linkdirs", path.join("sdk/native/3rdparty/libs", package:targetarch()))
  102. package:add("includedirs", "sdk/native/jni/include")
  103. elseif package:version():ge("4.0") then
  104. package:add("includedirs", "include/opencv4")
  105. package:add("linkdirs", "lib", "lib/opencv4/3rdparty")
  106. end
  107. if package:config("blas") then
  108. package:add("deps", package:config("blas"))
  109. end
  110. if package:config("cuda") then
  111. package:add("deps", "cuda", {system = true, configs = {utils = {"cudnn", "cufft", "cublas"}}})
  112. end
  113. if package:config("ffmpeg") then
  114. if not package:is_plat("windows") or not package:is_arch("i386", "x86", "arm64") then
  115. package:add("deps", "ffmpeg")
  116. end
  117. end
  118. if package:is_plat("linux") then
  119. if package:config("gtk") then
  120. package:add("deps", "gtk3", {optional = true})
  121. end
  122. end
  123. if not package:is_precompiled() then
  124. package:add("deps", "cmake", "python 3.x", {kind = "binary"})
  125. end
  126. if package:config("tesseract") then
  127. package:add("deps", "tesseract 4.1.3") -- OpenCV need tesseract from the v4 series
  128. end
  129. end)
  130. if on_check then
  131. on_check("windows|arm64", function (package)
  132. import("core.base.semver")
  133. if package:version() and package:version():lt("4.10.0") then
  134. raise("current opencv version does not support windows/arm64!")
  135. end
  136. local vs = package:toolchain("msvc"):config("vs")
  137. assert(tonumber(vs) >= 2022, "package(opencv) requires Visual Studio 2022 and later for arm targets")
  138. end)
  139. end
  140. on_install("android", "linux", "macosx", "windows", "mingw@windows,msys", function (package)
  141. io.replace("cmake/OpenCVUtils.cmake", "if(PKG_CONFIG_FOUND OR PkgConfig_FOUND)", "if(NOT WIN32 AND (PKG_CONFIG_FOUND OR PkgConfig_FOUND))", {plain = true})
  142. local configs = {"-DCMAKE_OSX_DEPLOYMENT_TARGET=",
  143. "-DBUILD_PERF_TESTS=OFF",
  144. "-DBUILD_TESTS=OFF",
  145. "-DBUILD_opencv_hdf=OFF",
  146. "-DBUILD_opencv_java=OFF",
  147. "-DBUILD_opencv_text=ON",
  148. "-DOPENCV_ENABLE_NONFREE=ON",
  149. "-DOPENCV_GENERATE_PKGCONFIG=ON",
  150. "-DBUILD_opencv_python2=OFF",
  151. "-DBUILD_opencv_python3=OFF",
  152. "-DBUILD_JAVA=OFF"}
  153. if package:config("tesseract") then
  154. table.insert(configs, "-DWITH_TESSERACT=ON")
  155. end
  156. if package:config("bundled") then
  157. table.insert(configs, "-DOPENCV_FORCE_3RDPARTY_BUILD=ON")
  158. end
  159. for _, feature in ipairs(features) do
  160. table.insert(configs, "-DWITH_" .. feature:upper() .. "=" .. (package:config(feature) and "ON" or "OFF"))
  161. end
  162. if package:config("cuda") then
  163. table.insert(configs, "-DWITH_CUDA=ON")
  164. end
  165. if package:config("mirror") then
  166. table.insert(configs, "-DOPENCV_DOWNLOAD_MIRROR_ID=" .. package:config("mirror"))
  167. end
  168. table.insert(configs, "-DPARALLEL_ENABLE_PLUGINS=" .. (package:config("dynamic_parallel") and "ON" or "OFF"))
  169. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  170. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  171. if package:is_plat("windows") then
  172. table.insert(configs, "-DBUILD_WITH_STATIC_CRT=" .. (package:has_runtime("MT", "MTd") and "ON" or "OFF"))
  173. if package:is_arch("arm64") then
  174. -- https://github.com/opencv/opencv/issues/25052
  175. table.insert(configs, "-DCPU_NEON_FP16_SUPPORTED=OFF")
  176. -- Newest Windows ARM worker issue
  177. if package:has_runtime("MT", "MTd") then
  178. table.insert(configs, "-DCPU_NEON_DOTPROD_SUPPORTED=OFF")
  179. end
  180. -- https://github.com/opencv/opencv/issues/24235
  181. table.insert(configs, "-DOPENCV_SKIP_SYSTEM_PROCESSOR_DETECTION=ON")
  182. -- Enforce ARM64 without check
  183. table.insert(configs, "-DAARCH64=ON")
  184. end
  185. end
  186. if package:is_cross() or (package:is_plat("mingw") and not package:is_arch(os.arch())) then
  187. if package:is_plat("windows") or package:is_plat("mingw") then
  188. table.insert(configs, "-DCMAKE_SYSTEM_NAME=Windows")
  189. elseif package:is_plat("macosx") then
  190. table.insert(configs, "-DCMAKE_SYSTEM_NAME=Darwin")
  191. elseif package:is_plat("linux") then
  192. table.insert(configs, "-DCMAKE_SYSTEM_NAME=Linux")
  193. elseif package:is_plat("android") then
  194. table.insert(configs, "-DCMAKE_SYSTEM_NAME=Android")
  195. -- from https://github.com/opencv/opencv/issues/15769#issuecomment-549570072
  196. table.insert(configs, "-DBUILD_ANDROID_EXAMPLES=OFF")
  197. table.insert(configs, "-DBUILD_ANDROID_PROJECTS=OFF")
  198. end
  199. -- In case of android we prefer to set CMAKE_ANDROID_ARCH_ABI rather than CMAKE_SYSTEM_PROCESSOR
  200. if package:is_plat("android") then
  201. table.insert(configs, "-DCMAKE_ANDROID_ARCH_ABI=" .. package:targetarch())
  202. else
  203. table.insert(configs, "-DCMAKE_SYSTEM_PROCESSOR=" .. package:targetarch())
  204. end
  205. end
  206. local resourcedir = package:resourcedir("opencv_contrib")
  207. if resourcedir then
  208. import("lib.detect.find_path")
  209. local modulesdir = assert(find_path("modules", path.join(resourcedir, "*")), "modules not found!")
  210. table.insert(configs, "-DOPENCV_EXTRA_MODULES_PATH=" .. path.absolute(path.join(modulesdir, "modules")))
  211. end
  212. local shflags, ldflags
  213. if package:config("ffmpeg") then
  214. -- fix https://github.com/opencv/opencv/issues/22418
  215. if package:version() and package:version():le("4.6") then
  216. io.replace("modules/videoio/src/ffmpeg_codecs.hpp",
  217. "#include <libavformat/avformat.h>",
  218. "#include <libavcodec/version.h>\n#include <libavformat/avformat.h>", {plain = true})
  219. end
  220. -- https://www.ffmpeg.org/platform.html#toc-Advanced-linking-configuration
  221. if package:config("shared") and not package:is_plat("windows", "macosx", "iphoneos") then
  222. ldflags = {"-Wl,-Bsymbolic"}
  223. shflags = {"-Wl,-Bsymbolic"}
  224. end
  225. end
  226. import("package.tools.cmake").install(package, configs, {builddir = "bd", shflags = shflags, ldflags = ldflags})
  227. if not package:is_plat("windows", "android") then
  228. local cmakefile = os.files(package:installdir("**/OpenCVModules.cmake"))
  229. if cmakefile then
  230. io.replace(cmakefile[1], "opencv_wechat_qrcode\n",
  231. "opencv_wechat_qrcode\ninclude(CMakeFindDependencyMacro)\nfind_dependency(Iconv)\n", {plain = true})
  232. end
  233. end
  234. for _, link in ipairs({"opencv_phase_unwrapping", "opencv_surface_matching", "opencv_saliency",
  235. "opencv_wechat_qrcode", "opencv_mcc", "opencv_face",
  236. "opencv_img_hash", "opencv_videostab", "opencv_structured_light", "opencv_intensity_transform",
  237. "opencv_ccalib", "opencv_line_descriptor", "opencv_stereo", "opencv_dnn_objdetect", "opencv_dnn_superres",
  238. "opencv_fuzzy", "opencv_hfs", "opencv_rapid", "opencv_bgsegm", "opencv_bioinspired", "opencv_rgbd",
  239. "opencv_dpm", "opencv_aruco", "opencv_reg", "opencv_tracking", "opencv_datasets", "opencv_xfeatures2d",
  240. "opencv_shape", "opencv_barcode", "opencv_superres", "opencv_viz", "opencv_plot", "opencv_quality",
  241. "opencv_text", "opencv_cudaoptflow", "opencv_optflow", "opencv_ximgproc", "opencv_xobjdetect",
  242. "opencv_xphoto", "opencv_stitching", "opencv_ml", "opencv_photo", "opencv_cudaobjdetect", "opencv_cudalegacy",
  243. "opencv_cudabgsegm", "opencv_cudafeatures2d", "opencv_cudastereo", "opencv_cudaimgproc", "opencv_cudafilters",
  244. "opencv_cudaarithm", "opencv_cudawarping", "opencv_cudacodec", "opencv_cudev", "opencv_gapi", "opencv_objdetect",
  245. "opencv_highgui", "opencv_videoio", "opencv_video", "opencv_calib3d", "opencv_dnn", "opencv_features2d",
  246. "opencv_flann", "opencv_imgcodecs", "opencv_imgproc", "opencv_core"}) do
  247. local reallink = link
  248. if package:is_plat("windows", "mingw") then
  249. reallink = reallink .. package:version():gsub("%.", "")
  250. reallink = reallink .. (package:debug() and "d" or "")
  251. end
  252. package:add("links", reallink)
  253. end
  254. if package:is_plat("android") then
  255. for _, suffix in ipairs({"*.a", "*.so"}) do
  256. for _, f in ipairs(os.files(path.join(package:installdir(path.join("sdk/native/3rdparty/libs", package:targetarch())), suffix))) do
  257. package:add("links", path.basename(f):match("lib(.+)"))
  258. end
  259. end
  260. elseif package:is_plat("windows") then
  261. local arch = "x64"
  262. if package:is_arch("x86") then arch = "x86"
  263. elseif package:is_arch("arm64") then arch = "ARM64"
  264. end
  265. -- Workaround for arm64
  266. if package:is_arch("arm64") then
  267. os.trymv(path.join(package:installdir(), "x64"), path.join(package:installdir(), "ARM64"))
  268. os.trymv(path.join(package:installdir(), "x86"), path.join(package:installdir(), "ARM64"))
  269. end
  270. local linkdir = (package:config("shared") and "lib" or "staticlib")
  271. local vs = package:toolchain("msvc"):config("vs")
  272. local vc_ver = "vc13"
  273. if vs == "2015" then vc_ver = "vc14"
  274. elseif vs == "2017" then vc_ver = "vc15"
  275. elseif vs == "2019" then vc_ver = "vc16"
  276. elseif vs == "2022" then vc_ver = "vc17"
  277. end
  278. local installdir = package:installdir(arch, vc_ver)
  279. local libfiles = {}
  280. table.join2(libfiles, os.files(path.join(package:installdir(), linkdir, "*.lib")))
  281. table.join2(libfiles, os.files(path.join(package:installdir(), arch, vc_ver, linkdir, "*.lib")))
  282. for _, f in ipairs(libfiles) do
  283. if not f:match("opencv_.+") then
  284. package:add("links", path.basename(f))
  285. end
  286. end
  287. package:addenv("PATH", "bin") -- Fix path for 4.9.0 / vs2022
  288. package:addenv("PATH", path.join(arch, vc_ver, "bin"))
  289. elseif package:is_plat("mingw") then
  290. local arch = package:is_arch("x86_64") and "x64" or "x86"
  291. local linkdir = (package:config("shared") and "lib" or "staticlib")
  292. for _, f in ipairs(os.files(path.join(package:installdir(), arch, "mingw", linkdir, "lib*.a"))) do
  293. if not f:match("libopencv_.+") then
  294. package:add("links", path.basename(f):match("lib(.+)"))
  295. end
  296. end
  297. package:addenv("PATH", path.join(arch, "mingw", "bin"))
  298. else
  299. if package:version():ge("4.0") then
  300. for _, suffix in ipairs({"*.a", "*.so", "*.dylib"}) do
  301. for _, f in ipairs(os.files(path.join(package:installdir("lib/opencv4/3rdparty"), suffix))) do
  302. package:add("links", path.basename(f):match("lib(.+)"))
  303. end
  304. end
  305. end
  306. package:addenv("PATH", "bin")
  307. end
  308. end)
  309. on_test(function (package)
  310. if not package:is_cross() then
  311. -- FIXME, it will crash on action ci/2022/MD, but it works on local machine
  312. if not package:has_runtime("MD", "MDd") then
  313. if package:debug() and package:is_plat("windows", "mingw") then
  314. os.vrun("opencv_versiond")
  315. else
  316. os.vrun("opencv_version")
  317. end
  318. end
  319. end
  320. assert(package:check_cxxsnippets({test = [[
  321. #include <iostream>
  322. void test(int argc, char** argv) {
  323. cv::CommandLineParser parser(argc, argv, "{help h||show help message}");
  324. if (parser.has("help")) {
  325. parser.printMessage();
  326. }
  327. cv::Mat image(3, 3, CV_8UC1);
  328. std::cout << CV_VERSION << std::endl;
  329. }
  330. ]]}, {configs = {languages = "c++11"},
  331. includes = package:version():ge("4.0") and "opencv2/opencv.hpp" or "opencv/cv.h"}))
  332. end)