xmake.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package("opencl")
  2. set_homepage("https://opencl.org/")
  3. set_description("OpenCL is an open, royalty-free industry standard that makes much faster computations possible through parallel computing.")
  4. if is_plat("windows") then
  5. if is_arch("x86", "i386") then
  6. set_urls("https://github.com/KhronosGroup/OpenCL-SDK/releases/download/$(version)/OpenCL-SDK-$(version)-Win-x86.zip")
  7. add_versions("v2023.04.17", "ff6fa1b4e311a3f655eff4eda28008ec48dccd559ec3ed95ce6d9a584cd3b581")
  8. elseif is_arch("x64") then
  9. set_urls("https://github.com/KhronosGroup/OpenCL-SDK/releases/download/$(version)/OpenCL-SDK-$(version)-Win-x64.zip")
  10. add_versions("v2023.04.17", "11844a1d69a71f82dc14ce66382c6b9fc8a4aee5840c21a786c5accb1d69bc0a")
  11. end
  12. else
  13. set_urls("https://github.com/KhronosGroup/OpenCL-SDK.git")
  14. add_versions("v2023.04.17", "ae7fcae82fe0b7bcc272e43fc324181b2d544eea")
  15. end
  16. add_configs("vendor", {description = "Set OpenCL Vendor.", default = nil, type = "string", values = {"nvidia", "intel", "amd"}})
  17. if not is_plat("windows") then
  18. add_deps("cmake")
  19. end
  20. on_fetch(function (package, opt)
  21. if opt.system then
  22. import("lib.detect.find_path")
  23. import("lib.detect.find_library")
  24. import("detect.sdks.find_cuda")
  25. local result = {includedirs = {}, linkdirs = {}, links = {"OpenCL"}}
  26. local vendor = package:config("vendor")
  27. local archsuffixes = {"lib"}
  28. if package:is_arch("x64") then
  29. table.insert(archsuffixes, "lib64")
  30. table.insert(archsuffixes, path.join("lib", "x64"))
  31. elseif package:is_arch("x86", "i386") then
  32. table.insert(archsuffixes, path.join("lib", "x86"))
  33. elseif package:is_arch("x86_64") then
  34. table.insert(archsuffixes, "lib64")
  35. table.insert(archsuffixes, path.join("lib", "x86_64"))
  36. end
  37. if not vendor or vendor == "nvidia" then
  38. local cuda = find_cuda()
  39. if cuda then
  40. result.includedirs = cuda.includedirs
  41. result.linkdirs = cuda.linkdirs
  42. return result
  43. elseif vendor == "nvidia" then
  44. return nil
  45. end
  46. elseif not vendor or vendor == "intel" then
  47. local intelsdkpaths = {"$(env INTELOCLSDKROOT)"}
  48. local linkinfo = find_library("OpenCL", intelsdkpaths, {suffixes = archsuffixes})
  49. if linkinfo then
  50. table.insert(result.linkdirs, linkinfo.linkdir)
  51. local incpath = find_path(path.join("CL", "cl.h"), intelsdkpaths, {suffixes = {"include"}})
  52. if incpath then
  53. table.insert(result.includedirs, incpath)
  54. return result
  55. end
  56. end
  57. if vendor == "intel" then
  58. return nil
  59. end
  60. elseif not vendor or vendor == "amd" then
  61. local amdsdkpaths = {"$(env AMDAPPSDKROOT)"}
  62. local linkinfo = find_library("OpenCL", amdsdkpaths, {suffixes = archsuffixes})
  63. if linkinfo then
  64. table.insert(result.linkdirs, linkinfo.linkdir)
  65. local incpath = find_path(path.join("CL", "cl.h"), amdsdkpaths, {suffixes = {"include"}})
  66. if incpath then
  67. table.insert(result.includedirs, incpath)
  68. return result
  69. end
  70. end
  71. if vendor == "amd" then
  72. return nil
  73. end
  74. end
  75. end
  76. end)
  77. on_install("windows|x86", "windows|x64", function (package)
  78. os.cp("lib", package:installdir())
  79. os.cp("bin", package:installdir())
  80. os.cp("include", package:installdir())
  81. end)
  82. on_install("linux", "macosx", "android", function (package)
  83. package:add("links", "OpenCL")
  84. package:add("links", "OpenCLUtils")
  85. package:add("links", "OpenCLUtilsCpp")
  86. package:add("links", "OpenCLExt")
  87. local configs = {"-DOPENCL_SDK_BUILD_SAMPLES=OFF"}
  88. table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:debug() and "Debug" or "Release"))
  89. import("package.tools.cmake").install(package, configs)
  90. end)
  91. on_test(function (package)
  92. assert(package:check_csnippets({test = [[
  93. #include <stddef.h>
  94. #include <CL/cl.h>
  95. void test () {
  96. cl_uint num_platforms;
  97. clGetPlatformIDs(0, NULL, &num_platforms);
  98. }
  99. ]]}, {configs = {languages = "c11"}}))
  100. end)