xmake.lua 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package("flashlight")
  2. set_homepage("https://github.com/flashlight/flashlight")
  3. set_description("A C++ standalone library for machine learning.")
  4. set_license("MIT")
  5. add_urls("https://github.com/flashlight/flashlight/archive/refs/tags/$(version).tar.gz",
  6. "https://github.com/flashlight/flashlight.git")
  7. add_versions("v0.3.2", "6557f65ef2fbacc867bb6721d9134d0bc15d29e7413cbce0ae5e28d857164029")
  8. add_configs("core", {description = "Build flashlight core.", default = true, type = "boolean"})
  9. add_configs("arrayfire", {description = "Build ArrayFire tensor backend.", default = true, type = "boolean"})
  10. add_configs("contrib", {description = "Build and link additional flashlight contrib assets.", default = true, type = "boolean"})
  11. add_configs("distributed", {description = "Build and link a distributed backend with flashlight.", default = true, type = "boolean"})
  12. add_configs("backend", {description = "Backend with which to build flashlight.", default = "cpu", type = "string", values = {"cpu", "cuda", "opencl"}})
  13. add_configs("profiling", {description = "Enable profiling with Flashlight.", default = false, type = "boolean"})
  14. add_configs("all_libs", {description = "Build all flashlight libraries.", default = false, type = "boolean"})
  15. local libs = {"set", "sequence", "audio", "common", "text"}
  16. for _, lib in ipairs(libs) do
  17. add_configs("lib_" .. lib, {description = "Build flashlight " .. lib .. " library.", default = nil, type = "boolean"})
  18. end
  19. add_configs("all_pkgs", {description = "Build all flashlight packages.", default = false, type = "boolean"})
  20. local pkgs = {"runtime", "vision", "text", "speech"}
  21. for _, pkg in ipairs(pkgs) do
  22. add_configs("pkg_" .. pkg, {description = "Build flashlight " .. pkg .. " library.", default = nil, type = "boolean"})
  23. end
  24. add_configs("cuda", {description = "Use CUDA in flashlight libraries build.", default = true, type = "boolean"})
  25. add_configs("kenlm", {description = "Use KenLM in flashlight libraries build.", default = true, type = "boolean"})
  26. add_configs("mkl", {description = "Use MKL in flashlight libraries build.", default = true, type = "boolean"})
  27. add_configs("openblas", {description = "Use OpenBLAS instead of MKL.", default = false, type = "boolean"})
  28. add_configs("cublas", {description = "Use CUBLAS instead of MKL.", default = false, type = "boolean"})
  29. add_deps("cmake")
  30. add_deps("cereal")
  31. if is_plat("linux", "bsd") then
  32. add_syslinks("pthread")
  33. end
  34. on_load(function (package)
  35. local cuda_utils = {}
  36. -- flahslight core
  37. if package:config("core") then
  38. if package:config("backend") == "cpu" then
  39. package:add("deps", "onednn 2.5")
  40. package:add("defines", "FL_BACKEND_CPU=1")
  41. else
  42. package:add("defines", "FL_BACKEND_CPU=0")
  43. end
  44. if package:config("backend") == "cuda" then
  45. package:add("deps", "nvtx")
  46. package:add("defines", "FL_BACKEND_CUDA=1", "NO_CUDNN_DESTROY_HANDLE")
  47. table.insert(cuda_utils, "cudnn")
  48. else
  49. package:add("defines", "FL_BACKEND_CUDA=0")
  50. end
  51. if package:config("backend") == "opencl" then
  52. package:add("defines", "FL_BACKEND_OPENCL=1")
  53. else
  54. package:add("defines", "FL_BACKEND_OPENCL=0")
  55. end
  56. if package:config("profiling") then
  57. package:add("defines", "FL_BUILD_PROFILING=1")
  58. else
  59. package:add("defines", "FL_BUILD_PROFILING=0")
  60. end
  61. if package:config("arrayfire") then
  62. package:add("deps", "arrayfire")
  63. package:add("defines", "FL_USE_ARRAYFIRE=1")
  64. else
  65. package:add("defines", "FL_USE_ARRAYFIRE=0")
  66. end
  67. if package:config("distributed") then
  68. package:add("deps", "mpich")
  69. if package:config("backend") == "cuda" then
  70. package:add("defines", "NO_NCCL_COMM_DESTROY_HANDLE")
  71. else
  72. package:add("deps", "gloo", {configs = {mpi = true}})
  73. end
  74. end
  75. else
  76. package:config_set("contrib", false)
  77. package:config_set("distributed", false)
  78. package:config_set("profiling", false)
  79. package:config_set("arrayfire", false)
  80. end
  81. -- flashlight library dependencies
  82. if package:config("mkl") then
  83. package:add("deps", "mkl")
  84. package:add("defines", "FL_LIBRARIES_USE_MKL")
  85. elseif package:config("openblas") then
  86. package:add("deps", "openblas")
  87. elseif package:config("cublas") then
  88. table.insert(cuda_utils, "cublas")
  89. end
  90. if package:config("kenlm") then
  91. package:add("defines", "FL_LIBRARIES_USE_KENLM")
  92. package:add("deps", "kenlm")
  93. end
  94. -- flashlight libraries
  95. if package:config("all_libs") then
  96. for _, lib in ipairs(libs) do
  97. if package:config("lib_" .. lib) ~= nil then
  98. package:config_set("lib_" .. lib, true)
  99. end
  100. end
  101. end
  102. if package:config("lib_audio") then
  103. package:add("deps", "fftw", "openmp")
  104. end
  105. if package:config("lib_text") and package:config("kenlm") then
  106. package:add("defines", "KENLM_MAX_ORDER=6")
  107. end
  108. -- flashlight packages
  109. if package:config("all_pkgs") then
  110. for _, lib in ipairs(libs) do
  111. if package:config("pkg_" .. lib) ~= nil then
  112. package:config_set("pkg_" .. lib, true)
  113. end
  114. end
  115. end
  116. if package:config("pkg_runtime") then
  117. package:add("deps", "glog", "gflags")
  118. end
  119. if package:config("pkg_speech") then
  120. package:add("deps", "libsndfile")
  121. end
  122. if package:config("pkg_vision") then
  123. package:add("deps", "stb")
  124. end
  125. -- flashlight backend
  126. if package:config("backend") == "opencl" then
  127. package:add("deps", "opencl", "opencl-headers")
  128. end
  129. if package:config("backend") == "cuda" then
  130. package:add("deps", "cuda", {configs = {utils = cuda_utils}})
  131. else
  132. package:config_set("cuda", false)
  133. package:config_set("cublas", false)
  134. end
  135. if package:config("cuda") then
  136. package:add("defines", "FL_LIBRARIES_USE_CUDA")
  137. end
  138. end)
  139. on_install("linux|x86_64", function (package)
  140. local configs = {
  141. "-DFL_BUILD_TESTS=OFF",
  142. "-DFL_BUILD_EXAMPLES=OFF",
  143. "-DFL_BUILD_STANDALONE=OFF"
  144. }
  145. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  146. table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
  147. table.insert(configs, "-DFL_BUILD_CORE=" .. (package:config("core") and "ON" or "OFF"))
  148. table.insert(configs, "-DFL_BUILD_CONTRIB=" .. (package:config("contrib") and "ON" or "OFF"))
  149. table.insert(configs, "-DFL_BUILD_DISTRIBUTED=" .. (package:config("distributed") and "ON" or "OFF"))
  150. table.insert(configs, "-DFL_BUILD_PROFILING=" .. (package:config("profiling") and "ON" or "OFF"))
  151. table.insert(configs, "-DFL_USE_ARRAYFIRE=" .. (package:config("arrayfire") and "ON" or "OFF"))
  152. table.insert(configs, "-DFL_BACKEND=" .. package:config("backend"):upper())
  153. for _, lib in ipairs(libs) do
  154. if package:config("lib_" .. lib) then
  155. table.insert(configs, ("-DFL_BUILD_LIB_%s=ON"):format(lib:upper()))
  156. end
  157. end
  158. for _, pkg in ipairs(pkgs) do
  159. if package:config("pkg_" .. pkg) then
  160. table.insert(configs, ("-DFL_BUILD_PKG_%s=ON"):format(lib:upper()))
  161. end
  162. end
  163. for _, lib in ipairs({"cuda", "kenlm", "mkl"}) do
  164. if package:config(lib) then
  165. table.insert(configs, ("-DFL_LIBRARIES_USE_%s=ON"):format(lib:upper()))
  166. end
  167. end
  168. io.replace("flashlight/fl/autograd/CMakeLists.txt", "DNNL 2.0 CONFIG", "DNNL", {plain = true})
  169. io.replace("CMakeLists.txt", "find_package(cereal)", [[
  170. find_package(PkgConfig REQUIRED)
  171. pkg_check_modules(cereal REQUIRED cereal)
  172. include_directories(${cereal_INCLUDE_DIRS})
  173. ]], {plain = true})
  174. io.replace("CMakeLists.txt", "target_link_libraries(flashlight PRIVATE cereal)", "", {plain = true})
  175. io.replace("flashlight/fl/common/Logging.cpp", "#include <utility>", "#include <utility>\n#include <array>", {plain = true})
  176. io.replace("flashlight/fl/tensor/TensorBase.h", "#include <vector>", "#include <vector>\n#include <cstdint>", {plain = true})
  177. io.replace("flashlight/fl/tensor/TensorBase.cpp", "#include <utility>", "#include <utility>\n#include <algorithm>", {plain = true})
  178. import("package.tools.cmake").install(package, configs)
  179. end)
  180. on_test(function (package)
  181. assert(package:check_cxxsnippets({test = [[
  182. void test() {
  183. fl::init();
  184. }
  185. ]]}, {configs = {languages = "c++17"}, includes = "flashlight/fl/flashlight.h"}))
  186. end)