xmake.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package("mnn")
  2. set_homepage("https://www.mnn.zone/")
  3. set_description("MNN is a highly efficient and lightweight deep learning framework.")
  4. set_license("Apache-2.0")
  5. add_urls("https://github.com/alibaba/MNN/archive/refs/tags/$(version).zip",
  6. "https://github.com/alibaba/MNN.git")
  7. add_versions("3.3.0", "08424890d610b01a62df3f9dfa654cd37dd39bf921521639a1c233cc062c58c1")
  8. add_versions("3.2.4", "46fd6179e5817bcb2e523b958d18674da0acaa6cc30c937b834385b3ab8bff8a")
  9. add_versions("3.1.1", "d42b975e2712aa269deb91156572bdc88091c9e6b387faa50f8b5428278e0c51")
  10. add_versions("3.0.5", "23179be245aefe2e1546e94ad6312fde6fdd14c669ff5123ee5a5a9ef14542ef")
  11. add_versions("1.2.2", "78698b879f796a84d1aeb02f60ee38f6860dfdd03c27d1649aaaf9e0adfc8630")
  12. add_versions("1.2.1", "485ae09558ff5626a63d1467ca81ebe0e17fbc60222c386d8f0e857f487c74d0")
  13. for _, name in ipairs({"metal", "opencl", "opengl", "vulkan", "arm82", "onednn", "avx512", "cuda", "tensorrt", "coreml"}) do
  14. add_configs(name, {description = "Enable " .. name .. " support.", default = false, type = "boolean"})
  15. end
  16. for _, name in ipairs({"train", "quantools", "convert"}) do
  17. add_configs(name, {description = "Build " .. name .. " tool.", default = false, type = "boolean"})
  18. end
  19. add_configs("thread_pool", {description = "Use MNN's own thread pool implementation. Will disabel openmp.", default = true, type = "boolean"})
  20. add_configs("openmp", {description = "Use OpenMP's thread pool implementation. Does not work on iOS or Mac OS.", default = false, type = "boolean"})
  21. add_configs("use_system_lib", {description = "When compiling OpenCL/Vulkan, it depends on the OpenCL / Vulkan library of the system.", default = false, type = "boolean"})
  22. add_deps("cmake")
  23. on_load("windows|!arm*", "linux", "macosx", "android", "iphoneos|!x86_64", function (package)
  24. local mnn_path = package:installdir("include")
  25. local mnn_lib_dir = string.sub(mnn_path, 1, string.len(mnn_path) - 7) .. "lib"
  26. if package:config("shared") then
  27. package:add("ldflags", "-L" .. mnn_lib_dir .. " -lMNN")
  28. package:add("shflags", "-L" .. mnn_lib_dir .. " -lMNN")
  29. else
  30. if package:is_plat("linux", "android", "cross") then
  31. package:add("shflags", " -Wl,--whole-archive " .. mnn_lib_dir .. "/libMNN.a -Wl,--no-whole-archive")
  32. package:add("ldflags", " -Wl,--whole-archive " .. mnn_lib_dir .. "/libMNN.a -Wl,--no-whole-archive")
  33. elseif package:is_plat("macosx") then
  34. package:add("ldflags", "-Wl,-force_load " .. mnn_lib_dir .. "/libMNN.a")
  35. package:add("shflags", "-Wl,-force_load " .. mnn_lib_dir .. "/libMNN.a")
  36. elseif package:is_plat("windows") then
  37. package:add("linkdirs", mnn_lib_dir)
  38. package:add("shflags", "/WHOLEARCHIVE:MNN")
  39. package:add("ldflags", "/WHOLEARCHIVE:MNN")
  40. end
  41. end
  42. if package:is_plat("windows") and package:config("shared") then
  43. package:add("defines", "USING_MNN_DLL")
  44. end
  45. if package:is_plat("macosx", "iphoneos") then
  46. if package:config("MNN_OPENCL") then
  47. package:add("frameworks", "OpenCL")
  48. end
  49. if package:config("MNN_OPENGL") then
  50. package:add("frameworks", "OpenGL")
  51. end
  52. if package:config("MNN_METAL") then
  53. package:add("frameworks", "Metal")
  54. end
  55. end
  56. end)
  57. on_install("windows|!arm*", "linux", "macosx", "android", "iphoneos|!x86_64", function (package)
  58. if package:is_plat("windows") then
  59. io.replace("CMakeLists.txt", "set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:Debug>)", "", {plain = true})
  60. io.replace("CMakeLists.txt", "set(CMAKE_MSVC_RUNTIME_LIBRARY ${CMAKE_MSVC_RUNTIME_LIBRARY}DLL)", "", {plain = true})
  61. io.replace("CMakeLists.txt",
  62. 'SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Zi")',
  63. 'SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")', {plain = true})
  64. io.replace("CMakeLists.txt",
  65. 'SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi")',
  66. 'SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")', {plain = true})
  67. end
  68. local configs = {
  69. "-DMNN_BUILD_TEST=OFF",
  70. "-DMNN_BUILD_DEMO=OFF",
  71. "-DMNN_SUPPORT_TFLITE_QUAN=ON",
  72. "-DMNN_PORTABLE_BUILD=OFF",
  73. "-DMNN_SEP_BUILD=OFF",
  74. }
  75. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
  76. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  77. table.insert(configs, "-DMNN_BUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  78. table.insert(configs, "-DMNN_WIN_RUNTIME_MT=" .. (package:has_runtime("MT") and "ON" or "OFF"))
  79. table.insert(configs, "-DMNN_USE_SYSTEM_LIB=" .. (package:config("use_system_lib") and "ON" or "OFF"))
  80. table.insert(configs, "-DMNN_USE_THREAD_POOL=" .. (package:config("thread_pool") and "ON" or "OFF"))
  81. table.insert(configs, "-DMNN_OPENMP=" .. (package:config("openmp") and "ON" or "OFF"))
  82. if package:config("thread_pool") and package:config("openmp") then
  83. wprint("You are using mnn's thread pool, it will disable openmp!")
  84. end
  85. for _, name in ipairs({"metal", "opencl", "opengl", "vulkan", "arm82", "onednn", "avx512", "cuda", "tensorrt", "coreml"}) do
  86. table.insert(configs, "-DMNN_" .. string.upper(name) .. "=" .. (package:config(name) and "ON" or "OFF"))
  87. end
  88. for _, name in ipairs({"train", "quantools", "convert"}) do
  89. table.insert(configs, "-DMNN_BUILD_" .. string.upper(name) .. "=" .. (package:config(name) and "ON" or "OFF"))
  90. end
  91. if package:is_plat("android") then
  92. table.insert(configs, "-DMNN_USE_SSE=OFF")
  93. table.insert(configs, "-DMNN_BUILD_FOR_ANDROID_COMMAND=ON")
  94. end
  95. if package:is_plat("iphoneos") then
  96. table.insert(configs, '-DARCHS=arm64')
  97. table.insert(configs, "-DENABLE_BITCODE=0")
  98. table.insert(configs, "-DMNN_ARM82=ON")
  99. end
  100. import("package.tools.cmake").install(package, configs, {buildir = "bd"})
  101. if package:is_plat("windows") then
  102. os.cp("bd/Release/*.exe", package:installdir("bin"))
  103. os.cp("bd/Release/*.dll", package:installdir("bin"))
  104. elseif package:is_plat("macosx") then
  105. os.cp("include/MNN", package:installdir("include"))
  106. os.cp("bd/*.out", package:installdir("bin"))
  107. else
  108. os.cp("bd/*.out", package:installdir("bin"))
  109. end
  110. package:addenv("PATH", "bin")
  111. end)
  112. on_test(function (package)
  113. assert(package:check_cxxsnippets({test = [[
  114. #include <MNN/Interpreter.hpp>
  115. #include <assert.h>
  116. static void test() {
  117. MNN::Interpreter* session = MNN::Interpreter::createFromFile(nullptr);
  118. assert(session == nullptr);
  119. }
  120. ]]}, {configs = {languages = "c++11"}, includes = "MNN/Interpreter.hpp"}))
  121. end)