xmake.lua 6.9 KB

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