xmake.lua 7.0 KB

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